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_synth.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* nekobee DSSI software synthesizer plugin
  2. *
  3. * Copyright (C) 2004 Sean Bolton and others.
  4. *
  5. * Portions of this file may have come from Steve Brookes'
  6. * nekobee, copyright (C) 1999 S. J. Brookes.
  7. * Portions of this file may have come from Peter Hanappe's
  8. * Fluidsynth, copyright (C) 2003 Peter Hanappe and others.
  9. * Portions of this file may have come from Chris Cannam and Steve
  10. * Harris's public domain DSSI example code.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be
  18. * useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  20. * PURPOSE. See the GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public
  23. * License along with this program; if not, write to the Free
  24. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307, USA.
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include <pthread.h>
  32. #include "nekobee.h"
  33. #include "nekobee_synth.h"
  34. #include "nekobee_voice.h"
  35. /*
  36. * nekobee_synth_all_voices_off
  37. *
  38. * stop processing all notes immediately
  39. */
  40. void
  41. nekobee_synth_all_voices_off(nekobee_synth_t *synth)
  42. {
  43. int i;
  44. nekobee_voice_t *voice;
  45. for (i = 0; i < synth->voices; i++) {
  46. //voice = synth->voice[i];
  47. voice = synth->voice;
  48. if (_PLAYING(voice)) {
  49. nekobee_voice_off(voice);
  50. }
  51. }
  52. for (i = 0; i < 8; i++) synth->held_keys[i] = -1;
  53. }
  54. /*
  55. * nekobee_synth_note_off
  56. *
  57. * handle a note off message
  58. */
  59. void
  60. nekobee_synth_note_off(nekobee_synth_t *synth, unsigned char key, unsigned char rvelocity)
  61. {
  62. int i, count = 0;
  63. nekobee_voice_t *voice;
  64. for (i = 0; i < synth->voices; i++) {
  65. voice = synth->voice;
  66. if (_PLAYING(voice)) {
  67. XDB_MESSAGE(XDB_NOTE, " nekobee_synth_note_off: key %d rvel %d voice %d note id %d\n", key, rvelocity, i, voice->note_id);
  68. nekobee_voice_note_off(synth, voice, key, 64);
  69. count++;
  70. }
  71. }
  72. if (!count)
  73. nekobee_voice_remove_held_key(synth, key);
  74. return;
  75. (void)rvelocity;
  76. }
  77. /*
  78. * nekobee_synth_all_notes_off
  79. *
  80. * put all notes into the released state
  81. */
  82. void
  83. nekobee_synth_all_notes_off(nekobee_synth_t* synth)
  84. {
  85. int i;
  86. nekobee_voice_t *voice;
  87. /* reset the sustain controller */
  88. synth->cc[MIDI_CTL_SUSTAIN] = 0;
  89. for (i = 0; i < synth->voices; i++) {
  90. //voice = synth->voice[i];
  91. voice = synth->voice;
  92. if (_ON(voice) || _SUSTAINED(voice)) {
  93. nekobee_voice_release_note(synth, voice);
  94. }
  95. }
  96. }
  97. /*
  98. * nekobee_synth_note_on
  99. */
  100. void
  101. nekobee_synth_note_on(nekobee_synth_t *synth, unsigned char key, unsigned char velocity)
  102. {
  103. nekobee_voice_t* voice;
  104. voice = synth->voice;
  105. if (_PLAYING(synth->voice)) {
  106. XDB_MESSAGE(XDB_NOTE, " nekobee_synth_note_on: retriggering mono voice on new key %d\n", key);
  107. }
  108. voice->note_id = synth->note_id++;
  109. nekobee_voice_note_on(synth, voice, key, velocity);
  110. }
  111. /*
  112. * nekobee_synth_update_volume
  113. */
  114. void
  115. nekobee_synth_update_volume(nekobee_synth_t* synth)
  116. {
  117. synth->cc_volume = (float)(synth->cc[MIDI_CTL_MSB_MAIN_VOLUME] * 128 +
  118. synth->cc[MIDI_CTL_LSB_MAIN_VOLUME]) / 16256.0f;
  119. if (synth->cc_volume > 1.0f)
  120. synth->cc_volume = 1.0f;
  121. /* don't need to check if any playing voices need updating, because it's global */
  122. }
  123. /*
  124. * nekobee_synth_control_change
  125. */
  126. void
  127. nekobee_synth_control_change(nekobee_synth_t *synth, unsigned int param, signed int value)
  128. {
  129. synth->cc[param] = value;
  130. switch (param) {
  131. case MIDI_CTL_MSB_MAIN_VOLUME:
  132. case MIDI_CTL_LSB_MAIN_VOLUME:
  133. nekobee_synth_update_volume(synth);
  134. break;
  135. case MIDI_CTL_ALL_SOUNDS_OFF:
  136. nekobee_synth_all_voices_off(synth);
  137. break;
  138. case MIDI_CTL_RESET_CONTROLLERS:
  139. nekobee_synth_init_controls(synth);
  140. break;
  141. case MIDI_CTL_ALL_NOTES_OFF:
  142. nekobee_synth_all_notes_off(synth);
  143. break;
  144. /* what others should we respond to? */
  145. /* these we ignore (let the host handle):
  146. * BANK_SELECT_MSB
  147. * BANK_SELECT_LSB
  148. * DATA_ENTRY_MSB
  149. * NRPN_MSB
  150. * NRPN_LSB
  151. * RPN_MSB
  152. * RPN_LSB
  153. * -FIX- no! we need RPN (0, 0) Pitch Bend Sensitivity!
  154. */
  155. }
  156. }
  157. /*
  158. * nekobee_synth_init_controls
  159. */
  160. void
  161. nekobee_synth_init_controls(nekobee_synth_t *synth)
  162. {
  163. int i;
  164. for (i = 0; i < 128; i++) {
  165. synth->cc[i] = 0;
  166. }
  167. synth->cc[7] = 127; /* full volume */
  168. nekobee_synth_update_volume(synth);
  169. }
  170. /*
  171. * nekobee_synth_render_voices
  172. */
  173. void
  174. nekobee_synth_render_voices(nekobee_synth_t *synth, float *out, unsigned long sample_count,
  175. int do_control_update)
  176. {
  177. unsigned long i;
  178. float res, wow;
  179. /* clear the buffer */
  180. for (i = 0; i < sample_count; i++)
  181. out[i] = 0.0f;
  182. // we can do anything that must be updated all the time here
  183. // this is called even when a voice isn't playing
  184. // approximate a log scale
  185. res = 1-synth->resonance;
  186. wow = res*res;
  187. wow = wow/10.0f;
  188. // as the resonance is increased, "wow" slows down the accent attack
  189. if ((synth->voice->velocity>90) && (synth->vcf_accent < synth->voice->vcf_eg)) {
  190. synth->vcf_accent=(0.985-wow)*synth->vcf_accent+(0.015+wow)*synth->voice->vcf_eg;
  191. } else {
  192. synth->vcf_accent=(0.985-wow)*synth->vcf_accent; // or just decay
  193. }
  194. if (synth->voice->velocity>90) {
  195. synth->vca_accent=0.95*synth->vca_accent+0.05; // ramp up accent on with a time constant
  196. } else {
  197. synth->vca_accent=0.95*synth->vca_accent; // accent off with time constant
  198. }
  199. #if defined(XSYNTH_DEBUG) && (XSYNTH_DEBUG & XDB_AUDIO)
  200. out[0] += 0.10f; /* add a 'buzz' to output so there's something audible even when quiescent */
  201. #endif /* defined(XSYNTH_DEBUG) && (XSYNTH_DEBUG & XDB_AUDIO) */
  202. if (_PLAYING(synth->voice)) {
  203. nekobee_voice_render(synth, synth->voice, out, sample_count, do_control_update);
  204. }
  205. }