opl3_synth.c 17 KB

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