1 /* 2 * Copyright (c) by Uros Bizjak <uros@kss-loka.si> 3 * 4 * OPL2/OPL3/OPL4 FM routines for internal percussion channels 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 22 #include "opl3_voice.h" 23 24 static char snd_opl3_drum_table[47] = 25 { 26 OPL3_BASSDRUM_ON, OPL3_BASSDRUM_ON, OPL3_HIHAT_ON, /* 35 - 37 */ 27 OPL3_SNAREDRUM_ON, OPL3_HIHAT_ON, OPL3_SNAREDRUM_ON, /* 38 - 40 */ 28 OPL3_BASSDRUM_ON, OPL3_HIHAT_ON, OPL3_BASSDRUM_ON, /* 41 - 43 */ 29 OPL3_HIHAT_ON, OPL3_TOMTOM_ON, OPL3_HIHAT_ON, /* 44 - 46 */ 30 OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_CYMBAL_ON, /* 47 - 49 */ 31 32 OPL3_TOMTOM_ON, OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, /* 50 - 52 */ 33 OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, /* 53 - 55 */ 34 OPL3_HIHAT_ON, OPL3_CYMBAL_ON, OPL3_TOMTOM_ON, /* 56 - 58 */ 35 OPL3_CYMBAL_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 59 - 61 */ 36 OPL3_HIHAT_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 62 - 64 */ 37 38 OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 65 - 67 */ 39 OPL3_TOMTOM_ON, OPL3_HIHAT_ON, OPL3_HIHAT_ON, /* 68 - 70 */ 40 OPL3_HIHAT_ON, OPL3_HIHAT_ON, OPL3_TOMTOM_ON, /* 71 - 73 */ 41 OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 74 - 76 */ 42 OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 77 - 79 */ 43 OPL3_CYMBAL_ON, OPL3_CYMBAL_ON /* 80 - 81 */ 44 }; 45 46 struct snd_opl3_drum_voice { 47 int voice; 48 int op; 49 unsigned char am_vib; 50 unsigned char ksl_level; 51 unsigned char attack_decay; 52 unsigned char sustain_release; 53 unsigned char feedback_connection; 54 unsigned char wave_select; 55 }; 56 57 struct snd_opl3_drum_note { 58 int voice; 59 unsigned char fnum; 60 unsigned char octave_f; 61 unsigned char feedback_connection; 62 }; 63 64 static struct snd_opl3_drum_voice bass_op0 = {6, 0, 0x00, 0x32, 0xf8, 0x66, 0x30, 0x00}; 65 static struct snd_opl3_drum_voice bass_op1 = {6, 1, 0x00, 0x03, 0xf6, 0x57, 0x30, 0x00}; 66 static struct snd_opl3_drum_note bass_note = {6, 0x90, 0x09}; 67 68 static struct snd_opl3_drum_voice hihat = {7, 0, 0x00, 0x03, 0xf0, 0x06, 0x20, 0x00}; 69 70 static struct snd_opl3_drum_voice snare = {7, 1, 0x00, 0x03, 0xf0, 0x07, 0x20, 0x02}; 71 static struct snd_opl3_drum_note snare_note = {7, 0xf4, 0x0d}; 72 73 static struct snd_opl3_drum_voice tomtom = {8, 0, 0x02, 0x03, 0xf0, 0x06, 0x10, 0x00}; 74 static struct snd_opl3_drum_note tomtom_note = {8, 0xf4, 0x09}; 75 76 static struct snd_opl3_drum_voice cymbal = {8, 1, 0x04, 0x03, 0xf0, 0x06, 0x10, 0x00}; 77 78 /* 79 * set drum voice characteristics 80 */ 81 static void snd_opl3_drum_voice_set(struct snd_opl3 *opl3, 82 struct snd_opl3_drum_voice *data) 83 { 84 unsigned char op_offset = snd_opl3_regmap[data->voice][data->op]; 85 unsigned char voice_offset = data->voice; 86 unsigned short opl3_reg; 87 88 /* Set OPL3 AM_VIB register */ 89 opl3_reg = OPL3_LEFT | (OPL3_REG_AM_VIB + op_offset); 90 opl3->command(opl3, opl3_reg, data->am_vib); 91 92 /* Set OPL3 KSL_LEVEL register */ 93 opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset); 94 opl3->command(opl3, opl3_reg, data->ksl_level); 95 96 /* Set OPL3 ATTACK_DECAY register */ 97 opl3_reg = OPL3_LEFT | (OPL3_REG_ATTACK_DECAY + op_offset); 98 opl3->command(opl3, opl3_reg, data->attack_decay); 99 100 /* Set OPL3 SUSTAIN_RELEASE register */ 101 opl3_reg = OPL3_LEFT | (OPL3_REG_SUSTAIN_RELEASE + op_offset); 102 opl3->command(opl3, opl3_reg, data->sustain_release); 103 104 /* Set OPL3 FEEDBACK_CONNECTION register */ 105 opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset); 106 opl3->command(opl3, opl3_reg, data->feedback_connection); 107 108 /* Select waveform */ 109 opl3_reg = OPL3_LEFT | (OPL3_REG_WAVE_SELECT + op_offset); 110 opl3->command(opl3, opl3_reg, data->wave_select); 111 } 112 113 /* 114 * Set drum voice pitch 115 */ 116 static void snd_opl3_drum_note_set(struct snd_opl3 *opl3, 117 struct snd_opl3_drum_note *data) 118 { 119 unsigned char voice_offset = data->voice; 120 unsigned short opl3_reg; 121 122 /* Set OPL3 FNUM_LOW register */ 123 opl3_reg = OPL3_LEFT | (OPL3_REG_FNUM_LOW + voice_offset); 124 opl3->command(opl3, opl3_reg, data->fnum); 125 126 /* Set OPL3 KEYON_BLOCK register */ 127 opl3_reg = OPL3_LEFT | (OPL3_REG_KEYON_BLOCK + voice_offset); 128 opl3->command(opl3, opl3_reg, data->octave_f); 129 } 130 131 /* 132 * Set drum voice volume and position 133 */ 134 static void snd_opl3_drum_vol_set(struct snd_opl3 *opl3, 135 struct snd_opl3_drum_voice *data, 136 int vel, struct snd_midi_channel *chan) 137 { 138 unsigned char op_offset = snd_opl3_regmap[data->voice][data->op]; 139 unsigned char voice_offset = data->voice; 140 unsigned char reg_val; 141 unsigned short opl3_reg; 142 143 /* Set OPL3 KSL_LEVEL register */ 144 reg_val = data->ksl_level; 145 snd_opl3_calc_volume(®_val, vel, chan); 146 opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset); 147 opl3->command(opl3, opl3_reg, reg_val); 148 149 /* Set OPL3 FEEDBACK_CONNECTION register */ 150 /* Set output voice connection */ 151 reg_val = data->feedback_connection | OPL3_STEREO_BITS; 152 if (chan->gm_pan < 43) 153 reg_val &= ~OPL3_VOICE_TO_RIGHT; 154 if (chan->gm_pan > 85) 155 reg_val &= ~OPL3_VOICE_TO_LEFT; 156 opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset); 157 opl3->command(opl3, opl3_reg, reg_val); 158 } 159 160 /* 161 * Loads drum voices at init time 162 */ 163 void snd_opl3_load_drums(struct snd_opl3 *opl3) 164 { 165 snd_opl3_drum_voice_set(opl3, &bass_op0); 166 snd_opl3_drum_voice_set(opl3, &bass_op1); 167 snd_opl3_drum_note_set(opl3, &bass_note); 168 169 snd_opl3_drum_voice_set(opl3, &hihat); 170 171 snd_opl3_drum_voice_set(opl3, &snare); 172 snd_opl3_drum_note_set(opl3, &snare_note); 173 174 snd_opl3_drum_voice_set(opl3, &tomtom); 175 snd_opl3_drum_note_set(opl3, &tomtom_note); 176 177 snd_opl3_drum_voice_set(opl3, &cymbal); 178 } 179 180 /* 181 * Switch drum voice on or off 182 */ 183 void snd_opl3_drum_switch(struct snd_opl3 *opl3, int note, int vel, int on_off, 184 struct snd_midi_channel *chan) 185 { 186 unsigned char drum_mask; 187 struct snd_opl3_drum_voice *drum_voice; 188 189 if (!(opl3->drum_reg & OPL3_PERCUSSION_ENABLE)) 190 return; 191 192 if ((note < 35) || (note > 81)) 193 return; 194 drum_mask = snd_opl3_drum_table[note - 35]; 195 196 if (on_off) { 197 switch (drum_mask) { 198 case OPL3_BASSDRUM_ON: 199 drum_voice = &bass_op1; 200 break; 201 case OPL3_HIHAT_ON: 202 drum_voice = &hihat; 203 break; 204 case OPL3_SNAREDRUM_ON: 205 drum_voice = &snare; 206 break; 207 case OPL3_TOMTOM_ON: 208 drum_voice = &tomtom; 209 break; 210 case OPL3_CYMBAL_ON: 211 drum_voice = &cymbal; 212 break; 213 default: 214 drum_voice = &tomtom; 215 } 216 217 snd_opl3_drum_vol_set(opl3, drum_voice, vel, chan); 218 opl3->drum_reg |= drum_mask; 219 } else { 220 opl3->drum_reg &= ~drum_mask; 221 } 222 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 223 opl3->drum_reg); 224 } 225