opl3_midi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
  3. *
  4. * Midi synth routines for OPL2/OPL3/OPL4 FM
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #undef DEBUG_ALLOC
  22. #undef DEBUG_MIDI
  23. #include "opl3_voice.h"
  24. #include <sound/asoundef.h>
  25. extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
  26. extern bool use_internal_drums;
  27. static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  28. struct snd_midi_channel *chan);
  29. /*
  30. * The next table looks magical, but it certainly is not. Its values have
  31. * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  32. * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  33. * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
  34. * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
  35. * volume -8 it was implemented as a table because it is only 128 bytes and
  36. * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
  37. */
  38. static char opl3_volume_table[128] =
  39. {
  40. -63, -48, -40, -35, -32, -29, -27, -26,
  41. -24, -23, -21, -20, -19, -18, -18, -17,
  42. -16, -15, -15, -14, -13, -13, -12, -12,
  43. -11, -11, -10, -10, -10, -9, -9, -8,
  44. -8, -8, -7, -7, -7, -6, -6, -6,
  45. -5, -5, -5, -5, -4, -4, -4, -4,
  46. -3, -3, -3, -3, -2, -2, -2, -2,
  47. -2, -1, -1, -1, -1, 0, 0, 0,
  48. 0, 0, 0, 1, 1, 1, 1, 1,
  49. 1, 2, 2, 2, 2, 2, 2, 2,
  50. 3, 3, 3, 3, 3, 3, 3, 4,
  51. 4, 4, 4, 4, 4, 4, 4, 5,
  52. 5, 5, 5, 5, 5, 5, 5, 5,
  53. 6, 6, 6, 6, 6, 6, 6, 6,
  54. 6, 7, 7, 7, 7, 7, 7, 7,
  55. 7, 7, 7, 8, 8, 8, 8, 8
  56. };
  57. void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
  58. struct snd_midi_channel *chan)
  59. {
  60. int oldvol, newvol, n;
  61. int volume;
  62. volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
  63. if (volume > 127)
  64. volume = 127;
  65. oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
  66. newvol = opl3_volume_table[volume] + oldvol;
  67. if (newvol > OPL3_TOTAL_LEVEL_MASK)
  68. newvol = OPL3_TOTAL_LEVEL_MASK;
  69. else if (newvol < 0)
  70. newvol = 0;
  71. n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
  72. *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
  73. }
  74. /*
  75. * Converts the note frequency to block and fnum values for the FM chip
  76. */
  77. static short opl3_note_table[16] =
  78. {
  79. 305, 323, /* for pitch bending, -2 semitones */
  80. 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
  81. 686, 726 /* for pitch bending, +2 semitones */
  82. };
  83. static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
  84. int note, struct snd_midi_channel *chan)
  85. {
  86. int block = ((note / 12) & 0x07) - 1;
  87. int idx = (note % 12) + 2;
  88. int freq;
  89. if (chan->midi_pitchbend) {
  90. int pitchbend = chan->midi_pitchbend;
  91. int segment;
  92. if (pitchbend > 0x1FFF)
  93. pitchbend = 0x1FFF;
  94. segment = pitchbend / 0x1000;
  95. freq = opl3_note_table[idx+segment];
  96. freq += ((opl3_note_table[idx+segment+1] - freq) *
  97. (pitchbend % 0x1000)) / 0x1000;
  98. } else {
  99. freq = opl3_note_table[idx];
  100. }
  101. *fnum = (unsigned char) freq;
  102. *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
  103. ((block << 2) & OPL3_BLOCKNUM_MASK);
  104. }
  105. #ifdef DEBUG_ALLOC
  106. static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
  107. int i;
  108. char *str = "x.24";
  109. printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
  110. for (i = 0; i < opl3->max_voices; i++)
  111. printk("%c", *(str + opl3->voices[i].state + 1));
  112. printk("\n");
  113. }
  114. #endif
  115. /*
  116. * Get a FM voice (channel) to play a note on.
  117. */
  118. static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
  119. struct snd_midi_channel *chan) {
  120. int chan_4op_1; /* first voice for 4op instrument */
  121. int chan_4op_2; /* second voice for 4op instrument */
  122. struct snd_opl3_voice *vp, *vp2;
  123. unsigned int voice_time;
  124. int i;
  125. #ifdef DEBUG_ALLOC
  126. char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
  127. #endif
  128. /* This is our "allocation cost" table */
  129. enum {
  130. FREE = 0, CHEAP, EXPENSIVE, END
  131. };
  132. /* Keeps track of what we are finding */
  133. struct best {
  134. unsigned int time;
  135. int voice;
  136. } best[END];
  137. struct best *bp;
  138. for (i = 0; i < END; i++) {
  139. best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
  140. best[i].voice = -1;
  141. }
  142. /* Look through all the channels for the most suitable. */
  143. for (i = 0; i < opl3->max_voices; i++) {
  144. vp = &opl3->voices[i];
  145. if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
  146. /* skip unavailable channels, allocated by
  147. drum voices or by bounded 4op voices) */
  148. continue;
  149. voice_time = vp->time;
  150. bp = best;
  151. chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
  152. chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
  153. if (instr_4op) {
  154. /* allocate 4op voice */
  155. /* skip channels unavailable to 4op instrument */
  156. if (!chan_4op_1)
  157. continue;
  158. if (vp->state)
  159. /* kill one voice, CHEAP */
  160. bp++;
  161. /* get state of bounded 2op channel
  162. to be allocated for 4op instrument */
  163. vp2 = &opl3->voices[i + 3];
  164. if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
  165. /* kill two voices, EXPENSIVE */
  166. bp++;
  167. voice_time = (voice_time > vp->time) ?
  168. voice_time : vp->time;
  169. }
  170. } else {
  171. /* allocate 2op voice */
  172. if ((chan_4op_1) || (chan_4op_2))
  173. /* use bounded channels for 2op, CHEAP */
  174. bp++;
  175. else if (vp->state)
  176. /* kill one voice on 2op channel, CHEAP */
  177. bp++;
  178. /* raise kill cost to EXPENSIVE for all channels */
  179. if (vp->state)
  180. bp++;
  181. }
  182. if (voice_time < bp->time) {
  183. bp->time = voice_time;
  184. bp->voice = i;
  185. }
  186. }
  187. for (i = 0; i < END; i++) {
  188. if (best[i].voice >= 0) {
  189. #ifdef DEBUG_ALLOC
  190. printk(KERN_DEBUG "%s %iop allocation on voice %i\n",
  191. alloc_type[i], instr_4op ? 4 : 2,
  192. best[i].voice);
  193. #endif
  194. return best[i].voice;
  195. }
  196. }
  197. /* not found */
  198. return -1;
  199. }
  200. /* ------------------------------ */
  201. /*
  202. * System timer interrupt function
  203. */
  204. void snd_opl3_timer_func(unsigned long data)
  205. {
  206. struct snd_opl3 *opl3 = (struct snd_opl3 *)data;
  207. unsigned long flags;
  208. int again = 0;
  209. int i;
  210. spin_lock_irqsave(&opl3->voice_lock, flags);
  211. for (i = 0; i < opl3->max_voices; i++) {
  212. struct snd_opl3_voice *vp = &opl3->voices[i];
  213. if (vp->state > 0 && vp->note_off_check) {
  214. if (vp->note_off == jiffies)
  215. snd_opl3_note_off_unsafe(opl3, vp->note, 0,
  216. vp->chan);
  217. else
  218. again++;
  219. }
  220. }
  221. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  222. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  223. if (again) {
  224. opl3->tlist.expires = jiffies + 1; /* invoke again */
  225. add_timer(&opl3->tlist);
  226. } else {
  227. opl3->sys_timer_status = 0;
  228. }
  229. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  230. }
  231. /*
  232. * Start system timer
  233. */
  234. static void snd_opl3_start_timer(struct snd_opl3 *opl3)
  235. {
  236. unsigned long flags;
  237. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  238. if (! opl3->sys_timer_status) {
  239. opl3->tlist.expires = jiffies + 1;
  240. add_timer(&opl3->tlist);
  241. opl3->sys_timer_status = 1;
  242. }
  243. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  244. }
  245. /* ------------------------------ */
  246. static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
  247. 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
  248. };
  249. /*
  250. * Start a note.
  251. */
  252. void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
  253. {
  254. struct snd_opl3 *opl3;
  255. int instr_4op;
  256. int voice;
  257. struct snd_opl3_voice *vp, *vp2;
  258. unsigned short connect_mask;
  259. unsigned char connection;
  260. unsigned char vol_op[4];
  261. int extra_prg = 0;
  262. unsigned short reg_side;
  263. unsigned char op_offset;
  264. unsigned char voice_offset;
  265. unsigned short opl3_reg;
  266. unsigned char reg_val;
  267. unsigned char prg, bank;
  268. int key = note;
  269. unsigned char fnum, blocknum;
  270. int i;
  271. struct fm_patch *patch;
  272. struct fm_instrument *fm;
  273. unsigned long flags;
  274. opl3 = p;
  275. #ifdef DEBUG_MIDI
  276. snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
  277. chan->number, chan->midi_program, note, vel);
  278. #endif
  279. /* in SYNTH mode, application takes care of voices */
  280. /* in SEQ mode, drum voice numbers are notes on drum channel */
  281. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  282. if (chan->drum_channel) {
  283. /* percussion instruments are located in bank 128 */
  284. bank = 128;
  285. prg = note;
  286. } else {
  287. bank = chan->gm_bank_select;
  288. prg = chan->midi_program;
  289. }
  290. } else {
  291. /* Prepare for OSS mode */
  292. if (chan->number >= MAX_OPL3_VOICES)
  293. return;
  294. /* OSS instruments are located in bank 127 */
  295. bank = 127;
  296. prg = chan->midi_program;
  297. }
  298. spin_lock_irqsave(&opl3->voice_lock, flags);
  299. if (use_internal_drums) {
  300. snd_opl3_drum_switch(opl3, note, vel, 1, chan);
  301. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  302. return;
  303. }
  304. __extra_prg:
  305. patch = snd_opl3_find_patch(opl3, prg, bank, 0);
  306. if (!patch) {
  307. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  308. return;
  309. }
  310. fm = &patch->inst;
  311. switch (patch->type) {
  312. case FM_PATCH_OPL2:
  313. instr_4op = 0;
  314. break;
  315. case FM_PATCH_OPL3:
  316. if (opl3->hardware >= OPL3_HW_OPL3) {
  317. instr_4op = 1;
  318. break;
  319. }
  320. default:
  321. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  322. return;
  323. }
  324. #ifdef DEBUG_MIDI
  325. snd_printk(KERN_DEBUG " --> OPL%i instrument: %s\n",
  326. instr_4op ? 3 : 2, patch->name);
  327. #endif
  328. /* in SYNTH mode, application takes care of voices */
  329. /* in SEQ mode, allocate voice on free OPL3 channel */
  330. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  331. voice = opl3_get_voice(opl3, instr_4op, chan);
  332. } else {
  333. /* remap OSS voice */
  334. voice = snd_opl3_oss_map[chan->number];
  335. }
  336. if (voice < 0) {
  337. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  338. return;
  339. }
  340. if (voice < MAX_OPL2_VOICES) {
  341. /* Left register block for voices 0 .. 8 */
  342. reg_side = OPL3_LEFT;
  343. voice_offset = voice;
  344. connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
  345. } else {
  346. /* Right register block for voices 9 .. 17 */
  347. reg_side = OPL3_RIGHT;
  348. voice_offset = voice - MAX_OPL2_VOICES;
  349. connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
  350. }
  351. /* kill voice on channel */
  352. vp = &opl3->voices[voice];
  353. if (vp->state > 0) {
  354. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  355. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  356. opl3->command(opl3, opl3_reg, reg_val);
  357. }
  358. if (instr_4op) {
  359. vp2 = &opl3->voices[voice + 3];
  360. if (vp->state > 0) {
  361. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
  362. voice_offset + 3);
  363. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  364. opl3->command(opl3, opl3_reg, reg_val);
  365. }
  366. }
  367. /* set connection register */
  368. if (instr_4op) {
  369. if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
  370. opl3->connection_reg |= connect_mask;
  371. /* set connection bit */
  372. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  373. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  374. }
  375. } else {
  376. if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
  377. opl3->connection_reg &= ~connect_mask;
  378. /* clear connection bit */
  379. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  380. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  381. }
  382. }
  383. #ifdef DEBUG_MIDI
  384. snd_printk(KERN_DEBUG " --> setting OPL3 connection: 0x%x\n",
  385. opl3->connection_reg);
  386. #endif
  387. /*
  388. * calculate volume depending on connection
  389. * between FM operators (see include/opl3.h)
  390. */
  391. for (i = 0; i < (instr_4op ? 4 : 2); i++)
  392. vol_op[i] = fm->op[i].ksl_level;
  393. connection = fm->feedback_connection[0] & 0x01;
  394. if (instr_4op) {
  395. connection <<= 1;
  396. connection |= fm->feedback_connection[1] & 0x01;
  397. snd_opl3_calc_volume(&vol_op[3], vel, chan);
  398. switch (connection) {
  399. case 0x03:
  400. snd_opl3_calc_volume(&vol_op[2], vel, chan);
  401. /* fallthru */
  402. case 0x02:
  403. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  404. break;
  405. case 0x01:
  406. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  407. }
  408. } else {
  409. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  410. if (connection)
  411. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  412. }
  413. /* Program the FM voice characteristics */
  414. for (i = 0; i < (instr_4op ? 4 : 2); i++) {
  415. #ifdef DEBUG_MIDI
  416. snd_printk(KERN_DEBUG " --> programming operator %i\n", i);
  417. #endif
  418. op_offset = snd_opl3_regmap[voice_offset][i];
  419. /* Set OPL3 AM_VIB register of requested voice/operator */
  420. reg_val = fm->op[i].am_vib;
  421. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  422. opl3->command(opl3, opl3_reg, reg_val);
  423. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  424. reg_val = vol_op[i];
  425. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  426. opl3->command(opl3, opl3_reg, reg_val);
  427. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  428. reg_val = fm->op[i].attack_decay;
  429. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  430. opl3->command(opl3, opl3_reg, reg_val);
  431. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  432. reg_val = fm->op[i].sustain_release;
  433. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  434. opl3->command(opl3, opl3_reg, reg_val);
  435. /* Select waveform */
  436. reg_val = fm->op[i].wave_select;
  437. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  438. opl3->command(opl3, opl3_reg, reg_val);
  439. }
  440. /* Set operator feedback and 2op inter-operator connection */
  441. reg_val = fm->feedback_connection[0];
  442. /* Set output voice connection */
  443. reg_val |= OPL3_STEREO_BITS;
  444. if (chan->gm_pan < 43)
  445. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  446. if (chan->gm_pan > 85)
  447. reg_val &= ~OPL3_VOICE_TO_LEFT;
  448. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  449. opl3->command(opl3, opl3_reg, reg_val);
  450. if (instr_4op) {
  451. /* Set 4op inter-operator connection */
  452. reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
  453. /* Set output voice connection */
  454. reg_val |= OPL3_STEREO_BITS;
  455. if (chan->gm_pan < 43)
  456. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  457. if (chan->gm_pan > 85)
  458. reg_val &= ~OPL3_VOICE_TO_LEFT;
  459. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
  460. voice_offset + 3);
  461. opl3->command(opl3, opl3_reg, reg_val);
  462. }
  463. /*
  464. * Special treatment of percussion notes for fm:
  465. * Requested pitch is really program, and pitch for
  466. * device is whatever was specified in the patch library.
  467. */
  468. if (fm->fix_key)
  469. note = fm->fix_key;
  470. /*
  471. * use transpose if defined in patch library
  472. */
  473. if (fm->trnsps)
  474. note += (fm->trnsps - 64);
  475. snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
  476. /* Set OPL3 FNUM_LOW register of requested voice */
  477. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  478. opl3->command(opl3, opl3_reg, fnum);
  479. opl3->voices[voice].keyon_reg = blocknum;
  480. /* Set output sound flag */
  481. blocknum |= OPL3_KEYON_BIT;
  482. #ifdef DEBUG_MIDI
  483. snd_printk(KERN_DEBUG " --> trigger voice %i\n", voice);
  484. #endif
  485. /* Set OPL3 KEYON_BLOCK register of requested voice */
  486. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  487. opl3->command(opl3, opl3_reg, blocknum);
  488. /* kill note after fixed duration (in centiseconds) */
  489. if (fm->fix_dur) {
  490. opl3->voices[voice].note_off = jiffies +
  491. (fm->fix_dur * HZ) / 100;
  492. snd_opl3_start_timer(opl3);
  493. opl3->voices[voice].note_off_check = 1;
  494. } else
  495. opl3->voices[voice].note_off_check = 0;
  496. /* get extra pgm, but avoid possible loops */
  497. extra_prg = (extra_prg) ? 0 : fm->modes;
  498. /* do the bookkeeping */
  499. vp->time = opl3->use_time++;
  500. vp->note = key;
  501. vp->chan = chan;
  502. if (instr_4op) {
  503. vp->state = SNDRV_OPL3_ST_ON_4OP;
  504. vp2 = &opl3->voices[voice + 3];
  505. vp2->time = opl3->use_time++;
  506. vp2->note = key;
  507. vp2->chan = chan;
  508. vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
  509. } else {
  510. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  511. /* 4op killed by 2op, release bounded voice */
  512. vp2 = &opl3->voices[voice + 3];
  513. vp2->time = opl3->use_time++;
  514. vp2->state = SNDRV_OPL3_ST_OFF;
  515. }
  516. vp->state = SNDRV_OPL3_ST_ON_2OP;
  517. }
  518. #ifdef DEBUG_ALLOC
  519. debug_alloc(opl3, "note on ", voice);
  520. #endif
  521. /* allocate extra program if specified in patch library */
  522. if (extra_prg) {
  523. if (extra_prg > 128) {
  524. bank = 128;
  525. /* percussions start at 35 */
  526. prg = extra_prg - 128 + 35 - 1;
  527. } else {
  528. bank = 0;
  529. prg = extra_prg - 1;
  530. }
  531. #ifdef DEBUG_MIDI
  532. snd_printk(KERN_DEBUG " *** allocating extra program\n");
  533. #endif
  534. goto __extra_prg;
  535. }
  536. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  537. }
  538. static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
  539. {
  540. unsigned short reg_side;
  541. unsigned char voice_offset;
  542. unsigned short opl3_reg;
  543. struct snd_opl3_voice *vp, *vp2;
  544. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  545. return;
  546. vp = &opl3->voices[voice];
  547. if (voice < MAX_OPL2_VOICES) {
  548. /* Left register block for voices 0 .. 8 */
  549. reg_side = OPL3_LEFT;
  550. voice_offset = voice;
  551. } else {
  552. /* Right register block for voices 9 .. 17 */
  553. reg_side = OPL3_RIGHT;
  554. voice_offset = voice - MAX_OPL2_VOICES;
  555. }
  556. /* kill voice */
  557. #ifdef DEBUG_MIDI
  558. snd_printk(KERN_DEBUG " --> kill voice %i\n", voice);
  559. #endif
  560. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  561. /* clear Key ON bit */
  562. opl3->command(opl3, opl3_reg, vp->keyon_reg);
  563. /* do the bookkeeping */
  564. vp->time = opl3->use_time++;
  565. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  566. vp2 = &opl3->voices[voice + 3];
  567. vp2->time = opl3->use_time++;
  568. vp2->state = SNDRV_OPL3_ST_OFF;
  569. }
  570. vp->state = SNDRV_OPL3_ST_OFF;
  571. #ifdef DEBUG_ALLOC
  572. debug_alloc(opl3, "note off", voice);
  573. #endif
  574. }
  575. /*
  576. * Release a note in response to a midi note off.
  577. */
  578. static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  579. struct snd_midi_channel *chan)
  580. {
  581. struct snd_opl3 *opl3;
  582. int voice;
  583. struct snd_opl3_voice *vp;
  584. opl3 = p;
  585. #ifdef DEBUG_MIDI
  586. snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
  587. chan->number, chan->midi_program, note);
  588. #endif
  589. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  590. if (chan->drum_channel && use_internal_drums) {
  591. snd_opl3_drum_switch(opl3, note, vel, 0, chan);
  592. return;
  593. }
  594. /* this loop will hopefully kill all extra voices, because
  595. they are grouped by the same channel and note values */
  596. for (voice = 0; voice < opl3->max_voices; voice++) {
  597. vp = &opl3->voices[voice];
  598. if (vp->state > 0 && vp->chan == chan && vp->note == note) {
  599. snd_opl3_kill_voice(opl3, voice);
  600. }
  601. }
  602. } else {
  603. /* remap OSS voices */
  604. if (chan->number < MAX_OPL3_VOICES) {
  605. voice = snd_opl3_oss_map[chan->number];
  606. snd_opl3_kill_voice(opl3, voice);
  607. }
  608. }
  609. }
  610. void snd_opl3_note_off(void *p, int note, int vel,
  611. struct snd_midi_channel *chan)
  612. {
  613. struct snd_opl3 *opl3 = p;
  614. unsigned long flags;
  615. spin_lock_irqsave(&opl3->voice_lock, flags);
  616. snd_opl3_note_off_unsafe(p, note, vel, chan);
  617. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  618. }
  619. /*
  620. * key pressure change
  621. */
  622. void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
  623. {
  624. struct snd_opl3 *opl3;
  625. opl3 = p;
  626. #ifdef DEBUG_MIDI
  627. snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
  628. chan->number, chan->midi_program);
  629. #endif
  630. }
  631. /*
  632. * terminate note
  633. */
  634. void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
  635. {
  636. struct snd_opl3 *opl3;
  637. opl3 = p;
  638. #ifdef DEBUG_MIDI
  639. snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
  640. chan->number, chan->midi_program);
  641. #endif
  642. }
  643. static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
  644. {
  645. unsigned short reg_side;
  646. unsigned char voice_offset;
  647. unsigned short opl3_reg;
  648. unsigned char fnum, blocknum;
  649. struct snd_opl3_voice *vp;
  650. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  651. return;
  652. vp = &opl3->voices[voice];
  653. if (vp->chan == NULL)
  654. return; /* not allocated? */
  655. if (voice < MAX_OPL2_VOICES) {
  656. /* Left register block for voices 0 .. 8 */
  657. reg_side = OPL3_LEFT;
  658. voice_offset = voice;
  659. } else {
  660. /* Right register block for voices 9 .. 17 */
  661. reg_side = OPL3_RIGHT;
  662. voice_offset = voice - MAX_OPL2_VOICES;
  663. }
  664. snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
  665. /* Set OPL3 FNUM_LOW register of requested voice */
  666. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  667. opl3->command(opl3, opl3_reg, fnum);
  668. vp->keyon_reg = blocknum;
  669. /* Set output sound flag */
  670. blocknum |= OPL3_KEYON_BIT;
  671. /* Set OPL3 KEYON_BLOCK register of requested voice */
  672. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  673. opl3->command(opl3, opl3_reg, blocknum);
  674. vp->time = opl3->use_time++;
  675. }
  676. /*
  677. * Update voice pitch controller
  678. */
  679. static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
  680. {
  681. int voice;
  682. struct snd_opl3_voice *vp;
  683. unsigned long flags;
  684. spin_lock_irqsave(&opl3->voice_lock, flags);
  685. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  686. for (voice = 0; voice < opl3->max_voices; voice++) {
  687. vp = &opl3->voices[voice];
  688. if (vp->state > 0 && vp->chan == chan) {
  689. snd_opl3_update_pitch(opl3, voice);
  690. }
  691. }
  692. } else {
  693. /* remap OSS voices */
  694. if (chan->number < MAX_OPL3_VOICES) {
  695. voice = snd_opl3_oss_map[chan->number];
  696. snd_opl3_update_pitch(opl3, voice);
  697. }
  698. }
  699. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  700. }
  701. /*
  702. * Deal with a controller type event. This includes all types of
  703. * control events, not just the midi controllers
  704. */
  705. void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
  706. {
  707. struct snd_opl3 *opl3;
  708. opl3 = p;
  709. #ifdef DEBUG_MIDI
  710. snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
  711. type, chan->number, chan->midi_program);
  712. #endif
  713. switch (type) {
  714. case MIDI_CTL_MSB_MODWHEEL:
  715. if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
  716. opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
  717. else
  718. opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
  719. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  720. opl3->drum_reg);
  721. break;
  722. case MIDI_CTL_E2_TREMOLO_DEPTH:
  723. if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
  724. opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
  725. else
  726. opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
  727. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  728. opl3->drum_reg);
  729. break;
  730. case MIDI_CTL_PITCHBEND:
  731. snd_opl3_pitch_ctrl(opl3, chan);
  732. break;
  733. }
  734. }
  735. /*
  736. * NRPN events
  737. */
  738. void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
  739. struct snd_midi_channel_set *chset)
  740. {
  741. struct snd_opl3 *opl3;
  742. opl3 = p;
  743. #ifdef DEBUG_MIDI
  744. snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
  745. chan->number, chan->midi_program);
  746. #endif
  747. }
  748. /*
  749. * receive sysex
  750. */
  751. void snd_opl3_sysex(void *p, unsigned char *buf, int len,
  752. int parsed, struct snd_midi_channel_set *chset)
  753. {
  754. struct snd_opl3 *opl3;
  755. opl3 = p;
  756. #ifdef DEBUG_MIDI
  757. snd_printk(KERN_DEBUG "SYSEX\n");
  758. #endif
  759. }