All files / src/midi audio.ts

5.26% Statements 6/114
0% Branches 0/60
0% Functions 0/9
5.45% Lines 6/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245  1x 1x 1x 1x       1x                                                                                                                                                                                                                                                                                                                                                                                                               1x                                                                        
import { SoundOff, SoundOn, NoOp } from "../digital/types";
import { SoundSystem } from '../digital/audio';
import { ADSR, Compressor, Filter, MidiToFreq, NoteToMidi, Patch, TimeInSeconds } from './types';
 
// Things like G#9, F-3, etc
export type NoteName = string;
 
/** This is an amount of time added to the end of a patch to
 * give extra time to fade out. To stop the "wave non hitting 
 * zero" audible click */
export const DURATION_CLICK_PAD = .015;

function createCompressor(system: SoundSystem, vpc: Compressor, _length: number, time: TimeInSeconds): AudioNode {
  const compressor = system.context().createDynamicsCompressor();
  compressor.threshold.setValueAtTime(vpc.threshold, time);
  compressor.knee.setValueAtTime(vpc.knee, time);
  compressor.ratio.setValueAtTime(vpc.ratio, time);
  compressor.attack.setValueAtTime(vpc.attack, time);
  compressor.release.setValueAtTime(vpc.release, time);
  return compressor;
}

function createADSR(system: SoundSystem, padsr: ADSR, length: number, time: TimeInSeconds): AudioNode {
  const asdr = system.context().createGain();
  // asdr.gain.cancelScheduledValues(time);
  const now = time;
  const atkDuration = padsr.attack * length;
  const atkEndTime = now + atkDuration;
  const decayDuration = padsr.decay * length;
 
  asdr.gain.setValueAtTime(0.00001, now);
  // Go to full volume when the attack time ends. Attack is given in percent
  // so this would go to full volume when X% of the sample is played
  asdr.gain.exponentialRampToValueAtTime(1, atkEndTime);
  // Next, set ramp the volume to the _sustain_ value when the (% of attack + % of decay)
  // % of time into the sample has passed
  // So if attack is 3% and decay is 4%, this will happen 7% of the way through the
  // playing of the sample
  asdr.gain.exponentialRampToValueAtTime(padsr.sustain, now + decayDuration);
 
  // here, the sustain level is used...
 
  const relDuration = padsr.release * length;
  const relEndTime = now + relDuration;
  // Fade to zero when we are % of release through the sample. 
  // _Note_ this does not take the attack and decay into account. This
  // will start the ramp down x% through the sample no matter what. So if you
  // want a nice curve you'd do something like:
  // attack 3%; decay 3%; sustain 0.5; release 80%
  // asdr.gain.exponentialRampToValueAtTime(0.00001, relEndTime + DURATION_CLICK_PAD);
  asdr.gain.exponentialRampToValueAtTime(padsr.sustain, now + relDuration);

  asdr.gain.exponentialRampToValueAtTime(0.00001, now + length + DURATION_CLICK_PAD);

  return asdr;
}
 
function createFilter(system: SoundSystem, pf: Filter, _length: number, time: TimeInSeconds): AudioNode {
  const filter = system.context().createBiquadFilter();
  filter.type = pf.type;

  filter.frequency.setValueAtTime(pf.frequency, time);
  filter.detune.setValueAtTime(pf?.detune ?? 1, time);
  filter.Q.setValueAtTime(pf.q, time);
  filter.gain.setValueAtTime(pf.gain, time);

  return filter;
}

export function patchPlayer(system: SoundSystem, patch: Patch): SoundOn {
  return (note: NoteName, time: TimeInSeconds, output: AudioNode): SoundOff => {
    const midi = NoteToMidi.get(note);
    if (!midi) {
      console.warn(`Could not find midi value for note: ${note} Bad note?`);
      return NoOp;
    }

    const fundamental = MidiToFreq[midi];
    // We couldn't parse the fundamental note
    if (!fundamental) throw Error(`Could not find frequency value for note. Bad midi? ${midi}`);

    // The node the additive sine waves will
    // all write to
    let oscillatorOutput: AudioNode;
 
    // const soundLen = Math.min(duration, patch.len);
    const soundLen = patch.len;

    // Gain for the whole patch
    const patchGain = system.context().createGain();
    const sampleGain = patch.vca?.gain ?? 1;
    // patchGain.gain.setValueAtTime(sampleGain, time);
    patchGain.gain.linearRampToValueAtTime(sampleGain, time);
 
    // all the effects needed for the oscillators
    const processing = new Array<AudioNode>();
 
    // Filter
    if (patch.filter) {
      processing.push(createFilter(system, patch.filter, soundLen, time));
    }
 
    // ADSR
    if (patch.adsr) {
      processing.push(
        createADSR(system, patch.adsr, soundLen, time)
      );
    }
 
    // COMPRESSOR
    if (patch.compressor) {
      processing.push(createCompressor(system, patch.compressor, soundLen, time));
    }

    // SAMPLE GAIN
    processing.push(patchGain);

    // oscillator 
    //    \
    // [filter, adsr, compressor, gain]
    //        filter.connect(adsr)
    //        asdr.connect(compressor)
    //        compressor.connect(gain)
    //     \
    //     speakers
    if (processing.length > 1) {
      for (let z = 1; z < processing.length; z++) {
        const fx = processing[z];
        (processing[z - 1]).connect(fx);
      }
    }

    // Here, output Node should be the start of the chain
    // above or just right to the gain node
    oscillatorOutput = processing[0];
 
    // Max number of sub-sine waves
    const vcoCleanUp: Array<(time: number) => void> = [];
    const voiceLen = Math.min(patch.voices.length, 8);
    for (let v = 0; v < voiceLen; v++) {
      const voice = patch.voices[v];

      let vco: AudioNode;
 
      // VCO
      if (voice.vco.type === 'custom') {
        vco = new AudioWorkletNode(system.context(), 'white-noise-processor');
      } else {
        vco = system.context().createOscillator();
        const lvco = vco as OscillatorNode;
        const voice = patch.voices[v];
        lvco.type = voice.vco.type;

        if (voice.vco.frequency) {
          lvco.frequency.value = voice.vco.frequency;
        } else if (voice.vco.freqOffset) {
          lvco.frequency.value = fundamental + voice.vco.freqOffset;
        } else if (voice.vco.freqMult) {
          lvco.frequency.value = fundamental * voice.vco.freqMult;
        } else {
          lvco.frequency.value = fundamental;
        }

        lvco.detune.value = voice.vco.detune;
 
        // Start this thing when the time comes
        lvco.start(time);
        // Stop the oscillator after we're done with the note
        // and add some extra time to stop clicking
        if (soundLen > 0)
          lvco.stop(time + soundLen + DURATION_CLICK_PAD * 10);
      }

      // VCA
      const vca = system.context().createGain();
      const pvca = patch.voices[v].vca;
      let vcaVolume = 1;
      // vca.gain.value = patch.voices[v].vca.gain; <- don't do that
      if (pvca.gainOffset) {
        vcaVolume = 1 + pvca.gainOffset;
        // vca.gain.setValueAtTime(, time);
      }
      if (pvca.gain) {
        vcaVolume = pvca.gain;
        // vca.gain.setValueAtTime(pvca.gain, time);
      }
      vca.gain.setValueAtTime(vcaVolume, time);
 
      // CONNECTION
      vco.connect(vca);
      vca.connect(oscillatorOutput);
 
      // If we play the sound for the whole length, stop the end click.
      if (soundLen > 0) {
        vca.gain.exponentialRampToValueAtTime(vcaVolume, (time + soundLen - DURATION_CLICK_PAD));
        vca.gain.exponentialRampToValueAtTime(0.00001, (time + soundLen + DURATION_CLICK_PAD));
      }

      vcoCleanUp.push((t: number) => {
        // vca.gain.exponentialRampToValueAtTime(vcaVolume, t + DURATION_CLICK_PAD);
        vca.gain.exponentialRampToValueAtTime(0.00001, t);
        if ((vco as OscillatorNode)?.stop) {
          // Noise nodes wont have a stop.
          (vco as OscillatorNode)?.stop(t);
        }
        vco.disconnect(vca);
        vca.disconnect(oscillatorOutput);
      })
    }
 
    patchGain.connect(output);
 
    // Clean up when SoundOff 
    return (t?: number) => {
      const time = t ?? system.currentTime();
 
      // Disconnect so GC and do it's job
      // TODO: Make sure we're not leaking
 
      // Try to fade out the whole page after SoundOff to try to keep clicks from
      // happening (TODO: this isn't working)
      patchGain.gain.setValueAtTime(patchGain.gain.value, time);
      patchGain.gain.linearRampToValueAtTime(0, time + (DURATION_CLICK_PAD * 22));
 
      // Not sure I love this, but if we don't wait for a while
      // we get a really nasty click if we don't fadeout before
      // we cleanup and disconnect. Seems to work ok, but feels 
      // a bit dirty
      setTimeout(() => {
        vcoCleanUp.forEach((cleanUp) => cleanUp(time + (DURATION_CLICK_PAD * 10)));
 
        if (processing.length > 1) {
          for (let z = 1; z < processing.length; z++) {
            const fx = processing[z];
            (processing[z - 1]).disconnect(fx);
          }
        }
 
        patchGain.disconnect(output);
      }, 333);
    }
  }
}