Back to Find the Invisible Cow
SOURCE / PINNED RELEASE

Made of little things.

Find the Invisible Cow

Release
e48b3dced31c…
Author-recorded commit
cf79302f143d…
License
LICENSE
Author’s source reference
nostr://npub1p8rxwea4e0l4jml0q53n5mvh3n7pywq523jk3lm336v04r8ny9jqtq7ttf/wss%3A%2F%2Fgit.napplet.soy%2F/n-3b179f1d7df

Archive hash verified: f8e300eb672b5ee8…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/moo.ts
/**
 * Synthesized cow moo — no recorded assets, so the whole game stays inside the
 * single-file artifact with nothing to license or fetch.
 *
 * Every voice connects through the destination node handed in by
 * CoordinatedAudio (the NAP-MEDIA session gain), never straight to
 * context.destination, so host pause/stop/volume commands affect all output.
 */

interface MooParams {
  /** 0..1 final loudness after proximity mapping. */
  volume: number;
  /** -1 (left) .. 1 (right). */
  pan: number;
  /** Base pitch in Hz (~150). */
  pitch: number;
}

export class MooSynth {
  constructor(
    private context: AudioContext,
    private destination: AudioNode,
  ) {}

  /** Schedule one moo. Returns its duration in seconds. */
  play({ volume, pan, pitch }: MooParams): number {
    const context = this.context;
    const t0 = context.currentTime + 0.02;
    const duration = 0.72 + Math.random() * 0.14;
    const v = Math.max(0, Math.min(1, volume));

    // Voice gain envelope: soft attack, held body, gentle release.
    const voice = context.createGain();
    voice.gain.setValueAtTime(0.0001, t0);
    voice.gain.linearRampToValueAtTime(v, t0 + 0.09);
    voice.gain.setValueAtTime(Math.max(0.0001, v * 0.85), t0 + duration - 0.3);
    voice.gain.exponentialRampToValueAtTime(0.0001, t0 + duration);

    // Timbre: band-limited body with a nasal formant bump.
    const lowpass = context.createBiquadFilter();
    lowpass.type = 'lowpass';
    lowpass.Q.value = 1.4;
    lowpass.frequency.setValueAtTime(950, t0);
    lowpass.frequency.exponentialRampToValueAtTime(480, t0 + duration * 0.8);
    const formant = context.createBiquadFilter();
    formant.type = 'peaking';
    formant.frequency.value = 340;
    formant.Q.value = 1.1;
    formant.gain.value = 9;
    voice.connect(lowpass);
    lowpass.connect(formant);

    const panner = context.createStereoPanner();
    panner.pan.value = Math.max(-0.85, Math.min(0.85, pan));
    formant.connect(panner);
    panner.connect(this.destination);

    // Pitch contour: a slight lift into the "ooo", then a downward slide.
    const bend = (osc: OscillatorNode, base: number) => {
      osc.frequency.setValueAtTime(base * 1.12, t0);
      osc.frequency.linearRampToValueAtTime(base * 1.28, t0 + 0.12);
      osc.frequency.exponentialRampToValueAtTime(base * 0.74, t0 + duration - 0.12);
    };

    // Vibrato keeps the tone animal instead of buzzy.
    const lfo = context.createOscillator();
    lfo.frequency.value = 5.3 + Math.random() * 1.2;
    const lfoDepth = context.createGain();
    lfoDepth.gain.value = 6.5;
    lfo.connect(lfoDepth);

    const sources: Array<OscillatorNode | AudioBufferSourceNode> = [];
    const addOsc = (type: OscillatorType, base: number, level: number) => {
      const osc = context.createOscillator();
      osc.type = type;
      bend(osc, base);
      const level_ = context.createGain();
      level_.gain.value = level;
      lfoDepth.connect(osc.frequency);
      osc.connect(level_);
      level_.connect(voice);
      osc.start(t0);
      osc.stop(t0 + duration + 0.05);
      sources.push(osc);
      return osc;
    };
    addOsc('sawtooth', pitch, 0.5);
    addOsc('sawtooth', pitch * 1.006, 0.42);
    addOsc('sine', pitch / 2, 0.6);
    lfo.start(t0);
    lfo.stop(t0 + duration + 0.05);

    // A breathy attack transient ("m" before "oo").
    const noise = context.createBufferSource();
    noise.buffer = this.noise();
    const noiseFilter = context.createBiquadFilter();
    noiseFilter.type = 'bandpass';
    noiseFilter.frequency.value = 300;
    noiseFilter.Q.value = 0.9;
    const noiseGain = context.createGain();
    noiseGain.gain.setValueAtTime(v * 0.3, t0);
    noiseGain.gain.exponentialRampToValueAtTime(0.0001, t0 + 0.18);
    noise.connect(noiseFilter);
    noiseFilter.connect(noiseGain);
    noiseGain.connect(voice);
    noise.start(t0);
    noise.stop(t0 + 0.2);
    sources.push(noise);

    // Release every node once the last source ends.
    const last = sources[sources.length - 1];
    const cleanup = () => {
      for (const node of [voice, lowpass, formant, panner, lfoDepth, noiseFilter, noiseGain]) {
        try {
          node.disconnect();
        } catch {
          /* Already torn down. */
        }
      }
    };
    if (last instanceof OscillatorNode) {
      last.onended = cleanup;
    } else {
      sources[0]?.addEventListener('ended', cleanup);
    }

    return duration;
  }

  /** A short triumphant double moo for the reveal. */
  fanfare(): void {
    this.play({ volume: 0.9, pan: 0, pitch: 175 });
    window.setTimeout(() => {
      if (this.context.state === 'running') {
        this.play({ volume: 1, pan: 0, pitch: 158 });
      }
    }, 550);
  }

  private noiseBuffer?: AudioBuffer;
  private noise(): AudioBuffer {
    if (this.noiseBuffer) return this.noiseBuffer;
    const length = Math.ceil(this.context.sampleRate * 0.25);
    const buffer = this.context.createBuffer(1, length, this.context.sampleRate);
    const data = buffer.getChannelData(0);
    for (let i = 0; i < length; i += 1) data[i] = Math.random() * 2 - 1;
    this.noiseBuffer = buffer;
    return buffer;
  }
}