opl3_synth.c 17 KB

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