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.1KB

  1. -- Voltage-controlled oscillator example
  2. -- by Andrew Belt
  3. -- For audio synthesis and process, Lua 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. phase = 0
  7. function process(block)
  8. -- Knob ranges from -5 to 5 octaves
  9. pitch = block.knobs[1] * 10 - 5
  10. -- Input follows 1V/oct standard
  11. -- Take the first input's first buffer value
  12. pitch = pitch + block.inputs[1][1]
  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. freq = 261.6256 * math.pow(2, pitch)
  17. display("Freq: " .. string.format("%.3f", freq) .. " Hz")
  18. -- Set all samples in output buffer
  19. deltaPhase = config.frameDivider * block.sampleTime * freq
  20. for i=1,block.bufferSize do
  21. -- Accumulate phase
  22. phase = phase + deltaPhase
  23. -- Wrap phase around range [0, 1]
  24. phase = phase % 1
  25. -- Convert phase to sine output
  26. block.outputs[1][i] = math.sin(2 * math.pi * phase) * 5
  27. end
  28. end