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