Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
0004  *
0005  *   OPL2/OPL3/OPL4 FM routines for internal percussion channels
0006  */
0007 
0008 #include "opl3_voice.h"
0009 
0010 static const char snd_opl3_drum_table[47] =
0011 {
0012     OPL3_BASSDRUM_ON,  OPL3_BASSDRUM_ON,  OPL3_HIHAT_ON,    /* 35 - 37 */
0013     OPL3_SNAREDRUM_ON, OPL3_HIHAT_ON,     OPL3_SNAREDRUM_ON, /* 38 - 40 */
0014     OPL3_BASSDRUM_ON,  OPL3_HIHAT_ON,     OPL3_BASSDRUM_ON, /* 41 - 43 */
0015     OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,    OPL3_HIHAT_ON,    /* 44 - 46 */
0016     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_CYMBAL_ON,   /* 47 - 49 */
0017 
0018     OPL3_TOMTOM_ON,    OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,   /* 50 - 52 */
0019     OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON,   /* 53 - 55 */
0020     OPL3_HIHAT_ON,     OPL3_CYMBAL_ON,    OPL3_TOMTOM_ON,   /* 56 - 58 */
0021     OPL3_CYMBAL_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,   /* 59 - 61 */
0022     OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,   /* 62 - 64 */
0023 
0024     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,   /* 65 - 67 */
0025     OPL3_TOMTOM_ON,    OPL3_HIHAT_ON,     OPL3_HIHAT_ON,    /* 68 - 70 */
0026     OPL3_HIHAT_ON,     OPL3_HIHAT_ON,     OPL3_TOMTOM_ON,   /* 71 - 73 */
0027     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,   /* 74 - 76 */
0028     OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,    OPL3_TOMTOM_ON,   /* 77 - 79 */
0029     OPL3_CYMBAL_ON,    OPL3_CYMBAL_ON           /* 80 - 81 */
0030 };
0031 
0032 struct snd_opl3_drum_voice {
0033     int voice;
0034     int op;
0035     unsigned char am_vib;
0036     unsigned char ksl_level;
0037     unsigned char attack_decay;
0038     unsigned char sustain_release;
0039     unsigned char feedback_connection;
0040     unsigned char wave_select;
0041 };
0042 
0043 struct snd_opl3_drum_note {
0044     int voice;
0045     unsigned char fnum;
0046     unsigned char octave_f;
0047     unsigned char feedback_connection;
0048 };
0049 
0050 static const struct snd_opl3_drum_voice bass_op0 = {6, 0, 0x00, 0x32, 0xf8, 0x66, 0x30, 0x00};
0051 static const struct snd_opl3_drum_voice bass_op1 = {6, 1, 0x00, 0x03, 0xf6, 0x57, 0x30, 0x00};
0052 static const struct snd_opl3_drum_note bass_note = {6, 0x90, 0x09};
0053 
0054 static const struct snd_opl3_drum_voice hihat = {7, 0, 0x00, 0x03, 0xf0, 0x06, 0x20, 0x00};
0055 
0056 static const struct snd_opl3_drum_voice snare = {7, 1, 0x00, 0x03, 0xf0, 0x07, 0x20, 0x02};
0057 static const struct snd_opl3_drum_note snare_note = {7, 0xf4, 0x0d};
0058 
0059 static const struct snd_opl3_drum_voice tomtom = {8, 0, 0x02, 0x03, 0xf0, 0x06, 0x10, 0x00};
0060 static const struct snd_opl3_drum_note tomtom_note = {8, 0xf4, 0x09};
0061 
0062 static const struct snd_opl3_drum_voice cymbal = {8, 1, 0x04, 0x03, 0xf0, 0x06, 0x10, 0x00};
0063 
0064 /*
0065  * set drum voice characteristics
0066  */
0067 static void snd_opl3_drum_voice_set(struct snd_opl3 *opl3,
0068                     const struct snd_opl3_drum_voice *data)
0069 {
0070     unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
0071     unsigned char voice_offset = data->voice;
0072     unsigned short opl3_reg;
0073     
0074     /* Set OPL3 AM_VIB register */ 
0075     opl3_reg = OPL3_LEFT | (OPL3_REG_AM_VIB + op_offset);
0076     opl3->command(opl3, opl3_reg, data->am_vib);
0077 
0078     /* Set OPL3 KSL_LEVEL register */ 
0079     opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
0080     opl3->command(opl3, opl3_reg, data->ksl_level);
0081 
0082     /* Set OPL3 ATTACK_DECAY register */ 
0083     opl3_reg = OPL3_LEFT | (OPL3_REG_ATTACK_DECAY + op_offset);
0084     opl3->command(opl3, opl3_reg, data->attack_decay);
0085 
0086     /* Set OPL3 SUSTAIN_RELEASE register */ 
0087     opl3_reg = OPL3_LEFT | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
0088     opl3->command(opl3, opl3_reg, data->sustain_release);
0089 
0090     /* Set OPL3 FEEDBACK_CONNECTION register */ 
0091     opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
0092     opl3->command(opl3, opl3_reg, data->feedback_connection);
0093 
0094     /* Select waveform */
0095     opl3_reg = OPL3_LEFT | (OPL3_REG_WAVE_SELECT + op_offset);
0096     opl3->command(opl3, opl3_reg, data->wave_select);
0097 }
0098 
0099 /*
0100  * Set drum voice pitch
0101  */
0102 static void snd_opl3_drum_note_set(struct snd_opl3 *opl3,
0103                    const struct snd_opl3_drum_note *data)
0104 {
0105     unsigned char voice_offset = data->voice;
0106     unsigned short opl3_reg;
0107 
0108     /* Set OPL3 FNUM_LOW register */ 
0109     opl3_reg = OPL3_LEFT | (OPL3_REG_FNUM_LOW + voice_offset);
0110     opl3->command(opl3, opl3_reg, data->fnum);
0111 
0112     /* Set OPL3 KEYON_BLOCK register */ 
0113     opl3_reg = OPL3_LEFT | (OPL3_REG_KEYON_BLOCK + voice_offset);
0114     opl3->command(opl3, opl3_reg, data->octave_f);
0115 }
0116 
0117 /*
0118  * Set drum voice volume and position
0119  */
0120 static void snd_opl3_drum_vol_set(struct snd_opl3 *opl3,
0121                   const struct snd_opl3_drum_voice *data,
0122                   int vel, struct snd_midi_channel *chan)
0123 {
0124     unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
0125     unsigned char voice_offset = data->voice;
0126     unsigned char reg_val;
0127     unsigned short opl3_reg;
0128 
0129     /* Set OPL3 KSL_LEVEL register */ 
0130     reg_val = data->ksl_level;
0131     snd_opl3_calc_volume(&reg_val, vel, chan);
0132     opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
0133     opl3->command(opl3, opl3_reg, reg_val);
0134 
0135     /* Set OPL3 FEEDBACK_CONNECTION register */ 
0136     /* Set output voice connection */
0137     reg_val = data->feedback_connection | OPL3_STEREO_BITS;
0138     if (chan->gm_pan < 43)
0139         reg_val &= ~OPL3_VOICE_TO_RIGHT;
0140     if (chan->gm_pan > 85)
0141         reg_val &= ~OPL3_VOICE_TO_LEFT;
0142     opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
0143     opl3->command(opl3, opl3_reg, reg_val);
0144 }
0145 
0146 /*
0147  * Loads drum voices at init time
0148  */
0149 void snd_opl3_load_drums(struct snd_opl3 *opl3)
0150 {
0151     snd_opl3_drum_voice_set(opl3, &bass_op0);
0152     snd_opl3_drum_voice_set(opl3, &bass_op1);
0153     snd_opl3_drum_note_set(opl3, &bass_note);
0154 
0155     snd_opl3_drum_voice_set(opl3, &hihat);
0156 
0157     snd_opl3_drum_voice_set(opl3, &snare);
0158     snd_opl3_drum_note_set(opl3, &snare_note);
0159 
0160     snd_opl3_drum_voice_set(opl3, &tomtom);
0161     snd_opl3_drum_note_set(opl3, &tomtom_note);
0162 
0163     snd_opl3_drum_voice_set(opl3, &cymbal);
0164 }
0165 
0166 /*
0167  * Switch drum voice on or off
0168  */
0169 void snd_opl3_drum_switch(struct snd_opl3 *opl3, int note, int vel, int on_off,
0170               struct snd_midi_channel *chan)
0171 {
0172     unsigned char drum_mask;
0173     const struct snd_opl3_drum_voice *drum_voice;
0174 
0175     if (!(opl3->drum_reg & OPL3_PERCUSSION_ENABLE))
0176         return;
0177 
0178     if ((note < 35) || (note > 81))
0179         return;
0180     drum_mask = snd_opl3_drum_table[note - 35];
0181 
0182     if (on_off) {
0183         switch (drum_mask) {
0184         case OPL3_BASSDRUM_ON:
0185             drum_voice = &bass_op1;
0186             break;
0187         case OPL3_HIHAT_ON:
0188             drum_voice = &hihat;
0189             break;
0190         case OPL3_SNAREDRUM_ON:
0191             drum_voice = &snare;
0192             break;
0193         case OPL3_TOMTOM_ON:
0194             drum_voice = &tomtom;
0195             break;
0196         case OPL3_CYMBAL_ON:
0197             drum_voice = &cymbal;
0198             break;
0199         default:
0200             drum_voice = &tomtom;
0201         }
0202 
0203         snd_opl3_drum_vol_set(opl3, drum_voice, vel, chan);
0204         opl3->drum_reg |= drum_mask;
0205     } else {
0206         opl3->drum_reg &= ~drum_mask;
0207     }
0208     opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
0209              opl3->drum_reg);
0210 }