opl3_drums.c 7.0 KB

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