opl3_synth.c 17 KB

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