emux_synth.c 25 KB

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