opl3_midi.c 21 KB

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