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.

28 lines
913B

  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. var phase = 0
  6. function process(args) {
  7. // Knob ranges from -5 to 5 octaves
  8. var pitch = args.knobs[0] * 10 - 5
  9. // Input follows 1V/oct standard
  10. pitch += args.inputs[0]
  11. // The relationship between 1V/oct pitch and frequency is `freq = 2^pitch`.
  12. // Default frequency is middle C (C4) in Hz.
  13. // https://vcvrack.com/manual/VoltageStandards.html#pitch-and-frequencies
  14. var freq = 261.6256 * Math.pow(2, pitch)
  15. display("Freq: " + freq.toFixed(3) + " Hz")
  16. // Accumulate phase
  17. phase += args.sampleTime * config.frameDivider * freq
  18. // Wrap phase around range [0, 1]
  19. phase %= 1
  20. // Convert phase to sine output
  21. args.outputs[0] = Math.sin(2 * Math.PI * phase) * 5
  22. }