opl3_synth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
  3. *
  4. * Routines for OPL2/OPL3/OPL4 control
  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 <sound/opl3.h>
  22. #include <sound/asound_fm.h>
  23. /*
  24. * There is 18 possible 2 OP voices
  25. * (9 in the left and 9 in the right).
  26. * The first OP is the modulator and 2nd is the carrier.
  27. *
  28. * The first three voices in the both sides may be connected
  29. * with another voice to a 4 OP voice. For example voice 0
  30. * can be connected with voice 3. The operators of voice 3 are
  31. * used as operators 3 and 4 of the new 4 OP voice.
  32. * In this case the 2 OP voice number 0 is the 'first half' and
  33. * voice 3 is the second.
  34. */
  35. /*
  36. * Register offset table for OPL2/3 voices,
  37. * OPL2 / one OPL3 register array side only
  38. */
  39. char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
  40. {
  41. /* OP1 OP2 OP3 OP4 */
  42. /* ------------------------ */
  43. { 0x00, 0x03, 0x08, 0x0b },
  44. { 0x01, 0x04, 0x09, 0x0c },
  45. { 0x02, 0x05, 0x0a, 0x0d },
  46. { 0x08, 0x0b, 0x00, 0x00 },
  47. { 0x09, 0x0c, 0x00, 0x00 },
  48. { 0x0a, 0x0d, 0x00, 0x00 },
  49. { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
  50. { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
  51. { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
  52. };
  53. /*
  54. * prototypes
  55. */
  56. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
  57. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
  58. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
  59. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
  60. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
  61. /* ------------------------------ */
  62. /*
  63. * open the device exclusively
  64. */
  65. int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
  66. {
  67. struct snd_opl3 *opl3 = hw->private_data;
  68. mutex_lock(&opl3->access_mutex);
  69. if (opl3->used) {
  70. mutex_unlock(&opl3->access_mutex);
  71. return -EAGAIN;
  72. }
  73. opl3->used++;
  74. mutex_unlock(&opl3->access_mutex);
  75. return 0;
  76. }
  77. /*
  78. * ioctl for hwdep device:
  79. */
  80. int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
  81. unsigned int cmd, unsigned long arg)
  82. {
  83. struct snd_opl3 *opl3 = hw->private_data;
  84. void __user *argp = (void __user *)arg;
  85. snd_assert(opl3 != NULL, return -EINVAL);
  86. switch (cmd) {
  87. /* get information */
  88. case SNDRV_DM_FM_IOCTL_INFO:
  89. {
  90. struct snd_dm_fm_info info;
  91. info.fm_mode = opl3->fm_mode;
  92. info.rhythm = opl3->rhythm;
  93. if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
  94. return -EFAULT;
  95. return 0;
  96. }
  97. case SNDRV_DM_FM_IOCTL_RESET:
  98. #ifdef CONFIG_SND_OSSEMUL
  99. case SNDRV_DM_FM_OSS_IOCTL_RESET:
  100. #endif
  101. snd_opl3_reset(opl3);
  102. return 0;
  103. case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
  104. #ifdef CONFIG_SND_OSSEMUL
  105. case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
  106. #endif
  107. {
  108. struct snd_dm_fm_note note;
  109. if (copy_from_user(&note, argp, sizeof(struct snd_dm_fm_note)))
  110. return -EFAULT;
  111. return snd_opl3_play_note(opl3, &note);
  112. }
  113. case SNDRV_DM_FM_IOCTL_SET_VOICE:
  114. #ifdef CONFIG_SND_OSSEMUL
  115. case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
  116. #endif
  117. {
  118. struct snd_dm_fm_voice voice;
  119. if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
  120. return -EFAULT;
  121. return snd_opl3_set_voice(opl3, &voice);
  122. }
  123. case SNDRV_DM_FM_IOCTL_SET_PARAMS:
  124. #ifdef CONFIG_SND_OSSEMUL
  125. case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
  126. #endif
  127. {
  128. struct snd_dm_fm_params params;
  129. if (copy_from_user(&params, argp, sizeof(struct snd_dm_fm_params)))
  130. return -EFAULT;
  131. return snd_opl3_set_params(opl3, &params);
  132. }
  133. case SNDRV_DM_FM_IOCTL_SET_MODE:
  134. #ifdef CONFIG_SND_OSSEMUL
  135. case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
  136. #endif
  137. return snd_opl3_set_mode(opl3, (int) arg);
  138. case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
  139. #ifdef CONFIG_SND_OSSEMUL
  140. case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
  141. #endif
  142. return snd_opl3_set_connection(opl3, (int) arg);
  143. #ifdef CONFIG_SND_DEBUG
  144. default:
  145. snd_printk("unknown IOCTL: 0x%x\n", cmd);
  146. #endif
  147. }
  148. return -ENOTTY;
  149. }
  150. /*
  151. * close the device
  152. */
  153. int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
  154. {
  155. struct snd_opl3 *opl3 = hw->private_data;
  156. snd_opl3_reset(opl3);
  157. mutex_lock(&opl3->access_mutex);
  158. opl3->used--;
  159. mutex_unlock(&opl3->access_mutex);
  160. return 0;
  161. }
  162. /* ------------------------------ */
  163. void snd_opl3_reset(struct snd_opl3 * opl3)
  164. {
  165. unsigned short opl3_reg;
  166. unsigned short reg_side;
  167. unsigned char voice_offset;
  168. int max_voices, i;
  169. max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
  170. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  171. for (i = 0; i < max_voices; i++) {
  172. /* Get register array side and offset of voice */
  173. if (i < MAX_OPL2_VOICES) {
  174. /* Left register block for voices 0 .. 8 */
  175. reg_side = OPL3_LEFT;
  176. voice_offset = i;
  177. } else {
  178. /* Right register block for voices 9 .. 17 */
  179. reg_side = OPL3_RIGHT;
  180. voice_offset = i - MAX_OPL2_VOICES;
  181. }
  182. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
  183. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
  184. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
  185. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
  186. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  187. opl3->command(opl3, opl3_reg, 0x00); /* Note off */
  188. }
  189. opl3->max_voices = MAX_OPL2_VOICES;
  190. opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
  191. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
  192. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
  193. opl3->rhythm = 0;
  194. }
  195. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
  196. {
  197. unsigned short reg_side;
  198. unsigned char voice_offset;
  199. unsigned short opl3_reg;
  200. unsigned char reg_val;
  201. /* Voices 0 - 8 in OPL2 mode */
  202. /* Voices 0 - 17 in OPL3 mode */
  203. if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  204. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  205. return -EINVAL;
  206. /* Get register array side and offset of voice */
  207. if (note->voice < MAX_OPL2_VOICES) {
  208. /* Left register block for voices 0 .. 8 */
  209. reg_side = OPL3_LEFT;
  210. voice_offset = note->voice;
  211. } else {
  212. /* Right register block for voices 9 .. 17 */
  213. reg_side = OPL3_RIGHT;
  214. voice_offset = note->voice - MAX_OPL2_VOICES;
  215. }
  216. /* Set lower 8 bits of note frequency */
  217. reg_val = (unsigned char) note->fnum;
  218. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  219. opl3->command(opl3, opl3_reg, reg_val);
  220. reg_val = 0x00;
  221. /* Set output sound flag */
  222. if (note->key_on)
  223. reg_val |= OPL3_KEYON_BIT;
  224. /* Set octave */
  225. reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
  226. /* Set higher 2 bits of note frequency */
  227. reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
  228. /* Set OPL3 KEYON_BLOCK register of requested voice */
  229. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  230. opl3->command(opl3, opl3_reg, reg_val);
  231. return 0;
  232. }
  233. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
  234. {
  235. unsigned short reg_side;
  236. unsigned char op_offset;
  237. unsigned char voice_offset;
  238. unsigned short opl3_reg;
  239. unsigned char reg_val;
  240. /* Only operators 1 and 2 */
  241. if (voice->op > 1)
  242. return -EINVAL;
  243. /* Voices 0 - 8 in OPL2 mode */
  244. /* Voices 0 - 17 in OPL3 mode */
  245. if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  246. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  247. return -EINVAL;
  248. /* Get register array side and offset of voice */
  249. if (voice->voice < MAX_OPL2_VOICES) {
  250. /* Left register block for voices 0 .. 8 */
  251. reg_side = OPL3_LEFT;
  252. voice_offset = voice->voice;
  253. } else {
  254. /* Right register block for voices 9 .. 17 */
  255. reg_side = OPL3_RIGHT;
  256. voice_offset = voice->voice - MAX_OPL2_VOICES;
  257. }
  258. /* Get register offset of operator */
  259. op_offset = snd_opl3_regmap[voice_offset][voice->op];
  260. reg_val = 0x00;
  261. /* Set amplitude modulation (tremolo) effect */
  262. if (voice->am)
  263. reg_val |= OPL3_TREMOLO_ON;
  264. /* Set vibrato effect */
  265. if (voice->vibrato)
  266. reg_val |= OPL3_VIBRATO_ON;
  267. /* Set sustaining sound phase */
  268. if (voice->do_sustain)
  269. reg_val |= OPL3_SUSTAIN_ON;
  270. /* Set keyboard scaling bit */
  271. if (voice->kbd_scale)
  272. reg_val |= OPL3_KSR;
  273. /* Set harmonic or frequency multiplier */
  274. reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
  275. /* Set OPL3 AM_VIB register of requested voice/operator */
  276. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  277. opl3->command(opl3, opl3_reg, reg_val);
  278. /* Set decreasing volume of higher notes */
  279. reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
  280. /* Set output volume */
  281. reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
  282. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  283. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  284. opl3->command(opl3, opl3_reg, reg_val);
  285. /* Set attack phase level */
  286. reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
  287. /* Set decay phase level */
  288. reg_val |= voice->decay & OPL3_DECAY_MASK;
  289. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  290. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  291. opl3->command(opl3, opl3_reg, reg_val);
  292. /* Set sustain phase level */
  293. reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
  294. /* Set release phase level */
  295. reg_val |= voice->release & OPL3_RELEASE_MASK;
  296. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  297. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  298. opl3->command(opl3, opl3_reg, reg_val);
  299. /* Set inter-operator feedback */
  300. reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
  301. /* Set inter-operator connection */
  302. if (voice->connection)
  303. reg_val |= OPL3_CONNECTION_BIT;
  304. /* OPL-3 only */
  305. if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
  306. if (voice->left)
  307. reg_val |= OPL3_VOICE_TO_LEFT;
  308. if (voice->right)
  309. reg_val |= OPL3_VOICE_TO_RIGHT;
  310. }
  311. /* Feedback/connection bits are applicable to voice */
  312. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  313. opl3->command(opl3, opl3_reg, reg_val);
  314. /* Select waveform */
  315. reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
  316. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  317. opl3->command(opl3, opl3_reg, reg_val);
  318. return 0;
  319. }
  320. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
  321. {
  322. unsigned char reg_val;
  323. reg_val = 0x00;
  324. /* Set keyboard split method */
  325. if (params->kbd_split)
  326. reg_val |= OPL3_KEYBOARD_SPLIT;
  327. opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
  328. reg_val = 0x00;
  329. /* Set amplitude modulation (tremolo) depth */
  330. if (params->am_depth)
  331. reg_val |= OPL3_TREMOLO_DEPTH;
  332. /* Set vibrato depth */
  333. if (params->vib_depth)
  334. reg_val |= OPL3_VIBRATO_DEPTH;
  335. /* Set percussion mode */
  336. if (params->rhythm) {
  337. reg_val |= OPL3_PERCUSSION_ENABLE;
  338. opl3->rhythm = 1;
  339. } else {
  340. opl3->rhythm = 0;
  341. }
  342. /* Play percussion instruments */
  343. if (params->bass)
  344. reg_val |= OPL3_BASSDRUM_ON;
  345. if (params->snare)
  346. reg_val |= OPL3_SNAREDRUM_ON;
  347. if (params->tomtom)
  348. reg_val |= OPL3_TOMTOM_ON;
  349. if (params->cymbal)
  350. reg_val |= OPL3_CYMBAL_ON;
  351. if (params->hihat)
  352. reg_val |= OPL3_HIHAT_ON;
  353. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
  354. return 0;
  355. }
  356. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
  357. {
  358. if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
  359. return -EINVAL;
  360. opl3->fm_mode = mode;
  361. if (opl3->hardware >= OPL3_HW_OPL3)
  362. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
  363. return 0;
  364. }
  365. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
  366. {
  367. unsigned char reg_val;
  368. /* OPL-3 only */
  369. if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
  370. return -EINVAL;
  371. reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
  372. OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
  373. /* Set 4-op connections */
  374. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
  375. return 0;
  376. }