opl3_synth.c 17 KB

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