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