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.

synth.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "synth.h"
  6. #include "config.h"
  7. #include "tables.inc"
  8. void synth_init(struct Synth *synth) {
  9. synth_set_cutoff_freq(synth, 0);
  10. synth_set_resonance(synth, 0);
  11. srandom(1234);
  12. synth->key_stack_size = 0;
  13. synth->tuning = 440.0;
  14. synth->volume = 1.0;
  15. synth->pitch_bend = 0.0;
  16. synth->lfo_depth = 0.0;
  17. sine_osc_init(&synth->lfo_osc);
  18. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  19. voice_init(&synth->voices[i], synth);
  20. }
  21. }
  22. static float detune(float freq, float semitones) {
  23. return freq * pow(2, semitones / 12.0);
  24. }
  25. static float midifreq(uint8_t note) {
  26. return pow(2, (note - 69) / 12.0) * 440.0;
  27. }
  28. static void synth_set_pitch(struct Synth *synth) {
  29. float lfo_detune = synth->lfo_depth * sine_osc_sample(&synth->lfo_osc);
  30. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  31. struct Voice *voice = &synth->voices[i];
  32. if(voice->osc_env.state != EnvNone)
  33. oscillator_set_freq(&voice->osc, detune(midifreq(voice->note), voice->detune + synth->pitch_bend * synth->pitch_bend_range + lfo_detune));
  34. }
  35. }
  36. static void synth_spread_unison(struct Synth *synth) {
  37. synth->voices[0].detune = 0;
  38. for(int i = 1; i <= 3; i++) {
  39. synth->voices[i].detune = i * synth->unison_spread / 6.0;
  40. }
  41. for(int i = 4; i < 7; i++) {
  42. synth->voices[i].detune = (i - 7) * synth->unison_spread / 6.0;
  43. }
  44. // for(int i = 0; i < 7; i++) {
  45. // printf("voice %d detune %f\n", i, synth->voices[i].detune);
  46. // }
  47. }
  48. static void synth_spread_stereo(struct Synth *synth) {
  49. synth->voices[0].pan = 0;
  50. for(int i = 1; i <= 7; i++) {
  51. synth->voices[i].pan = ((i&1) * 2 - 1) * synth->stereo_spread * ((i + 1) / 2) / 3;
  52. }
  53. // for(int i = 0; i < 7; i++) {
  54. // printf("voice %d pan %f\n", i, synth->voices[i].pan);
  55. // }
  56. }
  57. static void synth_note_on_monophonic(struct Synth *synth, uint8_t note, uint8_t velocity) {
  58. synth_spread_unison(synth);
  59. synth_spread_stereo(synth);
  60. for(int i = 0; i < 7; i++) {
  61. struct Voice *voice = synth->voices + i;
  62. voice->osc.phase = random();
  63. envelope_set_attack_rate(&voice->osc_env, synth->osc_attack);
  64. envelope_set_decay_rate(&voice->osc_env, synth->osc_decay);
  65. envelope_set_sustain_level(&voice->osc_env, synth->osc_sustain);
  66. envelope_set_release_rate(&voice->osc_env, synth->osc_release);
  67. envelope_set_attack_rate(&voice->filter_env, synth->filter_attack);
  68. envelope_set_decay_rate(&voice->filter_env, synth->filter_decay);
  69. envelope_set_sustain_level(&voice->filter_env, synth->filter_sustain);
  70. envelope_set_release_rate(&voice->filter_env, synth->filter_release);
  71. voice_note_start(voice, note, velocity);
  72. }
  73. }
  74. void synth_note_on(struct Synth *synth, uint8_t note, uint8_t velocity) {
  75. if(synth->monophonic) {
  76. synth->key_stack[synth->key_stack_size].note = note;
  77. synth->key_stack[synth->key_stack_size].velocity = velocity;
  78. synth->key_stack_size++;
  79. if(synth->key_stack_size >= SYNTH_NUM_VOICES) {
  80. memmove(synth->key_stack, synth->key_stack + 1, sizeof(synth->key_stack[0]) * (SYNTH_NUM_VOICES - 1));
  81. synth->key_stack_size--;
  82. }
  83. synth_note_on_monophonic(synth, note, velocity);
  84. } else {
  85. struct Voice *found = 0;
  86. uint32_t oldest_time = 0;
  87. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  88. struct Voice *voice = &synth->voices[i];
  89. if(voice->osc_env.state == EnvNone) {
  90. found = voice;
  91. break;
  92. }
  93. if(voice->time > oldest_time) {
  94. oldest_time = voice->time;
  95. found = voice;
  96. }
  97. }
  98. envelope_set_attack_rate(&found->osc_env, synth->osc_attack);
  99. envelope_set_decay_rate(&found->osc_env, synth->osc_decay);
  100. envelope_set_sustain_level(&found->osc_env, synth->osc_sustain);
  101. envelope_set_release_rate(&found->osc_env, synth->osc_release);
  102. envelope_set_attack_rate(&found->filter_env, synth->filter_attack);
  103. envelope_set_decay_rate(&found->filter_env, synth->filter_decay);
  104. envelope_set_sustain_level(&found->filter_env, synth->filter_sustain);
  105. envelope_set_release_rate(&found->filter_env, synth->filter_release);
  106. voice_note_start(found, note, velocity);
  107. }
  108. }
  109. static void synth_note_off_monophonic(struct Synth *synth) {
  110. for(int i = 0; i < 7; i++) {
  111. struct Voice *voice = synth->voices + i;
  112. voice_stop(voice);
  113. }
  114. }
  115. void synth_note_off(struct Synth *synth, uint8_t note, uint8_t velocity) {
  116. if(synth->monophonic) {
  117. if(synth->key_stack_size > 1) {
  118. if(synth->key_stack[synth->key_stack_size - 1].note == note) {
  119. synth_note_on_monophonic(synth, synth->key_stack[synth->key_stack_size - 2].note, synth->key_stack[synth->key_stack_size - 2].velocity);
  120. } else {
  121. for(int i = 0; i < synth->key_stack_size - 1; i++) {
  122. if(synth->key_stack[i].note == note) {
  123. memmove(synth->key_stack + i, synth->key_stack + i + 1, sizeof(synth->key_stack[0]) * (synth->key_stack_size - i - 1));
  124. break;
  125. }
  126. }
  127. }
  128. } else {
  129. synth_note_off_monophonic(synth);
  130. }
  131. synth->key_stack_size--;
  132. } else {
  133. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  134. struct Voice *voice = synth->voices + i;
  135. if(voice->note == note && voice->osc_env.state != EnvNone && voice->osc_env.state != EnvRelease) {
  136. voice_stop(&synth->voices[i]);
  137. }
  138. }
  139. }
  140. }
  141. void synth_render_sample(struct Synth *synth, float *out) {
  142. float smpl[2] = { 0, 0 };
  143. synth_set_pitch(synth);
  144. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  145. struct Voice *v = &synth->voices[i];
  146. float vsmpl[2];
  147. voice_render_sample(v, vsmpl);
  148. for(int j = 0; j < 2; j++) {
  149. smpl[j] += vsmpl[j];
  150. if(smpl[j] > 1) smpl[j] = 1;
  151. else if(smpl[j] < -1) smpl[j] = -1;
  152. }
  153. }
  154. out[0] = smpl[0];
  155. out[1] = smpl[1];
  156. synth->time++;
  157. }
  158. void synth_set_cutoff_freq(struct Synth *synth, uint8_t f) {
  159. synth->cutoff = f / 127.0;
  160. }
  161. void synth_set_resonance(struct Synth *synth, uint8_t f) {
  162. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  163. filter_set_resonance(&synth->voices[i].filter, f / 127.0);
  164. }
  165. }
  166. void synth_set_unison_spread(struct Synth *synth, uint8_t w) {
  167. synth->unison_spread = w / 127.0;
  168. synth_spread_unison(synth);
  169. }
  170. void synth_set_stereo_spread(struct Synth *synth, uint8_t w) {
  171. synth->stereo_spread = w / 127.0;
  172. synth_spread_stereo(synth);
  173. }
  174. void synth_set_volume(struct Synth *s, uint8_t vol) {
  175. s->volume = vol / 127.0;
  176. }
  177. void synth_pitch_bend(struct Synth *s, int16_t bend) {
  178. s->pitch_bend = bend / 8191.0;
  179. if(s->pitch_bend < -1.0) s->pitch_bend = -1.0;
  180. if(s->pitch_bend > 1.0) s->pitch_bend = 1.0;
  181. synth_set_pitch(s);
  182. }
  183. void synth_set_lfo_depth(struct Synth *s, uint8_t mod) {
  184. s->lfo_depth = mod / 127.0;
  185. }
  186. void synth_load_patch(struct Synth *s, const char *filename) {
  187. FILE *f = fopen(filename, "r");
  188. if(!f) {
  189. perror(filename);
  190. return;
  191. }
  192. char buf[256];
  193. while(!feof(f)) {
  194. fgets(buf, sizeof(buf), f);
  195. char *tok = strtok(buf, " \t");
  196. if(tok) {
  197. char *val = strtok(NULL, " \t");
  198. if(val) {
  199. if(!strcmp(tok, "lfo_freq")) {
  200. sine_osc_set_freq(&s->lfo_osc, strtof(val, NULL));
  201. } else if(!strcmp(tok, "osc_env.attack")) {
  202. s->osc_attack = strtof(val, NULL);
  203. } else if(!strcmp(tok, "osc_env.decay")) {
  204. s->osc_decay = strtof(val, NULL);
  205. } else if(!strcmp(tok, "osc_env.sustain")) {
  206. s->osc_sustain = strtof(val, NULL);
  207. } else if(!strcmp(tok, "osc_env.release")) {
  208. s->osc_release = strtof(val, NULL);
  209. } else if(!strcmp(tok, "filter_env.attack")) {
  210. s->filter_attack = strtof(val, NULL);
  211. } else if(!strcmp(tok, "filter_env.decay")) {
  212. s->filter_decay = strtof(val, NULL);
  213. } else if(!strcmp(tok, "filter_env.sustain")) {
  214. s->filter_sustain = strtof(val, NULL);
  215. } else if(!strcmp(tok, "filter_env.release")) {
  216. s->filter_release = strtof(val, NULL);
  217. } else if(!strcmp(tok, "filter_eg_intensity")) {
  218. s->filter_eg_intensity = strtof(val, NULL);
  219. } else if(!strcmp(tok, "filter_kbd_track")) {
  220. s->filter_kbd_track = strtof(val, NULL);
  221. } else if(!strcmp(tok, "pitch_bend_range")) {
  222. s->pitch_bend_range = strtof(val, NULL);
  223. } else if(!strcmp(tok, "monophonic")) {
  224. s->monophonic = atoi(val);
  225. } else if(!strcmp(tok, "unison_spread")) {
  226. s->unison_spread = strtof(val, NULL);
  227. } else if(!strcmp(tok, "stereo_spread")) {
  228. s->stereo_spread = strtof(val, NULL);
  229. } else if(!strcmp(tok, "cutoff")) {
  230. s->cutoff = strtof(val, NULL);
  231. } else if(!strcmp(tok, "resonance")) {
  232. for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
  233. filter_set_resonance(&s->voices[i].filter, strtof(val, NULL));
  234. }
  235. }
  236. }
  237. }
  238. }
  239. fclose(f);
  240. }