Skip to content

XY Pad ​

Control frequency and gain in real-time with a visual XY pad. Drag slowly to slide between pitches, or move vertically to change the level of a held note.

Try It: XY Pad ​

Click or drag on the pad to play. The X-axis controls frequency (100-2000 Hz, logarithmic scale), and the Y-axis controls gain (0-100%).

Expressive continuous control, theremin-style. X = frequency, Y = gain.

←→↑↓ move the point
440 Hzfrequency
50 %gain
A4note

How to Use ​

  1. Click and hold anywhere on the pad to start the oscillator
  2. Drag to change frequency (horizontal) and gain (vertical)
  3. Release to stop the sound

The frequency scale is logarithmic, meaning equal horizontal distances represent equal musical intervals. This makes the pad feel "musical" — sliding left-to-right sounds like moving up the musical scale.

How It Works ​

The pad keeps one oscillator playing for the duration of a gesture. To smooth changes, it ramps the oscillator's frequency and gain to each new value over 30 milliseconds. This allows the pitch or level to move even when pointer events arrive several milliseconds apart.

Logarithmic Frequency Mapping ​

The XY pad uses logarithmic frequency scaling so equal horizontal distances sound like equal pitch intervals:

typescript
// Linear scaling (sounds wrong - lower frequencies cramped on left)
const linearFreq = minFreq + (maxFreq - minFreq) * ratio

// Logarithmic scaling (sounds musical - equal steps = equal intervals)
const logFreq = minFreq * (maxFreq / minFreq) ** ratio

// For 100-2000 Hz range:
const frequency = 100 * 20 ** ratio // where ratio is 0-1

This is why piano keys are evenly spaced even though the frequencies double each octave (100 Hz → 200 Hz → 400 Hz → 800 Hz).

Building Your Own XY Controller ​

Give the pad a visible size and set its CSS to touch-action: none so touch drags stay on the instrument. Pass the element to this function. It returns the cleanup function to call when you remove the pad.

typescript
import type { Oscillator } from 'ez-web-audio'
import { createOscillator } from 'ez-web-audio'

export function attachPad(pad: HTMLElement) {
  const listeners = new AbortController()
  const options = { signal: listeners.signal }
  let held: PointerEvent | null = null
  let voice: Oscillator | null = null
  let hz = 440
  let gain = 0

  function move(event: PointerEvent) {
    const rect = pad.getBoundingClientRect()
    const clamp = (value: number) => Math.max(0, Math.min(1, value))
    hz = 100 * 20 ** clamp((event.clientX - rect.left) / rect.width)
    gain = 0.3 * (1 - clamp((event.clientY - rect.top) / rect.height))
    if (voice) {
      const now = voice.audioContext.currentTime
      voice.audioSourceNode.frequency.setTargetAtTime(hz, now, 0.01)
      voice.getGainNode().gain.setTargetAtTime(gain, now, 0.01)
    }
  }

  function release() {
    held = null
    if (voice) {
      const ending = voice
      voice = null
      ending.once('end', () => ending.dispose())
      void ending.stop()
    }
  }

  pad.addEventListener('pointerdown', async (press) => {
    if (held)
      return
    held = press
    pad.setPointerCapture(press.pointerId)
    move(press)
    const synth = await createOscillator({ type: 'sine', frequency: hz })
    // The user may release or start a different note during the first load.
    if (held !== press) {
      synth.dispose()
      return
    }
    synth.changeGainTo(gain)
    synth.update('frequency').to(hz).as('ratio')
    voice = synth
    await synth.play()
  }, options)
  pad.addEventListener('pointermove', (event) => {
    if (event.pointerId === held?.pointerId)
      move(event)
  }, options)
  pad.addEventListener('lostpointercapture', (event) => {
    if (event.pointerId === held?.pointerId)
      release()
  }, options)

  return () => {
    listeners.abort()
    release()
  }
}

API Used ​

  • createOscillator() - Creates a synthesizer
  • update() - Real-time parameter modulation
  • changeGainTo() - Set volume

Next Steps ​

  • Synthesis - Learn about waveforms and ADSR envelopes
  • Effects - Add filters to shape your sound