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.

35 lines
1.1KB

  1. // Voltage-controlled oscillator example
  2. // by Andrew Belt
  3. // For audio synthesis and process, JavaScript is 10-100x less efficient than C++, but it's still an easy way to learn to program 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. // Take the first input's first buffer value
  12. pitch += block.inputs[0][0]
  13. // The relationship between 1V/oct pitch and frequency is `freq = 2^pitch`.
  14. // Default frequency is middle C (C4) in Hz.
  15. // https://vcvrack.com/manual/VoltageStandards.html#pitch-and-frequencies
  16. let freq = 261.6256 * Math.pow(2, pitch)
  17. display("Freq: " + freq.toFixed(3) + " Hz")
  18. // Set all samples in output buffer
  19. let deltaPhase = config.frameDivider * block.sampleTime * freq
  20. for (let i = 0; i < block.bufferSize; i++) {
  21. // Accumulate phase
  22. phase += deltaPhase
  23. // Wrap phase around range [0, 1]
  24. phase %= 1
  25. // Convert phase to sine output
  26. block.outputs[0][i] = Math.sin(2 * Math.PI * phase) * 5
  27. }
  28. }