emux_synth.c 26 KB

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