opl3_midi.c 22 KB

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