emux_synth.c 25 KB

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