eas_synth.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*----------------------------------------------------------------------------
  2. *
  3. * File:
  4. * eas_synth.h
  5. *
  6. * Contents and purpose:
  7. * Declarations, interfaces, and prototypes for synth.
  8. *
  9. * Copyright Sonic Network Inc. 2004, 2005
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. *----------------------------------------------------------------------------
  23. * Revision Control:
  24. * $Revision: 718 $
  25. * $Date: 2007-06-08 16:43:16 -0700 (Fri, 08 Jun 2007) $
  26. *----------------------------------------------------------------------------
  27. */
  28. #ifndef _EAS_SYNTH_H
  29. #define _EAS_SYNTH_H
  30. #include "eas_types.h"
  31. #include "eas_sndlib.h"
  32. #ifdef _WT_SYNTH
  33. #include "eas_wtsynth.h"
  34. #endif
  35. #ifdef _FM_SYNTH
  36. #include "eas_fmsynth.h"
  37. #endif
  38. #ifndef NUM_OUTPUT_CHANNELS
  39. #define NUM_OUTPUT_CHANNELS 2
  40. #endif
  41. #ifndef MAX_SYNTH_VOICES
  42. #define MAX_SYNTH_VOICES 64
  43. #endif
  44. #ifndef MAX_VIRTUAL_SYNTHESIZERS
  45. #define MAX_VIRTUAL_SYNTHESIZERS 4
  46. #endif
  47. /* defines */
  48. #ifndef NUM_PRIMARY_VOICES
  49. #define NUM_PRIMARY_VOICES MAX_SYNTH_VOICES
  50. #elif !defined(NUM_SECONDARY_VOICES)
  51. #define NUM_SECONDARY_VOICES (MAX_SYNTH_VOICES - NUM_PRIMARY_VOICES)
  52. #endif
  53. #if defined(EAS_WT_SYNTH)
  54. #define NUM_WT_VOICES MAX_SYNTH_VOICES
  55. /* FM on MCU */
  56. #elif defined(EAS_FM_SYNTH)
  57. #define NUM_FM_VOICES MAX_SYNTH_VOICES
  58. /* wavetable drums on MCU, wavetable melodic on DSP */
  59. #elif defined(EAS_SPLIT_WT_SYNTH)
  60. #define NUM_WT_VOICES MAX_SYNTH_VOICES
  61. /* wavetable drums and FM melodic on MCU */
  62. #elif defined(EAS_HYBRID_SYNTH)
  63. #define NUM_WT_VOICES NUM_PRIMARY_VOICES
  64. #define NUM_FM_VOICES NUM_SECONDARY_VOICES
  65. /* wavetable drums on MCU, FM melodic on DSP */
  66. #elif defined(EAS_SPLIT_HYBRID_SYNTH)
  67. #define NUM_WT_VOICES NUM_PRIMARY_VOICES
  68. #define NUM_FM_VOICES NUM_SECONDARY_VOICES
  69. /* FM synth on DSP */
  70. #elif defined(EAS_SPLIT_FM_SYNTH)
  71. #define NUM_FM_VOICES MAX_SYNTH_VOICES
  72. #else
  73. #error "Unrecognized architecture option"
  74. #endif
  75. #define NUM_SYNTH_CHANNELS 16
  76. #define DEFAULT_SYNTH_VOICES MAX_SYNTH_VOICES
  77. /* use the following values to specify unassigned channels or voices */
  78. #define UNASSIGNED_SYNTH_CHANNEL NUM_SYNTH_CHANNELS
  79. #define UNASSIGNED_SYNTH_VOICE MAX_SYNTH_VOICES
  80. /* synth parameters are updated every SYNTH_UPDATE_PERIOD_IN_SAMPLES */
  81. #define SYNTH_UPDATE_PERIOD_IN_SAMPLES (EAS_I32)(0x1L << SYNTH_UPDATE_PERIOD_IN_BITS)
  82. /* stealing weighting factors */
  83. #define NOTE_AGE_STEAL_WEIGHT 1
  84. #define NOTE_GAIN_STEAL_WEIGHT 4
  85. #define CHANNEL_POLY_STEAL_WEIGHT 12
  86. #define CHANNEL_PRIORITY_STEAL_WEIGHT 2
  87. #define NOTE_MATCH_PENALTY 128
  88. #define SYNTH_PRIORITY_WEIGHT 8
  89. /* default synth master volume */
  90. #define DEFAULT_SYNTH_MASTER_VOLUME 0x7fff
  91. #define DEFAULT_SYNTH_PRIORITY 5
  92. /* default tuning values */
  93. #define DEFAULT_PITCH_BEND_SENSITIVITY 200 /* 2 semitones */
  94. #define DEFAULT_FINE_PITCH 0 /* 0 cents */
  95. #define DEFAULT_COARSE_PITCH 0 /* 0 semitones */
  96. /* default drum channel is 10, but is internally 9 due to unit offset */
  97. #define DEFAULT_DRUM_CHANNEL 9
  98. /* drum channel can simultaneously play this many voices at most */
  99. #define DEFAULT_CHANNEL_POLYPHONY_LIMIT 2
  100. /* default instrument is acoustic piano */
  101. #define DEFAULT_MELODY_BANK_MSB 0x79
  102. #define DEFAULT_RHYTHM_BANK_MSB 0x78
  103. #define DEFAULT_MELODY_BANK_NUMBER (DEFAULT_MELODY_BANK_MSB << 8)
  104. #define DEFAULT_RHYTHM_BANK_NUMBER (DEFAULT_RHYTHM_BANK_MSB << 8)
  105. #define DEFAULT_SYNTH_PROGRAM_NUMBER 0
  106. #define DEFAULT_PITCH_BEND 0x2000 /* 0x2000 == (0x40 << 7) | 0x00 */
  107. #define DEFAULT_MOD_WHEEL 0
  108. #define DEFAULT_CHANNEL_VOLUME 0x64
  109. #define DEFAULT_PAN 0x40 /* decimal 64, center */
  110. #ifdef _REVERB
  111. #define DEFAULT_REVERB_SEND 40 /* some reverb */
  112. #endif
  113. #ifdef _CHORUS
  114. #define DEFAULT_CHORUS_SEND 0 /* no chorus */
  115. #endif
  116. #define DEFAULT_EAS_FILTER_CUTOFF_FREQUENCY 0 /* EAS synth uses a different default */
  117. #define DEFAULT_FILTER_RESONANCE 0
  118. #define DEFAULT_EXPRESSION 0x7F
  119. #define DEFAULT_CHANNEL_PRESSURE 0
  120. #define DEFAULT_REGISTERED_PARAM 0x3FFF
  121. #define DEFAULT_CHANNEL_STATIC_GAIN 0
  122. #define DEFAULT_CHANNEL_STATIC_PITCH 0
  123. #define DEFAULT_LFO_MOD_WHEEL_TO_PITCH_CENTS 50
  124. #define DEFAULT_LFO_CHANNEL_PRESSURE_TO_PITCH_CENTS 50
  125. #define DEFAULT_KEY_NUMBER 0x69
  126. #define DEFAULT_VELOCITY 0x64
  127. #define DEFAULT_REGION_INDEX 0
  128. #define DEFAULT_ARTICULATION_INDEX 0
  129. #define DEFAULT_VOICE_GAIN 0
  130. #define DEFAULT_AGE 0
  131. #define DEFAULT_SP_MIDI_PRIORITY 16
  132. /* filter defines */
  133. #define DEFAULT_FILTER_ZERO 0
  134. #define FILTER_CUTOFF_MAX_PITCH_CENTS 1919
  135. #define FILTER_CUTOFF_MIN_PITCH_CENTS -4467
  136. #define A5_PITCH_OFFSET_IN_CENTS 6900
  137. /*------------------------------------
  138. * S_SYNTH_CHANNEL data structure
  139. *------------------------------------
  140. */
  141. /* S_SYNTH_CHANNEL.m_nFlags */
  142. #define CHANNEL_FLAG_SUSTAIN_PEDAL 0x01
  143. #define CHANNEL_FLAG_MUTE 0x02
  144. #define CHANNEL_FLAG_UPDATE_CHANNEL_PARAMETERS 0x04
  145. #define CHANNEL_FLAG_RHYTHM_CHANNEL 0x08
  146. #define CHANNEL_FLAG_EXTERNAL_AUDIO 0x10
  147. #define DEFAULT_CHANNEL_FLAGS 0
  148. /* macros for extracting virtual synth and channel numbers */
  149. #define GET_VSYNTH(a) ((a) >> 4)
  150. #define GET_CHANNEL(a) ((a) & 15)
  151. typedef struct s_synth_channel_tag
  152. {
  153. /* use static channel parameters to reduce MIPs */
  154. /* parameters shared by multiple voices assigned to same channel */
  155. EAS_I32 staticPitch; /* (pitch bend * pitch sens) + fine pitch */
  156. EAS_I16 staticGain; /* (CC7 * CC11 * master vol)^2 */
  157. EAS_U16 regionIndex; /* index of first region in program */
  158. EAS_U16 bankNum; /* play programs from this bank */
  159. EAS_I16 pitchBend; /* pitch wheel value */
  160. EAS_I16 pitchBendSensitivity;
  161. EAS_I16 registeredParam; /* currently selected registered param */
  162. #if defined(_FM_SYNTH)
  163. EAS_I16 lfoAmt; /* amount of LFO to apply to voice */
  164. #endif
  165. EAS_U8 programNum; /* play this instrument number */
  166. EAS_U8 modWheel; /* CC1 */
  167. EAS_U8 volume; /* CC7 */
  168. EAS_U8 pan; /* CC10 */
  169. EAS_U8 expression; /* CC11 */
  170. /* the following parameters are controlled by RPNs */
  171. EAS_I8 finePitch;
  172. EAS_I8 coarsePitch;
  173. EAS_U8 channelPressure; /* applied to all voices on a given channel */
  174. EAS_U8 channelFlags; /* bit field channelFlags for */
  175. /* CC64, SP-MIDI channel masking */
  176. EAS_U8 pool; /* SPMIDI channel voice pool */
  177. EAS_U8 mip; /* SPMIDI MIP setting */
  178. #ifdef _REVERB
  179. EAS_U8 reverbSend; /* CC91 */
  180. #endif
  181. #ifdef _CHORUS
  182. EAS_U8 chorusSend; /* CC93 */
  183. #endif
  184. } S_SYNTH_CHANNEL;
  185. /*------------------------------------
  186. * S_SYNTH_VOICE data structure
  187. *------------------------------------
  188. */
  189. /* S_SYNTH_VOICE.m_nFlags */
  190. #define VOICE_FLAG_UPDATE_VOICE_PARAMETERS 0x01
  191. #define VOICE_FLAG_SUSTAIN_PEDAL_DEFER_NOTE_OFF 0x02
  192. #define VOICE_FLAG_DEFER_MIDI_NOTE_OFF 0x04
  193. #define VOICE_FLAG_NO_SAMPLES_SYNTHESIZED_YET 0x08
  194. #define VOICE_FLAG_DEFER_MUTE 0x40
  195. #define DEFAULT_VOICE_FLAGS 0
  196. /* S_SYNTH_VOICE.m_eState */
  197. typedef enum {
  198. eVoiceStateFree = 0,
  199. eVoiceStateStart,
  200. eVoiceStatePlay,
  201. eVoiceStateRelease,
  202. eVoiceStateMuting,
  203. eVoiceStateStolen,
  204. eVoiceStateInvalid /* should never be in this state! */
  205. } E_VOICE_STATE;
  206. #define DEFAULT_VOICE_STATE eVoiceStateFree
  207. typedef struct s_synth_voice_tag
  208. {
  209. /* These parameters are common to both wavetable and FM
  210. * synthesizers. The voice manager should only access this data.
  211. * Any other data should be manipulated by the code that is
  212. * specific to that synthesizer and reflected back through the
  213. * common state data available here.
  214. */
  215. EAS_U16 regionIndex; /* index to wave and playback params */
  216. EAS_I16 gain; /* current gain */
  217. EAS_U16 age; /* large value means old note */
  218. EAS_U16 nextRegionIndex; /* index to wave and playback params */
  219. EAS_U8 voiceState; /* current voice state */
  220. EAS_U8 voiceFlags; /* misc flags/bit fields */
  221. EAS_U8 channel; /* this voice plays on this synth channel */
  222. EAS_U8 note; /* 12 <= key number <= 108 */
  223. EAS_U8 velocity; /* 0 <= velocity <= 127 */
  224. EAS_U8 nextChannel; /* play stolen voice on this channel */
  225. EAS_U8 nextNote; /* 12 <= key number <= 108 */
  226. EAS_U8 nextVelocity; /* 0 <= velocity <= 127 */
  227. } S_SYNTH_VOICE;
  228. /*------------------------------------
  229. * S_SYNTH data structure
  230. *
  231. * One instance for each MIDI stream
  232. *------------------------------------
  233. */
  234. /* S_SYNTH.m_nFlags */
  235. #define SYNTH_FLAG_RESET_IS_REQUESTED 0x01
  236. #define SYNTH_FLAG_SP_MIDI_ON 0x02
  237. #define SYNTH_FLAG_UPDATE_ALL_CHANNEL_PARAMETERS 0x04
  238. #define SYNTH_FLAG_DEFERRED_MIDI_NOTE_OFF_PENDING 0x08
  239. #define DEFAULT_SYNTH_FLAGS SYNTH_FLAG_UPDATE_ALL_CHANNEL_PARAMETERS
  240. typedef struct s_synth_tag
  241. {
  242. struct s_eas_data_tag *pEASData;
  243. const S_EAS *pEAS;
  244. #ifdef DLS_SYNTHESIZER
  245. S_DLS *pDLS;
  246. #endif
  247. #ifdef EXTERNAL_AUDIO
  248. EAS_EXT_PRG_CHG_FUNC cbProgChgFunc;
  249. EAS_EXT_EVENT_FUNC cbEventFunc;
  250. EAS_VOID_PTR *pExtAudioInstData;
  251. #endif
  252. S_SYNTH_CHANNEL channels[NUM_SYNTH_CHANNELS];
  253. EAS_I32 totalNoteCount;
  254. EAS_U16 maxPolyphony;
  255. EAS_U16 numActiveVoices;
  256. EAS_U16 masterVolume;
  257. EAS_U8 channelsByPriority[NUM_SYNTH_CHANNELS];
  258. EAS_U8 poolCount[NUM_SYNTH_CHANNELS];
  259. EAS_U8 poolAlloc[NUM_SYNTH_CHANNELS];
  260. EAS_U8 synthFlags;
  261. EAS_I8 globalTranspose;
  262. EAS_U8 vSynthNum;
  263. EAS_U8 refCount;
  264. EAS_U8 priority;
  265. } S_SYNTH;
  266. /*------------------------------------
  267. * S_VOICE_MGR data structure
  268. *
  269. * One instance for each EAS library instance
  270. *------------------------------------
  271. */
  272. typedef struct s_voice_mgr_tag
  273. {
  274. S_SYNTH *pSynth[MAX_VIRTUAL_SYNTHESIZERS];
  275. EAS_PCM voiceBuffer[SYNTH_UPDATE_PERIOD_IN_SAMPLES];
  276. #ifdef _FM_SYNTH
  277. EAS_PCM operMixBuffer[SYNTH_UPDATE_PERIOD_IN_SAMPLES];
  278. S_FM_VOICE fmVoices[NUM_FM_VOICES];
  279. #endif
  280. #ifdef _WT_SYNTH
  281. S_WT_VOICE wtVoices[NUM_WT_VOICES];
  282. #endif
  283. #ifdef _REVERB
  284. EAS_PCM reverbSendBuffer[NUM_OUTPUT_CHANNELS * SYNTH_UPDATE_PERIOD_IN_SAMPLES];
  285. #endif
  286. #ifdef _CHORUS
  287. EAS_PCM chorusSendBuffer[NUM_OUTPUT_CHANNELS * SYNTH_UPDATE_PERIOD_IN_SAMPLES];
  288. #endif
  289. S_SYNTH_VOICE voices[MAX_SYNTH_VOICES];
  290. EAS_SNDLIB_HANDLE pGlobalEAS;
  291. #ifdef DLS_SYNTHESIZER
  292. S_DLS *pGlobalDLS;
  293. #endif
  294. #ifdef _SPLIT_ARCHITECTURE
  295. EAS_FRAME_BUFFER_HANDLE pFrameBuffer;
  296. #endif
  297. #if defined(_SECONDARY_SYNTH) || defined(EAS_SPLIT_WT_SYNTH)
  298. EAS_U16 maxPolyphonyPrimary;
  299. EAS_U16 maxPolyphonySecondary;
  300. #endif
  301. EAS_I32 workload;
  302. EAS_I32 maxWorkLoad;
  303. EAS_U16 activeVoices;
  304. EAS_U16 maxPolyphony;
  305. EAS_U16 age;
  306. /* limits the number of voice starts in a frame for split architecture */
  307. #ifdef MAX_VOICE_STARTS
  308. EAS_U16 numVoiceStarts;
  309. #endif
  310. } S_VOICE_MGR;
  311. #endif /* #ifdef _EAS_SYNTH_H */