opl3_midi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 < MAX_OPL2_VOICES) {
  337. /* Left register block for voices 0 .. 8 */
  338. reg_side = OPL3_LEFT;
  339. voice_offset = voice;
  340. connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
  341. } else {
  342. /* Right register block for voices 9 .. 17 */
  343. reg_side = OPL3_RIGHT;
  344. voice_offset = voice - MAX_OPL2_VOICES;
  345. connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
  346. }
  347. /* kill voice on channel */
  348. vp = &opl3->voices[voice];
  349. if (vp->state > 0) {
  350. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  351. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  352. opl3->command(opl3, opl3_reg, reg_val);
  353. }
  354. if (instr_4op) {
  355. vp2 = &opl3->voices[voice + 3];
  356. if (vp->state > 0) {
  357. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
  358. voice_offset + 3);
  359. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  360. opl3->command(opl3, opl3_reg, reg_val);
  361. }
  362. }
  363. /* set connection register */
  364. if (instr_4op) {
  365. if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
  366. opl3->connection_reg |= connect_mask;
  367. /* set connection bit */
  368. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  369. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  370. }
  371. } else {
  372. if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
  373. opl3->connection_reg &= ~connect_mask;
  374. /* clear connection bit */
  375. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  376. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  377. }
  378. }
  379. #ifdef DEBUG_MIDI
  380. snd_printk(KERN_DEBUG " --> setting OPL3 connection: 0x%x\n",
  381. opl3->connection_reg);
  382. #endif
  383. /*
  384. * calculate volume depending on connection
  385. * between FM operators (see include/opl3.h)
  386. */
  387. for (i = 0; i < (instr_4op ? 4 : 2); i++)
  388. vol_op[i] = fm->op[i].ksl_level;
  389. connection = fm->feedback_connection[0] & 0x01;
  390. if (instr_4op) {
  391. connection <<= 1;
  392. connection |= fm->feedback_connection[1] & 0x01;
  393. snd_opl3_calc_volume(&vol_op[3], vel, chan);
  394. switch (connection) {
  395. case 0x03:
  396. snd_opl3_calc_volume(&vol_op[2], vel, chan);
  397. /* fallthru */
  398. case 0x02:
  399. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  400. break;
  401. case 0x01:
  402. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  403. }
  404. } else {
  405. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  406. if (connection)
  407. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  408. }
  409. /* Program the FM voice characteristics */
  410. for (i = 0; i < (instr_4op ? 4 : 2); i++) {
  411. #ifdef DEBUG_MIDI
  412. snd_printk(KERN_DEBUG " --> programming operator %i\n", i);
  413. #endif
  414. op_offset = snd_opl3_regmap[voice_offset][i];
  415. /* Set OPL3 AM_VIB register of requested voice/operator */
  416. reg_val = fm->op[i].am_vib;
  417. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  418. opl3->command(opl3, opl3_reg, reg_val);
  419. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  420. reg_val = vol_op[i];
  421. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  422. opl3->command(opl3, opl3_reg, reg_val);
  423. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  424. reg_val = fm->op[i].attack_decay;
  425. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  426. opl3->command(opl3, opl3_reg, reg_val);
  427. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  428. reg_val = fm->op[i].sustain_release;
  429. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  430. opl3->command(opl3, opl3_reg, reg_val);
  431. /* Select waveform */
  432. reg_val = fm->op[i].wave_select;
  433. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  434. opl3->command(opl3, opl3_reg, reg_val);
  435. }
  436. /* Set operator feedback and 2op inter-operator connection */
  437. reg_val = fm->feedback_connection[0];
  438. /* Set output voice connection */
  439. reg_val |= OPL3_STEREO_BITS;
  440. if (chan->gm_pan < 43)
  441. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  442. if (chan->gm_pan > 85)
  443. reg_val &= ~OPL3_VOICE_TO_LEFT;
  444. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  445. opl3->command(opl3, opl3_reg, reg_val);
  446. if (instr_4op) {
  447. /* Set 4op inter-operator connection */
  448. reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
  449. /* Set output voice connection */
  450. reg_val |= OPL3_STEREO_BITS;
  451. if (chan->gm_pan < 43)
  452. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  453. if (chan->gm_pan > 85)
  454. reg_val &= ~OPL3_VOICE_TO_LEFT;
  455. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
  456. voice_offset + 3);
  457. opl3->command(opl3, opl3_reg, reg_val);
  458. }
  459. /*
  460. * Special treatment of percussion notes for fm:
  461. * Requested pitch is really program, and pitch for
  462. * device is whatever was specified in the patch library.
  463. */
  464. if (fm->fix_key)
  465. note = fm->fix_key;
  466. /*
  467. * use transpose if defined in patch library
  468. */
  469. if (fm->trnsps)
  470. note += (fm->trnsps - 64);
  471. snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
  472. /* Set OPL3 FNUM_LOW register of requested voice */
  473. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  474. opl3->command(opl3, opl3_reg, fnum);
  475. opl3->voices[voice].keyon_reg = blocknum;
  476. /* Set output sound flag */
  477. blocknum |= OPL3_KEYON_BIT;
  478. #ifdef DEBUG_MIDI
  479. snd_printk(KERN_DEBUG " --> trigger voice %i\n", voice);
  480. #endif
  481. /* Set OPL3 KEYON_BLOCK register of requested voice */
  482. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  483. opl3->command(opl3, opl3_reg, blocknum);
  484. /* kill note after fixed duration (in centiseconds) */
  485. if (fm->fix_dur) {
  486. opl3->voices[voice].note_off = jiffies +
  487. (fm->fix_dur * HZ) / 100;
  488. snd_opl3_start_timer(opl3);
  489. opl3->voices[voice].note_off_check = 1;
  490. } else
  491. opl3->voices[voice].note_off_check = 0;
  492. /* get extra pgm, but avoid possible loops */
  493. extra_prg = (extra_prg) ? 0 : fm->modes;
  494. /* do the bookkeeping */
  495. vp->time = opl3->use_time++;
  496. vp->note = key;
  497. vp->chan = chan;
  498. if (instr_4op) {
  499. vp->state = SNDRV_OPL3_ST_ON_4OP;
  500. vp2 = &opl3->voices[voice + 3];
  501. vp2->time = opl3->use_time++;
  502. vp2->note = key;
  503. vp2->chan = chan;
  504. vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
  505. } else {
  506. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  507. /* 4op killed by 2op, release bounded voice */
  508. vp2 = &opl3->voices[voice + 3];
  509. vp2->time = opl3->use_time++;
  510. vp2->state = SNDRV_OPL3_ST_OFF;
  511. }
  512. vp->state = SNDRV_OPL3_ST_ON_2OP;
  513. }
  514. #ifdef DEBUG_ALLOC
  515. debug_alloc(opl3, "note on ", voice);
  516. #endif
  517. /* allocate extra program if specified in patch library */
  518. if (extra_prg) {
  519. if (extra_prg > 128) {
  520. bank = 128;
  521. /* percussions start at 35 */
  522. prg = extra_prg - 128 + 35 - 1;
  523. } else {
  524. bank = 0;
  525. prg = extra_prg - 1;
  526. }
  527. #ifdef DEBUG_MIDI
  528. snd_printk(KERN_DEBUG " *** allocating extra program\n");
  529. #endif
  530. goto __extra_prg;
  531. }
  532. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  533. }
  534. static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
  535. {
  536. unsigned short reg_side;
  537. unsigned char voice_offset;
  538. unsigned short opl3_reg;
  539. struct snd_opl3_voice *vp, *vp2;
  540. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  541. return;
  542. vp = &opl3->voices[voice];
  543. if (voice < MAX_OPL2_VOICES) {
  544. /* Left register block for voices 0 .. 8 */
  545. reg_side = OPL3_LEFT;
  546. voice_offset = voice;
  547. } else {
  548. /* Right register block for voices 9 .. 17 */
  549. reg_side = OPL3_RIGHT;
  550. voice_offset = voice - MAX_OPL2_VOICES;
  551. }
  552. /* kill voice */
  553. #ifdef DEBUG_MIDI
  554. snd_printk(KERN_DEBUG " --> kill voice %i\n", voice);
  555. #endif
  556. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  557. /* clear Key ON bit */
  558. opl3->command(opl3, opl3_reg, vp->keyon_reg);
  559. /* do the bookkeeping */
  560. vp->time = opl3->use_time++;
  561. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  562. vp2 = &opl3->voices[voice + 3];
  563. vp2->time = opl3->use_time++;
  564. vp2->state = SNDRV_OPL3_ST_OFF;
  565. }
  566. vp->state = SNDRV_OPL3_ST_OFF;
  567. #ifdef DEBUG_ALLOC
  568. debug_alloc(opl3, "note off", voice);
  569. #endif
  570. }
  571. /*
  572. * Release a note in response to a midi note off.
  573. */
  574. static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  575. struct snd_midi_channel *chan)
  576. {
  577. struct snd_opl3 *opl3;
  578. int voice;
  579. struct snd_opl3_voice *vp;
  580. opl3 = p;
  581. #ifdef DEBUG_MIDI
  582. snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
  583. chan->number, chan->midi_program, note);
  584. #endif
  585. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  586. if (chan->drum_channel && use_internal_drums) {
  587. snd_opl3_drum_switch(opl3, note, vel, 0, chan);
  588. return;
  589. }
  590. /* this loop will hopefully kill all extra voices, because
  591. they are grouped by the same channel and note values */
  592. for (voice = 0; voice < opl3->max_voices; voice++) {
  593. vp = &opl3->voices[voice];
  594. if (vp->state > 0 && vp->chan == chan && vp->note == note) {
  595. snd_opl3_kill_voice(opl3, voice);
  596. }
  597. }
  598. } else {
  599. /* remap OSS voices */
  600. if (chan->number < MAX_OPL3_VOICES) {
  601. voice = snd_opl3_oss_map[chan->number];
  602. snd_opl3_kill_voice(opl3, voice);
  603. }
  604. }
  605. }
  606. void snd_opl3_note_off(void *p, int note, int vel,
  607. struct snd_midi_channel *chan)
  608. {
  609. struct snd_opl3 *opl3 = p;
  610. unsigned long flags;
  611. spin_lock_irqsave(&opl3->voice_lock, flags);
  612. snd_opl3_note_off_unsafe(p, note, vel, chan);
  613. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  614. }
  615. /*
  616. * key pressure change
  617. */
  618. void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
  619. {
  620. struct snd_opl3 *opl3;
  621. opl3 = p;
  622. #ifdef DEBUG_MIDI
  623. snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
  624. chan->number, chan->midi_program);
  625. #endif
  626. }
  627. /*
  628. * terminate note
  629. */
  630. void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
  631. {
  632. struct snd_opl3 *opl3;
  633. opl3 = p;
  634. #ifdef DEBUG_MIDI
  635. snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
  636. chan->number, chan->midi_program);
  637. #endif
  638. }
  639. static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
  640. {
  641. unsigned short reg_side;
  642. unsigned char voice_offset;
  643. unsigned short opl3_reg;
  644. unsigned char fnum, blocknum;
  645. struct snd_opl3_voice *vp;
  646. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  647. return;
  648. vp = &opl3->voices[voice];
  649. if (vp->chan == NULL)
  650. return; /* not allocated? */
  651. if (voice < MAX_OPL2_VOICES) {
  652. /* Left register block for voices 0 .. 8 */
  653. reg_side = OPL3_LEFT;
  654. voice_offset = voice;
  655. } else {
  656. /* Right register block for voices 9 .. 17 */
  657. reg_side = OPL3_RIGHT;
  658. voice_offset = voice - MAX_OPL2_VOICES;
  659. }
  660. snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
  661. /* Set OPL3 FNUM_LOW register of requested voice */
  662. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  663. opl3->command(opl3, opl3_reg, fnum);
  664. vp->keyon_reg = blocknum;
  665. /* Set output sound flag */
  666. blocknum |= OPL3_KEYON_BIT;
  667. /* Set OPL3 KEYON_BLOCK register of requested voice */
  668. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  669. opl3->command(opl3, opl3_reg, blocknum);
  670. vp->time = opl3->use_time++;
  671. }
  672. /*
  673. * Update voice pitch controller
  674. */
  675. static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
  676. {
  677. int voice;
  678. struct snd_opl3_voice *vp;
  679. unsigned long flags;
  680. spin_lock_irqsave(&opl3->voice_lock, flags);
  681. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  682. for (voice = 0; voice < opl3->max_voices; voice++) {
  683. vp = &opl3->voices[voice];
  684. if (vp->state > 0 && vp->chan == chan) {
  685. snd_opl3_update_pitch(opl3, voice);
  686. }
  687. }
  688. } else {
  689. /* remap OSS voices */
  690. if (chan->number < MAX_OPL3_VOICES) {
  691. voice = snd_opl3_oss_map[chan->number];
  692. snd_opl3_update_pitch(opl3, voice);
  693. }
  694. }
  695. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  696. }
  697. /*
  698. * Deal with a controller type event. This includes all types of
  699. * control events, not just the midi controllers
  700. */
  701. void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
  702. {
  703. struct snd_opl3 *opl3;
  704. opl3 = p;
  705. #ifdef DEBUG_MIDI
  706. snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
  707. type, chan->number, chan->midi_program);
  708. #endif
  709. switch (type) {
  710. case MIDI_CTL_MSB_MODWHEEL:
  711. if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
  712. opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
  713. else
  714. opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
  715. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  716. opl3->drum_reg);
  717. break;
  718. case MIDI_CTL_E2_TREMOLO_DEPTH:
  719. if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
  720. opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
  721. else
  722. opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
  723. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  724. opl3->drum_reg);
  725. break;
  726. case MIDI_CTL_PITCHBEND:
  727. snd_opl3_pitch_ctrl(opl3, chan);
  728. break;
  729. }
  730. }
  731. /*
  732. * NRPN events
  733. */
  734. void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
  735. struct snd_midi_channel_set *chset)
  736. {
  737. struct snd_opl3 *opl3;
  738. opl3 = p;
  739. #ifdef DEBUG_MIDI
  740. snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
  741. chan->number, chan->midi_program);
  742. #endif
  743. }
  744. /*
  745. * receive sysex
  746. */
  747. void snd_opl3_sysex(void *p, unsigned char *buf, int len,
  748. int parsed, struct snd_midi_channel_set *chset)
  749. {
  750. struct snd_opl3 *opl3;
  751. opl3 = p;
  752. #ifdef DEBUG_MIDI
  753. snd_printk(KERN_DEBUG "SYSEX\n");
  754. #endif
  755. }