opl3_synth.c 17 KB

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