opl3_synth.c 12 KB

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