파형의 종류

Waveform types

오실레이터가 만드는 4가지 기본 파형 — 사인·사각·톱니·삼각. 파형의 모양이 곧 소리의 배음 구조이자 음색입니다.

다른 이름: Oscillator waveformsSine/Square/Saw/Triangle
···
html
<div class="wrap">
  <div class="grid">
    <div class="cell" id="c-sine"><canvas data-type="sine"></canvas><span>sine</span></div>
    <div class="cell" id="c-square"><canvas data-type="square"></canvas><span>square</span></div>
    <div class="cell" id="c-saw"><canvas data-type="sawtooth"></canvas><span>saw</span></div>
    <div class="cell" id="c-triangle"><canvas data-type="triangle"></canvas><span>triangle</span></div>
  </div>
  <button class="play" id="play" type="button" aria-label="play sound"><span class="tri"></span></button>
</div>
css
.wrap{position:relative;width:94%;height:88%}
.grid{display:grid;grid-template-columns:1fr 1fr;grid-template-rows:1fr 1fr;gap:6%;width:100%;height:100%}
.cell{position:relative;border:1px solid var(--line);border-radius:10px;background:var(--surface);overflow:hidden;transition:box-shadow .2s,border-color .2s}
.cell.go{border-color:var(--accent);box-shadow:0 0 0 2px color-mix(in srgb,var(--accent) 30%,transparent)}
.cell canvas{position:absolute;inset:0;width:100%;height:100%}
.cell span{position:absolute;left:6px;bottom:4px;font:600 9px/1 monospace;color:var(--muted);z-index:2}
.play{position:absolute;right:0;bottom:0;width:clamp(26px,14%,38px);aspect-ratio:1;border-radius:50%;border:1px solid var(--line);background:var(--surface);display:grid;place-items:center;cursor:pointer;box-shadow:0 2px 8px rgba(0,0,0,.12);z-index:3}
.play .tri{width:0;height:0;border-style:solid;border-width:6px 0 6px 9px;border-color:transparent transparent transparent var(--fg);margin-left:2px}
.play:active{transform:scale(.92)}
js
const TYPES = ['sine', 'square', 'sawtooth', 'triangle'];
const canvases = {};
TYPES.forEach((t) => { canvases[t] = document.querySelector('canvas[data-type="' + t + '"]'); });
const cs = getComputedStyle(document.documentElement);
const ACCENT = cs.getPropertyValue('--accent').trim() || '#5b5bf7';
function wave(type, phase) {
  const p = phase - Math.floor(phase);
  if (type === 'sine') return Math.sin(p * Math.PI * 2);
  if (type === 'square') return p < 0.5 ? 1 : -1;
  if (type === 'sawtooth') return p * 2 - 1;
  return p < 0.5 ? p * 4 - 1 : 3 - p * 4;
}
function draw(type, t) {
  const cv = canvases[type];
  const g2d = cv.getContext('2d');
  const dpr = Math.min(devicePixelRatio || 1, 2);
  const w = cv.clientWidth, h = cv.clientHeight;
  if (cv.width !== w * dpr || cv.height !== h * dpr) { cv.width = w * dpr; cv.height = h * dpr; }
  g2d.setTransform(dpr, 0, 0, dpr, 0, 0);
  g2d.clearRect(0, 0, w, h);
  g2d.beginPath();
  const cycles = 2.4, scroll = t * 0.00035;
  for (let x = 0; x <= w; x++) {
    const phase = (x / w) * cycles + scroll;
    const y = h / 2 - wave(type, phase) * (h * 0.34);
    if (x === 0) g2d.moveTo(x, y); else g2d.lineTo(x, y);
  }
  g2d.strokeStyle = ACCENT;
  g2d.lineWidth = 1.8;
  g2d.stroke();
}
function loop(t) {
  TYPES.forEach((type) => draw(type, t));
  requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
document.getElementById('play').addEventListener('click', () => {
  const ctx = new (window.AudioContext || window.webkitAudioContext)();
  const now = ctx.currentTime;
  TYPES.forEach((type, i) => {
    const t = now + i * 0.24;
    const osc = ctx.createOscillator();
    const gain = ctx.createGain();
    osc.type = type;
    osc.frequency.value = 261.63;
    gain.gain.setValueAtTime(0, t);
    gain.gain.linearRampToValueAtTime(0.1, t + 0.02);
    gain.gain.exponentialRampToValueAtTime(0.0001, t + 0.22);
    osc.connect(gain).connect(ctx.destination);
    osc.start(t);
    osc.stop(t + 0.23);
    const cellId = type === 'sawtooth' ? 'c-saw' : ('c-' + type);
    setTimeout(() => {
      const el = document.getElementById(cellId);
      el.classList.add('go');
      setTimeout(() => el.classList.remove('go'), 200);
    }, i * 240);
  });
  setTimeout(() => ctx.close(), 1100);
});

신시사이저의 오실레이터는 매 순간의 진폭을 시간에 따라 반복하는 패턴, 즉 파형으로 소리를 만듭니다. 사인파는 배음이 전혀 없는 가장 "순수한" 단일 주파수 음이고, 사각파는 홀수 배음만 포함해 속이 빈 듯한 음색을, 톱니파는 모든 배음을 촘촘히 포함해 가장 날카롭고 풍부한 음색을, 삼각파는 사인파에 가까운 부드러움에 옅은 배음을 더한 중간 음색을 냅니다.

배음이 많을수록(사각·톱니) 소리가 더 공격적이고 멀리 뚫고 나가므로 알림음처럼 짧게 주의를 끌어야 할 때 어울리지만, 오래 들으면 피로합니다. 사인·삼각파는 부드럽고 배경에 잘 섞여 잔잔한 확인음에 적합합니다. 저음역(100Hz 이하)에서 사각·톱니파를 큰 음량으로 쓰면 스피커·이어폰에서 클리핑이 나기 쉬우므로 음량을 더 낮춰야 합니다.

신시사이저·8비트 게임 사운드(사각파), 신스 베이스·리드(톱니파), 목관 계열 흉내(사각파), 부드러운 UI 톤(사인·삼각파)에 쓰입니다.

언제 쓰나

알림음처럼 주의를 끌어야 하면 사각·톱니파, 은은한 확인음이면 사인·삼각파를 고르세요.