2 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 * OPL2/OPL3/OPL4 FM routines for internal percussion channels
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.
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.
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
22 #include "opl3_voice.h"
24 extern char snd_opl3_regmap
[MAX_OPL2_VOICES
][4];
26 static char snd_opl3_drum_table
[47] =
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 */
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 */
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 */
48 typedef struct snd_opl3_drum_voice
{
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
;
59 typedef struct snd_opl3_drum_note
{
62 unsigned char octave_f
;
63 unsigned char feedback_connection
;
64 } snd_opl3_drum_note_t
;
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};
70 static snd_opl3_drum_voice_t hihat
= {7, 0, 0x00, 0x03, 0xf0, 0x06, 0x20, 0x00};
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};
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};
78 static snd_opl3_drum_voice_t cymbal
= {8, 1, 0x04, 0x03, 0xf0, 0x06, 0x10, 0x00};
81 * set drum voice characteristics
83 static void snd_opl3_drum_voice_set(opl3_t
*opl3
, snd_opl3_drum_voice_t
*data
)
85 unsigned char op_offset
= snd_opl3_regmap
[data
->voice
][data
->op
];
86 unsigned char voice_offset
= data
->voice
;
87 unsigned short opl3_reg
;
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
);
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
);
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
);
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
);
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
);
109 /* Select waveform */
110 opl3_reg
= OPL3_LEFT
| (OPL3_REG_WAVE_SELECT
+ op_offset
);
111 opl3
->command(opl3
, opl3_reg
, data
->wave_select
);
115 * Set drum voice pitch
117 static void snd_opl3_drum_note_set(opl3_t
*opl3
, snd_opl3_drum_note_t
*data
)
119 unsigned char voice_offset
= data
->voice
;
120 unsigned short opl3_reg
;
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
);
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
);
132 * Set drum voice volume and position
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
)
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
;
142 /* Set OPL3 KSL_LEVEL register */
143 reg_val
= data
->ksl_level
;
144 snd_opl3_calc_volume(®_val
, vel
, chan
);
145 opl3_reg
= OPL3_LEFT
| (OPL3_REG_KSL_LEVEL
+ op_offset
);
146 opl3
->command(opl3
, opl3_reg
, reg_val
);
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
);
160 * Loads drum voices at init time
162 void snd_opl3_load_drums(opl3_t
*opl3
)
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
);
168 snd_opl3_drum_voice_set(opl3
, &hihat
);
170 snd_opl3_drum_voice_set(opl3
, &snare
);
171 snd_opl3_drum_note_set(opl3
, &snare_note
);
173 snd_opl3_drum_voice_set(opl3
, &tomtom
);
174 snd_opl3_drum_note_set(opl3
, &tomtom_note
);
176 snd_opl3_drum_voice_set(opl3
, &cymbal
);
180 * Switch drum voice on or off
182 void snd_opl3_drum_switch(opl3_t
*opl3
, int note
, int vel
, int on_off
,
183 snd_midi_channel_t
*chan
)
185 unsigned char drum_mask
;
186 snd_opl3_drum_voice_t
*drum_voice
;
188 if (!(opl3
->drum_reg
& OPL3_PERCUSSION_ENABLE
))
191 if ((note
< 35) || (note
> 81))
193 drum_mask
= snd_opl3_drum_table
[note
- 35];
197 case OPL3_BASSDRUM_ON
:
198 drum_voice
= &bass_op1
;
203 case OPL3_SNAREDRUM_ON
:
207 drum_voice
= &tomtom
;
210 drum_voice
= &cymbal
;
213 drum_voice
= &tomtom
;
216 snd_opl3_drum_vol_set(opl3
, drum_voice
, vel
, chan
);
217 opl3
->drum_reg
|= drum_mask
;
219 opl3
->drum_reg
&= ~drum_mask
;
221 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,