emux_synth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Midi synth routines for the Emu8k/Emu10k1
  3. *
  4. * Copyright (C) 1999 Steve Ratcliffe
  5. * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  6. *
  7. * Contains code based on awe_wave.c by Takashi Iwai
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include "emux_voice.h"
  25. #include <sound/asoundef.h>
  26. /*
  27. * Prototypes
  28. */
  29. /*
  30. * Ensure a value is between two points
  31. * macro evaluates its args more than once, so changed to upper-case.
  32. */
  33. #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
  34. #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
  35. static int get_zone(snd_emux_t *emu, snd_emux_port_t *port, int *notep, int vel, snd_midi_channel_t *chan, snd_sf_zone_t **table);
  36. static int get_bank(snd_emux_port_t *port, snd_midi_channel_t *chan);
  37. static void terminate_note1(snd_emux_t *emu, int note, snd_midi_channel_t *chan, int free);
  38. static void exclusive_note_off(snd_emux_t *emu, snd_emux_port_t *port, int exclass);
  39. static void terminate_voice(snd_emux_t *emu, snd_emux_voice_t *vp, int free);
  40. static void update_voice(snd_emux_t *emu, snd_emux_voice_t *vp, int update);
  41. static void setup_voice(snd_emux_voice_t *vp);
  42. static int calc_pan(snd_emux_voice_t *vp);
  43. static int calc_volume(snd_emux_voice_t *vp);
  44. static int calc_pitch(snd_emux_voice_t *vp);
  45. /*
  46. * Start a note.
  47. */
  48. void
  49. snd_emux_note_on(void *p, int note, int vel, snd_midi_channel_t *chan)
  50. {
  51. snd_emux_t *emu;
  52. int i, key, nvoices;
  53. snd_emux_voice_t *vp;
  54. snd_sf_zone_t *table[SNDRV_EMUX_MAX_MULTI_VOICES];
  55. unsigned long flags;
  56. snd_emux_port_t *port;
  57. port = p;
  58. snd_assert(port != NULL && chan != NULL, return);
  59. emu = port->emu;
  60. snd_assert(emu != NULL, return);
  61. snd_assert(emu->ops.get_voice != NULL, return);
  62. snd_assert(emu->ops.trigger != NULL, return);
  63. key = note; /* remember the original note */
  64. nvoices = get_zone(emu, port, &note, vel, chan, table);
  65. if (! nvoices)
  66. return;
  67. /* exclusive note off */
  68. for (i = 0; i < nvoices; i++) {
  69. snd_sf_zone_t *zp = table[i];
  70. if (zp && zp->v.exclusiveClass)
  71. exclusive_note_off(emu, port, zp->v.exclusiveClass);
  72. }
  73. #if 0 // seems not necessary
  74. /* Turn off the same note on the same channel. */
  75. terminate_note1(emu, key, chan, 0);
  76. #endif
  77. spin_lock_irqsave(&emu->voice_lock, flags);
  78. for (i = 0; i < nvoices; i++) {
  79. /* set up each voice parameter */
  80. /* at this stage, we don't trigger the voice yet. */
  81. if (table[i] == NULL)
  82. continue;
  83. vp = emu->ops.get_voice(emu, port);
  84. if (vp == NULL || vp->ch < 0)
  85. continue;
  86. snd_assert(vp->emu != NULL && vp->hw != NULL, return);
  87. if (STATE_IS_PLAYING(vp->state))
  88. emu->ops.terminate(vp);
  89. vp->time = emu->use_time++;
  90. vp->chan = chan;
  91. vp->port = port;
  92. vp->key = key;
  93. vp->note = note;
  94. vp->velocity = vel;
  95. vp->zone = table[i];
  96. if (vp->zone->sample)
  97. vp->block = vp->zone->sample->block;
  98. else
  99. vp->block = NULL;
  100. setup_voice(vp);
  101. vp->state = SNDRV_EMUX_ST_STANDBY;
  102. if (emu->ops.prepare) {
  103. vp->state = SNDRV_EMUX_ST_OFF;
  104. if (emu->ops.prepare(vp) >= 0)
  105. vp->state = SNDRV_EMUX_ST_STANDBY;
  106. }
  107. }
  108. /* start envelope now */
  109. for (i = 0; i < emu->max_voices; i++) {
  110. vp = &emu->voices[i];
  111. if (vp->state == SNDRV_EMUX_ST_STANDBY &&
  112. vp->chan == chan) {
  113. emu->ops.trigger(vp);
  114. vp->state = SNDRV_EMUX_ST_ON;
  115. vp->ontime = jiffies; /* remember the trigger timing */
  116. }
  117. }
  118. spin_unlock_irqrestore(&emu->voice_lock, flags);
  119. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  120. if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
  121. /* clear voice position for the next note on this channel */
  122. snd_emux_effect_table_t *fx = chan->private;
  123. if (fx) {
  124. fx->flag[EMUX_FX_SAMPLE_START] = 0;
  125. fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
  126. }
  127. }
  128. #endif
  129. }
  130. /*
  131. * Release a note in response to a midi note off.
  132. */
  133. void
  134. snd_emux_note_off(void *p, int note, int vel, snd_midi_channel_t *chan)
  135. {
  136. int ch;
  137. snd_emux_t *emu;
  138. snd_emux_voice_t *vp;
  139. unsigned long flags;
  140. snd_emux_port_t *port;
  141. port = p;
  142. snd_assert(port != NULL && chan != NULL, return);
  143. emu = port->emu;
  144. snd_assert(emu != NULL, return);
  145. snd_assert(emu->ops.release != NULL, return);
  146. spin_lock_irqsave(&emu->voice_lock, flags);
  147. for (ch = 0; ch < emu->max_voices; ch++) {
  148. vp = &emu->voices[ch];
  149. if (STATE_IS_PLAYING(vp->state) &&
  150. vp->chan == chan && vp->key == note) {
  151. vp->time = emu->use_time++;
  152. vp->state = SNDRV_EMUX_ST_RELEASED;
  153. if (vp->ontime == jiffies) {
  154. /* if note-off is sent too shortly after
  155. * note-on, emuX engine cannot produce the sound
  156. * correctly. so we'll release this note
  157. * a bit later via timer callback.
  158. */
  159. vp->state = SNDRV_EMUX_ST_PENDING;
  160. if (! emu->timer_active) {
  161. emu->tlist.expires = jiffies + 1;
  162. add_timer(&emu->tlist);
  163. emu->timer_active = 1;
  164. }
  165. } else
  166. /* ok now release the note */
  167. emu->ops.release(vp);
  168. }
  169. }
  170. spin_unlock_irqrestore(&emu->voice_lock, flags);
  171. }
  172. /*
  173. * timer callback
  174. *
  175. * release the pending note-offs
  176. */
  177. void snd_emux_timer_callback(unsigned long data)
  178. {
  179. snd_emux_t *emu = (snd_emux_t*) data;
  180. snd_emux_voice_t *vp;
  181. int ch, do_again = 0;
  182. spin_lock(&emu->voice_lock);
  183. for (ch = 0; ch < emu->max_voices; ch++) {
  184. vp = &emu->voices[ch];
  185. if (vp->state == SNDRV_EMUX_ST_PENDING) {
  186. if (vp->ontime == jiffies)
  187. do_again++; /* release this at the next interrupt */
  188. else {
  189. emu->ops.release(vp);
  190. vp->state = SNDRV_EMUX_ST_RELEASED;
  191. }
  192. }
  193. }
  194. if (do_again) {
  195. emu->tlist.expires = jiffies + 1;
  196. add_timer(&emu->tlist);
  197. emu->timer_active = 1;
  198. } else
  199. emu->timer_active = 0;
  200. spin_unlock(&emu->voice_lock);
  201. }
  202. /*
  203. * key pressure change
  204. */
  205. void
  206. snd_emux_key_press(void *p, int note, int vel, snd_midi_channel_t *chan)
  207. {
  208. int ch;
  209. snd_emux_t *emu;
  210. snd_emux_voice_t *vp;
  211. unsigned long flags;
  212. snd_emux_port_t *port;
  213. port = p;
  214. snd_assert(port != NULL && chan != NULL, return);
  215. emu = port->emu;
  216. snd_assert(emu != NULL, return);
  217. snd_assert(emu->ops.update != NULL, return);
  218. spin_lock_irqsave(&emu->voice_lock, flags);
  219. for (ch = 0; ch < emu->max_voices; ch++) {
  220. vp = &emu->voices[ch];
  221. if (vp->state == SNDRV_EMUX_ST_ON &&
  222. vp->chan == chan && vp->key == note) {
  223. vp->velocity = vel;
  224. update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
  225. }
  226. }
  227. spin_unlock_irqrestore(&emu->voice_lock, flags);
  228. }
  229. /*
  230. * Modulate the voices which belong to the channel
  231. */
  232. void
  233. snd_emux_update_channel(snd_emux_port_t *port, snd_midi_channel_t *chan, int update)
  234. {
  235. snd_emux_t *emu;
  236. snd_emux_voice_t *vp;
  237. int i;
  238. unsigned long flags;
  239. if (! update)
  240. return;
  241. emu = port->emu;
  242. snd_assert(emu != NULL, return);
  243. snd_assert(emu->ops.update != NULL, return);
  244. spin_lock_irqsave(&emu->voice_lock, flags);
  245. for (i = 0; i < emu->max_voices; i++) {
  246. vp = &emu->voices[i];
  247. if (vp->chan == chan)
  248. update_voice(emu, vp, update);
  249. }
  250. spin_unlock_irqrestore(&emu->voice_lock, flags);
  251. }
  252. /*
  253. * Modulate all the voices which belong to the port.
  254. */
  255. void
  256. snd_emux_update_port(snd_emux_port_t *port, int update)
  257. {
  258. snd_emux_t *emu;
  259. snd_emux_voice_t *vp;
  260. int i;
  261. unsigned long flags;
  262. if (! update)
  263. return;
  264. emu = port->emu;
  265. snd_assert(emu != NULL, return);
  266. snd_assert(emu->ops.update != NULL, return);
  267. spin_lock_irqsave(&emu->voice_lock, flags);
  268. for (i = 0; i < emu->max_voices; i++) {
  269. vp = &emu->voices[i];
  270. if (vp->port == port)
  271. update_voice(emu, vp, update);
  272. }
  273. spin_unlock_irqrestore(&emu->voice_lock, flags);
  274. }
  275. /*
  276. * Deal with a controler type event. This includes all types of
  277. * control events, not just the midi controllers
  278. */
  279. void
  280. snd_emux_control(void *p, int type, snd_midi_channel_t *chan)
  281. {
  282. snd_emux_port_t *port;
  283. port = p;
  284. snd_assert(port != NULL && chan != NULL, return);
  285. switch (type) {
  286. case MIDI_CTL_MSB_MAIN_VOLUME:
  287. case MIDI_CTL_MSB_EXPRESSION:
  288. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
  289. break;
  290. case MIDI_CTL_MSB_PAN:
  291. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
  292. break;
  293. case MIDI_CTL_SOFT_PEDAL:
  294. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  295. /* FIXME: this is an emulation */
  296. snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
  297. EMUX_FX_FLAG_ADD);
  298. #endif
  299. break;
  300. case MIDI_CTL_PITCHBEND:
  301. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
  302. break;
  303. case MIDI_CTL_MSB_MODWHEEL:
  304. case MIDI_CTL_CHAN_PRESSURE:
  305. snd_emux_update_channel(port, chan,
  306. SNDRV_EMUX_UPDATE_FMMOD |
  307. SNDRV_EMUX_UPDATE_FM2FRQ2);
  308. break;
  309. }
  310. if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
  311. snd_emux_xg_control(port, chan, type);
  312. }
  313. }
  314. /*
  315. * terminate note - if free flag is true, free the terminated voice
  316. */
  317. static void
  318. terminate_note1(snd_emux_t *emu, int note, snd_midi_channel_t *chan, int free)
  319. {
  320. int i;
  321. snd_emux_voice_t *vp;
  322. unsigned long flags;
  323. spin_lock_irqsave(&emu->voice_lock, flags);
  324. for (i = 0; i < emu->max_voices; i++) {
  325. vp = &emu->voices[i];
  326. if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
  327. vp->key == note)
  328. terminate_voice(emu, vp, free);
  329. }
  330. spin_unlock_irqrestore(&emu->voice_lock, flags);
  331. }
  332. /*
  333. * terminate note - exported for midi emulation
  334. */
  335. void
  336. snd_emux_terminate_note(void *p, int note, snd_midi_channel_t *chan)
  337. {
  338. snd_emux_t *emu;
  339. snd_emux_port_t *port;
  340. port = p;
  341. snd_assert(port != NULL && chan != NULL, return);
  342. emu = port->emu;
  343. snd_assert(emu != NULL, return);
  344. snd_assert(emu->ops.terminate != NULL, return);
  345. terminate_note1(emu, note, chan, 1);
  346. }
  347. /*
  348. * Terminate all the notes
  349. */
  350. void
  351. snd_emux_terminate_all(snd_emux_t *emu)
  352. {
  353. int i;
  354. snd_emux_voice_t *vp;
  355. unsigned long flags;
  356. spin_lock_irqsave(&emu->voice_lock, flags);
  357. for (i = 0; i < emu->max_voices; i++) {
  358. vp = &emu->voices[i];
  359. if (STATE_IS_PLAYING(vp->state))
  360. terminate_voice(emu, vp, 0);
  361. if (vp->state == SNDRV_EMUX_ST_OFF) {
  362. if (emu->ops.free_voice)
  363. emu->ops.free_voice(vp);
  364. if (emu->ops.reset)
  365. emu->ops.reset(emu, i);
  366. }
  367. vp->time = 0;
  368. }
  369. /* initialize allocation time */
  370. emu->use_time = 0;
  371. spin_unlock_irqrestore(&emu->voice_lock, flags);
  372. }
  373. /*
  374. * Terminate all voices associated with the given port
  375. */
  376. void
  377. snd_emux_sounds_off_all(snd_emux_port_t *port)
  378. {
  379. int i;
  380. snd_emux_t *emu;
  381. snd_emux_voice_t *vp;
  382. unsigned long flags;
  383. snd_assert(port != NULL, return);
  384. emu = port->emu;
  385. snd_assert(emu != NULL, return);
  386. snd_assert(emu->ops.terminate != NULL, return);
  387. spin_lock_irqsave(&emu->voice_lock, flags);
  388. for (i = 0; i < emu->max_voices; i++) {
  389. vp = &emu->voices[i];
  390. if (STATE_IS_PLAYING(vp->state) &&
  391. vp->port == port)
  392. terminate_voice(emu, vp, 0);
  393. if (vp->state == SNDRV_EMUX_ST_OFF) {
  394. if (emu->ops.free_voice)
  395. emu->ops.free_voice(vp);
  396. if (emu->ops.reset)
  397. emu->ops.reset(emu, i);
  398. }
  399. }
  400. spin_unlock_irqrestore(&emu->voice_lock, flags);
  401. }
  402. /*
  403. * Terminate all voices that have the same exclusive class. This
  404. * is mainly for drums.
  405. */
  406. static void
  407. exclusive_note_off(snd_emux_t *emu, snd_emux_port_t *port, int exclass)
  408. {
  409. snd_emux_voice_t *vp;
  410. int i;
  411. unsigned long flags;
  412. spin_lock_irqsave(&emu->voice_lock, flags);
  413. for (i = 0; i < emu->max_voices; i++) {
  414. vp = &emu->voices[i];
  415. if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
  416. vp->reg.exclusiveClass == exclass) {
  417. terminate_voice(emu, vp, 0);
  418. }
  419. }
  420. spin_unlock_irqrestore(&emu->voice_lock, flags);
  421. }
  422. /*
  423. * terminate a voice
  424. * if free flag is true, call free_voice after termination
  425. */
  426. static void
  427. terminate_voice(snd_emux_t *emu, snd_emux_voice_t *vp, int free)
  428. {
  429. emu->ops.terminate(vp);
  430. vp->time = emu->use_time++;
  431. vp->chan = NULL;
  432. vp->port = NULL;
  433. vp->zone = NULL;
  434. vp->block = NULL;
  435. vp->state = SNDRV_EMUX_ST_OFF;
  436. if (free && emu->ops.free_voice)
  437. emu->ops.free_voice(vp);
  438. }
  439. /*
  440. * Modulate the voice
  441. */
  442. static void
  443. update_voice(snd_emux_t *emu, snd_emux_voice_t *vp, int update)
  444. {
  445. if (!STATE_IS_PLAYING(vp->state))
  446. return;
  447. if (vp->chan == NULL || vp->port == NULL)
  448. return;
  449. if (update & SNDRV_EMUX_UPDATE_VOLUME)
  450. calc_volume(vp);
  451. if (update & SNDRV_EMUX_UPDATE_PITCH)
  452. calc_pitch(vp);
  453. if (update & SNDRV_EMUX_UPDATE_PAN) {
  454. if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
  455. return;
  456. }
  457. emu->ops.update(vp, update);
  458. }
  459. #if 0 // not used
  460. /* table for volume target calculation */
  461. static unsigned short voltarget[16] = {
  462. 0xEAC0, 0xE0C8, 0xD740, 0xCE20, 0xC560, 0xBD08, 0xB500, 0xAD58,
  463. 0xA5F8, 0x9EF0, 0x9830, 0x91C0, 0x8B90, 0x85A8, 0x8000, 0x7A90
  464. };
  465. #endif
  466. #define LO_BYTE(v) ((v) & 0xff)
  467. #define HI_BYTE(v) (((v) >> 8) & 0xff)
  468. /*
  469. * Sets up the voice structure by calculating some values that
  470. * will be needed later.
  471. */
  472. static void
  473. setup_voice(snd_emux_voice_t *vp)
  474. {
  475. soundfont_voice_parm_t *parm;
  476. int pitch;
  477. /* copy the original register values */
  478. vp->reg = vp->zone->v;
  479. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  480. snd_emux_setup_effect(vp);
  481. #endif
  482. /* reset status */
  483. vp->apan = -1;
  484. vp->avol = -1;
  485. vp->apitch = -1;
  486. calc_volume(vp);
  487. calc_pitch(vp);
  488. calc_pan(vp);
  489. parm = &vp->reg.parm;
  490. /* compute filter target and correct modulation parameters */
  491. if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
  492. parm->moddelay = 0xbfff;
  493. pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
  494. if (pitch > 0xffff)
  495. pitch = 0xffff;
  496. /* calculate filter target */
  497. vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
  498. LIMITVALUE(vp->ftarget, 0, 255);
  499. vp->ftarget <<= 8;
  500. } else {
  501. vp->ftarget = parm->cutoff;
  502. vp->ftarget <<= 8;
  503. pitch = vp->apitch;
  504. }
  505. /* compute pitch target */
  506. if (pitch != 0xffff) {
  507. vp->ptarget = 1 << (pitch >> 12);
  508. if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
  509. if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
  510. if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
  511. vp->ptarget += (vp->ptarget >> 1);
  512. if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
  513. } else
  514. vp->ptarget = 0xffff;
  515. if (LO_BYTE(parm->modatkhld) >= 0x80) {
  516. parm->modatkhld &= ~0xff;
  517. parm->modatkhld |= 0x7f;
  518. }
  519. /* compute volume target and correct volume parameters */
  520. vp->vtarget = 0;
  521. #if 0 /* FIXME: this leads to some clicks.. */
  522. if (LO_BYTE(parm->volatkhld) >= 0x80 && parm->voldelay >= 0x8000) {
  523. parm->voldelay = 0xbfff;
  524. vp->vtarget = voltarget[vp->avol % 0x10] >> (vp->avol >> 4);
  525. }
  526. #endif
  527. if (LO_BYTE(parm->volatkhld) >= 0x80) {
  528. parm->volatkhld &= ~0xff;
  529. parm->volatkhld |= 0x7f;
  530. }
  531. }
  532. /*
  533. * calculate pitch parameter
  534. */
  535. static unsigned char pan_volumes[256] = {
  536. 0x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
  537. 0x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
  538. 0x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
  539. 0x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
  540. 0x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
  541. 0xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
  542. 0xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
  543. 0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
  544. 0xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
  545. 0xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
  546. 0xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
  547. 0xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
  548. 0xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
  549. 0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
  550. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  551. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  552. };
  553. static int
  554. calc_pan(snd_emux_voice_t *vp)
  555. {
  556. snd_midi_channel_t *chan = vp->chan;
  557. int pan;
  558. /* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
  559. if (vp->reg.fixpan > 0) /* 0-127 */
  560. pan = 255 - (int)vp->reg.fixpan * 2;
  561. else {
  562. pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
  563. if (vp->reg.pan >= 0) /* 0-127 */
  564. pan += vp->reg.pan - 64;
  565. pan = 127 - (int)pan * 2;
  566. }
  567. LIMITVALUE(pan, 0, 255);
  568. if (vp->emu->linear_panning) {
  569. /* assuming linear volume */
  570. if (pan != vp->apan) {
  571. vp->apan = pan;
  572. if (pan == 0)
  573. vp->aaux = 0xff;
  574. else
  575. vp->aaux = (-pan) & 0xff;
  576. return 1;
  577. } else
  578. return 0;
  579. } else {
  580. /* using volume table */
  581. if (vp->apan != (int)pan_volumes[pan]) {
  582. vp->apan = pan_volumes[pan];
  583. vp->aaux = pan_volumes[255 - pan];
  584. return 1;
  585. }
  586. return 0;
  587. }
  588. }
  589. /*
  590. * calculate volume attenuation
  591. *
  592. * Voice volume is controlled by volume attenuation parameter.
  593. * So volume becomes maximum when avol is 0 (no attenuation), and
  594. * minimum when 255 (-96dB or silence).
  595. */
  596. /* tables for volume->attenuation calculation */
  597. static unsigned char voltab1[128] = {
  598. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  599. 0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
  600. 0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
  601. 0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
  602. 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
  603. 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
  604. 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
  605. 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
  606. 0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
  607. 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
  608. 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
  609. 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
  610. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  611. };
  612. static unsigned char voltab2[128] = {
  613. 0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
  614. 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
  615. 0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
  616. 0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
  617. 0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
  618. 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
  619. 0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
  620. 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
  621. 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  622. 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  623. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
  624. 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
  625. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  626. };
  627. static unsigned char expressiontab[128] = {
  628. 0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
  629. 0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
  630. 0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
  631. 0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
  632. 0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
  633. 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
  634. 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
  635. 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
  636. 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
  637. 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  638. 0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
  639. 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
  640. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  641. };
  642. /*
  643. * Magic to calculate the volume (actually attenuation) from all the
  644. * voice and channels parameters.
  645. */
  646. static int
  647. calc_volume(snd_emux_voice_t *vp)
  648. {
  649. int vol;
  650. int main_vol, expression_vol, master_vol;
  651. snd_midi_channel_t *chan = vp->chan;
  652. snd_emux_port_t *port = vp->port;
  653. expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
  654. LIMITMAX(vp->velocity, 127);
  655. LIMITVALUE(expression_vol, 0, 127);
  656. if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
  657. /* 0 - 127 */
  658. main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
  659. vol = (vp->velocity * main_vol * expression_vol) / (127*127);
  660. vol = vol * vp->reg.amplitude / 127;
  661. LIMITVALUE(vol, 0, 127);
  662. /* calc to attenuation */
  663. vol = snd_sf_vol_table[vol];
  664. } else {
  665. main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
  666. LIMITVALUE(main_vol, 0, 127);
  667. vol = voltab1[main_vol] + voltab2[vp->velocity];
  668. vol = (vol * 8) / 3;
  669. vol += vp->reg.attenuation;
  670. vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
  671. }
  672. master_vol = port->chset.gs_master_volume;
  673. LIMITVALUE(master_vol, 0, 127);
  674. vol += snd_sf_vol_table[master_vol];
  675. vol += port->volume_atten;
  676. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  677. if (chan->private) {
  678. snd_emux_effect_table_t *fx = chan->private;
  679. vol += fx->val[EMUX_FX_ATTEN];
  680. }
  681. #endif
  682. LIMITVALUE(vol, 0, 255);
  683. if (vp->avol == vol)
  684. return 0; /* value unchanged */
  685. vp->avol = vol;
  686. if (!SF_IS_DRUM_BANK(get_bank(port, chan))
  687. && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
  688. int atten;
  689. if (vp->velocity < 70)
  690. atten = 70;
  691. else
  692. atten = vp->velocity;
  693. vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
  694. } else {
  695. vp->acutoff = vp->reg.parm.cutoff;
  696. }
  697. return 1; /* value changed */
  698. }
  699. /*
  700. * calculate pitch offset
  701. *
  702. * 0xE000 is no pitch offset at 44100Hz sample.
  703. * Every 4096 is one octave.
  704. */
  705. static int
  706. calc_pitch(snd_emux_voice_t *vp)
  707. {
  708. snd_midi_channel_t *chan = vp->chan;
  709. int offset;
  710. /* calculate offset */
  711. if (vp->reg.fixkey >= 0) {
  712. offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
  713. } else {
  714. offset = (vp->note - vp->reg.root) * 4096 / 12;
  715. }
  716. offset = (offset * vp->reg.scaleTuning) / 100;
  717. offset += vp->reg.tune * 4096 / 1200;
  718. if (chan->midi_pitchbend != 0) {
  719. /* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
  720. offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
  721. }
  722. /* tuning via RPN:
  723. * coarse = -8192 to 8192 (100 cent per 128)
  724. * fine = -8192 to 8192 (max=100cent)
  725. */
  726. /* 4096 = 1200 cents in emu8000 parameter */
  727. offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
  728. offset += chan->gm_rpn_fine_tuning / 24;
  729. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  730. /* add initial pitch correction */
  731. if (chan->private) {
  732. snd_emux_effect_table_t *fx = chan->private;
  733. if (fx->flag[EMUX_FX_INIT_PITCH])
  734. offset += fx->val[EMUX_FX_INIT_PITCH];
  735. }
  736. #endif
  737. /* 0xe000: root pitch */
  738. offset += 0xe000 + vp->reg.rate_offset;
  739. offset += vp->emu->pitch_shift;
  740. LIMITVALUE(offset, 0, 0xffff);
  741. if (offset == vp->apitch)
  742. return 0; /* unchanged */
  743. vp->apitch = offset;
  744. return 1; /* value changed */
  745. }
  746. /*
  747. * Get the bank number assigned to the channel
  748. */
  749. static int
  750. get_bank(snd_emux_port_t *port, snd_midi_channel_t *chan)
  751. {
  752. int val;
  753. switch (port->chset.midi_mode) {
  754. case SNDRV_MIDI_MODE_XG:
  755. val = chan->control[MIDI_CTL_MSB_BANK];
  756. if (val == 127)
  757. return 128; /* return drum bank */
  758. return chan->control[MIDI_CTL_LSB_BANK];
  759. case SNDRV_MIDI_MODE_GS:
  760. if (chan->drum_channel)
  761. return 128;
  762. /* ignore LSB (bank map) */
  763. return chan->control[MIDI_CTL_MSB_BANK];
  764. default:
  765. if (chan->drum_channel)
  766. return 128;
  767. return chan->control[MIDI_CTL_MSB_BANK];
  768. }
  769. }
  770. /* Look for the zones matching with the given note and velocity.
  771. * The resultant zones are stored on table.
  772. */
  773. static int
  774. get_zone(snd_emux_t *emu, snd_emux_port_t *port,
  775. int *notep, int vel, snd_midi_channel_t *chan, snd_sf_zone_t **table)
  776. {
  777. int preset, bank, def_preset, def_bank;
  778. bank = get_bank(port, chan);
  779. preset = chan->midi_program;
  780. if (SF_IS_DRUM_BANK(bank)) {
  781. def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
  782. def_bank = bank;
  783. } else {
  784. def_preset = preset;
  785. def_bank = port->ctrls[EMUX_MD_DEF_BANK];
  786. }
  787. return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
  788. def_preset, def_bank,
  789. table, SNDRV_EMUX_MAX_MULTI_VOICES);
  790. }
  791. /*
  792. */
  793. void
  794. snd_emux_init_voices(snd_emux_t *emu)
  795. {
  796. snd_emux_voice_t *vp;
  797. int i;
  798. unsigned long flags;
  799. spin_lock_irqsave(&emu->voice_lock, flags);
  800. for (i = 0; i < emu->max_voices; i++) {
  801. vp = &emu->voices[i];
  802. vp->ch = -1; /* not used */
  803. vp->state = SNDRV_EMUX_ST_OFF;
  804. vp->chan = NULL;
  805. vp->port = NULL;
  806. vp->time = 0;
  807. vp->emu = emu;
  808. vp->hw = emu->hw;
  809. }
  810. spin_unlock_irqrestore(&emu->voice_lock, flags);
  811. }
  812. /*
  813. */
  814. void snd_emux_lock_voice(snd_emux_t *emu, int voice)
  815. {
  816. unsigned long flags;
  817. spin_lock_irqsave(&emu->voice_lock, flags);
  818. if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
  819. emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
  820. else
  821. snd_printk("invalid voice for lock %d (state = %x)\n",
  822. voice, emu->voices[voice].state);
  823. spin_unlock_irqrestore(&emu->voice_lock, flags);
  824. }
  825. /*
  826. */
  827. void snd_emux_unlock_voice(snd_emux_t *emu, int voice)
  828. {
  829. unsigned long flags;
  830. spin_lock_irqsave(&emu->voice_lock, flags);
  831. if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
  832. emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
  833. else
  834. snd_printk("invalid voice for unlock %d (state = %x)\n",
  835. voice, emu->voices[voice].state);
  836. spin_unlock_irqrestore(&emu->voice_lock, flags);
  837. }