No Description
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.

voice.c 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "synth.h"
  2. void voice_init(struct Voice *v, struct Synth *synth) {
  3. v->synth = synth;
  4. envelope_init(&v->osc_env);
  5. envelope_init(&v->filter_env);
  6. }
  7. void voice_render_sample(struct Voice *v, float *out) {
  8. float amplitude = envelope_sample(&v->osc_env); // calculate based on key velocity and envelope
  9. float cutoff = envelope_sample(&v->filter_env);
  10. float s = oscillator_render_sample(&v->osc);
  11. filter_set_cutoff(&v->filter, v->synth->cutoff + cutoff * v->synth->filter_eg_intensity);
  12. float f = filter_sample(&v->filter, v->volume * amplitude * s / 32768.0);
  13. out[0] = (1 + v->pan) * f / 2;
  14. out[1] = (1 - v->pan) * f / 2;
  15. v->time++;
  16. }
  17. void voice_note_start(struct Voice *voice, uint8_t note, uint8_t velocity) {
  18. voice->volume = (1+velocity) / 128.0; // in the range (0, 1]
  19. envelope_start(&voice->osc_env);
  20. envelope_start(&voice->filter_env);
  21. note &= 0x7f;
  22. voice->time = 0;
  23. voice->note = note;
  24. voice->osc.sub_phase = voice->osc.phase;
  25. voice->osc.sub_oct = 1;
  26. }
  27. void voice_stop(struct Voice *voice) {
  28. envelope_stop(&voice->osc_env);
  29. envelope_stop(&voice->filter_env);
  30. }