2 * synth callback routines for the emu8000 (AWE32/64)
4 * Copyright (C) 1999 Steve Ratcliffe
5 * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "emu8000_local.h"
23 #include <sound/asoundef.h>
28 static snd_emux_voice_t
*get_voice(snd_emux_t
*emu
, snd_emux_port_t
*port
);
29 static int start_voice(snd_emux_voice_t
*vp
);
30 static void trigger_voice(snd_emux_voice_t
*vp
);
31 static void release_voice(snd_emux_voice_t
*vp
);
32 static void update_voice(snd_emux_voice_t
*vp
, int update
);
33 static void reset_voice(snd_emux_t
*emu
, int ch
);
34 static void terminate_voice(snd_emux_voice_t
*vp
);
35 static void sysex(snd_emux_t
*emu
, char *buf
, int len
, int parsed
, snd_midi_channel_set_t
*chset
);
36 #ifdef CONFIG_SND_SEQUENCER_OSS
37 static int oss_ioctl(snd_emux_t
*emu
, int cmd
, int p1
, int p2
);
39 static int load_fx(snd_emux_t
*emu
, int type
, int mode
, const void __user
*buf
, long len
);
41 static void set_pitch(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
42 static void set_volume(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
43 static void set_pan(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
44 static void set_fmmod(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
45 static void set_tremfreq(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
46 static void set_fm2frq2(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
47 static void set_filterQ(emu8000_t
*hw
, snd_emux_voice_t
*vp
);
48 static void snd_emu8000_tweak_voice(emu8000_t
*emu
, int ch
);
51 * Ensure a value is between two points
52 * macro evaluates its args more than once, so changed to upper-case.
54 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
55 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
61 static snd_emux_operators_t emu8000_ops
= {
63 .get_voice
= get_voice
,
64 .prepare
= start_voice
,
65 .trigger
= trigger_voice
,
66 .release
= release_voice
,
67 .update
= update_voice
,
68 .terminate
= terminate_voice
,
70 .sample_new
= snd_emu8000_sample_new
,
71 .sample_free
= snd_emu8000_sample_free
,
72 .sample_reset
= snd_emu8000_sample_reset
,
75 #ifdef CONFIG_SND_SEQUENCER_OSS
76 .oss_ioctl
= oss_ioctl
,
81 snd_emu8000_ops_setup(emu8000_t
*hw
)
83 hw
->emu
->ops
= emu8000_ops
;
92 release_voice(snd_emux_voice_t
*vp
)
98 dcysusv
= 0x8000 | (unsigned char)vp
->reg
.parm
.modrelease
;
99 EMU8000_DCYSUS_WRITE(hw
, vp
->ch
, dcysusv
);
100 dcysusv
= 0x8000 | (unsigned char)vp
->reg
.parm
.volrelease
;
101 EMU8000_DCYSUSV_WRITE(hw
, vp
->ch
, dcysusv
);
108 terminate_voice(snd_emux_voice_t
*vp
)
113 EMU8000_DCYSUSV_WRITE(hw
, vp
->ch
, 0x807F);
120 update_voice(snd_emux_voice_t
*vp
, int update
)
125 if (update
& SNDRV_EMUX_UPDATE_VOLUME
)
127 if (update
& SNDRV_EMUX_UPDATE_PITCH
)
129 if ((update
& SNDRV_EMUX_UPDATE_PAN
) &&
130 vp
->port
->ctrls
[EMUX_MD_REALTIME_PAN
])
132 if (update
& SNDRV_EMUX_UPDATE_FMMOD
)
134 if (update
& SNDRV_EMUX_UPDATE_TREMFREQ
)
135 set_tremfreq(hw
, vp
);
136 if (update
& SNDRV_EMUX_UPDATE_FM2FRQ2
)
138 if (update
& SNDRV_EMUX_UPDATE_Q
)
144 * Find a channel (voice) within the EMU that is not in use or at least
145 * less in use than other channels. Always returns a valid pointer
146 * no matter what. If there is a real shortage of voices then one
147 * will be cut. Such is life.
149 * The channel index (vp->ch) must be initialized in this routine.
150 * In Emu8k, it is identical with the array index.
152 static snd_emux_voice_t
*
153 get_voice(snd_emux_t
*emu
, snd_emux_port_t
*port
)
156 snd_emux_voice_t
*vp
;
159 /* what we are looking for, in order of preference */
161 OFF
=0, RELEASED
, PLAYING
, END
164 /* Keeps track of what we are finding */
173 for (i
= 0; i
< END
; i
++) {
174 best
[i
].time
= (unsigned int)(-1); /* XXX MAX_?INT really */;
179 * Go through them all and get a best one to use.
181 for (i
= 0; i
< emu
->max_voices
; i
++) {
184 vp
= &emu
->voices
[i
];
187 if (state
== SNDRV_EMUX_ST_OFF
)
189 else if (state
== SNDRV_EMUX_ST_RELEASED
||
190 state
== SNDRV_EMUX_ST_PENDING
) {
191 bp
= best
+ RELEASED
;
192 val
= (EMU8000_CVCF_READ(hw
, vp
->ch
) >> 16) & 0xffff;
196 else if (state
& SNDRV_EMUX_ST_ON
)
201 /* check if sample is finished playing (non-looping only) */
202 if (state
!= SNDRV_EMUX_ST_OFF
&&
203 (vp
->reg
.sample_mode
& SNDRV_SFNT_SAMPLE_SINGLESHOT
)) {
204 val
= EMU8000_CCCA_READ(hw
, vp
->ch
) & 0xffffff;
205 if (val
>= vp
->reg
.loopstart
)
209 if (vp
->time
< bp
->time
) {
215 for (i
= 0; i
< END
; i
++) {
216 if (best
[i
].voice
>= 0) {
217 vp
= &emu
->voices
[best
[i
].voice
];
218 vp
->ch
= best
[i
].voice
;
230 start_voice(snd_emux_voice_t
*vp
)
235 snd_midi_channel_t
*chan
;
242 /* channel to be silent and idle */
243 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x0080);
244 EMU8000_VTFT_WRITE(hw
, ch
, 0x0000FFFF);
245 EMU8000_CVCF_WRITE(hw
, ch
, 0x0000FFFF);
246 EMU8000_PTRX_WRITE(hw
, ch
, 0);
247 EMU8000_CPF_WRITE(hw
, ch
, 0);
249 /* set pitch offset */
252 /* set envelope parameters */
253 EMU8000_ENVVAL_WRITE(hw
, ch
, vp
->reg
.parm
.moddelay
);
254 EMU8000_ATKHLD_WRITE(hw
, ch
, vp
->reg
.parm
.modatkhld
);
255 EMU8000_DCYSUS_WRITE(hw
, ch
, vp
->reg
.parm
.moddcysus
);
256 EMU8000_ENVVOL_WRITE(hw
, ch
, vp
->reg
.parm
.voldelay
);
257 EMU8000_ATKHLDV_WRITE(hw
, ch
, vp
->reg
.parm
.volatkhld
);
258 /* decay/sustain parameter for volume envelope is used
259 for triggerg the voice */
261 /* cutoff and volume */
264 /* modulation envelope heights */
265 EMU8000_PEFE_WRITE(hw
, ch
, vp
->reg
.parm
.pefe
);
268 EMU8000_LFO1VAL_WRITE(hw
, ch
, vp
->reg
.parm
.lfo1delay
);
269 EMU8000_LFO2VAL_WRITE(hw
, ch
, vp
->reg
.parm
.lfo2delay
);
271 /* lfo1 pitch & cutoff shift */
273 /* lfo1 volume & freq */
274 set_tremfreq(hw
, vp
);
275 /* lfo2 pitch & freq */
277 /* pan & loop start */
280 /* chorus & loop end (chorus 8bit, MSB) */
281 addr
= vp
->reg
.loopend
- 1;
282 temp
= vp
->reg
.parm
.chorus
;
283 temp
+= (int)chan
->control
[MIDI_CTL_E3_CHORUS_DEPTH
] * 9 / 10;
285 temp
= (temp
<<24) | (unsigned int)addr
;
286 EMU8000_CSL_WRITE(hw
, ch
, temp
);
288 /* Q & current address (Q 4bit value, MSB) */
289 addr
= vp
->reg
.start
- 1;
290 temp
= vp
->reg
.parm
.filterQ
;
291 temp
= (temp
<<28) | (unsigned int)addr
;
292 EMU8000_CCCA_WRITE(hw
, ch
, temp
);
294 /* clear unknown registers */
295 EMU8000_00A0_WRITE(hw
, ch
, 0);
296 EMU8000_0080_WRITE(hw
, ch
, 0);
299 temp
= vp
->vtarget
<< 16;
300 EMU8000_VTFT_WRITE(hw
, ch
, temp
| vp
->ftarget
);
301 EMU8000_CVCF_WRITE(hw
, ch
, temp
| 0xff00);
310 trigger_voice(snd_emux_voice_t
*vp
)
318 /* set reverb and pitch target */
319 temp
= vp
->reg
.parm
.reverb
;
320 temp
+= (int)vp
->chan
->control
[MIDI_CTL_E1_REVERB_DEPTH
] * 9 / 10;
322 temp
= (temp
<< 8) | (vp
->ptarget
<< 16) | vp
->aaux
;
323 EMU8000_PTRX_WRITE(hw
, ch
, temp
);
324 EMU8000_CPF_WRITE(hw
, ch
, vp
->ptarget
<< 16);
325 EMU8000_DCYSUSV_WRITE(hw
, ch
, vp
->reg
.parm
.voldcysus
);
329 * reset voice parameters
332 reset_voice(snd_emux_t
*emu
, int ch
)
337 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x807F);
338 snd_emu8000_tweak_voice(hw
, ch
);
342 * Set the pitch of a possibly playing note.
345 set_pitch(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
347 EMU8000_IP_WRITE(hw
, vp
->ch
, vp
->apitch
);
351 * Set the volume of a possibly already playing note
354 set_volume(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
358 ifatn
= (unsigned char)vp
->acutoff
;
359 ifatn
= (ifatn
<< 8);
360 ifatn
|= (unsigned char)vp
->avol
;
361 EMU8000_IFATN_WRITE(hw
, vp
->ch
, ifatn
);
365 * Set pan and loop start address.
368 set_pan(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
372 temp
= ((unsigned int)vp
->apan
<<24) | ((unsigned int)vp
->reg
.loopstart
- 1);
373 EMU8000_PSST_WRITE(hw
, vp
->ch
, temp
);
379 set_fmmod(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
381 unsigned short fmmod
;
383 unsigned char cutoff
;
386 pitch
= (char)(vp
->reg
.parm
.fmmod
>>8);
387 cutoff
= (vp
->reg
.parm
.fmmod
& 0xff);
388 modulation
= vp
->chan
->gm_modulation
+ vp
->chan
->midi_pressure
;
389 pitch
+= (MOD_SENSE
* modulation
) / 1200;
390 LIMITVALUE(pitch
, -128, 127);
391 fmmod
= ((unsigned char)pitch
<<8) | cutoff
;
392 EMU8000_FMMOD_WRITE(hw
, vp
->ch
, fmmod
);
395 /* set tremolo (lfo1) volume & frequency */
397 set_tremfreq(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
399 EMU8000_TREMFRQ_WRITE(hw
, vp
->ch
, vp
->reg
.parm
.tremfrq
);
402 /* set lfo2 pitch & frequency */
404 set_fm2frq2(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
406 unsigned short fm2frq2
;
411 pitch
= (char)(vp
->reg
.parm
.fm2frq2
>>8);
412 freq
= vp
->reg
.parm
.fm2frq2
& 0xff;
413 modulation
= vp
->chan
->gm_modulation
+ vp
->chan
->midi_pressure
;
414 pitch
+= (MOD_SENSE
* modulation
) / 1200;
415 LIMITVALUE(pitch
, -128, 127);
416 fm2frq2
= ((unsigned char)pitch
<<8) | freq
;
417 EMU8000_FM2FRQ2_WRITE(hw
, vp
->ch
, fm2frq2
);
422 set_filterQ(emu8000_t
*hw
, snd_emux_voice_t
*vp
)
425 addr
= EMU8000_CCCA_READ(hw
, vp
->ch
) & 0xffffff;
426 addr
|= (vp
->reg
.parm
.filterQ
<< 28);
427 EMU8000_CCCA_WRITE(hw
, vp
->ch
, addr
);
431 * set the envelope & LFO parameters to the default values
434 snd_emu8000_tweak_voice(emu8000_t
*emu
, int i
)
436 /* set all mod/vol envelope shape to minimum */
437 EMU8000_ENVVOL_WRITE(emu
, i
, 0x8000);
438 EMU8000_ENVVAL_WRITE(emu
, i
, 0x8000);
439 EMU8000_DCYSUS_WRITE(emu
, i
, 0x7F7F);
440 EMU8000_ATKHLDV_WRITE(emu
, i
, 0x7F7F);
441 EMU8000_ATKHLD_WRITE(emu
, i
, 0x7F7F);
442 EMU8000_PEFE_WRITE(emu
, i
, 0); /* mod envelope height to zero */
443 EMU8000_LFO1VAL_WRITE(emu
, i
, 0x8000); /* no delay for LFO1 */
444 EMU8000_LFO2VAL_WRITE(emu
, i
, 0x8000);
445 EMU8000_IP_WRITE(emu
, i
, 0xE000); /* no pitch shift */
446 EMU8000_IFATN_WRITE(emu
, i
, 0xFF00); /* volume to minimum */
447 EMU8000_FMMOD_WRITE(emu
, i
, 0);
448 EMU8000_TREMFRQ_WRITE(emu
, i
, 0);
449 EMU8000_FM2FRQ2_WRITE(emu
, i
, 0);
456 sysex(snd_emux_t
*emu
, char *buf
, int len
, int parsed
, snd_midi_channel_set_t
*chset
)
463 case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE
:
464 hw
->chorus_mode
= chset
->gs_chorus_mode
;
465 snd_emu8000_update_chorus_mode(hw
);
468 case SNDRV_MIDI_SYSEX_GS_REVERB_MODE
:
469 hw
->reverb_mode
= chset
->gs_reverb_mode
;
470 snd_emu8000_update_reverb_mode(hw
);
476 #ifdef CONFIG_SND_SEQUENCER_OSS
481 oss_ioctl(snd_emux_t
*emu
, int cmd
, int p1
, int p2
)
488 case _EMUX_OSS_REVERB_MODE
:
489 hw
->reverb_mode
= p1
;
490 snd_emu8000_update_reverb_mode(hw
);
493 case _EMUX_OSS_CHORUS_MODE
:
494 hw
->chorus_mode
= p1
;
495 snd_emu8000_update_chorus_mode(hw
);
498 case _EMUX_OSS_INITIALIZE_CHIP
:
499 /* snd_emu8000_init(hw); */ /*ignored*/
502 case _EMUX_OSS_EQUALIZER
:
504 hw
->treble_level
= p2
;
505 snd_emu8000_update_equalizer(hw
);
514 * additional patch keys
517 #define SNDRV_EMU8000_LOAD_CHORUS_FX 0x10 /* optarg=mode */
518 #define SNDRV_EMU8000_LOAD_REVERB_FX 0x11 /* optarg=mode */
526 load_fx(snd_emux_t
*emu
, int type
, int mode
, const void __user
*buf
, long len
)
536 case SNDRV_EMU8000_LOAD_CHORUS_FX
:
537 return snd_emu8000_load_chorus_fx(hw
, mode
, buf
, len
);
538 case SNDRV_EMU8000_LOAD_REVERB_FX
:
539 return snd_emu8000_load_reverb_fx(hw
, mode
, buf
, len
);