You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0KB

  1. // Voltage-controlled oscillator example
  2. // by Andrew Belt
  3. // JavaScript isn't ideal for audio generating and processing due to it being 10-100 less efficient than C++, but it's still an easy way to learn simple DSP.
  4. config.frameDivider = 1
  5. config.bufferSize = 16
  6. let phase = 0
  7. function process(block) {
  8. // Knob ranges from -5 to 5 octaves
  9. let pitch = block.knobs[0] * 10 - 5
  10. // Input follows 1V/oct standard
  11. pitch += block.inputs[0][0]
  12. // The relationship between 1V/oct pitch and frequency is `freq = 2^pitch`.
  13. // Default frequency is middle C (C4) in Hz.
  14. // https://vcvrack.com/manual/VoltageStandards.html#pitch-and-frequencies
  15. let freq = 261.6256 * Math.pow(2, pitch)
  16. display("Freq: " + freq.toFixed(3) + " Hz")
  17. // Set all samples in output buffer
  18. let deltaPhase = config.frameDivider * block.sampleTime * freq
  19. for (let i = 0; i < block.bufferSize; i++) {
  20. // Accumulate phase
  21. phase += deltaPhase
  22. // Wrap phase around range [0, 1]
  23. phase %= 1
  24. // Convert phase to sine output
  25. block.outputs[0][i] = Math.sin(2 * Math.PI * phase) * 5
  26. }
  27. }