opl3_synth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
  24. #define OPL3_SUPPORT_SYNTH
  25. #endif
  26. /*
  27. * There is 18 possible 2 OP voices
  28. * (9 in the left and 9 in the right).
  29. * The first OP is the modulator and 2nd is the carrier.
  30. *
  31. * The first three voices in the both sides may be connected
  32. * with another voice to a 4 OP voice. For example voice 0
  33. * can be connected with voice 3. The operators of voice 3 are
  34. * used as operators 3 and 4 of the new 4 OP voice.
  35. * In this case the 2 OP voice number 0 is the 'first half' and
  36. * voice 3 is the second.
  37. */
  38. /*
  39. * Register offset table for OPL2/3 voices,
  40. * OPL2 / one OPL3 register array side only
  41. */
  42. char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
  43. {
  44. /* OP1 OP2 OP3 OP4 */
  45. /* ------------------------ */
  46. { 0x00, 0x03, 0x08, 0x0b },
  47. { 0x01, 0x04, 0x09, 0x0c },
  48. { 0x02, 0x05, 0x0a, 0x0d },
  49. { 0x08, 0x0b, 0x00, 0x00 },
  50. { 0x09, 0x0c, 0x00, 0x00 },
  51. { 0x0a, 0x0d, 0x00, 0x00 },
  52. { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
  53. { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
  54. { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
  55. };
  56. EXPORT_SYMBOL(snd_opl3_regmap);
  57. /*
  58. * prototypes
  59. */
  60. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
  61. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
  62. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
  63. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
  64. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
  65. /* ------------------------------ */
  66. /*
  67. * open the device exclusively
  68. */
  69. int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * ioctl for hwdep device:
  75. */
  76. int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
  77. unsigned int cmd, unsigned long arg)
  78. {
  79. struct snd_opl3 *opl3 = hw->private_data;
  80. void __user *argp = (void __user *)arg;
  81. if (snd_BUG_ON(!opl3))
  82. return -EINVAL;
  83. switch (cmd) {
  84. /* get information */
  85. case SNDRV_DM_FM_IOCTL_INFO:
  86. {
  87. struct snd_dm_fm_info info;
  88. info.fm_mode = opl3->fm_mode;
  89. info.rhythm = opl3->rhythm;
  90. if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
  91. return -EFAULT;
  92. return 0;
  93. }
  94. case SNDRV_DM_FM_IOCTL_RESET:
  95. #ifdef CONFIG_SND_OSSEMUL
  96. case SNDRV_DM_FM_OSS_IOCTL_RESET:
  97. #endif
  98. snd_opl3_reset(opl3);
  99. return 0;
  100. case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
  101. #ifdef CONFIG_SND_OSSEMUL
  102. case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
  103. #endif
  104. {
  105. struct snd_dm_fm_note note;
  106. if (copy_from_user(&note, argp, sizeof(struct snd_dm_fm_note)))
  107. return -EFAULT;
  108. return snd_opl3_play_note(opl3, &note);
  109. }
  110. case SNDRV_DM_FM_IOCTL_SET_VOICE:
  111. #ifdef CONFIG_SND_OSSEMUL
  112. case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
  113. #endif
  114. {
  115. struct snd_dm_fm_voice voice;
  116. if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
  117. return -EFAULT;
  118. return snd_opl3_set_voice(opl3, &voice);
  119. }
  120. case SNDRV_DM_FM_IOCTL_SET_PARAMS:
  121. #ifdef CONFIG_SND_OSSEMUL
  122. case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
  123. #endif
  124. {
  125. struct snd_dm_fm_params params;
  126. if (copy_from_user(&params, argp, sizeof(struct snd_dm_fm_params)))
  127. return -EFAULT;
  128. return snd_opl3_set_params(opl3, &params);
  129. }
  130. case SNDRV_DM_FM_IOCTL_SET_MODE:
  131. #ifdef CONFIG_SND_OSSEMUL
  132. case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
  133. #endif
  134. return snd_opl3_set_mode(opl3, (int) arg);
  135. case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
  136. #ifdef CONFIG_SND_OSSEMUL
  137. case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
  138. #endif
  139. return snd_opl3_set_connection(opl3, (int) arg);
  140. #ifdef OPL3_SUPPORT_SYNTH
  141. case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES:
  142. snd_opl3_clear_patches(opl3);
  143. return 0;
  144. #endif
  145. #ifdef CONFIG_SND_DEBUG
  146. default:
  147. snd_printk("unknown IOCTL: 0x%x\n", cmd);
  148. #endif
  149. }
  150. return -ENOTTY;
  151. }
  152. /*
  153. * close the device
  154. */
  155. int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
  156. {
  157. struct snd_opl3 *opl3 = hw->private_data;
  158. snd_opl3_reset(opl3);
  159. return 0;
  160. }
  161. #ifdef OPL3_SUPPORT_SYNTH
  162. /*
  163. * write the device - load patches
  164. */
  165. long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
  166. loff_t *offset)
  167. {
  168. struct snd_opl3 *opl3 = hw->private_data;
  169. long result = 0;
  170. int err = 0;
  171. struct sbi_patch inst;
  172. while (count >= sizeof(inst)) {
  173. unsigned char type;
  174. if (copy_from_user(&inst, buf, sizeof(inst)))
  175. return -EFAULT;
  176. if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
  177. !memcmp(inst.key, FM_KEY_2OP, 4))
  178. type = FM_PATCH_OPL2;
  179. else if (!memcmp(inst.key, FM_KEY_4OP, 4))
  180. type = FM_PATCH_OPL3;
  181. else /* invalid type */
  182. break;
  183. err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
  184. inst.name, inst.extension,
  185. inst.data);
  186. if (err < 0)
  187. break;
  188. result += sizeof(inst);
  189. count -= sizeof(inst);
  190. }
  191. return result > 0 ? result : err;
  192. }
  193. /*
  194. * Patch management
  195. */
  196. /* offsets for SBI params */
  197. #define AM_VIB 0
  198. #define KSL_LEVEL 2
  199. #define ATTACK_DECAY 4
  200. #define SUSTAIN_RELEASE 6
  201. #define WAVE_SELECT 8
  202. /* offset for SBI instrument */
  203. #define CONNECTION 10
  204. #define OFFSET_4OP 11
  205. /*
  206. * load a patch, obviously.
  207. *
  208. * loaded on the given program and bank numbers with the given type
  209. * (FM_PATCH_OPLx).
  210. * data is the pointer of SBI record _without_ header (key and name).
  211. * name is the name string of the patch.
  212. * ext is the extension data of 7 bytes long (stored in name of SBI
  213. * data up to offset 25), or NULL to skip.
  214. * return 0 if successful or a negative error code.
  215. */
  216. int snd_opl3_load_patch(struct snd_opl3 *opl3,
  217. int prog, int bank, int type,
  218. const char *name,
  219. const unsigned char *ext,
  220. const unsigned char *data)
  221. {
  222. struct fm_patch *patch;
  223. int i;
  224. patch = snd_opl3_find_patch(opl3, prog, bank, 1);
  225. if (!patch)
  226. return -ENOMEM;
  227. patch->type = type;
  228. for (i = 0; i < 2; i++) {
  229. patch->inst.op[i].am_vib = data[AM_VIB + i];
  230. patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
  231. patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
  232. patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
  233. patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
  234. }
  235. patch->inst.feedback_connection[0] = data[CONNECTION];
  236. if (type == FM_PATCH_OPL3) {
  237. for (i = 0; i < 2; i++) {
  238. patch->inst.op[i+2].am_vib =
  239. data[OFFSET_4OP + AM_VIB + i];
  240. patch->inst.op[i+2].ksl_level =
  241. data[OFFSET_4OP + KSL_LEVEL + i];
  242. patch->inst.op[i+2].attack_decay =
  243. data[OFFSET_4OP + ATTACK_DECAY + i];
  244. patch->inst.op[i+2].sustain_release =
  245. data[OFFSET_4OP + SUSTAIN_RELEASE + i];
  246. patch->inst.op[i+2].wave_select =
  247. data[OFFSET_4OP + WAVE_SELECT + i];
  248. }
  249. patch->inst.feedback_connection[1] =
  250. data[OFFSET_4OP + CONNECTION];
  251. }
  252. if (ext) {
  253. patch->inst.echo_delay = ext[0];
  254. patch->inst.echo_atten = ext[1];
  255. patch->inst.chorus_spread = ext[2];
  256. patch->inst.trnsps = ext[3];
  257. patch->inst.fix_dur = ext[4];
  258. patch->inst.modes = ext[5];
  259. patch->inst.fix_key = ext[6];
  260. }
  261. if (name)
  262. strlcpy(patch->name, name, sizeof(patch->name));
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(snd_opl3_load_patch);
  266. /*
  267. * find a patch with the given program and bank numbers, returns its pointer
  268. * if no matching patch is found and create_patch is set, it creates a
  269. * new patch object.
  270. */
  271. struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
  272. int create_patch)
  273. {
  274. /* pretty dumb hash key */
  275. unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
  276. struct fm_patch *patch;
  277. for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
  278. if (patch->prog == prog && patch->bank == bank)
  279. return patch;
  280. }
  281. if (!create_patch)
  282. return NULL;
  283. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  284. if (!patch)
  285. return NULL;
  286. patch->prog = prog;
  287. patch->bank = bank;
  288. patch->next = opl3->patch_table[key];
  289. opl3->patch_table[key] = patch;
  290. return patch;
  291. }
  292. EXPORT_SYMBOL(snd_opl3_find_patch);
  293. /*
  294. * Clear all patches of the given OPL3 instance
  295. */
  296. void snd_opl3_clear_patches(struct snd_opl3 *opl3)
  297. {
  298. int i;
  299. for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
  300. struct fm_patch *patch, *next;
  301. for (patch = opl3->patch_table[i]; patch; patch = next) {
  302. next = patch->next;
  303. kfree(patch);
  304. }
  305. }
  306. memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
  307. }
  308. #endif /* OPL3_SUPPORT_SYNTH */
  309. /* ------------------------------ */
  310. void snd_opl3_reset(struct snd_opl3 * opl3)
  311. {
  312. unsigned short opl3_reg;
  313. unsigned short reg_side;
  314. unsigned char voice_offset;
  315. int max_voices, i;
  316. max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
  317. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  318. for (i = 0; i < max_voices; i++) {
  319. /* Get register array side and offset of voice */
  320. if (i < MAX_OPL2_VOICES) {
  321. /* Left register block for voices 0 .. 8 */
  322. reg_side = OPL3_LEFT;
  323. voice_offset = i;
  324. } else {
  325. /* Right register block for voices 9 .. 17 */
  326. reg_side = OPL3_RIGHT;
  327. voice_offset = i - MAX_OPL2_VOICES;
  328. }
  329. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
  330. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
  331. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
  332. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
  333. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  334. opl3->command(opl3, opl3_reg, 0x00); /* Note off */
  335. }
  336. opl3->max_voices = MAX_OPL2_VOICES;
  337. opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
  338. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
  339. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
  340. opl3->rhythm = 0;
  341. }
  342. EXPORT_SYMBOL(snd_opl3_reset);
  343. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
  344. {
  345. unsigned short reg_side;
  346. unsigned char voice_offset;
  347. unsigned short opl3_reg;
  348. unsigned char reg_val;
  349. /* Voices 0 - 8 in OPL2 mode */
  350. /* Voices 0 - 17 in OPL3 mode */
  351. if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  352. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  353. return -EINVAL;
  354. /* Get register array side and offset of voice */
  355. if (note->voice < MAX_OPL2_VOICES) {
  356. /* Left register block for voices 0 .. 8 */
  357. reg_side = OPL3_LEFT;
  358. voice_offset = note->voice;
  359. } else {
  360. /* Right register block for voices 9 .. 17 */
  361. reg_side = OPL3_RIGHT;
  362. voice_offset = note->voice - MAX_OPL2_VOICES;
  363. }
  364. /* Set lower 8 bits of note frequency */
  365. reg_val = (unsigned char) note->fnum;
  366. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  367. opl3->command(opl3, opl3_reg, reg_val);
  368. reg_val = 0x00;
  369. /* Set output sound flag */
  370. if (note->key_on)
  371. reg_val |= OPL3_KEYON_BIT;
  372. /* Set octave */
  373. reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
  374. /* Set higher 2 bits of note frequency */
  375. reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
  376. /* Set OPL3 KEYON_BLOCK register of requested voice */
  377. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  378. opl3->command(opl3, opl3_reg, reg_val);
  379. return 0;
  380. }
  381. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
  382. {
  383. unsigned short reg_side;
  384. unsigned char op_offset;
  385. unsigned char voice_offset;
  386. unsigned short opl3_reg;
  387. unsigned char reg_val;
  388. /* Only operators 1 and 2 */
  389. if (voice->op > 1)
  390. return -EINVAL;
  391. /* Voices 0 - 8 in OPL2 mode */
  392. /* Voices 0 - 17 in OPL3 mode */
  393. if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  394. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  395. return -EINVAL;
  396. /* Get register array side and offset of voice */
  397. if (voice->voice < MAX_OPL2_VOICES) {
  398. /* Left register block for voices 0 .. 8 */
  399. reg_side = OPL3_LEFT;
  400. voice_offset = voice->voice;
  401. } else {
  402. /* Right register block for voices 9 .. 17 */
  403. reg_side = OPL3_RIGHT;
  404. voice_offset = voice->voice - MAX_OPL2_VOICES;
  405. }
  406. /* Get register offset of operator */
  407. op_offset = snd_opl3_regmap[voice_offset][voice->op];
  408. reg_val = 0x00;
  409. /* Set amplitude modulation (tremolo) effect */
  410. if (voice->am)
  411. reg_val |= OPL3_TREMOLO_ON;
  412. /* Set vibrato effect */
  413. if (voice->vibrato)
  414. reg_val |= OPL3_VIBRATO_ON;
  415. /* Set sustaining sound phase */
  416. if (voice->do_sustain)
  417. reg_val |= OPL3_SUSTAIN_ON;
  418. /* Set keyboard scaling bit */
  419. if (voice->kbd_scale)
  420. reg_val |= OPL3_KSR;
  421. /* Set harmonic or frequency multiplier */
  422. reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
  423. /* Set OPL3 AM_VIB register of requested voice/operator */
  424. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  425. opl3->command(opl3, opl3_reg, reg_val);
  426. /* Set decreasing volume of higher notes */
  427. reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
  428. /* Set output volume */
  429. reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
  430. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  431. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  432. opl3->command(opl3, opl3_reg, reg_val);
  433. /* Set attack phase level */
  434. reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
  435. /* Set decay phase level */
  436. reg_val |= voice->decay & OPL3_DECAY_MASK;
  437. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  438. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  439. opl3->command(opl3, opl3_reg, reg_val);
  440. /* Set sustain phase level */
  441. reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
  442. /* Set release phase level */
  443. reg_val |= voice->release & OPL3_RELEASE_MASK;
  444. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  445. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  446. opl3->command(opl3, opl3_reg, reg_val);
  447. /* Set inter-operator feedback */
  448. reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
  449. /* Set inter-operator connection */
  450. if (voice->connection)
  451. reg_val |= OPL3_CONNECTION_BIT;
  452. /* OPL-3 only */
  453. if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
  454. if (voice->left)
  455. reg_val |= OPL3_VOICE_TO_LEFT;
  456. if (voice->right)
  457. reg_val |= OPL3_VOICE_TO_RIGHT;
  458. }
  459. /* Feedback/connection bits are applicable to voice */
  460. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  461. opl3->command(opl3, opl3_reg, reg_val);
  462. /* Select waveform */
  463. reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
  464. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  465. opl3->command(opl3, opl3_reg, reg_val);
  466. return 0;
  467. }
  468. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
  469. {
  470. unsigned char reg_val;
  471. reg_val = 0x00;
  472. /* Set keyboard split method */
  473. if (params->kbd_split)
  474. reg_val |= OPL3_KEYBOARD_SPLIT;
  475. opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
  476. reg_val = 0x00;
  477. /* Set amplitude modulation (tremolo) depth */
  478. if (params->am_depth)
  479. reg_val |= OPL3_TREMOLO_DEPTH;
  480. /* Set vibrato depth */
  481. if (params->vib_depth)
  482. reg_val |= OPL3_VIBRATO_DEPTH;
  483. /* Set percussion mode */
  484. if (params->rhythm) {
  485. reg_val |= OPL3_PERCUSSION_ENABLE;
  486. opl3->rhythm = 1;
  487. } else {
  488. opl3->rhythm = 0;
  489. }
  490. /* Play percussion instruments */
  491. if (params->bass)
  492. reg_val |= OPL3_BASSDRUM_ON;
  493. if (params->snare)
  494. reg_val |= OPL3_SNAREDRUM_ON;
  495. if (params->tomtom)
  496. reg_val |= OPL3_TOMTOM_ON;
  497. if (params->cymbal)
  498. reg_val |= OPL3_CYMBAL_ON;
  499. if (params->hihat)
  500. reg_val |= OPL3_HIHAT_ON;
  501. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
  502. return 0;
  503. }
  504. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
  505. {
  506. if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
  507. return -EINVAL;
  508. opl3->fm_mode = mode;
  509. if (opl3->hardware >= OPL3_HW_OPL3)
  510. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
  511. return 0;
  512. }
  513. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
  514. {
  515. unsigned char reg_val;
  516. /* OPL-3 only */
  517. if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
  518. return -EINVAL;
  519. reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
  520. OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
  521. /* Set 4-op connections */
  522. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
  523. return 0;
  524. }