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