emux_synth.c 25 KB

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