Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  synth callback routines for the emu8000 (AWE32/64)
0004  *
0005  *  Copyright (C) 1999 Steve Ratcliffe
0006  *  Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
0007  */
0008 
0009 #include "emu8000_local.h"
0010 #include <linux/export.h>
0011 #include <sound/asoundef.h>
0012 
0013 /*
0014  * prototypes
0015  */
0016 static struct snd_emux_voice *get_voice(struct snd_emux *emu,
0017                     struct snd_emux_port *port);
0018 static int start_voice(struct snd_emux_voice *vp);
0019 static void trigger_voice(struct snd_emux_voice *vp);
0020 static void release_voice(struct snd_emux_voice *vp);
0021 static void update_voice(struct snd_emux_voice *vp, int update);
0022 static void reset_voice(struct snd_emux *emu, int ch);
0023 static void terminate_voice(struct snd_emux_voice *vp);
0024 static void sysex(struct snd_emux *emu, char *buf, int len, int parsed,
0025           struct snd_midi_channel_set *chset);
0026 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
0027 static int oss_ioctl(struct snd_emux *emu, int cmd, int p1, int p2);
0028 #endif
0029 static int load_fx(struct snd_emux *emu, int type, int mode,
0030            const void __user *buf, long len);
0031 
0032 static void set_pitch(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0033 static void set_volume(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0034 static void set_pan(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0035 static void set_fmmod(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0036 static void set_tremfreq(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0037 static void set_fm2frq2(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0038 static void set_filterQ(struct snd_emu8000 *hw, struct snd_emux_voice *vp);
0039 static void snd_emu8000_tweak_voice(struct snd_emu8000 *emu, int ch);
0040 
0041 /*
0042  * Ensure a value is between two points
0043  * macro evaluates its args more than once, so changed to upper-case.
0044  */
0045 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
0046 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
0047 
0048 
0049 /*
0050  * set up operators
0051  */
0052 static const struct snd_emux_operators emu8000_ops = {
0053     .owner =    THIS_MODULE,
0054     .get_voice =    get_voice,
0055     .prepare =  start_voice,
0056     .trigger =  trigger_voice,
0057     .release =  release_voice,
0058     .update =   update_voice,
0059     .terminate =    terminate_voice,
0060     .reset =    reset_voice,
0061     .sample_new =   snd_emu8000_sample_new,
0062     .sample_free =  snd_emu8000_sample_free,
0063     .sample_reset = snd_emu8000_sample_reset,
0064     .load_fx =  load_fx,
0065     .sysex =    sysex,
0066 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
0067     .oss_ioctl =    oss_ioctl,
0068 #endif
0069 };
0070 
0071 void
0072 snd_emu8000_ops_setup(struct snd_emu8000 *hw)
0073 {
0074     hw->emu->ops = emu8000_ops;
0075 }
0076 
0077 
0078 
0079 /*
0080  * Terminate a voice
0081  */
0082 static void
0083 release_voice(struct snd_emux_voice *vp)
0084 {
0085     int dcysusv;
0086     struct snd_emu8000 *hw;
0087 
0088     hw = vp->hw;
0089     dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
0090     EMU8000_DCYSUS_WRITE(hw, vp->ch, dcysusv);
0091     dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease;
0092     EMU8000_DCYSUSV_WRITE(hw, vp->ch, dcysusv);
0093 }
0094 
0095 
0096 /*
0097  */
0098 static void
0099 terminate_voice(struct snd_emux_voice *vp)
0100 {
0101     struct snd_emu8000 *hw; 
0102 
0103     hw = vp->hw;
0104     EMU8000_DCYSUSV_WRITE(hw, vp->ch, 0x807F);
0105 }
0106 
0107 
0108 /*
0109  */
0110 static void
0111 update_voice(struct snd_emux_voice *vp, int update)
0112 {
0113     struct snd_emu8000 *hw;
0114 
0115     hw = vp->hw;
0116     if (update & SNDRV_EMUX_UPDATE_VOLUME)
0117         set_volume(hw, vp);
0118     if (update & SNDRV_EMUX_UPDATE_PITCH)
0119         set_pitch(hw, vp);
0120     if ((update & SNDRV_EMUX_UPDATE_PAN) &&
0121         vp->port->ctrls[EMUX_MD_REALTIME_PAN])
0122         set_pan(hw, vp);
0123     if (update & SNDRV_EMUX_UPDATE_FMMOD)
0124         set_fmmod(hw, vp);
0125     if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
0126         set_tremfreq(hw, vp);
0127     if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
0128         set_fm2frq2(hw, vp);
0129     if (update & SNDRV_EMUX_UPDATE_Q)
0130         set_filterQ(hw, vp);
0131 }
0132 
0133 
0134 /*
0135  * Find a channel (voice) within the EMU that is not in use or at least
0136  * less in use than other channels.  Always returns a valid pointer
0137  * no matter what.  If there is a real shortage of voices then one
0138  * will be cut. Such is life.
0139  *
0140  * The channel index (vp->ch) must be initialized in this routine.
0141  * In Emu8k, it is identical with the array index.
0142  */
0143 static struct snd_emux_voice *
0144 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
0145 {
0146     int  i;
0147     struct snd_emux_voice *vp;
0148     struct snd_emu8000 *hw;
0149 
0150     /* what we are looking for, in order of preference */
0151     enum {
0152         OFF=0, RELEASED, PLAYING, END
0153     };
0154 
0155     /* Keeps track of what we are finding */
0156     struct best {
0157         unsigned int  time;
0158         int voice;
0159     } best[END];
0160     struct best *bp;
0161 
0162     hw = emu->hw;
0163 
0164     for (i = 0; i < END; i++) {
0165         best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
0166         best[i].voice = -1;
0167     }
0168 
0169     /*
0170      * Go through them all and get a best one to use.
0171      */
0172     for (i = 0; i < emu->max_voices; i++) {
0173         int state, val;
0174 
0175         vp = &emu->voices[i];
0176         state = vp->state;
0177 
0178         if (state == SNDRV_EMUX_ST_OFF)
0179             bp = best + OFF;
0180         else if (state == SNDRV_EMUX_ST_RELEASED ||
0181              state == SNDRV_EMUX_ST_PENDING) {
0182             bp = best + RELEASED;
0183             val = (EMU8000_CVCF_READ(hw, vp->ch) >> 16) & 0xffff;
0184             if (! val)
0185                 bp = best + OFF;
0186         }
0187         else if (state & SNDRV_EMUX_ST_ON)
0188             bp = best + PLAYING;
0189         else
0190             continue;
0191 
0192         /* check if sample is finished playing (non-looping only) */
0193         if (state != SNDRV_EMUX_ST_OFF &&
0194             (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
0195             val = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
0196             if (val >= vp->reg.loopstart)
0197                 bp = best + OFF;
0198         }
0199 
0200         if (vp->time < bp->time) {
0201             bp->time = vp->time;
0202             bp->voice = i;
0203         }
0204     }
0205 
0206     for (i = 0; i < END; i++) {
0207         if (best[i].voice >= 0) {
0208             vp = &emu->voices[best[i].voice];
0209             vp->ch = best[i].voice;
0210             return vp;
0211         }
0212     }
0213 
0214     /* not found */
0215     return NULL;
0216 }
0217 
0218 /*
0219  */
0220 static int
0221 start_voice(struct snd_emux_voice *vp)
0222 {
0223     unsigned int temp;
0224     int ch;
0225     int addr;
0226     struct snd_midi_channel *chan;
0227     struct snd_emu8000 *hw;
0228 
0229     hw = vp->hw;
0230     ch = vp->ch;
0231     chan = vp->chan;
0232 
0233     /* channel to be silent and idle */
0234     EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
0235     EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
0236     EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
0237     EMU8000_PTRX_WRITE(hw, ch, 0);
0238     EMU8000_CPF_WRITE(hw, ch, 0);
0239 
0240     /* set pitch offset */
0241     set_pitch(hw, vp);
0242 
0243     /* set envelope parameters */
0244     EMU8000_ENVVAL_WRITE(hw, ch, vp->reg.parm.moddelay);
0245     EMU8000_ATKHLD_WRITE(hw, ch, vp->reg.parm.modatkhld);
0246     EMU8000_DCYSUS_WRITE(hw, ch, vp->reg.parm.moddcysus);
0247     EMU8000_ENVVOL_WRITE(hw, ch, vp->reg.parm.voldelay);
0248     EMU8000_ATKHLDV_WRITE(hw, ch, vp->reg.parm.volatkhld);
0249     /* decay/sustain parameter for volume envelope is used
0250        for triggerg the voice */
0251 
0252     /* cutoff and volume */
0253     set_volume(hw, vp);
0254 
0255     /* modulation envelope heights */
0256     EMU8000_PEFE_WRITE(hw, ch, vp->reg.parm.pefe);
0257 
0258     /* lfo1/2 delay */
0259     EMU8000_LFO1VAL_WRITE(hw, ch, vp->reg.parm.lfo1delay);
0260     EMU8000_LFO2VAL_WRITE(hw, ch, vp->reg.parm.lfo2delay);
0261 
0262     /* lfo1 pitch & cutoff shift */
0263     set_fmmod(hw, vp);
0264     /* lfo1 volume & freq */
0265     set_tremfreq(hw, vp);
0266     /* lfo2 pitch & freq */
0267     set_fm2frq2(hw, vp);
0268     /* pan & loop start */
0269     set_pan(hw, vp);
0270 
0271     /* chorus & loop end (chorus 8bit, MSB) */
0272     addr = vp->reg.loopend - 1;
0273     temp = vp->reg.parm.chorus;
0274     temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
0275     LIMITMAX(temp, 255);
0276     temp = (temp <<24) | (unsigned int)addr;
0277     EMU8000_CSL_WRITE(hw, ch, temp);
0278 
0279     /* Q & current address (Q 4bit value, MSB) */
0280     addr = vp->reg.start - 1;
0281     temp = vp->reg.parm.filterQ;
0282     temp = (temp<<28) | (unsigned int)addr;
0283     EMU8000_CCCA_WRITE(hw, ch, temp);
0284 
0285     /* clear unknown registers */
0286     EMU8000_00A0_WRITE(hw, ch, 0);
0287     EMU8000_0080_WRITE(hw, ch, 0);
0288 
0289     /* reset volume */
0290     temp = vp->vtarget << 16;
0291     EMU8000_VTFT_WRITE(hw, ch, temp | vp->ftarget);
0292     EMU8000_CVCF_WRITE(hw, ch, temp | 0xff00);
0293 
0294     return 0;
0295 }
0296 
0297 /*
0298  * Start envelope
0299  */
0300 static void
0301 trigger_voice(struct snd_emux_voice *vp)
0302 {
0303     int ch = vp->ch;
0304     unsigned int temp;
0305     struct snd_emu8000 *hw;
0306 
0307     hw = vp->hw;
0308 
0309     /* set reverb and pitch target */
0310     temp = vp->reg.parm.reverb;
0311     temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
0312     LIMITMAX(temp, 255);
0313     temp = (temp << 8) | (vp->ptarget << 16) | vp->aaux;
0314     EMU8000_PTRX_WRITE(hw, ch, temp);
0315     EMU8000_CPF_WRITE(hw, ch, vp->ptarget << 16);
0316     EMU8000_DCYSUSV_WRITE(hw, ch, vp->reg.parm.voldcysus);
0317 }
0318 
0319 /*
0320  * reset voice parameters
0321  */
0322 static void
0323 reset_voice(struct snd_emux *emu, int ch)
0324 {
0325     struct snd_emu8000 *hw;
0326 
0327     hw = emu->hw;
0328     EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
0329     snd_emu8000_tweak_voice(hw, ch);
0330 }
0331 
0332 /*
0333  * Set the pitch of a possibly playing note.
0334  */
0335 static void
0336 set_pitch(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0337 {
0338     EMU8000_IP_WRITE(hw, vp->ch, vp->apitch);
0339 }
0340 
0341 /*
0342  * Set the volume of a possibly already playing note
0343  */
0344 static void
0345 set_volume(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0346 {
0347     int  ifatn;
0348 
0349     ifatn = (unsigned char)vp->acutoff;
0350     ifatn = (ifatn << 8);
0351     ifatn |= (unsigned char)vp->avol;
0352     EMU8000_IFATN_WRITE(hw, vp->ch, ifatn);
0353 }
0354 
0355 /*
0356  * Set pan and loop start address.
0357  */
0358 static void
0359 set_pan(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0360 {
0361     unsigned int temp;
0362 
0363     temp = ((unsigned int)vp->apan<<24) | ((unsigned int)vp->reg.loopstart - 1);
0364     EMU8000_PSST_WRITE(hw, vp->ch, temp);
0365 }
0366 
0367 #define MOD_SENSE 18
0368 
0369 static void
0370 set_fmmod(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0371 {
0372     unsigned short fmmod;
0373     short pitch;
0374     unsigned char cutoff;
0375     int modulation;
0376 
0377     pitch = (char)(vp->reg.parm.fmmod>>8);
0378     cutoff = (vp->reg.parm.fmmod & 0xff);
0379     modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
0380     pitch += (MOD_SENSE * modulation) / 1200;
0381     LIMITVALUE(pitch, -128, 127);
0382     fmmod = ((unsigned char)pitch<<8) | cutoff;
0383     EMU8000_FMMOD_WRITE(hw, vp->ch, fmmod);
0384 }
0385 
0386 /* set tremolo (lfo1) volume & frequency */
0387 static void
0388 set_tremfreq(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0389 {
0390     EMU8000_TREMFRQ_WRITE(hw, vp->ch, vp->reg.parm.tremfrq);
0391 }
0392 
0393 /* set lfo2 pitch & frequency */
0394 static void
0395 set_fm2frq2(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0396 {
0397     unsigned short fm2frq2;
0398     short pitch;
0399     unsigned char freq;
0400     int modulation;
0401 
0402     pitch = (char)(vp->reg.parm.fm2frq2>>8);
0403     freq = vp->reg.parm.fm2frq2 & 0xff;
0404     modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
0405     pitch += (MOD_SENSE * modulation) / 1200;
0406     LIMITVALUE(pitch, -128, 127);
0407     fm2frq2 = ((unsigned char)pitch<<8) | freq;
0408     EMU8000_FM2FRQ2_WRITE(hw, vp->ch, fm2frq2);
0409 }
0410 
0411 /* set filterQ */
0412 static void
0413 set_filterQ(struct snd_emu8000 *hw, struct snd_emux_voice *vp)
0414 {
0415     unsigned int addr;
0416     addr = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
0417     addr |= (vp->reg.parm.filterQ << 28);
0418     EMU8000_CCCA_WRITE(hw, vp->ch, addr);
0419 }
0420 
0421 /*
0422  * set the envelope & LFO parameters to the default values
0423  */
0424 static void
0425 snd_emu8000_tweak_voice(struct snd_emu8000 *emu, int i)
0426 {
0427     /* set all mod/vol envelope shape to minimum */
0428     EMU8000_ENVVOL_WRITE(emu, i, 0x8000);
0429     EMU8000_ENVVAL_WRITE(emu, i, 0x8000);
0430     EMU8000_DCYSUS_WRITE(emu, i, 0x7F7F);
0431     EMU8000_ATKHLDV_WRITE(emu, i, 0x7F7F);
0432     EMU8000_ATKHLD_WRITE(emu, i, 0x7F7F);
0433     EMU8000_PEFE_WRITE(emu, i, 0);  /* mod envelope height to zero */
0434     EMU8000_LFO1VAL_WRITE(emu, i, 0x8000); /* no delay for LFO1 */
0435     EMU8000_LFO2VAL_WRITE(emu, i, 0x8000);
0436     EMU8000_IP_WRITE(emu, i, 0xE000);   /* no pitch shift */
0437     EMU8000_IFATN_WRITE(emu, i, 0xFF00);    /* volume to minimum */
0438     EMU8000_FMMOD_WRITE(emu, i, 0);
0439     EMU8000_TREMFRQ_WRITE(emu, i, 0);
0440     EMU8000_FM2FRQ2_WRITE(emu, i, 0);
0441 }
0442 
0443 /*
0444  * sysex callback
0445  */
0446 static void
0447 sysex(struct snd_emux *emu, char *buf, int len, int parsed, struct snd_midi_channel_set *chset)
0448 {
0449     struct snd_emu8000 *hw;
0450 
0451     hw = emu->hw;
0452 
0453     switch (parsed) {
0454     case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE:
0455         hw->chorus_mode = chset->gs_chorus_mode;
0456         snd_emu8000_update_chorus_mode(hw);
0457         break;
0458 
0459     case SNDRV_MIDI_SYSEX_GS_REVERB_MODE:
0460         hw->reverb_mode = chset->gs_reverb_mode;
0461         snd_emu8000_update_reverb_mode(hw);
0462         break;
0463     }
0464 }
0465 
0466 
0467 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
0468 /*
0469  * OSS ioctl callback
0470  */
0471 static int
0472 oss_ioctl(struct snd_emux *emu, int cmd, int p1, int p2)
0473 {
0474     struct snd_emu8000 *hw;
0475 
0476     hw = emu->hw;
0477 
0478     switch (cmd) {
0479     case _EMUX_OSS_REVERB_MODE:
0480         hw->reverb_mode = p1;
0481         snd_emu8000_update_reverb_mode(hw);
0482         break;
0483 
0484     case _EMUX_OSS_CHORUS_MODE:
0485         hw->chorus_mode = p1;
0486         snd_emu8000_update_chorus_mode(hw);
0487         break;
0488 
0489     case _EMUX_OSS_INITIALIZE_CHIP:
0490         /* snd_emu8000_init(hw); */ /*ignored*/
0491         break;
0492 
0493     case _EMUX_OSS_EQUALIZER:
0494         hw->bass_level = p1;
0495         hw->treble_level = p2;
0496         snd_emu8000_update_equalizer(hw);
0497         break;
0498     }
0499     return 0;
0500 }
0501 #endif
0502 
0503 
0504 /*
0505  * additional patch keys
0506  */
0507 
0508 #define SNDRV_EMU8000_LOAD_CHORUS_FX    0x10    /* optarg=mode */
0509 #define SNDRV_EMU8000_LOAD_REVERB_FX    0x11    /* optarg=mode */
0510 
0511 
0512 /*
0513  * callback routine
0514  */
0515 
0516 static int
0517 load_fx(struct snd_emux *emu, int type, int mode, const void __user *buf, long len)
0518 {
0519     struct snd_emu8000 *hw;
0520     hw = emu->hw;
0521 
0522     /* skip header */
0523     buf += 16;
0524     len -= 16;
0525 
0526     switch (type) {
0527     case SNDRV_EMU8000_LOAD_CHORUS_FX:
0528         return snd_emu8000_load_chorus_fx(hw, mode, buf, len);
0529     case SNDRV_EMU8000_LOAD_REVERB_FX:
0530         return snd_emu8000_load_reverb_fx(hw, mode, buf, len);
0531     }
0532     return -EINVAL;
0533 }
0534