opl3_midi.c 22 KB

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