11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) by Uros Bizjak <uros@kss-loka.si> 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Midi synth routines for OPL2/OPL3/OPL4 FM 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 71da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 81da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 91da177e4SLinus Torvalds * (at your option) any later version. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 121da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 131da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 141da177e4SLinus Torvalds * GNU General Public License for more details. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 171da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 181da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #undef DEBUG_ALLOC 231da177e4SLinus Torvalds #undef DEBUG_MIDI 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include "opl3_voice.h" 261da177e4SLinus Torvalds #include <sound/asoundef.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds extern char snd_opl3_regmap[MAX_OPL2_VOICES][4]; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds extern int use_internal_drums; 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * The next table looks magical, but it certainly is not. Its values have 341da177e4SLinus Torvalds * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception 351da177e4SLinus Torvalds * for i=0. This log-table converts a linear volume-scaling (0..127) to a 361da177e4SLinus Torvalds * logarithmic scaling as present in the FM-synthesizer chips. so : Volume 371da177e4SLinus Torvalds * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative 381da177e4SLinus Torvalds * volume -8 it was implemented as a table because it is only 128 bytes and 391da177e4SLinus Torvalds * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>) 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static char opl3_volume_table[128] = 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds -63, -48, -40, -35, -32, -29, -27, -26, 451da177e4SLinus Torvalds -24, -23, -21, -20, -19, -18, -18, -17, 461da177e4SLinus Torvalds -16, -15, -15, -14, -13, -13, -12, -12, 471da177e4SLinus Torvalds -11, -11, -10, -10, -10, -9, -9, -8, 481da177e4SLinus Torvalds -8, -8, -7, -7, -7, -6, -6, -6, 491da177e4SLinus Torvalds -5, -5, -5, -5, -4, -4, -4, -4, 501da177e4SLinus Torvalds -3, -3, -3, -3, -2, -2, -2, -2, 511da177e4SLinus Torvalds -2, -1, -1, -1, -1, 0, 0, 0, 521da177e4SLinus Torvalds 0, 0, 0, 1, 1, 1, 1, 1, 531da177e4SLinus Torvalds 1, 2, 2, 2, 2, 2, 2, 2, 541da177e4SLinus Torvalds 3, 3, 3, 3, 3, 3, 3, 4, 551da177e4SLinus Torvalds 4, 4, 4, 4, 4, 4, 4, 5, 561da177e4SLinus Torvalds 5, 5, 5, 5, 5, 5, 5, 5, 571da177e4SLinus Torvalds 6, 6, 6, 6, 6, 6, 6, 6, 581da177e4SLinus Torvalds 6, 7, 7, 7, 7, 7, 7, 7, 591da177e4SLinus Torvalds 7, 7, 7, 8, 8, 8, 8, 8 601da177e4SLinus Torvalds }; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds void snd_opl3_calc_volume(unsigned char *volbyte, int vel, 635b1646a8STakashi Iwai struct snd_midi_channel *chan) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds int oldvol, newvol, n; 661da177e4SLinus Torvalds int volume; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127); 691da177e4SLinus Torvalds if (volume > 127) 701da177e4SLinus Torvalds volume = 127; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds newvol = opl3_volume_table[volume] + oldvol; 751da177e4SLinus Torvalds if (newvol > OPL3_TOTAL_LEVEL_MASK) 761da177e4SLinus Torvalds newvol = OPL3_TOTAL_LEVEL_MASK; 771da177e4SLinus Torvalds else if (newvol < 0) 781da177e4SLinus Torvalds newvol = 0; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* 861da177e4SLinus Torvalds * Converts the note frequency to block and fnum values for the FM chip 871da177e4SLinus Torvalds */ 881da177e4SLinus Torvalds static short opl3_note_table[16] = 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds 305, 323, /* for pitch bending, -2 semitones */ 911da177e4SLinus Torvalds 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647, 921da177e4SLinus Torvalds 686, 726 /* for pitch bending, +2 semitones */ 931da177e4SLinus Torvalds }; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum, 965b1646a8STakashi Iwai int note, struct snd_midi_channel *chan) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds int block = ((note / 12) & 0x07) - 1; 991da177e4SLinus Torvalds int idx = (note % 12) + 2; 1001da177e4SLinus Torvalds int freq; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds if (chan->midi_pitchbend) { 1031da177e4SLinus Torvalds int pitchbend = chan->midi_pitchbend; 1041da177e4SLinus Torvalds int segment; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds if (pitchbend > 0x1FFF) 1071da177e4SLinus Torvalds pitchbend = 0x1FFF; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds segment = pitchbend / 0x1000; 1101da177e4SLinus Torvalds freq = opl3_note_table[idx+segment]; 1111da177e4SLinus Torvalds freq += ((opl3_note_table[idx+segment+1] - freq) * 1121da177e4SLinus Torvalds (pitchbend % 0x1000)) / 0x1000; 1131da177e4SLinus Torvalds } else { 1141da177e4SLinus Torvalds freq = opl3_note_table[idx]; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds *fnum = (unsigned char) freq; 1181da177e4SLinus Torvalds *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) | 1191da177e4SLinus Torvalds ((block << 2) & OPL3_BLOCKNUM_MASK); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds #ifdef DEBUG_ALLOC 1245b1646a8STakashi Iwai static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) { 1251da177e4SLinus Torvalds int i; 1261da177e4SLinus Torvalds char *str = "x.24"; 1271da177e4SLinus Torvalds 128*45203832STakashi Iwai printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice); 1291da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++) 1301da177e4SLinus Torvalds printk("%c", *(str + opl3->voices[i].state + 1)); 1311da177e4SLinus Torvalds printk("\n"); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Get a FM voice (channel) to play a note on. 1371da177e4SLinus Torvalds */ 1385b1646a8STakashi Iwai static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op, 1395b1646a8STakashi Iwai struct snd_midi_channel *chan) { 1401da177e4SLinus Torvalds int chan_4op_1; /* first voice for 4op instrument */ 1411da177e4SLinus Torvalds int chan_4op_2; /* second voice for 4op instrument */ 1421da177e4SLinus Torvalds 1435b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2; 1441da177e4SLinus Torvalds unsigned int voice_time; 1451da177e4SLinus Torvalds int i; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds #ifdef DEBUG_ALLOC 1481da177e4SLinus Torvalds char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" }; 1491da177e4SLinus Torvalds #endif 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* This is our "allocation cost" table */ 1521da177e4SLinus Torvalds enum { 1531da177e4SLinus Torvalds FREE = 0, CHEAP, EXPENSIVE, END 1541da177e4SLinus Torvalds }; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* Keeps track of what we are finding */ 1571da177e4SLinus Torvalds struct best { 1581da177e4SLinus Torvalds unsigned int time; 1591da177e4SLinus Torvalds int voice; 1601da177e4SLinus Torvalds } best[END]; 1611da177e4SLinus Torvalds struct best *bp; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds for (i = 0; i < END; i++) { 1641da177e4SLinus Torvalds best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */; 1651da177e4SLinus Torvalds best[i].voice = -1; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* Look through all the channels for the most suitable. */ 1691da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++) { 1701da177e4SLinus Torvalds vp = &opl3->voices[i]; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL) 1731da177e4SLinus Torvalds /* skip unavailable channels, allocated by 1741da177e4SLinus Torvalds drum voices or by bounded 4op voices) */ 1751da177e4SLinus Torvalds continue; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds voice_time = vp->time; 1781da177e4SLinus Torvalds bp = best; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds chan_4op_1 = ((i < 3) || (i > 8 && i < 12)); 1811da177e4SLinus Torvalds chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15)); 1821da177e4SLinus Torvalds if (instr_4op) { 1831da177e4SLinus Torvalds /* allocate 4op voice */ 1841da177e4SLinus Torvalds /* skip channels unavailable to 4op instrument */ 1851da177e4SLinus Torvalds if (!chan_4op_1) 1861da177e4SLinus Torvalds continue; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds if (vp->state) 1891da177e4SLinus Torvalds /* kill one voice, CHEAP */ 1901da177e4SLinus Torvalds bp++; 1911da177e4SLinus Torvalds /* get state of bounded 2op channel 1921da177e4SLinus Torvalds to be allocated for 4op instrument */ 1931da177e4SLinus Torvalds vp2 = &opl3->voices[i + 3]; 1941da177e4SLinus Torvalds if (vp2->state == SNDRV_OPL3_ST_ON_2OP) { 1951da177e4SLinus Torvalds /* kill two voices, EXPENSIVE */ 1961da177e4SLinus Torvalds bp++; 1971da177e4SLinus Torvalds voice_time = (voice_time > vp->time) ? 1981da177e4SLinus Torvalds voice_time : vp->time; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds } else { 2011da177e4SLinus Torvalds /* allocate 2op voice */ 2021da177e4SLinus Torvalds if ((chan_4op_1) || (chan_4op_2)) 2031da177e4SLinus Torvalds /* use bounded channels for 2op, CHEAP */ 2041da177e4SLinus Torvalds bp++; 2051da177e4SLinus Torvalds else if (vp->state) 2061da177e4SLinus Torvalds /* kill one voice on 2op channel, CHEAP */ 2071da177e4SLinus Torvalds bp++; 2081da177e4SLinus Torvalds /* raise kill cost to EXPENSIVE for all channels */ 2091da177e4SLinus Torvalds if (vp->state) 2101da177e4SLinus Torvalds bp++; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds if (voice_time < bp->time) { 2131da177e4SLinus Torvalds bp->time = voice_time; 2141da177e4SLinus Torvalds bp->voice = i; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds for (i = 0; i < END; i++) { 2191da177e4SLinus Torvalds if (best[i].voice >= 0) { 2201da177e4SLinus Torvalds #ifdef DEBUG_ALLOC 221*45203832STakashi Iwai printk(KERN_DEBUG "%s %iop allocation on voice %i\n", 2221da177e4SLinus Torvalds alloc_type[i], instr_4op ? 4 : 2, 2231da177e4SLinus Torvalds best[i].voice); 2241da177e4SLinus Torvalds #endif 2251da177e4SLinus Torvalds return best[i].voice; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds /* not found */ 2291da177e4SLinus Torvalds return -1; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* ------------------------------ */ 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * System timer interrupt function 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds void snd_opl3_timer_func(unsigned long data) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds 2405b1646a8STakashi Iwai struct snd_opl3 *opl3 = (struct snd_opl3 *)data; 241b32425acSTakashi Iwai unsigned long flags; 2421da177e4SLinus Torvalds int again = 0; 2431da177e4SLinus Torvalds int i; 2441da177e4SLinus Torvalds 245b32425acSTakashi Iwai spin_lock_irqsave(&opl3->sys_timer_lock, flags); 2461da177e4SLinus Torvalds for (i = 0; i < opl3->max_voices; i++) { 2475b1646a8STakashi Iwai struct snd_opl3_voice *vp = &opl3->voices[i]; 2481da177e4SLinus Torvalds if (vp->state > 0 && vp->note_off_check) { 2491da177e4SLinus Torvalds if (vp->note_off == jiffies) 2501da177e4SLinus Torvalds snd_opl3_note_off(opl3, vp->note, 0, vp->chan); 2511da177e4SLinus Torvalds else 2521da177e4SLinus Torvalds again++; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds if (again) { 2561da177e4SLinus Torvalds opl3->tlist.expires = jiffies + 1; /* invoke again */ 2571da177e4SLinus Torvalds add_timer(&opl3->tlist); 2581da177e4SLinus Torvalds } else { 2591da177e4SLinus Torvalds opl3->sys_timer_status = 0; 2601da177e4SLinus Torvalds } 261b32425acSTakashi Iwai spin_unlock_irqrestore(&opl3->sys_timer_lock, flags); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* 2651da177e4SLinus Torvalds * Start system timer 2661da177e4SLinus Torvalds */ 2675b1646a8STakashi Iwai static void snd_opl3_start_timer(struct snd_opl3 *opl3) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds unsigned long flags; 2701da177e4SLinus Torvalds spin_lock_irqsave(&opl3->sys_timer_lock, flags); 2711da177e4SLinus Torvalds if (! opl3->sys_timer_status) { 2721da177e4SLinus Torvalds opl3->tlist.expires = jiffies + 1; 2731da177e4SLinus Torvalds add_timer(&opl3->tlist); 2741da177e4SLinus Torvalds opl3->sys_timer_status = 1; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->sys_timer_lock, flags); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* ------------------------------ */ 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds static int snd_opl3_oss_map[MAX_OPL3_VOICES] = { 2831da177e4SLinus Torvalds 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14 2841da177e4SLinus Torvalds }; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* 2871da177e4SLinus Torvalds * Start a note. 2881da177e4SLinus Torvalds */ 2895b1646a8STakashi Iwai void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan) 2901da177e4SLinus Torvalds { 2915b1646a8STakashi Iwai struct snd_opl3 *opl3; 2921da177e4SLinus Torvalds int instr_4op; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds int voice; 2955b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2; 2961da177e4SLinus Torvalds unsigned short connect_mask; 2971da177e4SLinus Torvalds unsigned char connection; 2981da177e4SLinus Torvalds unsigned char vol_op[4]; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds int extra_prg = 0; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds unsigned short reg_side; 3031da177e4SLinus Torvalds unsigned char op_offset; 3041da177e4SLinus Torvalds unsigned char voice_offset; 3051da177e4SLinus Torvalds unsigned short opl3_reg; 3061da177e4SLinus Torvalds unsigned char reg_val; 307224a0332STakashi Iwai unsigned char prg, bank; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds int key = note; 3101da177e4SLinus Torvalds unsigned char fnum, blocknum; 3111da177e4SLinus Torvalds int i; 3121da177e4SLinus Torvalds 313224a0332STakashi Iwai struct fm_patch *patch; 3145b1646a8STakashi Iwai struct fm_instrument *fm; 3151da177e4SLinus Torvalds unsigned long flags; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds opl3 = p; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds #ifdef DEBUG_MIDI 320*45203832STakashi Iwai snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n", 3211da177e4SLinus Torvalds chan->number, chan->midi_program, note, vel); 3221da177e4SLinus Torvalds #endif 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* in SYNTH mode, application takes care of voices */ 3251da177e4SLinus Torvalds /* in SEQ mode, drum voice numbers are notes on drum channel */ 3261da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) { 3271da177e4SLinus Torvalds if (chan->drum_channel) { 3281da177e4SLinus Torvalds /* percussion instruments are located in bank 128 */ 329224a0332STakashi Iwai bank = 128; 330224a0332STakashi Iwai prg = note; 3311da177e4SLinus Torvalds } else { 332224a0332STakashi Iwai bank = chan->gm_bank_select; 333224a0332STakashi Iwai prg = chan->midi_program; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds } else { 3361da177e4SLinus Torvalds /* Prepare for OSS mode */ 3371da177e4SLinus Torvalds if (chan->number >= MAX_OPL3_VOICES) 3381da177e4SLinus Torvalds return; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* OSS instruments are located in bank 127 */ 341224a0332STakashi Iwai bank = 127; 342224a0332STakashi Iwai prg = chan->midi_program; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds spin_lock_irqsave(&opl3->voice_lock, flags); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds if (use_internal_drums) { 3481da177e4SLinus Torvalds snd_opl3_drum_switch(opl3, note, vel, 1, chan); 3491da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 3501da177e4SLinus Torvalds return; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds __extra_prg: 354224a0332STakashi Iwai patch = snd_opl3_find_patch(opl3, prg, bank, 0); 355224a0332STakashi Iwai if (!patch) { 3561da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 3571da177e4SLinus Torvalds return; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360224a0332STakashi Iwai fm = &patch->inst; 361224a0332STakashi Iwai switch (patch->type) { 3621da177e4SLinus Torvalds case FM_PATCH_OPL2: 3631da177e4SLinus Torvalds instr_4op = 0; 3641da177e4SLinus Torvalds break; 3651da177e4SLinus Torvalds case FM_PATCH_OPL3: 3661da177e4SLinus Torvalds if (opl3->hardware >= OPL3_HW_OPL3) { 3671da177e4SLinus Torvalds instr_4op = 1; 3681da177e4SLinus Torvalds break; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds default: 3711da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 3721da177e4SLinus Torvalds return; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds #ifdef DEBUG_MIDI 375*45203832STakashi Iwai snd_printk(KERN_DEBUG " --> OPL%i instrument: %s\n", 376224a0332STakashi Iwai instr_4op ? 3 : 2, patch->name); 3771da177e4SLinus Torvalds #endif 3781da177e4SLinus Torvalds /* in SYNTH mode, application takes care of voices */ 3791da177e4SLinus Torvalds /* in SEQ mode, allocate voice on free OPL3 channel */ 3801da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) { 3811da177e4SLinus Torvalds voice = opl3_get_voice(opl3, instr_4op, chan); 3821da177e4SLinus Torvalds } else { 3831da177e4SLinus Torvalds /* remap OSS voice */ 3841da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number]; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) { 3881da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */ 3891da177e4SLinus Torvalds reg_side = OPL3_LEFT; 3901da177e4SLinus Torvalds voice_offset = voice; 3911da177e4SLinus Torvalds connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07; 3921da177e4SLinus Torvalds } else { 3931da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */ 3941da177e4SLinus Torvalds reg_side = OPL3_RIGHT; 3951da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES; 3961da177e4SLinus Torvalds connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* kill voice on channel */ 4001da177e4SLinus Torvalds vp = &opl3->voices[voice]; 4011da177e4SLinus Torvalds if (vp->state > 0) { 4021da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset); 4031da177e4SLinus Torvalds reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT; 4041da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds if (instr_4op) { 4071da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3]; 4081da177e4SLinus Torvalds if (vp->state > 0) { 4091da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + 4101da177e4SLinus Torvalds voice_offset + 3); 4111da177e4SLinus Torvalds reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT; 4121da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* set connection register */ 4171da177e4SLinus Torvalds if (instr_4op) { 4181da177e4SLinus Torvalds if ((opl3->connection_reg ^ connect_mask) & connect_mask) { 4191da177e4SLinus Torvalds opl3->connection_reg |= connect_mask; 4201da177e4SLinus Torvalds /* set connection bit */ 4211da177e4SLinus Torvalds opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT; 4221da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, opl3->connection_reg); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds } else { 4251da177e4SLinus Torvalds if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) { 4261da177e4SLinus Torvalds opl3->connection_reg &= ~connect_mask; 4271da177e4SLinus Torvalds /* clear connection bit */ 4281da177e4SLinus Torvalds opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT; 4291da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, opl3->connection_reg); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds #ifdef DEBUG_MIDI 434*45203832STakashi Iwai snd_printk(KERN_DEBUG " --> setting OPL3 connection: 0x%x\n", 4351da177e4SLinus Torvalds opl3->connection_reg); 4361da177e4SLinus Torvalds #endif 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * calculate volume depending on connection 4391da177e4SLinus Torvalds * between FM operators (see include/opl3.h) 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds for (i = 0; i < (instr_4op ? 4 : 2); i++) 4421da177e4SLinus Torvalds vol_op[i] = fm->op[i].ksl_level; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds connection = fm->feedback_connection[0] & 0x01; 4451da177e4SLinus Torvalds if (instr_4op) { 4461da177e4SLinus Torvalds connection <<= 1; 4471da177e4SLinus Torvalds connection |= fm->feedback_connection[1] & 0x01; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[3], vel, chan); 4501da177e4SLinus Torvalds switch (connection) { 4511da177e4SLinus Torvalds case 0x03: 4521da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[2], vel, chan); 4531da177e4SLinus Torvalds /* fallthru */ 4541da177e4SLinus Torvalds case 0x02: 4551da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[0], vel, chan); 4561da177e4SLinus Torvalds break; 4571da177e4SLinus Torvalds case 0x01: 4581da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[1], vel, chan); 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds } else { 4611da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[1], vel, chan); 4621da177e4SLinus Torvalds if (connection) 4631da177e4SLinus Torvalds snd_opl3_calc_volume(&vol_op[0], vel, chan); 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* Program the FM voice characteristics */ 4671da177e4SLinus Torvalds for (i = 0; i < (instr_4op ? 4 : 2); i++) { 4681da177e4SLinus Torvalds #ifdef DEBUG_MIDI 469*45203832STakashi Iwai snd_printk(KERN_DEBUG " --> programming operator %i\n", i); 4701da177e4SLinus Torvalds #endif 4711da177e4SLinus Torvalds op_offset = snd_opl3_regmap[voice_offset][i]; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* Set OPL3 AM_VIB register of requested voice/operator */ 4741da177e4SLinus Torvalds reg_val = fm->op[i].am_vib; 4751da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset); 4761da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds /* Set OPL3 KSL_LEVEL register of requested voice/operator */ 4791da177e4SLinus Torvalds reg_val = vol_op[i]; 4801da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset); 4811da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* Set OPL3 ATTACK_DECAY register of requested voice/operator */ 4841da177e4SLinus Torvalds reg_val = fm->op[i].attack_decay; 4851da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset); 4861da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */ 4891da177e4SLinus Torvalds reg_val = fm->op[i].sustain_release; 4901da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset); 4911da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* Select waveform */ 4941da177e4SLinus Torvalds reg_val = fm->op[i].wave_select; 4951da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset); 4961da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* Set operator feedback and 2op inter-operator connection */ 5001da177e4SLinus Torvalds reg_val = fm->feedback_connection[0]; 5011da177e4SLinus Torvalds /* Set output voice connection */ 5021da177e4SLinus Torvalds reg_val |= OPL3_STEREO_BITS; 5031da177e4SLinus Torvalds if (chan->gm_pan < 43) 5041da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_RIGHT; 5051da177e4SLinus Torvalds if (chan->gm_pan > 85) 5061da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_LEFT; 5071da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset); 5081da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds if (instr_4op) { 5111da177e4SLinus Torvalds /* Set 4op inter-operator connection */ 5121da177e4SLinus Torvalds reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT; 5131da177e4SLinus Torvalds /* Set output voice connection */ 5141da177e4SLinus Torvalds reg_val |= OPL3_STEREO_BITS; 5151da177e4SLinus Torvalds if (chan->gm_pan < 43) 5161da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_RIGHT; 5171da177e4SLinus Torvalds if (chan->gm_pan > 85) 5181da177e4SLinus Torvalds reg_val &= ~OPL3_VOICE_TO_LEFT; 5191da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + 5201da177e4SLinus Torvalds voice_offset + 3); 5211da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, reg_val); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /* 5251da177e4SLinus Torvalds * Special treatment of percussion notes for fm: 5261da177e4SLinus Torvalds * Requested pitch is really program, and pitch for 5271da177e4SLinus Torvalds * device is whatever was specified in the patch library. 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds if (fm->fix_key) 5301da177e4SLinus Torvalds note = fm->fix_key; 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * use transpose if defined in patch library 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds if (fm->trnsps) 5351da177e4SLinus Torvalds note += (fm->trnsps - 64); 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds snd_opl3_calc_pitch(&fnum, &blocknum, note, chan); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* Set OPL3 FNUM_LOW register of requested voice */ 5401da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset); 5411da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, fnum); 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds opl3->voices[voice].keyon_reg = blocknum; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* Set output sound flag */ 5461da177e4SLinus Torvalds blocknum |= OPL3_KEYON_BIT; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds #ifdef DEBUG_MIDI 549*45203832STakashi Iwai snd_printk(KERN_DEBUG " --> trigger voice %i\n", voice); 5501da177e4SLinus Torvalds #endif 5511da177e4SLinus Torvalds /* Set OPL3 KEYON_BLOCK register of requested voice */ 5521da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset); 5531da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, blocknum); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /* kill note after fixed duration (in centiseconds) */ 5561da177e4SLinus Torvalds if (fm->fix_dur) { 5571da177e4SLinus Torvalds opl3->voices[voice].note_off = jiffies + 5581da177e4SLinus Torvalds (fm->fix_dur * HZ) / 100; 5591da177e4SLinus Torvalds snd_opl3_start_timer(opl3); 5601da177e4SLinus Torvalds opl3->voices[voice].note_off_check = 1; 5611da177e4SLinus Torvalds } else 5621da177e4SLinus Torvalds opl3->voices[voice].note_off_check = 0; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* get extra pgm, but avoid possible loops */ 5651da177e4SLinus Torvalds extra_prg = (extra_prg) ? 0 : fm->modes; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* do the bookkeeping */ 5681da177e4SLinus Torvalds vp->time = opl3->use_time++; 5691da177e4SLinus Torvalds vp->note = key; 5701da177e4SLinus Torvalds vp->chan = chan; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds if (instr_4op) { 5731da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_ON_4OP; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3]; 5761da177e4SLinus Torvalds vp2->time = opl3->use_time++; 5771da177e4SLinus Torvalds vp2->note = key; 5781da177e4SLinus Torvalds vp2->chan = chan; 5791da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_NOT_AVAIL; 5801da177e4SLinus Torvalds } else { 5811da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_ON_4OP) { 5821da177e4SLinus Torvalds /* 4op killed by 2op, release bounded voice */ 5831da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3]; 5841da177e4SLinus Torvalds vp2->time = opl3->use_time++; 5851da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_OFF; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_ON_2OP; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds #ifdef DEBUG_ALLOC 5911da177e4SLinus Torvalds debug_alloc(opl3, "note on ", voice); 5921da177e4SLinus Torvalds #endif 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds /* allocate extra program if specified in patch library */ 5951da177e4SLinus Torvalds if (extra_prg) { 5961da177e4SLinus Torvalds if (extra_prg > 128) { 597224a0332STakashi Iwai bank = 128; 5981da177e4SLinus Torvalds /* percussions start at 35 */ 599224a0332STakashi Iwai prg = extra_prg - 128 + 35 - 1; 6001da177e4SLinus Torvalds } else { 601224a0332STakashi Iwai bank = 0; 602224a0332STakashi Iwai prg = extra_prg - 1; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds #ifdef DEBUG_MIDI 605*45203832STakashi Iwai snd_printk(KERN_DEBUG " *** allocating extra program\n"); 6061da177e4SLinus Torvalds #endif 6071da177e4SLinus Torvalds goto __extra_prg; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6125b1646a8STakashi Iwai static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice) 6131da177e4SLinus Torvalds { 6141da177e4SLinus Torvalds unsigned short reg_side; 6151da177e4SLinus Torvalds unsigned char voice_offset; 6161da177e4SLinus Torvalds unsigned short opl3_reg; 6171da177e4SLinus Torvalds 6185b1646a8STakashi Iwai struct snd_opl3_voice *vp, *vp2; 6191da177e4SLinus Torvalds 6205e246b85STakashi Iwai if (snd_BUG_ON(voice >= MAX_OPL3_VOICES)) 6215e246b85STakashi Iwai return; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds vp = &opl3->voices[voice]; 6241da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) { 6251da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */ 6261da177e4SLinus Torvalds reg_side = OPL3_LEFT; 6271da177e4SLinus Torvalds voice_offset = voice; 6281da177e4SLinus Torvalds } else { 6291da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */ 6301da177e4SLinus Torvalds reg_side = OPL3_RIGHT; 6311da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* kill voice */ 6351da177e4SLinus Torvalds #ifdef DEBUG_MIDI 636*45203832STakashi Iwai snd_printk(KERN_DEBUG " --> kill voice %i\n", voice); 6371da177e4SLinus Torvalds #endif 6381da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset); 6391da177e4SLinus Torvalds /* clear Key ON bit */ 6401da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, vp->keyon_reg); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* do the bookkeeping */ 6431da177e4SLinus Torvalds vp->time = opl3->use_time++; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds if (vp->state == SNDRV_OPL3_ST_ON_4OP) { 6461da177e4SLinus Torvalds vp2 = &opl3->voices[voice + 3]; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds vp2->time = opl3->use_time++; 6491da177e4SLinus Torvalds vp2->state = SNDRV_OPL3_ST_OFF; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds vp->state = SNDRV_OPL3_ST_OFF; 6521da177e4SLinus Torvalds #ifdef DEBUG_ALLOC 6531da177e4SLinus Torvalds debug_alloc(opl3, "note off", voice); 6541da177e4SLinus Torvalds #endif 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* 6591da177e4SLinus Torvalds * Release a note in response to a midi note off. 6601da177e4SLinus Torvalds */ 6615b1646a8STakashi Iwai void snd_opl3_note_off(void *p, int note, int vel, struct snd_midi_channel *chan) 6621da177e4SLinus Torvalds { 6635b1646a8STakashi Iwai struct snd_opl3 *opl3; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds int voice; 6665b1646a8STakashi Iwai struct snd_opl3_voice *vp; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds unsigned long flags; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds opl3 = p; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds #ifdef DEBUG_MIDI 673*45203832STakashi Iwai snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n", 6741da177e4SLinus Torvalds chan->number, chan->midi_program, note); 6751da177e4SLinus Torvalds #endif 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds spin_lock_irqsave(&opl3->voice_lock, flags); 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) { 6801da177e4SLinus Torvalds if (chan->drum_channel && use_internal_drums) { 6811da177e4SLinus Torvalds snd_opl3_drum_switch(opl3, note, vel, 0, chan); 6821da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 6831da177e4SLinus Torvalds return; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds /* this loop will hopefully kill all extra voices, because 6861da177e4SLinus Torvalds they are grouped by the same channel and note values */ 6871da177e4SLinus Torvalds for (voice = 0; voice < opl3->max_voices; voice++) { 6881da177e4SLinus Torvalds vp = &opl3->voices[voice]; 6891da177e4SLinus Torvalds if (vp->state > 0 && vp->chan == chan && vp->note == note) { 6901da177e4SLinus Torvalds snd_opl3_kill_voice(opl3, voice); 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds } else { 6941da177e4SLinus Torvalds /* remap OSS voices */ 6951da177e4SLinus Torvalds if (chan->number < MAX_OPL3_VOICES) { 6961da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number]; 6971da177e4SLinus Torvalds snd_opl3_kill_voice(opl3, voice); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 7041da177e4SLinus Torvalds * key pressure change 7051da177e4SLinus Torvalds */ 7065b1646a8STakashi Iwai void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan) 7071da177e4SLinus Torvalds { 7085b1646a8STakashi Iwai struct snd_opl3 *opl3; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds opl3 = p; 7111da177e4SLinus Torvalds #ifdef DEBUG_MIDI 712*45203832STakashi Iwai snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n", 7131da177e4SLinus Torvalds chan->number, chan->midi_program); 7141da177e4SLinus Torvalds #endif 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* 7181da177e4SLinus Torvalds * terminate note 7191da177e4SLinus Torvalds */ 7205b1646a8STakashi Iwai void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan) 7211da177e4SLinus Torvalds { 7225b1646a8STakashi Iwai struct snd_opl3 *opl3; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds opl3 = p; 7251da177e4SLinus Torvalds #ifdef DEBUG_MIDI 726*45203832STakashi Iwai snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n", 7271da177e4SLinus Torvalds chan->number, chan->midi_program); 7281da177e4SLinus Torvalds #endif 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7315b1646a8STakashi Iwai static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds unsigned short reg_side; 7341da177e4SLinus Torvalds unsigned char voice_offset; 7351da177e4SLinus Torvalds unsigned short opl3_reg; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds unsigned char fnum, blocknum; 7381da177e4SLinus Torvalds 7395b1646a8STakashi Iwai struct snd_opl3_voice *vp; 7401da177e4SLinus Torvalds 7415e246b85STakashi Iwai if (snd_BUG_ON(voice >= MAX_OPL3_VOICES)) 7425e246b85STakashi Iwai return; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds vp = &opl3->voices[voice]; 7451da177e4SLinus Torvalds if (vp->chan == NULL) 7461da177e4SLinus Torvalds return; /* not allocated? */ 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds if (voice < MAX_OPL2_VOICES) { 7491da177e4SLinus Torvalds /* Left register block for voices 0 .. 8 */ 7501da177e4SLinus Torvalds reg_side = OPL3_LEFT; 7511da177e4SLinus Torvalds voice_offset = voice; 7521da177e4SLinus Torvalds } else { 7531da177e4SLinus Torvalds /* Right register block for voices 9 .. 17 */ 7541da177e4SLinus Torvalds reg_side = OPL3_RIGHT; 7551da177e4SLinus Torvalds voice_offset = voice - MAX_OPL2_VOICES; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* Set OPL3 FNUM_LOW register of requested voice */ 7611da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset); 7621da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, fnum); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds vp->keyon_reg = blocknum; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* Set output sound flag */ 7671da177e4SLinus Torvalds blocknum |= OPL3_KEYON_BIT; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* Set OPL3 KEYON_BLOCK register of requested voice */ 7701da177e4SLinus Torvalds opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset); 7711da177e4SLinus Torvalds opl3->command(opl3, opl3_reg, blocknum); 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds vp->time = opl3->use_time++; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * Update voice pitch controller 7781da177e4SLinus Torvalds */ 7795b1646a8STakashi Iwai static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan) 7801da177e4SLinus Torvalds { 7811da177e4SLinus Torvalds int voice; 7825b1646a8STakashi Iwai struct snd_opl3_voice *vp; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds unsigned long flags; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds spin_lock_irqsave(&opl3->voice_lock, flags); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) { 7891da177e4SLinus Torvalds for (voice = 0; voice < opl3->max_voices; voice++) { 7901da177e4SLinus Torvalds vp = &opl3->voices[voice]; 7911da177e4SLinus Torvalds if (vp->state > 0 && vp->chan == chan) { 7921da177e4SLinus Torvalds snd_opl3_update_pitch(opl3, voice); 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds } else { 7961da177e4SLinus Torvalds /* remap OSS voices */ 7971da177e4SLinus Torvalds if (chan->number < MAX_OPL3_VOICES) { 7981da177e4SLinus Torvalds voice = snd_opl3_oss_map[chan->number]; 7991da177e4SLinus Torvalds snd_opl3_update_pitch(opl3, voice); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds spin_unlock_irqrestore(&opl3->voice_lock, flags); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds /* 8063a4fa0a2SRobert P. J. Day * Deal with a controller type event. This includes all types of 8071da177e4SLinus Torvalds * control events, not just the midi controllers 8081da177e4SLinus Torvalds */ 8095b1646a8STakashi Iwai void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan) 8101da177e4SLinus Torvalds { 8115b1646a8STakashi Iwai struct snd_opl3 *opl3; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds opl3 = p; 8141da177e4SLinus Torvalds #ifdef DEBUG_MIDI 815*45203832STakashi Iwai snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n", 8161da177e4SLinus Torvalds type, chan->number, chan->midi_program); 8171da177e4SLinus Torvalds #endif 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds switch (type) { 8201da177e4SLinus Torvalds case MIDI_CTL_MSB_MODWHEEL: 8211da177e4SLinus Torvalds if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63) 8221da177e4SLinus Torvalds opl3->drum_reg |= OPL3_VIBRATO_DEPTH; 8231da177e4SLinus Torvalds else 8241da177e4SLinus Torvalds opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH; 8251da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 8261da177e4SLinus Torvalds opl3->drum_reg); 8271da177e4SLinus Torvalds break; 8281da177e4SLinus Torvalds case MIDI_CTL_E2_TREMOLO_DEPTH: 8291da177e4SLinus Torvalds if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63) 8301da177e4SLinus Torvalds opl3->drum_reg |= OPL3_TREMOLO_DEPTH; 8311da177e4SLinus Torvalds else 8321da177e4SLinus Torvalds opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH; 8331da177e4SLinus Torvalds opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 8341da177e4SLinus Torvalds opl3->drum_reg); 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds case MIDI_CTL_PITCHBEND: 8371da177e4SLinus Torvalds snd_opl3_pitch_ctrl(opl3, chan); 8381da177e4SLinus Torvalds break; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /* 8431da177e4SLinus Torvalds * NRPN events 8441da177e4SLinus Torvalds */ 8455b1646a8STakashi Iwai void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan, 8465b1646a8STakashi Iwai struct snd_midi_channel_set *chset) 8471da177e4SLinus Torvalds { 8485b1646a8STakashi Iwai struct snd_opl3 *opl3; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds opl3 = p; 8511da177e4SLinus Torvalds #ifdef DEBUG_MIDI 852*45203832STakashi Iwai snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n", 8531da177e4SLinus Torvalds chan->number, chan->midi_program); 8541da177e4SLinus Torvalds #endif 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * receive sysex 8591da177e4SLinus Torvalds */ 8601da177e4SLinus Torvalds void snd_opl3_sysex(void *p, unsigned char *buf, int len, 8615b1646a8STakashi Iwai int parsed, struct snd_midi_channel_set *chset) 8621da177e4SLinus Torvalds { 8635b1646a8STakashi Iwai struct snd_opl3 *opl3; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds opl3 = p; 8661da177e4SLinus Torvalds #ifdef DEBUG_MIDI 867*45203832STakashi Iwai snd_printk(KERN_DEBUG "SYSEX\n"); 8681da177e4SLinus Torvalds #endif 8691da177e4SLinus Torvalds } 870