Audio plugin host https://kx.studio/carla
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.

nekobee_voice_render.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* nekobee DSSI software synthesizer plugin
  2. */
  3. #define _BSD_SOURCE 1
  4. #define _SVID_SOURCE 1
  5. #define _ISOC99_SOURCE 1
  6. #include <math.h>
  7. #include "nekobee.h"
  8. #include "nekobee_synth.h"
  9. #include "nekobee_voice.h"
  10. #define M_2PI_F (2.0f * (float)M_PI)
  11. #define M_PI_F (float)M_PI
  12. #define VCF_FREQ_MAX (0.825f) /* original filters only stable to this frequency */
  13. static int tables_initialized = 0;
  14. float nekobee_pitch[128];
  15. #define pitch_ref_note 69
  16. #define volume_to_amplitude_scale 128
  17. static float volume_to_amplitude_table[4 + volume_to_amplitude_scale + 2];
  18. static float velocity_to_attenuation[128];
  19. static float qdB_to_amplitude_table[4 + 256 + 0];
  20. void
  21. nekobee_init_tables(void)
  22. {
  23. int i;
  24. float pexp;
  25. float volume, volume_exponent;
  26. float ol, amp;
  27. if (tables_initialized)
  28. return;
  29. /* MIDI note to pitch */
  30. for (i = 0; i < 128; ++i) {
  31. pexp = (float)(i - pitch_ref_note) / 12.0f;
  32. nekobee_pitch[i] = powf(2.0f, pexp);
  33. }
  34. /* volume to amplitude
  35. *
  36. * This generates a curve which is:
  37. * volume_to_amplitude_table[128 + 4] = 0.25 * 3.16... ~= -2dB
  38. * volume_to_amplitude_table[64 + 4] = 0.25 * 1.0 ~= -12dB
  39. * volume_to_amplitude_table[32 + 4] = 0.25 * 0.316... ~= -22dB
  40. * volume_to_amplitude_table[16 + 4] = 0.25 * 0.1 ~= -32dB
  41. * etc.
  42. */
  43. volume_exponent = 1.0f / (2.0f * log10f(2.0f));
  44. for (i = 0; i <= volume_to_amplitude_scale; i++) {
  45. volume = (float)i / (float)volume_to_amplitude_scale;
  46. volume_to_amplitude_table[i + 4] = powf(2.0f * volume, volume_exponent) / 4.0f;
  47. }
  48. volume_to_amplitude_table[ -1 + 4] = 0.0f;
  49. volume_to_amplitude_table[129 + 4] = volume_to_amplitude_table[128 + 4];
  50. /* velocity to attenuation
  51. *
  52. * Creates the velocity to attenuation lookup table, for converting
  53. * velocities [1, 127] to full-velocity-sensitivity attenuation in
  54. * quarter decibels. Modeled after my TX-7's velocity response.*/
  55. velocity_to_attenuation[0] = 253.9999f;
  56. for (i = 1; i < 127; i++) {
  57. if (i >= 10) {
  58. ol = (powf(((float)i / 127.0f), 0.32f) - 1.0f) * 100.0f;
  59. amp = powf(2.0f, ol / 8.0f);
  60. } else {
  61. ol = (powf(((float)10 / 127.0f), 0.32f) - 1.0f) * 100.0f;
  62. amp = powf(2.0f, ol / 8.0f) * (float)i / 10.0f;
  63. }
  64. velocity_to_attenuation[i] = log10f(amp) * -80.0f;
  65. }
  66. velocity_to_attenuation[127] = 0.0f;
  67. /* quarter-decibel attenuation to amplitude */
  68. qdB_to_amplitude_table[-1 + 4] = 1.0f;
  69. for (i = 0; i <= 255; i++) {
  70. qdB_to_amplitude_table[i + 4] = powf(10.0f, (float)i / -80.0f);
  71. }
  72. tables_initialized = 1;
  73. }
  74. static inline float
  75. volume(float level)
  76. {
  77. unsigned char segment;
  78. float fract;
  79. level *= (float)volume_to_amplitude_scale;
  80. segment = lrintf(level - 0.5f);
  81. fract = level - (float)segment;
  82. return volume_to_amplitude_table[segment + 4] + fract *
  83. (volume_to_amplitude_table[segment + 5] -
  84. volume_to_amplitude_table[segment + 4]);
  85. }
  86. static inline float
  87. qdB_to_amplitude(float qdB)
  88. {
  89. int i = lrintf(qdB - 0.5f);
  90. float f = qdB - (float)i;
  91. return qdB_to_amplitude_table[i + 4] + f *
  92. (qdB_to_amplitude_table[i + 5] -
  93. qdB_to_amplitude_table[i + 4]);
  94. }
  95. void blosc_place_step_dd(float *buffer, int index, float phase, float w, float scale){
  96. float r;
  97. int i;
  98. r = MINBLEP_PHASES * phase / w;
  99. i = lrintf(r - 0.5f);
  100. r -= (float)i;
  101. i &= MINBLEP_PHASE_MASK; /* port changes can cause i to be out-of-range */
  102. /* This would be better than the above, but more expensive:
  103. * while (i < 0) {
  104. * i += MINBLEP_PHASES;
  105. * index++;
  106. * }
  107. */
  108. while (i < MINBLEP_PHASES * STEP_DD_PULSE_LENGTH) {
  109. buffer[index] += scale * (step_dd_table[i].value + r * step_dd_table[i].delta);
  110. i += MINBLEP_PHASES;
  111. index++;
  112. }
  113. }
  114. void vco(unsigned long sample_count, nekobee_voice_t *voice, struct blosc *osc,
  115. int index, float w)
  116. {
  117. unsigned long sample;
  118. float pos = osc->pos;
  119. float pw, gain, halfgain, out;
  120. pw=0.46f;
  121. gain=1.0f;
  122. halfgain=gain*0.5f;
  123. int bp_high = osc->bp_high;
  124. out=(bp_high ? halfgain : -halfgain);
  125. switch (osc->waveform)
  126. {
  127. default:
  128. case 0: {
  129. for (sample = 0; sample < sample_count; sample++) {
  130. pos += w;
  131. if (bp_high) {
  132. if (pos >= pw) {
  133. blosc_place_step_dd(voice->osc_audio, index, pos - pw, w, -gain);
  134. bp_high = 0;
  135. out = -halfgain;
  136. }
  137. if (pos >= 1.0f) {
  138. pos -= 1.0f;
  139. blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
  140. bp_high = 1;
  141. out = halfgain;
  142. }
  143. } else {
  144. if (pos >= 1.0f) {
  145. pos -= 1.0f;
  146. blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
  147. bp_high = 1;
  148. out = halfgain;
  149. }
  150. if (bp_high && pos >= pw) {
  151. blosc_place_step_dd(voice->osc_audio, index, pos - pw, w, -gain);
  152. bp_high = 0;
  153. out = -halfgain;
  154. }
  155. }
  156. voice->osc_audio[index + DD_SAMPLE_DELAY] += out;
  157. index++;
  158. }
  159. osc->pos = pos;
  160. osc->bp_high = bp_high;
  161. break;
  162. }
  163. case 1: // sawtooth wave
  164. {
  165. for (sample=0; sample < sample_count; sample++) {
  166. pos += w;
  167. if (pos >= 1.0f) {
  168. pos -= 1.0f;
  169. blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
  170. }
  171. voice->osc_audio[index + DD_SAMPLE_DELAY] += gain * (0.5f - pos);
  172. index++;
  173. }
  174. break;
  175. }
  176. }
  177. osc->pos=pos;
  178. }
  179. static inline void
  180. vcf_4pole(nekobee_voice_t *voice, unsigned long sample_count,
  181. float *in, float *out, float *cutoff, float qres, float *amp)
  182. {
  183. unsigned long sample;
  184. float freqcut, freqcut2, highpass,
  185. delay1 = voice->delay1,
  186. delay2 = voice->delay2,
  187. delay3 = voice->delay3,
  188. delay4 = voice->delay4;
  189. qres = 2.0f - qres * 1.995f;
  190. for (sample = 0; sample < sample_count; sample++) {
  191. /* Hal Chamberlin's state variable filter */
  192. freqcut = cutoff[sample] * 2.0f;
  193. freqcut2 = cutoff[sample] * 4.0f;
  194. if (freqcut > VCF_FREQ_MAX) freqcut = VCF_FREQ_MAX;
  195. if (freqcut2 > VCF_FREQ_MAX) freqcut2 = VCF_FREQ_MAX;
  196. delay2 = delay2 + freqcut * delay1; /* delay2/4 = lowpass output */
  197. highpass = in[sample] - delay2 - qres * delay1;
  198. delay1 = freqcut * highpass + delay1; /* delay1/3 = bandpass output */
  199. delay4 = delay4 + freqcut2 * delay3;
  200. highpass = delay2 - delay4 - qres * delay3;
  201. delay3 = freqcut2 * highpass + delay3;
  202. /* mix filter output into output buffer */
  203. out[sample] += 0.1*atan(3*delay4 * amp[sample]);
  204. }
  205. voice->delay1 = delay1;
  206. voice->delay2 = delay2;
  207. voice->delay3 = delay3;
  208. voice->delay4 = delay4;
  209. voice->c5 = 0.0f;
  210. }
  211. /*
  212. * nekobee_voice_render
  213. *
  214. * generate the actual sound data for this voice
  215. */
  216. void
  217. nekobee_voice_render(nekobee_synth_t *synth, nekobee_voice_t *voice,
  218. float *out, unsigned long sample_count,
  219. int do_control_update)
  220. {
  221. unsigned long sample;
  222. /* state variables saved in voice */
  223. float lfo_pos = voice->lfo_pos,
  224. vca_eg = voice->vca_eg,
  225. vcf_eg = voice->vcf_eg;
  226. unsigned char vca_eg_phase = voice->vca_eg_phase,
  227. vcf_eg_phase = voice->vcf_eg_phase;
  228. int osc_index = voice->osc_index;
  229. /* temporary variables used in calculating voice */
  230. float fund_pitch;
  231. float deltat = synth->deltat;
  232. float freq, cutoff, vcf_amt;
  233. float vcf_acc_amt;
  234. /* set up synthesis variables from patch */
  235. float omega;
  236. float vca_eg_amp = qdB_to_amplitude(velocity_to_attenuation[voice->velocity] * 0);
  237. float vca_eg_rate_level[3], vca_eg_one_rate[3];
  238. float vcf_eg_amp = qdB_to_amplitude(velocity_to_attenuation[voice->velocity] * 0);
  239. float vcf_eg_rate_level[3], vcf_eg_one_rate[3];
  240. float qres = synth->resonance;
  241. float vol_out = volume(synth->volume);
  242. float velocity = (voice->velocity);
  243. float vcf_egdecay = synth->decay;
  244. fund_pitch = 0.1f*voice->target_pitch +0.9 * voice->prev_pitch; /* glide */
  245. if (do_control_update) {
  246. voice->prev_pitch = fund_pitch; /* save pitch for next time */
  247. }
  248. fund_pitch *= 440.0f;
  249. omega = synth->tuning * fund_pitch;
  250. // if we have triggered ACCENT
  251. // we need a shorter decay
  252. // we should probably have something like this in the note on code
  253. // that could trigger an ACCENT light
  254. if (velocity>90) {
  255. vcf_egdecay=.0005;
  256. }
  257. // VCA - In a real 303, it is set for around 2 seconds
  258. vca_eg_rate_level[0] = 0.1f * vca_eg_amp; // instant on attack
  259. vca_eg_one_rate[0] = 0.9f; // very fast
  260. vca_eg_rate_level[1] = 0.0f; // sustain is zero
  261. vca_eg_one_rate[1] = 1.0f - 0.00001f; // decay time is very slow
  262. vca_eg_rate_level[2] = 0.0f; // decays to zero
  263. vca_eg_one_rate[2] = 0.975f; // very fast release
  264. // VCF - funny things go on with the accent
  265. vcf_eg_rate_level[0] = 0.1f * vcf_eg_amp;
  266. vcf_eg_one_rate[0] = 1-0.1f; //0.9f;
  267. vcf_eg_rate_level[1] = 0.0f; // vcf_egdecay * *(synth->vcf_eg_sustain_level) * vcf_eg_amp;
  268. vcf_eg_one_rate[1] = 1.0f - vcf_egdecay;
  269. vcf_eg_rate_level[2] = 0.0f;
  270. vcf_eg_one_rate[2] = 0.9995f; // 1.0f - *(synth->vcf_eg_release_time);
  271. vca_eg_amp *= 0.99f;
  272. vcf_eg_amp *= 0.99f;
  273. freq = M_PI_F * deltat * fund_pitch * synth->mod_wheel; /* now (0 to 1) * pi */
  274. cutoff = 0.008f * synth->cutoff;
  275. // 303 always has slight VCF mod
  276. vcf_amt = 0.05f+(synth->envmod*0.75);
  277. /* copy some things so oscillator functions can see them */
  278. voice->osc1.waveform = lrintf(synth->waveform);
  279. // work out how much the accent will affect the filter
  280. vcf_acc_amt=.333f+ (synth->resonance/1.5f);
  281. for (sample = 0; sample < sample_count; sample++) {
  282. vca_eg = vca_eg_rate_level[vca_eg_phase] + vca_eg_one_rate[vca_eg_phase] * vca_eg;
  283. vcf_eg = vcf_eg_rate_level[vcf_eg_phase] + vcf_eg_one_rate[vcf_eg_phase] * vcf_eg;
  284. voice->freqcut_buf[sample] = (cutoff + (vcf_amt * vcf_eg/2.0f) + (synth->vcf_accent * synth->accent*0.5f));
  285. voice->vca_buf[sample] = vca_eg * vol_out*(1.0f + synth->accent*synth->vca_accent);
  286. if (!vca_eg_phase && vca_eg > vca_eg_amp) vca_eg_phase = 1; /* flip from attack to decay */
  287. if (!vcf_eg_phase && vcf_eg > vcf_eg_amp) vcf_eg_phase = 1; /* flip from attack to decay */
  288. }
  289. // oscillator
  290. vco(sample_count, voice, &voice->osc1, osc_index, deltat * omega);
  291. // VCF and VCA
  292. vcf_4pole(voice, sample_count, voice->osc_audio + osc_index, out, voice->freqcut_buf, qres, voice->vca_buf);
  293. osc_index += sample_count;
  294. if (do_control_update) {
  295. /* do those things should be done only once per control-calculation
  296. * interval ("nugget"), such as voice check-for-dead, pitch envelope
  297. * calculations, volume envelope phase transition checks, etc. */
  298. /* check if we've decayed to nothing, turn off voice if so */
  299. if (vca_eg_phase == 2 && voice->vca_buf[sample_count - 1] < 6.26e-6f) {
  300. // sound has completed its release phase (>96dB below volume '5' max)
  301. XDB_MESSAGE(XDB_NOTE, " nekobee_voice_render check for dead: killing note id %d\n", voice->note_id);
  302. nekobee_voice_off(voice);
  303. return; // we're dead now, so return
  304. }
  305. /* already saved prev_pitch above */
  306. /* check oscillator audio buffer index, shift buffer if necessary */
  307. if (osc_index > MINBLEP_BUFFER_LENGTH - (XSYNTH_NUGGET_SIZE + LONGEST_DD_PULSE_LENGTH)) {
  308. memcpy(voice->osc_audio, voice->osc_audio + osc_index,
  309. LONGEST_DD_PULSE_LENGTH * sizeof (float));
  310. memset(voice->osc_audio + LONGEST_DD_PULSE_LENGTH, 0,
  311. (MINBLEP_BUFFER_LENGTH - LONGEST_DD_PULSE_LENGTH) * sizeof (float));
  312. osc_index = 0;
  313. }
  314. }
  315. /* save things for next time around */
  316. voice->lfo_pos = lfo_pos;
  317. voice->vca_eg = vca_eg;
  318. voice->vca_eg_phase = vca_eg_phase;
  319. voice->vcf_eg = vcf_eg;
  320. voice->vcf_eg_phase = vcf_eg_phase;
  321. voice->osc_index = osc_index;
  322. return;
  323. (void)freq;
  324. (void)vcf_acc_amt;
  325. }