opl3_drums.c 7.0 KB

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