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.

oscillator.c 636B

12345678910111213141516171819202122
  1. #include "oscillator.h"
  2. #include "config.h"
  3. int16_t oscillator_render_sample(struct Oscillator *osc) {
  4. if(osc->period == 0) return 0;
  5. osc->phase += 0x10000; // add one sample in fixed
  6. osc->sub_phase += 0x10000;
  7. fixed sub_period = osc->period << 1;
  8. while(osc->phase > osc->period)
  9. osc->phase -= osc->period;
  10. while(osc->sub_phase > sub_period)
  11. osc->sub_phase -= sub_period;
  12. return (1 << 13) - (osc->phase << 3) / (osc->period >> 11) + (osc->sub_phase - osc->period / 2 > osc->period ? 4096 : -4096);
  13. }
  14. void oscillator_set_freq(struct Oscillator *osc, float freq) {
  15. osc->period = ((float)SAMPLE_RATE / freq) * 65536.0;
  16. }