opl3_midi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  26. struct snd_midi_channel *chan);
  27. /*
  28. * The next table looks magical, but it certainly is not. Its values have
  29. * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  30. * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  31. * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
  32. * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
  33. * volume -8 it was implemented as a table because it is only 128 bytes and
  34. * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
  35. */
  36. static char opl3_volume_table[128] =
  37. {
  38. -63, -48, -40, -35, -32, -29, -27, -26,
  39. -24, -23, -21, -20, -19, -18, -18, -17,
  40. -16, -15, -15, -14, -13, -13, -12, -12,
  41. -11, -11, -10, -10, -10, -9, -9, -8,
  42. -8, -8, -7, -7, -7, -6, -6, -6,
  43. -5, -5, -5, -5, -4, -4, -4, -4,
  44. -3, -3, -3, -3, -2, -2, -2, -2,
  45. -2, -1, -1, -1, -1, 0, 0, 0,
  46. 0, 0, 0, 1, 1, 1, 1, 1,
  47. 1, 2, 2, 2, 2, 2, 2, 2,
  48. 3, 3, 3, 3, 3, 3, 3, 4,
  49. 4, 4, 4, 4, 4, 4, 4, 5,
  50. 5, 5, 5, 5, 5, 5, 5, 5,
  51. 6, 6, 6, 6, 6, 6, 6, 6,
  52. 6, 7, 7, 7, 7, 7, 7, 7,
  53. 7, 7, 7, 8, 8, 8, 8, 8
  54. };
  55. void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
  56. struct snd_midi_channel *chan)
  57. {
  58. int oldvol, newvol, n;
  59. int volume;
  60. volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
  61. if (volume > 127)
  62. volume = 127;
  63. oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
  64. newvol = opl3_volume_table[volume] + oldvol;
  65. if (newvol > OPL3_TOTAL_LEVEL_MASK)
  66. newvol = OPL3_TOTAL_LEVEL_MASK;
  67. else if (newvol < 0)
  68. newvol = 0;
  69. n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
  70. *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
  71. }
  72. /*
  73. * Converts the note frequency to block and fnum values for the FM chip
  74. */
  75. static short opl3_note_table[16] =
  76. {
  77. 305, 323, /* for pitch bending, -2 semitones */
  78. 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
  79. 686, 726 /* for pitch bending, +2 semitones */
  80. };
  81. static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
  82. int note, struct snd_midi_channel *chan)
  83. {
  84. int block = ((note / 12) & 0x07) - 1;
  85. int idx = (note % 12) + 2;
  86. int freq;
  87. if (chan->midi_pitchbend) {
  88. int pitchbend = chan->midi_pitchbend;
  89. int segment;
  90. if (pitchbend < -0x2000)
  91. pitchbend = -0x2000;
  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(KERN_CONT "%c", *(str + opl3->voices[i].state + 1));
  112. printk(KERN_CONT "\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(struct timer_list *t)
  205. {
  206. struct snd_opl3 *opl3 = from_timer(opl3, t, tlist);
  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. mod_timer(&opl3->tlist, jiffies + 1); /* invoke again */
  225. else
  226. opl3->sys_timer_status = 0;
  227. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  228. }
  229. /*
  230. * Start system timer
  231. */
  232. static void snd_opl3_start_timer(struct snd_opl3 *opl3)
  233. {
  234. unsigned long flags;
  235. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  236. if (! opl3->sys_timer_status) {
  237. mod_timer(&opl3->tlist, jiffies + 1);
  238. opl3->sys_timer_status = 1;
  239. }
  240. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  241. }
  242. /* ------------------------------ */
  243. static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
  244. 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
  245. };
  246. /*
  247. * Start a note.
  248. */
  249. void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
  250. {
  251. struct snd_opl3 *opl3;
  252. int instr_4op;
  253. int voice;
  254. struct snd_opl3_voice *vp, *vp2;
  255. unsigned short connect_mask;
  256. unsigned char connection;
  257. unsigned char vol_op[4];
  258. int extra_prg = 0;
  259. unsigned short reg_side;
  260. unsigned char op_offset;
  261. unsigned char voice_offset;
  262. unsigned short opl3_reg;
  263. unsigned char reg_val;
  264. unsigned char prg, bank;
  265. int key = note;
  266. unsigned char fnum, blocknum;
  267. int i;
  268. struct fm_patch *patch;
  269. struct fm_instrument *fm;
  270. unsigned long flags;
  271. opl3 = p;
  272. #ifdef DEBUG_MIDI
  273. snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
  274. chan->number, chan->midi_program, note, vel);
  275. #endif
  276. /* in SYNTH mode, application takes care of voices */
  277. /* in SEQ mode, drum voice numbers are notes on drum channel */
  278. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  279. if (chan->drum_channel) {
  280. /* percussion instruments are located in bank 128 */
  281. bank = 128;
  282. prg = note;
  283. } else {
  284. bank = chan->gm_bank_select;
  285. prg = chan->midi_program;
  286. }
  287. } else {
  288. /* Prepare for OSS mode */
  289. if (chan->number >= MAX_OPL3_VOICES)
  290. return;
  291. /* OSS instruments are located in bank 127 */
  292. bank = 127;
  293. prg = chan->midi_program;
  294. }
  295. spin_lock_irqsave(&opl3->voice_lock, flags);
  296. if (use_internal_drums) {
  297. snd_opl3_drum_switch(opl3, note, vel, 1, chan);
  298. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  299. return;
  300. }
  301. __extra_prg:
  302. patch = snd_opl3_find_patch(opl3, prg, bank, 0);
  303. if (!patch) {
  304. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  305. return;
  306. }
  307. fm = &patch->inst;
  308. switch (patch->type) {
  309. case FM_PATCH_OPL2:
  310. instr_4op = 0;
  311. break;
  312. case FM_PATCH_OPL3:
  313. if (opl3->hardware >= OPL3_HW_OPL3) {
  314. instr_4op = 1;
  315. break;
  316. }
  317. /* fall through */
  318. default:
  319. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  320. return;
  321. }
  322. #ifdef DEBUG_MIDI
  323. snd_printk(KERN_DEBUG " --> OPL%i instrument: %s\n",
  324. instr_4op ? 3 : 2, patch->name);
  325. #endif
  326. /* in SYNTH mode, application takes care of voices */
  327. /* in SEQ mode, allocate voice on free OPL3 channel */
  328. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  329. voice = opl3_get_voice(opl3, instr_4op, chan);
  330. } else {
  331. /* remap OSS voice */
  332. voice = snd_opl3_oss_map[chan->number];
  333. }
  334. if (voice < 0) {
  335. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  336. return;
  337. }
  338. if (voice < MAX_OPL2_VOICES) {
  339. /* Left register block for voices 0 .. 8 */
  340. reg_side = OPL3_LEFT;
  341. voice_offset = voice;
  342. connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
  343. } else {
  344. /* Right register block for voices 9 .. 17 */
  345. reg_side = OPL3_RIGHT;
  346. voice_offset = voice - MAX_OPL2_VOICES;
  347. connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
  348. }
  349. /* kill voice on channel */
  350. vp = &opl3->voices[voice];
  351. if (vp->state > 0) {
  352. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  353. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  354. opl3->command(opl3, opl3_reg, reg_val);
  355. }
  356. if (instr_4op) {
  357. vp2 = &opl3->voices[voice + 3];
  358. if (vp->state > 0) {
  359. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
  360. voice_offset + 3);
  361. reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  362. opl3->command(opl3, opl3_reg, reg_val);
  363. }
  364. }
  365. /* set connection register */
  366. if (instr_4op) {
  367. if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
  368. opl3->connection_reg |= connect_mask;
  369. /* set connection bit */
  370. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  371. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  372. }
  373. } else {
  374. if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
  375. opl3->connection_reg &= ~connect_mask;
  376. /* clear connection bit */
  377. opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  378. opl3->command(opl3, opl3_reg, opl3->connection_reg);
  379. }
  380. }
  381. #ifdef DEBUG_MIDI
  382. snd_printk(KERN_DEBUG " --> setting OPL3 connection: 0x%x\n",
  383. opl3->connection_reg);
  384. #endif
  385. /*
  386. * calculate volume depending on connection
  387. * between FM operators (see include/opl3.h)
  388. */
  389. for (i = 0; i < (instr_4op ? 4 : 2); i++)
  390. vol_op[i] = fm->op[i].ksl_level;
  391. connection = fm->feedback_connection[0] & 0x01;
  392. if (instr_4op) {
  393. connection <<= 1;
  394. connection |= fm->feedback_connection[1] & 0x01;
  395. snd_opl3_calc_volume(&vol_op[3], vel, chan);
  396. switch (connection) {
  397. case 0x03:
  398. snd_opl3_calc_volume(&vol_op[2], vel, chan);
  399. /* fallthru */
  400. case 0x02:
  401. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  402. break;
  403. case 0x01:
  404. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  405. }
  406. } else {
  407. snd_opl3_calc_volume(&vol_op[1], vel, chan);
  408. if (connection)
  409. snd_opl3_calc_volume(&vol_op[0], vel, chan);
  410. }
  411. /* Program the FM voice characteristics */
  412. for (i = 0; i < (instr_4op ? 4 : 2); i++) {
  413. #ifdef DEBUG_MIDI
  414. snd_printk(KERN_DEBUG " --> programming operator %i\n", i);
  415. #endif
  416. op_offset = snd_opl3_regmap[voice_offset][i];
  417. /* Set OPL3 AM_VIB register of requested voice/operator */
  418. reg_val = fm->op[i].am_vib;
  419. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  420. opl3->command(opl3, opl3_reg, reg_val);
  421. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  422. reg_val = vol_op[i];
  423. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  424. opl3->command(opl3, opl3_reg, reg_val);
  425. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  426. reg_val = fm->op[i].attack_decay;
  427. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  428. opl3->command(opl3, opl3_reg, reg_val);
  429. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  430. reg_val = fm->op[i].sustain_release;
  431. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  432. opl3->command(opl3, opl3_reg, reg_val);
  433. /* Select waveform */
  434. reg_val = fm->op[i].wave_select;
  435. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  436. opl3->command(opl3, opl3_reg, reg_val);
  437. }
  438. /* Set operator feedback and 2op inter-operator connection */
  439. reg_val = fm->feedback_connection[0];
  440. /* Set output voice connection */
  441. reg_val |= OPL3_STEREO_BITS;
  442. if (chan->gm_pan < 43)
  443. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  444. if (chan->gm_pan > 85)
  445. reg_val &= ~OPL3_VOICE_TO_LEFT;
  446. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  447. opl3->command(opl3, opl3_reg, reg_val);
  448. if (instr_4op) {
  449. /* Set 4op inter-operator connection */
  450. reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
  451. /* Set output voice connection */
  452. reg_val |= OPL3_STEREO_BITS;
  453. if (chan->gm_pan < 43)
  454. reg_val &= ~OPL3_VOICE_TO_RIGHT;
  455. if (chan->gm_pan > 85)
  456. reg_val &= ~OPL3_VOICE_TO_LEFT;
  457. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
  458. voice_offset + 3);
  459. opl3->command(opl3, opl3_reg, reg_val);
  460. }
  461. /*
  462. * Special treatment of percussion notes for fm:
  463. * Requested pitch is really program, and pitch for
  464. * device is whatever was specified in the patch library.
  465. */
  466. if (fm->fix_key)
  467. note = fm->fix_key;
  468. /*
  469. * use transpose if defined in patch library
  470. */
  471. if (fm->trnsps)
  472. note += (fm->trnsps - 64);
  473. snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
  474. /* Set OPL3 FNUM_LOW register of requested voice */
  475. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  476. opl3->command(opl3, opl3_reg, fnum);
  477. opl3->voices[voice].keyon_reg = blocknum;
  478. /* Set output sound flag */
  479. blocknum |= OPL3_KEYON_BIT;
  480. #ifdef DEBUG_MIDI
  481. snd_printk(KERN_DEBUG " --> trigger voice %i\n", voice);
  482. #endif
  483. /* Set OPL3 KEYON_BLOCK register of requested voice */
  484. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  485. opl3->command(opl3, opl3_reg, blocknum);
  486. /* kill note after fixed duration (in centiseconds) */
  487. if (fm->fix_dur) {
  488. opl3->voices[voice].note_off = jiffies +
  489. (fm->fix_dur * HZ) / 100;
  490. snd_opl3_start_timer(opl3);
  491. opl3->voices[voice].note_off_check = 1;
  492. } else
  493. opl3->voices[voice].note_off_check = 0;
  494. /* get extra pgm, but avoid possible loops */
  495. extra_prg = (extra_prg) ? 0 : fm->modes;
  496. /* do the bookkeeping */
  497. vp->time = opl3->use_time++;
  498. vp->note = key;
  499. vp->chan = chan;
  500. if (instr_4op) {
  501. vp->state = SNDRV_OPL3_ST_ON_4OP;
  502. vp2 = &opl3->voices[voice + 3];
  503. vp2->time = opl3->use_time++;
  504. vp2->note = key;
  505. vp2->chan = chan;
  506. vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
  507. } else {
  508. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  509. /* 4op killed by 2op, release bounded voice */
  510. vp2 = &opl3->voices[voice + 3];
  511. vp2->time = opl3->use_time++;
  512. vp2->state = SNDRV_OPL3_ST_OFF;
  513. }
  514. vp->state = SNDRV_OPL3_ST_ON_2OP;
  515. }
  516. #ifdef DEBUG_ALLOC
  517. debug_alloc(opl3, "note on ", voice);
  518. #endif
  519. /* allocate extra program if specified in patch library */
  520. if (extra_prg) {
  521. if (extra_prg > 128) {
  522. bank = 128;
  523. /* percussions start at 35 */
  524. prg = extra_prg - 128 + 35 - 1;
  525. } else {
  526. bank = 0;
  527. prg = extra_prg - 1;
  528. }
  529. #ifdef DEBUG_MIDI
  530. snd_printk(KERN_DEBUG " *** allocating extra program\n");
  531. #endif
  532. goto __extra_prg;
  533. }
  534. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  535. }
  536. static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
  537. {
  538. unsigned short reg_side;
  539. unsigned char voice_offset;
  540. unsigned short opl3_reg;
  541. struct snd_opl3_voice *vp, *vp2;
  542. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  543. return;
  544. vp = &opl3->voices[voice];
  545. if (voice < MAX_OPL2_VOICES) {
  546. /* Left register block for voices 0 .. 8 */
  547. reg_side = OPL3_LEFT;
  548. voice_offset = voice;
  549. } else {
  550. /* Right register block for voices 9 .. 17 */
  551. reg_side = OPL3_RIGHT;
  552. voice_offset = voice - MAX_OPL2_VOICES;
  553. }
  554. /* kill voice */
  555. #ifdef DEBUG_MIDI
  556. snd_printk(KERN_DEBUG " --> kill voice %i\n", voice);
  557. #endif
  558. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  559. /* clear Key ON bit */
  560. opl3->command(opl3, opl3_reg, vp->keyon_reg);
  561. /* do the bookkeeping */
  562. vp->time = opl3->use_time++;
  563. if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  564. vp2 = &opl3->voices[voice + 3];
  565. vp2->time = opl3->use_time++;
  566. vp2->state = SNDRV_OPL3_ST_OFF;
  567. }
  568. vp->state = SNDRV_OPL3_ST_OFF;
  569. #ifdef DEBUG_ALLOC
  570. debug_alloc(opl3, "note off", voice);
  571. #endif
  572. }
  573. /*
  574. * Release a note in response to a midi note off.
  575. */
  576. static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  577. struct snd_midi_channel *chan)
  578. {
  579. struct snd_opl3 *opl3;
  580. int voice;
  581. struct snd_opl3_voice *vp;
  582. opl3 = p;
  583. #ifdef DEBUG_MIDI
  584. snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
  585. chan->number, chan->midi_program, note);
  586. #endif
  587. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  588. if (chan->drum_channel && use_internal_drums) {
  589. snd_opl3_drum_switch(opl3, note, vel, 0, chan);
  590. return;
  591. }
  592. /* this loop will hopefully kill all extra voices, because
  593. they are grouped by the same channel and note values */
  594. for (voice = 0; voice < opl3->max_voices; voice++) {
  595. vp = &opl3->voices[voice];
  596. if (vp->state > 0 && vp->chan == chan && vp->note == note) {
  597. snd_opl3_kill_voice(opl3, voice);
  598. }
  599. }
  600. } else {
  601. /* remap OSS voices */
  602. if (chan->number < MAX_OPL3_VOICES) {
  603. voice = snd_opl3_oss_map[chan->number];
  604. snd_opl3_kill_voice(opl3, voice);
  605. }
  606. }
  607. }
  608. void snd_opl3_note_off(void *p, int note, int vel,
  609. struct snd_midi_channel *chan)
  610. {
  611. struct snd_opl3 *opl3 = p;
  612. unsigned long flags;
  613. spin_lock_irqsave(&opl3->voice_lock, flags);
  614. snd_opl3_note_off_unsafe(p, note, vel, chan);
  615. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  616. }
  617. /*
  618. * key pressure change
  619. */
  620. void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
  621. {
  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. #ifdef DEBUG_MIDI
  633. snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
  634. chan->number, chan->midi_program);
  635. #endif
  636. }
  637. static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
  638. {
  639. unsigned short reg_side;
  640. unsigned char voice_offset;
  641. unsigned short opl3_reg;
  642. unsigned char fnum, blocknum;
  643. struct snd_opl3_voice *vp;
  644. if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  645. return;
  646. vp = &opl3->voices[voice];
  647. if (vp->chan == NULL)
  648. return; /* not allocated? */
  649. if (voice < MAX_OPL2_VOICES) {
  650. /* Left register block for voices 0 .. 8 */
  651. reg_side = OPL3_LEFT;
  652. voice_offset = voice;
  653. } else {
  654. /* Right register block for voices 9 .. 17 */
  655. reg_side = OPL3_RIGHT;
  656. voice_offset = voice - MAX_OPL2_VOICES;
  657. }
  658. snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
  659. /* Set OPL3 FNUM_LOW register of requested voice */
  660. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  661. opl3->command(opl3, opl3_reg, fnum);
  662. vp->keyon_reg = blocknum;
  663. /* Set output sound flag */
  664. blocknum |= OPL3_KEYON_BIT;
  665. /* Set OPL3 KEYON_BLOCK register of requested voice */
  666. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  667. opl3->command(opl3, opl3_reg, blocknum);
  668. vp->time = opl3->use_time++;
  669. }
  670. /*
  671. * Update voice pitch controller
  672. */
  673. static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
  674. {
  675. int voice;
  676. struct snd_opl3_voice *vp;
  677. unsigned long flags;
  678. spin_lock_irqsave(&opl3->voice_lock, flags);
  679. if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  680. for (voice = 0; voice < opl3->max_voices; voice++) {
  681. vp = &opl3->voices[voice];
  682. if (vp->state > 0 && vp->chan == chan) {
  683. snd_opl3_update_pitch(opl3, voice);
  684. }
  685. }
  686. } else {
  687. /* remap OSS voices */
  688. if (chan->number < MAX_OPL3_VOICES) {
  689. voice = snd_opl3_oss_map[chan->number];
  690. snd_opl3_update_pitch(opl3, voice);
  691. }
  692. }
  693. spin_unlock_irqrestore(&opl3->voice_lock, flags);
  694. }
  695. /*
  696. * Deal with a controller type event. This includes all types of
  697. * control events, not just the midi controllers
  698. */
  699. void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
  700. {
  701. struct snd_opl3 *opl3;
  702. opl3 = p;
  703. #ifdef DEBUG_MIDI
  704. snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
  705. type, chan->number, chan->midi_program);
  706. #endif
  707. switch (type) {
  708. case MIDI_CTL_MSB_MODWHEEL:
  709. if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
  710. opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
  711. else
  712. opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
  713. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  714. opl3->drum_reg);
  715. break;
  716. case MIDI_CTL_E2_TREMOLO_DEPTH:
  717. if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
  718. opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
  719. else
  720. opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
  721. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  722. opl3->drum_reg);
  723. break;
  724. case MIDI_CTL_PITCHBEND:
  725. snd_opl3_pitch_ctrl(opl3, chan);
  726. break;
  727. }
  728. }
  729. /*
  730. * NRPN events
  731. */
  732. void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
  733. struct snd_midi_channel_set *chset)
  734. {
  735. #ifdef DEBUG_MIDI
  736. snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
  737. chan->number, chan->midi_program);
  738. #endif
  739. }
  740. /*
  741. * receive sysex
  742. */
  743. void snd_opl3_sysex(void *p, unsigned char *buf, int len,
  744. int parsed, struct snd_midi_channel_set *chset)
  745. {
  746. #ifdef DEBUG_MIDI
  747. snd_printk(KERN_DEBUG "SYSEX\n");
  748. #endif
  749. }