emux_synth.c 25 KB

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