emux_synth.c 25 KB

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