xref: /openbmc/linux/sound/drivers/opl3/opl3_drums.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
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 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
25 
26 static char snd_opl3_drum_table[47] =
27 {
28 	OPL3_BASSDRUM_ON,  OPL3_BASSDRUM_ON,  OPL3_HIHAT_ON,	/* 35 - 37 */
29 	OPL3_SNAREDRUM_ON, OPL3_HIHAT_ON,     OPL3_SNAREDRUM_ON, /* 38 - 40 */
30 	OPL3_BASSDRUM_ON,  OPL3_HIHAT_ON,     OPL3_BASSDRUM_ON,	/* 41 - 43 */
31 	OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,    OPL3_HIHAT_ON,	/* 44 - 46 */
32 	OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_CYMBAL_ON,	/* 47 - 49 */
33 
34 	OPL3_TOMTOM_ON,    OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,	/* 50 - 52 */
35 	OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,	/* 53 - 55 */
36 	OPL3_HIHAT_ON,     OPL3_CYMBAL_ON,    OPL3_TOMTOM_ON,	/* 56 - 58 */
37 	OPL3_CYMBAL_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,	/* 59 - 61 */
38 	OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,	/* 62 - 64 */
39 
40 	OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,	/* 65 - 67 */
41 	OPL3_TOMTOM_ON,    OPL3_HIHAT_ON,     OPL3_HIHAT_ON,	/* 68 - 70 */
42 	OPL3_HIHAT_ON,     OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,	/* 71 - 73 */
43 	OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,	/* 74 - 76 */
44 	OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,	/* 77 - 79 */
45 	OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON			/* 80 - 81 */
46 };
47 
48 typedef struct snd_opl3_drum_voice {
49 	int voice;
50 	int op;
51 	unsigned char am_vib;
52 	unsigned char ksl_level;
53 	unsigned char attack_decay;
54 	unsigned char sustain_release;
55 	unsigned char feedback_connection;
56 	unsigned char wave_select;
57 } snd_opl3_drum_voice_t;
58 
59 typedef struct snd_opl3_drum_note {
60 	int voice;
61 	unsigned char fnum;
62 	unsigned char octave_f;
63 	unsigned char feedback_connection;
64 } snd_opl3_drum_note_t;
65 
66 static snd_opl3_drum_voice_t bass_op0 = {6, 0, 0x00, 0x32, 0xf8, 0x66, 0x30, 0x00};
67 static snd_opl3_drum_voice_t bass_op1 = {6, 1, 0x00, 0x03, 0xf6, 0x57, 0x30, 0x00};
68 static snd_opl3_drum_note_t bass_note = {6, 0x90, 0x09};
69 
70 static snd_opl3_drum_voice_t hihat = {7, 0, 0x00, 0x03, 0xf0, 0x06, 0x20, 0x00};
71 
72 static snd_opl3_drum_voice_t snare = {7, 1, 0x00, 0x03, 0xf0, 0x07, 0x20, 0x02};
73 static snd_opl3_drum_note_t snare_note = {7, 0xf4, 0x0d};
74 
75 static snd_opl3_drum_voice_t tomtom = {8, 0, 0x02, 0x03, 0xf0, 0x06, 0x10, 0x00};
76 static snd_opl3_drum_note_t tomtom_note = {8, 0xf4, 0x09};
77 
78 static snd_opl3_drum_voice_t cymbal = {8, 1, 0x04, 0x03, 0xf0, 0x06, 0x10, 0x00};
79 
80 /*
81  * set drum voice characteristics
82  */
83 static void snd_opl3_drum_voice_set(opl3_t *opl3, snd_opl3_drum_voice_t *data)
84 {
85 	unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
86 	unsigned char voice_offset = data->voice;
87 	unsigned short opl3_reg;
88 
89 	/* Set OPL3 AM_VIB register */
90 	opl3_reg = OPL3_LEFT | (OPL3_REG_AM_VIB + op_offset);
91 	opl3->command(opl3, opl3_reg, data->am_vib);
92 
93 	/* Set OPL3 KSL_LEVEL register */
94 	opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
95 	opl3->command(opl3, opl3_reg, data->ksl_level);
96 
97 	/* Set OPL3 ATTACK_DECAY register */
98 	opl3_reg = OPL3_LEFT | (OPL3_REG_ATTACK_DECAY + op_offset);
99 	opl3->command(opl3, opl3_reg, data->attack_decay);
100 
101 	/* Set OPL3 SUSTAIN_RELEASE register */
102 	opl3_reg = OPL3_LEFT | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
103 	opl3->command(opl3, opl3_reg, data->sustain_release);
104 
105 	/* Set OPL3 FEEDBACK_CONNECTION register */
106 	opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
107 	opl3->command(opl3, opl3_reg, data->feedback_connection);
108 
109 	/* Select waveform */
110 	opl3_reg = OPL3_LEFT | (OPL3_REG_WAVE_SELECT + op_offset);
111 	opl3->command(opl3, opl3_reg, data->wave_select);
112 }
113 
114 /*
115  * Set drum voice pitch
116  */
117 static void snd_opl3_drum_note_set(opl3_t *opl3, snd_opl3_drum_note_t *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(opl3_t *opl3, snd_opl3_drum_voice_t *data,
135 				  int vel, snd_midi_channel_t *chan)
136 {
137 	unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
138 	unsigned char voice_offset = data->voice;
139 	unsigned char reg_val;
140 	unsigned short opl3_reg;
141 
142 	/* Set OPL3 KSL_LEVEL register */
143 	reg_val = data->ksl_level;
144 	snd_opl3_calc_volume(&reg_val, vel, chan);
145 	opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
146 	opl3->command(opl3, opl3_reg, reg_val);
147 
148 	/* Set OPL3 FEEDBACK_CONNECTION register */
149 	/* Set output voice connection */
150 	reg_val = data->feedback_connection | OPL3_STEREO_BITS;
151 	if (chan->gm_pan < 43)
152 		reg_val &= ~OPL3_VOICE_TO_RIGHT;
153 	if (chan->gm_pan > 85)
154 		reg_val &= ~OPL3_VOICE_TO_LEFT;
155 	opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
156 	opl3->command(opl3, opl3_reg, reg_val);
157 }
158 
159 /*
160  * Loads drum voices at init time
161  */
162 void snd_opl3_load_drums(opl3_t *opl3)
163 {
164 	snd_opl3_drum_voice_set(opl3, &bass_op0);
165 	snd_opl3_drum_voice_set(opl3, &bass_op1);
166 	snd_opl3_drum_note_set(opl3, &bass_note);
167 
168 	snd_opl3_drum_voice_set(opl3, &hihat);
169 
170 	snd_opl3_drum_voice_set(opl3, &snare);
171 	snd_opl3_drum_note_set(opl3, &snare_note);
172 
173 	snd_opl3_drum_voice_set(opl3, &tomtom);
174 	snd_opl3_drum_note_set(opl3, &tomtom_note);
175 
176 	snd_opl3_drum_voice_set(opl3, &cymbal);
177 }
178 
179 /*
180  * Switch drum voice on or off
181  */
182 void snd_opl3_drum_switch(opl3_t *opl3, int note, int vel, int on_off,
183 			  snd_midi_channel_t *chan)
184 {
185 	unsigned char drum_mask;
186 	snd_opl3_drum_voice_t *drum_voice;
187 
188 	if (!(opl3->drum_reg & OPL3_PERCUSSION_ENABLE))
189 		return;
190 
191 	if ((note < 35) || (note > 81))
192 		return;
193 	drum_mask = snd_opl3_drum_table[note - 35];
194 
195 	if (on_off) {
196 		switch (drum_mask) {
197 		case OPL3_BASSDRUM_ON:
198 			drum_voice = &bass_op1;
199 			break;
200 		case OPL3_HIHAT_ON:
201 			drum_voice = &hihat;
202 			break;
203 		case OPL3_SNAREDRUM_ON:
204 			drum_voice = &snare;
205 			break;
206 		case OPL3_TOMTOM_ON:
207 			drum_voice = &tomtom;
208 			break;
209 		case OPL3_CYMBAL_ON:
210 			drum_voice = &cymbal;
211 			break;
212 		default:
213 			drum_voice = &tomtom;
214 		}
215 
216 		snd_opl3_drum_vol_set(opl3, drum_voice, vel, chan);
217 		opl3->drum_reg |= drum_mask;
218 	} else {
219 		opl3->drum_reg &= ~drum_mask;
220 	}
221 	opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
222 			 opl3->drum_reg);
223 }
224