11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Midi synth routines for OPL2/OPL3/OPL4 FM
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #undef DEBUG_ALLOC
91da177e4SLinus Torvalds #undef DEBUG_MIDI
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds #include "opl3_voice.h"
121da177e4SLinus Torvalds #include <sound/asoundef.h>
131da177e4SLinus Torvalds
148dce39b8SKrzysztof Helt static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
158dce39b8SKrzysztof Helt struct snd_midi_channel *chan);
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds * The next table looks magical, but it certainly is not. Its values have
181da177e4SLinus Torvalds * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
191da177e4SLinus Torvalds * for i=0. This log-table converts a linear volume-scaling (0..127) to a
201da177e4SLinus Torvalds * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
211da177e4SLinus Torvalds * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
221da177e4SLinus Torvalds * volume -8 it was implemented as a table because it is only 128 bytes and
231da177e4SLinus Torvalds * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
241da177e4SLinus Torvalds */
251da177e4SLinus Torvalds
26e3de2a40STakashi Iwai static const char opl3_volume_table[128] =
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds -63, -48, -40, -35, -32, -29, -27, -26,
291da177e4SLinus Torvalds -24, -23, -21, -20, -19, -18, -18, -17,
301da177e4SLinus Torvalds -16, -15, -15, -14, -13, -13, -12, -12,
311da177e4SLinus Torvalds -11, -11, -10, -10, -10, -9, -9, -8,
321da177e4SLinus Torvalds -8, -8, -7, -7, -7, -6, -6, -6,
331da177e4SLinus Torvalds -5, -5, -5, -5, -4, -4, -4, -4,
341da177e4SLinus Torvalds -3, -3, -3, -3, -2, -2, -2, -2,
351da177e4SLinus Torvalds -2, -1, -1, -1, -1, 0, 0, 0,
361da177e4SLinus Torvalds 0, 0, 0, 1, 1, 1, 1, 1,
371da177e4SLinus Torvalds 1, 2, 2, 2, 2, 2, 2, 2,
381da177e4SLinus Torvalds 3, 3, 3, 3, 3, 3, 3, 4,
391da177e4SLinus Torvalds 4, 4, 4, 4, 4, 4, 4, 5,
401da177e4SLinus Torvalds 5, 5, 5, 5, 5, 5, 5, 5,
411da177e4SLinus Torvalds 6, 6, 6, 6, 6, 6, 6, 6,
421da177e4SLinus Torvalds 6, 7, 7, 7, 7, 7, 7, 7,
431da177e4SLinus Torvalds 7, 7, 7, 8, 8, 8, 8, 8
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds
snd_opl3_calc_volume(unsigned char * volbyte,int vel,struct snd_midi_channel * chan)461da177e4SLinus Torvalds void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
475b1646a8STakashi Iwai struct snd_midi_channel *chan)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds int oldvol, newvol, n;
501da177e4SLinus Torvalds int volume;
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
531da177e4SLinus Torvalds if (volume > 127)
541da177e4SLinus Torvalds volume = 127;
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds newvol = opl3_volume_table[volume] + oldvol;
591da177e4SLinus Torvalds if (newvol > OPL3_TOTAL_LEVEL_MASK)
601da177e4SLinus Torvalds newvol = OPL3_TOTAL_LEVEL_MASK;
611da177e4SLinus Torvalds else if (newvol < 0)
621da177e4SLinus Torvalds newvol = 0;
631da177e4SLinus Torvalds
641da177e4SLinus Torvalds n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds * Converts the note frequency to block and fnum values for the FM chip
711da177e4SLinus Torvalds */
72e3de2a40STakashi Iwai static const short opl3_note_table[16] =
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 305, 323, /* for pitch bending, -2 semitones */
751da177e4SLinus Torvalds 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
761da177e4SLinus Torvalds 686, 726 /* for pitch bending, +2 semitones */
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds
snd_opl3_calc_pitch(unsigned char * fnum,unsigned char * blocknum,int note,struct snd_midi_channel * chan)791da177e4SLinus Torvalds static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
805b1646a8STakashi Iwai int note, struct snd_midi_channel *chan)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds int block = ((note / 12) & 0x07) - 1;
831da177e4SLinus Torvalds int idx = (note % 12) + 2;
841da177e4SLinus Torvalds int freq;
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds if (chan->midi_pitchbend) {
871da177e4SLinus Torvalds int pitchbend = chan->midi_pitchbend;
881da177e4SLinus Torvalds int segment;
891da177e4SLinus Torvalds
90d1243806SDan Carpenter if (pitchbend < -0x2000)
91d1243806SDan Carpenter pitchbend = -0x2000;
921da177e4SLinus Torvalds if (pitchbend > 0x1FFF)
931da177e4SLinus Torvalds pitchbend = 0x1FFF;
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds segment = pitchbend / 0x1000;
961da177e4SLinus Torvalds freq = opl3_note_table[idx+segment];
971da177e4SLinus Torvalds freq += ((opl3_note_table[idx+segment+1] - freq) *
981da177e4SLinus Torvalds (pitchbend % 0x1000)) / 0x1000;
991da177e4SLinus Torvalds } else {
1001da177e4SLinus Torvalds freq = opl3_note_table[idx];
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds *fnum = (unsigned char) freq;
1041da177e4SLinus Torvalds *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
1051da177e4SLinus Torvalds ((block << 2) & OPL3_BLOCKNUM_MASK);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds #ifdef DEBUG_ALLOC
debug_alloc(struct snd_opl3 * opl3,char * s,int voice)1105b1646a8STakashi Iwai static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
1111da177e4SLinus Torvalds int i;
1121da177e4SLinus Torvalds char *str = "x.24";
1131da177e4SLinus Torvalds
11445203832STakashi Iwai printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
1151da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++)
11653cfa99eSTakashi Iwai printk(KERN_CONT "%c", *(str + opl3->voices[i].state + 1));
11753cfa99eSTakashi Iwai printk(KERN_CONT "\n");
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds #endif
1201da177e4SLinus Torvalds
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds * Get a FM voice (channel) to play a note on.
1231da177e4SLinus Torvalds */
opl3_get_voice(struct snd_opl3 * opl3,int instr_4op,struct snd_midi_channel * chan)1245b1646a8STakashi Iwai static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
1255b1646a8STakashi Iwai struct snd_midi_channel *chan) {
1261da177e4SLinus Torvalds int chan_4op_1; /* first voice for 4op instrument */
1271da177e4SLinus Torvalds int chan_4op_2; /* second voice for 4op instrument */
1281da177e4SLinus Torvalds
1295b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2;
1301da177e4SLinus Torvalds unsigned int voice_time;
1311da177e4SLinus Torvalds int i;
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds #ifdef DEBUG_ALLOC
1341da177e4SLinus Torvalds char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
1351da177e4SLinus Torvalds #endif
1361da177e4SLinus Torvalds
1371da177e4SLinus Torvalds /* This is our "allocation cost" table */
1381da177e4SLinus Torvalds enum {
1391da177e4SLinus Torvalds FREE = 0, CHEAP, EXPENSIVE, END
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds
1421da177e4SLinus Torvalds /* Keeps track of what we are finding */
1431da177e4SLinus Torvalds struct best {
1441da177e4SLinus Torvalds unsigned int time;
1451da177e4SLinus Torvalds int voice;
1461da177e4SLinus Torvalds } best[END];
1471da177e4SLinus Torvalds struct best *bp;
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds for (i = 0; i < END; i++) {
150395d9dd5SPeter Senna Tschudin best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
1511da177e4SLinus Torvalds best[i].voice = -1;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds /* Look through all the channels for the most suitable. */
1551da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++) {
1561da177e4SLinus Torvalds vp = &opl3->voices[i];
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
1591da177e4SLinus Torvalds /* skip unavailable channels, allocated by
1601da177e4SLinus Torvalds drum voices or by bounded 4op voices) */
1611da177e4SLinus Torvalds continue;
1621da177e4SLinus Torvalds
1631da177e4SLinus Torvalds voice_time = vp->time;
1641da177e4SLinus Torvalds bp = best;
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
1671da177e4SLinus Torvalds chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
1681da177e4SLinus Torvalds if (instr_4op) {
1691da177e4SLinus Torvalds /* allocate 4op voice */
1701da177e4SLinus Torvalds /* skip channels unavailable to 4op instrument */
1711da177e4SLinus Torvalds if (!chan_4op_1)
1721da177e4SLinus Torvalds continue;
1731da177e4SLinus Torvalds
1741da177e4SLinus Torvalds if (vp->state)
1751da177e4SLinus Torvalds /* kill one voice, CHEAP */
1761da177e4SLinus Torvalds bp++;
1771da177e4SLinus Torvalds /* get state of bounded 2op channel
1781da177e4SLinus Torvalds to be allocated for 4op instrument */
1791da177e4SLinus Torvalds vp2 = &opl3->voices[i + 3];
1801da177e4SLinus Torvalds if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
1811da177e4SLinus Torvalds /* kill two voices, EXPENSIVE */
1821da177e4SLinus Torvalds bp++;
18393a5b85cSPierre-Louis Bossart voice_time = max(voice_time, vp2->time);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds } else {
1861da177e4SLinus Torvalds /* allocate 2op voice */
1871da177e4SLinus Torvalds if ((chan_4op_1) || (chan_4op_2))
1881da177e4SLinus Torvalds /* use bounded channels for 2op, CHEAP */
1891da177e4SLinus Torvalds bp++;
1901da177e4SLinus Torvalds else if (vp->state)
1911da177e4SLinus Torvalds /* kill one voice on 2op channel, CHEAP */
1921da177e4SLinus Torvalds bp++;
1931da177e4SLinus Torvalds /* raise kill cost to EXPENSIVE for all channels */
1941da177e4SLinus Torvalds if (vp->state)
1951da177e4SLinus Torvalds bp++;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds if (voice_time < bp->time) {
1981da177e4SLinus Torvalds bp->time = voice_time;
1991da177e4SLinus Torvalds bp->voice = i;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds
2031da177e4SLinus Torvalds for (i = 0; i < END; i++) {
2041da177e4SLinus Torvalds if (best[i].voice >= 0) {
2051da177e4SLinus Torvalds #ifdef DEBUG_ALLOC
20645203832STakashi Iwai printk(KERN_DEBUG "%s %iop allocation on voice %i\n",
2071da177e4SLinus Torvalds alloc_type[i], instr_4op ? 4 : 2,
2081da177e4SLinus Torvalds best[i].voice);
2091da177e4SLinus Torvalds #endif
2101da177e4SLinus Torvalds return best[i].voice;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds /* not found */
2141da177e4SLinus Torvalds return -1;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds /* ------------------------------ */
2181da177e4SLinus Torvalds
2191da177e4SLinus Torvalds /*
2201da177e4SLinus Torvalds * System timer interrupt function
2211da177e4SLinus Torvalds */
snd_opl3_timer_func(struct timer_list * t)222bc47ba90SKees Cook void snd_opl3_timer_func(struct timer_list *t)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds
225bc47ba90SKees Cook struct snd_opl3 *opl3 = from_timer(opl3, t, tlist);
226b32425acSTakashi Iwai unsigned long flags;
2271da177e4SLinus Torvalds int again = 0;
2281da177e4SLinus Torvalds int i;
2291da177e4SLinus Torvalds
2308dce39b8SKrzysztof Helt spin_lock_irqsave(&opl3->voice_lock, flags);
2311da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++) {
2325b1646a8STakashi Iwai struct snd_opl3_voice *vp = &opl3->voices[i];
2331da177e4SLinus Torvalds if (vp->state > 0 && vp->note_off_check) {
2341da177e4SLinus Torvalds if (vp->note_off == jiffies)
2358dce39b8SKrzysztof Helt snd_opl3_note_off_unsafe(opl3, vp->note, 0,
2368dce39b8SKrzysztof Helt vp->chan);
2371da177e4SLinus Torvalds else
2381da177e4SLinus Torvalds again++;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds }
2418dce39b8SKrzysztof Helt spin_unlock_irqrestore(&opl3->voice_lock, flags);
2428dce39b8SKrzysztof Helt
2438dce39b8SKrzysztof Helt spin_lock_irqsave(&opl3->sys_timer_lock, flags);
244f5d5f75fSTakashi Iwai if (again)
245f5d5f75fSTakashi Iwai mod_timer(&opl3->tlist, jiffies + 1); /* invoke again */
246f5d5f75fSTakashi Iwai else
2471da177e4SLinus Torvalds opl3->sys_timer_status = 0;
248b32425acSTakashi Iwai spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds /*
2521da177e4SLinus Torvalds * Start system timer
2531da177e4SLinus Torvalds */
snd_opl3_start_timer(struct snd_opl3 * opl3)2545b1646a8STakashi Iwai static void snd_opl3_start_timer(struct snd_opl3 *opl3)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds unsigned long flags;
2571da177e4SLinus Torvalds spin_lock_irqsave(&opl3->sys_timer_lock, flags);
2581da177e4SLinus Torvalds if (! opl3->sys_timer_status) {
259f5d5f75fSTakashi Iwai mod_timer(&opl3->tlist, jiffies + 1);
2601da177e4SLinus Torvalds opl3->sys_timer_status = 1;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds
2651da177e4SLinus Torvalds /* ------------------------------ */
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds
268e3de2a40STakashi Iwai static const int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
2691da177e4SLinus Torvalds 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
2701da177e4SLinus Torvalds };
2711da177e4SLinus Torvalds
2721da177e4SLinus Torvalds /*
2731da177e4SLinus Torvalds * Start a note.
2741da177e4SLinus Torvalds */
snd_opl3_note_on(void * p,int note,int vel,struct snd_midi_channel * chan)2755b1646a8STakashi Iwai void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
2761da177e4SLinus Torvalds {
2775b1646a8STakashi Iwai struct snd_opl3 *opl3;
2781da177e4SLinus Torvalds int instr_4op;
2791da177e4SLinus Torvalds
2801da177e4SLinus Torvalds int voice;
2815b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2;
2821da177e4SLinus Torvalds unsigned short connect_mask;
2831da177e4SLinus Torvalds unsigned char connection;
2841da177e4SLinus Torvalds unsigned char vol_op[4];
2851da177e4SLinus Torvalds
2861da177e4SLinus Torvalds int extra_prg = 0;
2871da177e4SLinus Torvalds
2881da177e4SLinus Torvalds unsigned short reg_side;
2891da177e4SLinus Torvalds unsigned char op_offset;
2901da177e4SLinus Torvalds unsigned char voice_offset;
2911da177e4SLinus Torvalds unsigned short opl3_reg;
2921da177e4SLinus Torvalds unsigned char reg_val;
293224a0332STakashi Iwai unsigned char prg, bank;
2941da177e4SLinus Torvalds
2951da177e4SLinus Torvalds int key = note;
2961da177e4SLinus Torvalds unsigned char fnum, blocknum;
2971da177e4SLinus Torvalds int i;
2981da177e4SLinus Torvalds
299224a0332STakashi Iwai struct fm_patch *patch;
3005b1646a8STakashi Iwai struct fm_instrument *fm;
3011da177e4SLinus Torvalds unsigned long flags;
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds opl3 = p;
3041da177e4SLinus Torvalds
3051da177e4SLinus Torvalds #ifdef DEBUG_MIDI
30645203832STakashi Iwai snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
3071da177e4SLinus Torvalds chan->number, chan->midi_program, note, vel);
3081da177e4SLinus Torvalds #endif
3091da177e4SLinus Torvalds
3101da177e4SLinus Torvalds /* in SYNTH mode, application takes care of voices */
3111da177e4SLinus Torvalds /* in SEQ mode, drum voice numbers are notes on drum channel */
3121da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
3131da177e4SLinus Torvalds if (chan->drum_channel) {
3141da177e4SLinus Torvalds /* percussion instruments are located in bank 128 */
315224a0332STakashi Iwai bank = 128;
316224a0332STakashi Iwai prg = note;
3171da177e4SLinus Torvalds } else {
318224a0332STakashi Iwai bank = chan->gm_bank_select;
319224a0332STakashi Iwai prg = chan->midi_program;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds } else {
3221da177e4SLinus Torvalds /* Prepare for OSS mode */
3231da177e4SLinus Torvalds if (chan->number >= MAX_OPL3_VOICES)
3241da177e4SLinus Torvalds return;
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds /* OSS instruments are located in bank 127 */
327224a0332STakashi Iwai bank = 127;
328224a0332STakashi Iwai prg = chan->midi_program;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds
3311da177e4SLinus Torvalds spin_lock_irqsave(&opl3->voice_lock, flags);
3321da177e4SLinus Torvalds
3331da177e4SLinus Torvalds if (use_internal_drums) {
3341da177e4SLinus Torvalds snd_opl3_drum_switch(opl3, note, vel, 1, chan);
3351da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
3361da177e4SLinus Torvalds return;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds
3391da177e4SLinus Torvalds __extra_prg:
340224a0332STakashi Iwai patch = snd_opl3_find_patch(opl3, prg, bank, 0);
341224a0332STakashi Iwai if (!patch) {
3421da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
3431da177e4SLinus Torvalds return;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds
346224a0332STakashi Iwai fm = &patch->inst;
347224a0332STakashi Iwai switch (patch->type) {
3481da177e4SLinus Torvalds case FM_PATCH_OPL2:
3491da177e4SLinus Torvalds instr_4op = 0;
3501da177e4SLinus Torvalds break;
3511da177e4SLinus Torvalds case FM_PATCH_OPL3:
3521da177e4SLinus Torvalds if (opl3->hardware >= OPL3_HW_OPL3) {
3531da177e4SLinus Torvalds instr_4op = 1;
3541da177e4SLinus Torvalds break;
3551da177e4SLinus Torvalds }
356c0dbbdadSGustavo A. R. Silva fallthrough;
3571da177e4SLinus Torvalds default:
3581da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
3591da177e4SLinus Torvalds return;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds #ifdef DEBUG_MIDI
36245203832STakashi Iwai snd_printk(KERN_DEBUG " --> OPL%i instrument: %s\n",
363224a0332STakashi Iwai instr_4op ? 3 : 2, patch->name);
3641da177e4SLinus Torvalds #endif
3651da177e4SLinus Torvalds /* in SYNTH mode, application takes care of voices */
3661da177e4SLinus Torvalds /* in SEQ mode, allocate voice on free OPL3 channel */
3671da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
3681da177e4SLinus Torvalds voice = opl3_get_voice(opl3, instr_4op, chan);
3691da177e4SLinus Torvalds } else {
3701da177e4SLinus Torvalds /* remap OSS voice */
3711da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number];
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds
374a8d09e3aSTakashi Iwai if (voice < 0) {
375a8d09e3aSTakashi Iwai spin_unlock_irqrestore(&opl3->voice_lock, flags);
376a8d09e3aSTakashi Iwai return;
377a8d09e3aSTakashi Iwai }
378a8d09e3aSTakashi Iwai
3791da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) {
3801da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */
3811da177e4SLinus Torvalds reg_side = OPL3_LEFT;
3821da177e4SLinus Torvalds voice_offset = voice;
3831da177e4SLinus Torvalds connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
3841da177e4SLinus Torvalds } else {
3851da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */
3861da177e4SLinus Torvalds reg_side = OPL3_RIGHT;
3871da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES;
3881da177e4SLinus Torvalds connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds
3911da177e4SLinus Torvalds /* kill voice on channel */
3921da177e4SLinus Torvalds vp = &opl3->voices[voice];
3931da177e4SLinus Torvalds if (vp->state > 0) {
3941da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
3951da177e4SLinus Torvalds reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
3961da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds if (instr_4op) {
3991da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3];
400*2dee54b2SColin Ian King if (vp2->state > 0) {
4011da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
4021da177e4SLinus Torvalds voice_offset + 3);
4031da177e4SLinus Torvalds reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
4041da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds /* set connection register */
4091da177e4SLinus Torvalds if (instr_4op) {
4101da177e4SLinus Torvalds if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
4111da177e4SLinus Torvalds opl3->connection_reg |= connect_mask;
4121da177e4SLinus Torvalds /* set connection bit */
4131da177e4SLinus Torvalds opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
4141da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, opl3->connection_reg);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds } else {
4171da177e4SLinus Torvalds if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
4181da177e4SLinus Torvalds opl3->connection_reg &= ~connect_mask;
4191da177e4SLinus Torvalds /* clear connection bit */
4201da177e4SLinus Torvalds opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
4211da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, opl3->connection_reg);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds
4251da177e4SLinus Torvalds #ifdef DEBUG_MIDI
42645203832STakashi Iwai snd_printk(KERN_DEBUG " --> setting OPL3 connection: 0x%x\n",
4271da177e4SLinus Torvalds opl3->connection_reg);
4281da177e4SLinus Torvalds #endif
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds * calculate volume depending on connection
4311da177e4SLinus Torvalds * between FM operators (see include/opl3.h)
4321da177e4SLinus Torvalds */
4331da177e4SLinus Torvalds for (i = 0; i < (instr_4op ? 4 : 2); i++)
4341da177e4SLinus Torvalds vol_op[i] = fm->op[i].ksl_level;
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvalds connection = fm->feedback_connection[0] & 0x01;
4371da177e4SLinus Torvalds if (instr_4op) {
4381da177e4SLinus Torvalds connection <<= 1;
4391da177e4SLinus Torvalds connection |= fm->feedback_connection[1] & 0x01;
4401da177e4SLinus Torvalds
4411da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[3], vel, chan);
4421da177e4SLinus Torvalds switch (connection) {
4431da177e4SLinus Torvalds case 0x03:
4441da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[2], vel, chan);
445c0dbbdadSGustavo A. R. Silva fallthrough;
4461da177e4SLinus Torvalds case 0x02:
4471da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[0], vel, chan);
4481da177e4SLinus Torvalds break;
4491da177e4SLinus Torvalds case 0x01:
4501da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[1], vel, chan);
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds } else {
4531da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[1], vel, chan);
4541da177e4SLinus Torvalds if (connection)
4551da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[0], vel, chan);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds
4581da177e4SLinus Torvalds /* Program the FM voice characteristics */
4591da177e4SLinus Torvalds for (i = 0; i < (instr_4op ? 4 : 2); i++) {
4601da177e4SLinus Torvalds #ifdef DEBUG_MIDI
46145203832STakashi Iwai snd_printk(KERN_DEBUG " --> programming operator %i\n", i);
4621da177e4SLinus Torvalds #endif
4631da177e4SLinus Torvalds op_offset = snd_opl3_regmap[voice_offset][i];
4641da177e4SLinus Torvalds
4651da177e4SLinus Torvalds /* Set OPL3 AM_VIB register of requested voice/operator */
4661da177e4SLinus Torvalds reg_val = fm->op[i].am_vib;
4671da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
4681da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds /* Set OPL3 KSL_LEVEL register of requested voice/operator */
4711da177e4SLinus Torvalds reg_val = vol_op[i];
4721da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
4731da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4741da177e4SLinus Torvalds
4751da177e4SLinus Torvalds /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
4761da177e4SLinus Torvalds reg_val = fm->op[i].attack_decay;
4771da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
4781da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4791da177e4SLinus Torvalds
4801da177e4SLinus Torvalds /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
4811da177e4SLinus Torvalds reg_val = fm->op[i].sustain_release;
4821da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
4831da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4841da177e4SLinus Torvalds
4851da177e4SLinus Torvalds /* Select waveform */
4861da177e4SLinus Torvalds reg_val = fm->op[i].wave_select;
4871da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
4881da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds /* Set operator feedback and 2op inter-operator connection */
4921da177e4SLinus Torvalds reg_val = fm->feedback_connection[0];
4931da177e4SLinus Torvalds /* Set output voice connection */
4941da177e4SLinus Torvalds reg_val |= OPL3_STEREO_BITS;
4951da177e4SLinus Torvalds if (chan->gm_pan < 43)
4961da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_RIGHT;
4971da177e4SLinus Torvalds if (chan->gm_pan > 85)
4981da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_LEFT;
4991da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
5001da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
5011da177e4SLinus Torvalds
5021da177e4SLinus Torvalds if (instr_4op) {
5031da177e4SLinus Torvalds /* Set 4op inter-operator connection */
5041da177e4SLinus Torvalds reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
5051da177e4SLinus Torvalds /* Set output voice connection */
5061da177e4SLinus Torvalds reg_val |= OPL3_STEREO_BITS;
5071da177e4SLinus Torvalds if (chan->gm_pan < 43)
5081da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_RIGHT;
5091da177e4SLinus Torvalds if (chan->gm_pan > 85)
5101da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_LEFT;
5111da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
5121da177e4SLinus Torvalds voice_offset + 3);
5131da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds * Special treatment of percussion notes for fm:
5181da177e4SLinus Torvalds * Requested pitch is really program, and pitch for
5191da177e4SLinus Torvalds * device is whatever was specified in the patch library.
5201da177e4SLinus Torvalds */
5211da177e4SLinus Torvalds if (fm->fix_key)
5221da177e4SLinus Torvalds note = fm->fix_key;
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds * use transpose if defined in patch library
5251da177e4SLinus Torvalds */
5261da177e4SLinus Torvalds if (fm->trnsps)
5271da177e4SLinus Torvalds note += (fm->trnsps - 64);
5281da177e4SLinus Torvalds
5291da177e4SLinus Torvalds snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
5301da177e4SLinus Torvalds
5311da177e4SLinus Torvalds /* Set OPL3 FNUM_LOW register of requested voice */
5321da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
5331da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, fnum);
5341da177e4SLinus Torvalds
5351da177e4SLinus Torvalds opl3->voices[voice].keyon_reg = blocknum;
5361da177e4SLinus Torvalds
5371da177e4SLinus Torvalds /* Set output sound flag */
5381da177e4SLinus Torvalds blocknum |= OPL3_KEYON_BIT;
5391da177e4SLinus Torvalds
5401da177e4SLinus Torvalds #ifdef DEBUG_MIDI
54145203832STakashi Iwai snd_printk(KERN_DEBUG " --> trigger voice %i\n", voice);
5421da177e4SLinus Torvalds #endif
5431da177e4SLinus Torvalds /* Set OPL3 KEYON_BLOCK register of requested voice */
5441da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
5451da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, blocknum);
5461da177e4SLinus Torvalds
5471da177e4SLinus Torvalds /* kill note after fixed duration (in centiseconds) */
5481da177e4SLinus Torvalds if (fm->fix_dur) {
5491da177e4SLinus Torvalds opl3->voices[voice].note_off = jiffies +
5501da177e4SLinus Torvalds (fm->fix_dur * HZ) / 100;
5511da177e4SLinus Torvalds snd_opl3_start_timer(opl3);
5521da177e4SLinus Torvalds opl3->voices[voice].note_off_check = 1;
5531da177e4SLinus Torvalds } else
5541da177e4SLinus Torvalds opl3->voices[voice].note_off_check = 0;
5551da177e4SLinus Torvalds
5561da177e4SLinus Torvalds /* get extra pgm, but avoid possible loops */
5571da177e4SLinus Torvalds extra_prg = (extra_prg) ? 0 : fm->modes;
5581da177e4SLinus Torvalds
5591da177e4SLinus Torvalds /* do the bookkeeping */
5601da177e4SLinus Torvalds vp->time = opl3->use_time++;
5611da177e4SLinus Torvalds vp->note = key;
5621da177e4SLinus Torvalds vp->chan = chan;
5631da177e4SLinus Torvalds
5641da177e4SLinus Torvalds if (instr_4op) {
5651da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_ON_4OP;
5661da177e4SLinus Torvalds
5671da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3];
5681da177e4SLinus Torvalds vp2->time = opl3->use_time++;
5691da177e4SLinus Torvalds vp2->note = key;
5701da177e4SLinus Torvalds vp2->chan = chan;
5711da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
5721da177e4SLinus Torvalds } else {
5731da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
5741da177e4SLinus Torvalds /* 4op killed by 2op, release bounded voice */
5751da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3];
5761da177e4SLinus Torvalds vp2->time = opl3->use_time++;
5771da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_OFF;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_ON_2OP;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds
5821da177e4SLinus Torvalds #ifdef DEBUG_ALLOC
5831da177e4SLinus Torvalds debug_alloc(opl3, "note on ", voice);
5841da177e4SLinus Torvalds #endif
5851da177e4SLinus Torvalds
5861da177e4SLinus Torvalds /* allocate extra program if specified in patch library */
5871da177e4SLinus Torvalds if (extra_prg) {
5881da177e4SLinus Torvalds if (extra_prg > 128) {
589224a0332STakashi Iwai bank = 128;
5901da177e4SLinus Torvalds /* percussions start at 35 */
591224a0332STakashi Iwai prg = extra_prg - 128 + 35 - 1;
5921da177e4SLinus Torvalds } else {
593224a0332STakashi Iwai bank = 0;
594224a0332STakashi Iwai prg = extra_prg - 1;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds #ifdef DEBUG_MIDI
59745203832STakashi Iwai snd_printk(KERN_DEBUG " *** allocating extra program\n");
5981da177e4SLinus Torvalds #endif
5991da177e4SLinus Torvalds goto __extra_prg;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds
snd_opl3_kill_voice(struct snd_opl3 * opl3,int voice)6045b1646a8STakashi Iwai static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds unsigned short reg_side;
6071da177e4SLinus Torvalds unsigned char voice_offset;
6081da177e4SLinus Torvalds unsigned short opl3_reg;
6091da177e4SLinus Torvalds
6105b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2;
6111da177e4SLinus Torvalds
6125e246b85STakashi Iwai if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
6135e246b85STakashi Iwai return;
6141da177e4SLinus Torvalds
6151da177e4SLinus Torvalds vp = &opl3->voices[voice];
6161da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) {
6171da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */
6181da177e4SLinus Torvalds reg_side = OPL3_LEFT;
6191da177e4SLinus Torvalds voice_offset = voice;
6201da177e4SLinus Torvalds } else {
6211da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */
6221da177e4SLinus Torvalds reg_side = OPL3_RIGHT;
6231da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds
6261da177e4SLinus Torvalds /* kill voice */
6271da177e4SLinus Torvalds #ifdef DEBUG_MIDI
62845203832STakashi Iwai snd_printk(KERN_DEBUG " --> kill voice %i\n", voice);
6291da177e4SLinus Torvalds #endif
6301da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
6311da177e4SLinus Torvalds /* clear Key ON bit */
6321da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, vp->keyon_reg);
6331da177e4SLinus Torvalds
6341da177e4SLinus Torvalds /* do the bookkeeping */
6351da177e4SLinus Torvalds vp->time = opl3->use_time++;
6361da177e4SLinus Torvalds
6371da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
6381da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3];
6391da177e4SLinus Torvalds
6401da177e4SLinus Torvalds vp2->time = opl3->use_time++;
6411da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_OFF;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_OFF;
6441da177e4SLinus Torvalds #ifdef DEBUG_ALLOC
6451da177e4SLinus Torvalds debug_alloc(opl3, "note off", voice);
6461da177e4SLinus Torvalds #endif
6471da177e4SLinus Torvalds
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds
6501da177e4SLinus Torvalds /*
6511da177e4SLinus Torvalds * Release a note in response to a midi note off.
6521da177e4SLinus Torvalds */
snd_opl3_note_off_unsafe(void * p,int note,int vel,struct snd_midi_channel * chan)6538dce39b8SKrzysztof Helt static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
6548dce39b8SKrzysztof Helt struct snd_midi_channel *chan)
6551da177e4SLinus Torvalds {
6565b1646a8STakashi Iwai struct snd_opl3 *opl3;
6571da177e4SLinus Torvalds
6581da177e4SLinus Torvalds int voice;
6595b1646a8STakashi Iwai struct snd_opl3_voice *vp;
6601da177e4SLinus Torvalds
6611da177e4SLinus Torvalds opl3 = p;
6621da177e4SLinus Torvalds
6631da177e4SLinus Torvalds #ifdef DEBUG_MIDI
66445203832STakashi Iwai snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
6651da177e4SLinus Torvalds chan->number, chan->midi_program, note);
6661da177e4SLinus Torvalds #endif
6671da177e4SLinus Torvalds
6681da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
6691da177e4SLinus Torvalds if (chan->drum_channel && use_internal_drums) {
6701da177e4SLinus Torvalds snd_opl3_drum_switch(opl3, note, vel, 0, chan);
6711da177e4SLinus Torvalds return;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds /* this loop will hopefully kill all extra voices, because
6741da177e4SLinus Torvalds they are grouped by the same channel and note values */
6751da177e4SLinus Torvalds for (voice = 0; voice < opl3->max_voices; voice++) {
6761da177e4SLinus Torvalds vp = &opl3->voices[voice];
6771da177e4SLinus Torvalds if (vp->state > 0 && vp->chan == chan && vp->note == note) {
6781da177e4SLinus Torvalds snd_opl3_kill_voice(opl3, voice);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds } else {
6821da177e4SLinus Torvalds /* remap OSS voices */
6831da177e4SLinus Torvalds if (chan->number < MAX_OPL3_VOICES) {
6841da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number];
6851da177e4SLinus Torvalds snd_opl3_kill_voice(opl3, voice);
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds }
6888dce39b8SKrzysztof Helt }
6898dce39b8SKrzysztof Helt
snd_opl3_note_off(void * p,int note,int vel,struct snd_midi_channel * chan)6908dce39b8SKrzysztof Helt void snd_opl3_note_off(void *p, int note, int vel,
6918dce39b8SKrzysztof Helt struct snd_midi_channel *chan)
6928dce39b8SKrzysztof Helt {
6938dce39b8SKrzysztof Helt struct snd_opl3 *opl3 = p;
6948dce39b8SKrzysztof Helt unsigned long flags;
6958dce39b8SKrzysztof Helt
6968dce39b8SKrzysztof Helt spin_lock_irqsave(&opl3->voice_lock, flags);
6978dce39b8SKrzysztof Helt snd_opl3_note_off_unsafe(p, note, vel, chan);
6981da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds
7011da177e4SLinus Torvalds /*
7021da177e4SLinus Torvalds * key pressure change
7031da177e4SLinus Torvalds */
snd_opl3_key_press(void * p,int note,int vel,struct snd_midi_channel * chan)7045b1646a8STakashi Iwai void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds #ifdef DEBUG_MIDI
70745203832STakashi Iwai snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
7081da177e4SLinus Torvalds chan->number, chan->midi_program);
7091da177e4SLinus Torvalds #endif
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds * terminate note
7141da177e4SLinus Torvalds */
snd_opl3_terminate_note(void * p,int note,struct snd_midi_channel * chan)7155b1646a8STakashi Iwai void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds #ifdef DEBUG_MIDI
71845203832STakashi Iwai snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
7191da177e4SLinus Torvalds chan->number, chan->midi_program);
7201da177e4SLinus Torvalds #endif
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds
snd_opl3_update_pitch(struct snd_opl3 * opl3,int voice)7235b1646a8STakashi Iwai static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds unsigned short reg_side;
7261da177e4SLinus Torvalds unsigned char voice_offset;
7271da177e4SLinus Torvalds unsigned short opl3_reg;
7281da177e4SLinus Torvalds
7291da177e4SLinus Torvalds unsigned char fnum, blocknum;
7301da177e4SLinus Torvalds
7315b1646a8STakashi Iwai struct snd_opl3_voice *vp;
7321da177e4SLinus Torvalds
7335e246b85STakashi Iwai if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
7345e246b85STakashi Iwai return;
7351da177e4SLinus Torvalds
7361da177e4SLinus Torvalds vp = &opl3->voices[voice];
7371da177e4SLinus Torvalds if (vp->chan == NULL)
7381da177e4SLinus Torvalds return; /* not allocated? */
7391da177e4SLinus Torvalds
7401da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) {
7411da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */
7421da177e4SLinus Torvalds reg_side = OPL3_LEFT;
7431da177e4SLinus Torvalds voice_offset = voice;
7441da177e4SLinus Torvalds } else {
7451da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */
7461da177e4SLinus Torvalds reg_side = OPL3_RIGHT;
7471da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds
7501da177e4SLinus Torvalds snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
7511da177e4SLinus Torvalds
7521da177e4SLinus Torvalds /* Set OPL3 FNUM_LOW register of requested voice */
7531da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
7541da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, fnum);
7551da177e4SLinus Torvalds
7561da177e4SLinus Torvalds vp->keyon_reg = blocknum;
7571da177e4SLinus Torvalds
7581da177e4SLinus Torvalds /* Set output sound flag */
7591da177e4SLinus Torvalds blocknum |= OPL3_KEYON_BIT;
7601da177e4SLinus Torvalds
7611da177e4SLinus Torvalds /* Set OPL3 KEYON_BLOCK register of requested voice */
7621da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
7631da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, blocknum);
7641da177e4SLinus Torvalds
7651da177e4SLinus Torvalds vp->time = opl3->use_time++;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds
7681da177e4SLinus Torvalds /*
7691da177e4SLinus Torvalds * Update voice pitch controller
7701da177e4SLinus Torvalds */
snd_opl3_pitch_ctrl(struct snd_opl3 * opl3,struct snd_midi_channel * chan)7715b1646a8STakashi Iwai static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds int voice;
7745b1646a8STakashi Iwai struct snd_opl3_voice *vp;
7751da177e4SLinus Torvalds
7761da177e4SLinus Torvalds unsigned long flags;
7771da177e4SLinus Torvalds
7781da177e4SLinus Torvalds spin_lock_irqsave(&opl3->voice_lock, flags);
7791da177e4SLinus Torvalds
7801da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
7811da177e4SLinus Torvalds for (voice = 0; voice < opl3->max_voices; voice++) {
7821da177e4SLinus Torvalds vp = &opl3->voices[voice];
7831da177e4SLinus Torvalds if (vp->state > 0 && vp->chan == chan) {
7841da177e4SLinus Torvalds snd_opl3_update_pitch(opl3, voice);
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds } else {
7881da177e4SLinus Torvalds /* remap OSS voices */
7891da177e4SLinus Torvalds if (chan->number < MAX_OPL3_VOICES) {
7901da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number];
7911da177e4SLinus Torvalds snd_opl3_update_pitch(opl3, voice);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags);
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds
7971da177e4SLinus Torvalds /*
7983a4fa0a2SRobert P. J. Day * Deal with a controller type event. This includes all types of
7991da177e4SLinus Torvalds * control events, not just the midi controllers
8001da177e4SLinus Torvalds */
snd_opl3_control(void * p,int type,struct snd_midi_channel * chan)8015b1646a8STakashi Iwai void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
8021da177e4SLinus Torvalds {
8035b1646a8STakashi Iwai struct snd_opl3 *opl3;
8041da177e4SLinus Torvalds
8051da177e4SLinus Torvalds opl3 = p;
8061da177e4SLinus Torvalds #ifdef DEBUG_MIDI
80745203832STakashi Iwai snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
8081da177e4SLinus Torvalds type, chan->number, chan->midi_program);
8091da177e4SLinus Torvalds #endif
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds switch (type) {
8121da177e4SLinus Torvalds case MIDI_CTL_MSB_MODWHEEL:
8131da177e4SLinus Torvalds if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
8141da177e4SLinus Torvalds opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
8151da177e4SLinus Torvalds else
8161da177e4SLinus Torvalds opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
8171da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
8181da177e4SLinus Torvalds opl3->drum_reg);
8191da177e4SLinus Torvalds break;
8201da177e4SLinus Torvalds case MIDI_CTL_E2_TREMOLO_DEPTH:
8211da177e4SLinus Torvalds if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
8221da177e4SLinus Torvalds opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
8231da177e4SLinus Torvalds else
8241da177e4SLinus Torvalds opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
8251da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
8261da177e4SLinus Torvalds opl3->drum_reg);
8271da177e4SLinus Torvalds break;
8281da177e4SLinus Torvalds case MIDI_CTL_PITCHBEND:
8291da177e4SLinus Torvalds snd_opl3_pitch_ctrl(opl3, chan);
8301da177e4SLinus Torvalds break;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds
8341da177e4SLinus Torvalds /*
8351da177e4SLinus Torvalds * NRPN events
8361da177e4SLinus Torvalds */
snd_opl3_nrpn(void * p,struct snd_midi_channel * chan,struct snd_midi_channel_set * chset)8375b1646a8STakashi Iwai void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
8385b1646a8STakashi Iwai struct snd_midi_channel_set *chset)
8391da177e4SLinus Torvalds {
8401da177e4SLinus Torvalds #ifdef DEBUG_MIDI
84145203832STakashi Iwai snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
8421da177e4SLinus Torvalds chan->number, chan->midi_program);
8431da177e4SLinus Torvalds #endif
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds * receive sysex
8481da177e4SLinus Torvalds */
snd_opl3_sysex(void * p,unsigned char * buf,int len,int parsed,struct snd_midi_channel_set * chset)8491da177e4SLinus Torvalds void snd_opl3_sysex(void *p, unsigned char *buf, int len,
8505b1646a8STakashi Iwai int parsed, struct snd_midi_channel_set *chset)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds #ifdef DEBUG_MIDI
85345203832STakashi Iwai snd_printk(KERN_DEBUG "SYSEX\n");
8541da177e4SLinus Torvalds #endif
8551da177e4SLinus Torvalds }
856