]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - sound/isa/sb/emu8000_callback.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / sound / isa / sb / emu8000_callback.c
1 /*
2 * synth callback routines for the emu8000 (AWE32/64)
3 *
4 * Copyright (C) 1999 Steve Ratcliffe
5 * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "emu8000_local.h"
23 #include <sound/asoundef.h>
24
25 /*
26 * prototypes
27 */
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);
38 #endif
39 static int load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len);
40
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);
49
50 /*
51 * Ensure a value is between two points
52 * macro evaluates its args more than once, so changed to upper-case.
53 */
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)
56
57
58 /*
59 * set up operators
60 */
61 static snd_emux_operators_t emu8000_ops = {
62 .owner = THIS_MODULE,
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,
69 .reset = reset_voice,
70 .sample_new = snd_emu8000_sample_new,
71 .sample_free = snd_emu8000_sample_free,
72 .sample_reset = snd_emu8000_sample_reset,
73 .load_fx = load_fx,
74 .sysex = sysex,
75 #ifdef CONFIG_SND_SEQUENCER_OSS
76 .oss_ioctl = oss_ioctl,
77 #endif
78 };
79
80 void
81 snd_emu8000_ops_setup(emu8000_t *hw)
82 {
83 hw->emu->ops = emu8000_ops;
84 }
85
86
87
88 /*
89 * Terminate a voice
90 */
91 static void
92 release_voice(snd_emux_voice_t *vp)
93 {
94 int dcysusv;
95 emu8000_t *hw;
96
97 hw = vp->hw;
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);
102 }
103
104
105 /*
106 */
107 static void
108 terminate_voice(snd_emux_voice_t *vp)
109 {
110 emu8000_t *hw;
111
112 hw = vp->hw;
113 EMU8000_DCYSUSV_WRITE(hw, vp->ch, 0x807F);
114 }
115
116
117 /*
118 */
119 static void
120 update_voice(snd_emux_voice_t *vp, int update)
121 {
122 emu8000_t *hw;
123
124 hw = vp->hw;
125 if (update & SNDRV_EMUX_UPDATE_VOLUME)
126 set_volume(hw, vp);
127 if (update & SNDRV_EMUX_UPDATE_PITCH)
128 set_pitch(hw, vp);
129 if ((update & SNDRV_EMUX_UPDATE_PAN) &&
130 vp->port->ctrls[EMUX_MD_REALTIME_PAN])
131 set_pan(hw, vp);
132 if (update & SNDRV_EMUX_UPDATE_FMMOD)
133 set_fmmod(hw, vp);
134 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
135 set_tremfreq(hw, vp);
136 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
137 set_fm2frq2(hw, vp);
138 if (update & SNDRV_EMUX_UPDATE_Q)
139 set_filterQ(hw, vp);
140 }
141
142
143 /*
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.
148 *
149 * The channel index (vp->ch) must be initialized in this routine.
150 * In Emu8k, it is identical with the array index.
151 */
152 static snd_emux_voice_t *
153 get_voice(snd_emux_t *emu, snd_emux_port_t *port)
154 {
155 int i;
156 snd_emux_voice_t *vp;
157 emu8000_t *hw;
158
159 /* what we are looking for, in order of preference */
160 enum {
161 OFF=0, RELEASED, PLAYING, END
162 };
163
164 /* Keeps track of what we are finding */
165 struct best {
166 unsigned int time;
167 int voice;
168 } best[END];
169 struct best *bp;
170
171 hw = emu->hw;
172
173 for (i = 0; i < END; i++) {
174 best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
175 best[i].voice = -1;
176 }
177
178 /*
179 * Go through them all and get a best one to use.
180 */
181 for (i = 0; i < emu->max_voices; i++) {
182 int state, val;
183
184 vp = &emu->voices[i];
185 state = vp->state;
186
187 if (state == SNDRV_EMUX_ST_OFF)
188 bp = best + 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;
193 if (! val)
194 bp = best + OFF;
195 }
196 else if (state & SNDRV_EMUX_ST_ON)
197 bp = best + PLAYING;
198 else
199 continue;
200
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)
206 bp = best + OFF;
207 }
208
209 if (vp->time < bp->time) {
210 bp->time = vp->time;
211 bp->voice = i;
212 }
213 }
214
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;
219 return vp;
220 }
221 }
222
223 /* not found */
224 return NULL;
225 }
226
227 /*
228 */
229 static int
230 start_voice(snd_emux_voice_t *vp)
231 {
232 unsigned int temp;
233 int ch;
234 int addr;
235 snd_midi_channel_t *chan;
236 emu8000_t *hw;
237
238 hw = vp->hw;
239 ch = vp->ch;
240 chan = vp->chan;
241
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);
248
249 /* set pitch offset */
250 set_pitch(hw, vp);
251
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 */
260
261 /* cutoff and volume */
262 set_volume(hw, vp);
263
264 /* modulation envelope heights */
265 EMU8000_PEFE_WRITE(hw, ch, vp->reg.parm.pefe);
266
267 /* lfo1/2 delay */
268 EMU8000_LFO1VAL_WRITE(hw, ch, vp->reg.parm.lfo1delay);
269 EMU8000_LFO2VAL_WRITE(hw, ch, vp->reg.parm.lfo2delay);
270
271 /* lfo1 pitch & cutoff shift */
272 set_fmmod(hw, vp);
273 /* lfo1 volume & freq */
274 set_tremfreq(hw, vp);
275 /* lfo2 pitch & freq */
276 set_fm2frq2(hw, vp);
277 /* pan & loop start */
278 set_pan(hw, vp);
279
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;
284 LIMITMAX(temp, 255);
285 temp = (temp <<24) | (unsigned int)addr;
286 EMU8000_CSL_WRITE(hw, ch, temp);
287
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);
293
294 /* clear unknown registers */
295 EMU8000_00A0_WRITE(hw, ch, 0);
296 EMU8000_0080_WRITE(hw, ch, 0);
297
298 /* reset volume */
299 temp = vp->vtarget << 16;
300 EMU8000_VTFT_WRITE(hw, ch, temp | vp->ftarget);
301 EMU8000_CVCF_WRITE(hw, ch, temp | 0xff00);
302
303 return 0;
304 }
305
306 /*
307 * Start envelope
308 */
309 static void
310 trigger_voice(snd_emux_voice_t *vp)
311 {
312 int ch = vp->ch;
313 unsigned int temp;
314 emu8000_t *hw;
315
316 hw = vp->hw;
317
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;
321 LIMITMAX(temp, 255);
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);
326 }
327
328 /*
329 * reset voice parameters
330 */
331 static void
332 reset_voice(snd_emux_t *emu, int ch)
333 {
334 emu8000_t *hw;
335
336 hw = emu->hw;
337 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
338 snd_emu8000_tweak_voice(hw, ch);
339 }
340
341 /*
342 * Set the pitch of a possibly playing note.
343 */
344 static void
345 set_pitch(emu8000_t *hw, snd_emux_voice_t *vp)
346 {
347 EMU8000_IP_WRITE(hw, vp->ch, vp->apitch);
348 }
349
350 /*
351 * Set the volume of a possibly already playing note
352 */
353 static void
354 set_volume(emu8000_t *hw, snd_emux_voice_t *vp)
355 {
356 int ifatn;
357
358 ifatn = (unsigned char)vp->acutoff;
359 ifatn = (ifatn << 8);
360 ifatn |= (unsigned char)vp->avol;
361 EMU8000_IFATN_WRITE(hw, vp->ch, ifatn);
362 }
363
364 /*
365 * Set pan and loop start address.
366 */
367 static void
368 set_pan(emu8000_t *hw, snd_emux_voice_t *vp)
369 {
370 unsigned int temp;
371
372 temp = ((unsigned int)vp->apan<<24) | ((unsigned int)vp->reg.loopstart - 1);
373 EMU8000_PSST_WRITE(hw, vp->ch, temp);
374 }
375
376 #define MOD_SENSE 18
377
378 static void
379 set_fmmod(emu8000_t *hw, snd_emux_voice_t *vp)
380 {
381 unsigned short fmmod;
382 short pitch;
383 unsigned char cutoff;
384 int modulation;
385
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);
393 }
394
395 /* set tremolo (lfo1) volume & frequency */
396 static void
397 set_tremfreq(emu8000_t *hw, snd_emux_voice_t *vp)
398 {
399 EMU8000_TREMFRQ_WRITE(hw, vp->ch, vp->reg.parm.tremfrq);
400 }
401
402 /* set lfo2 pitch & frequency */
403 static void
404 set_fm2frq2(emu8000_t *hw, snd_emux_voice_t *vp)
405 {
406 unsigned short fm2frq2;
407 short pitch;
408 unsigned char freq;
409 int modulation;
410
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);
418 }
419
420 /* set filterQ */
421 static void
422 set_filterQ(emu8000_t *hw, snd_emux_voice_t *vp)
423 {
424 unsigned int addr;
425 addr = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
426 addr |= (vp->reg.parm.filterQ << 28);
427 EMU8000_CCCA_WRITE(hw, vp->ch, addr);
428 }
429
430 /*
431 * set the envelope & LFO parameters to the default values
432 */
433 static void
434 snd_emu8000_tweak_voice(emu8000_t *emu, int i)
435 {
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);
450 }
451
452 /*
453 * sysex callback
454 */
455 static void
456 sysex(snd_emux_t *emu, char *buf, int len, int parsed, snd_midi_channel_set_t *chset)
457 {
458 emu8000_t *hw;
459
460 hw = emu->hw;
461
462 switch (parsed) {
463 case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE:
464 hw->chorus_mode = chset->gs_chorus_mode;
465 snd_emu8000_update_chorus_mode(hw);
466 break;
467
468 case SNDRV_MIDI_SYSEX_GS_REVERB_MODE:
469 hw->reverb_mode = chset->gs_reverb_mode;
470 snd_emu8000_update_reverb_mode(hw);
471 break;
472 }
473 }
474
475
476 #ifdef CONFIG_SND_SEQUENCER_OSS
477 /*
478 * OSS ioctl callback
479 */
480 static int
481 oss_ioctl(snd_emux_t *emu, int cmd, int p1, int p2)
482 {
483 emu8000_t *hw;
484
485 hw = emu->hw;
486
487 switch (cmd) {
488 case _EMUX_OSS_REVERB_MODE:
489 hw->reverb_mode = p1;
490 snd_emu8000_update_reverb_mode(hw);
491 break;
492
493 case _EMUX_OSS_CHORUS_MODE:
494 hw->chorus_mode = p1;
495 snd_emu8000_update_chorus_mode(hw);
496 break;
497
498 case _EMUX_OSS_INITIALIZE_CHIP:
499 /* snd_emu8000_init(hw); */ /*ignored*/
500 break;
501
502 case _EMUX_OSS_EQUALIZER:
503 hw->bass_level = p1;
504 hw->treble_level = p2;
505 snd_emu8000_update_equalizer(hw);
506 break;
507 }
508 return 0;
509 }
510 #endif
511
512
513 /*
514 * additional patch keys
515 */
516
517 #define SNDRV_EMU8000_LOAD_CHORUS_FX 0x10 /* optarg=mode */
518 #define SNDRV_EMU8000_LOAD_REVERB_FX 0x11 /* optarg=mode */
519
520
521 /*
522 * callback routine
523 */
524
525 static int
526 load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len)
527 {
528 emu8000_t *hw;
529 hw = emu->hw;
530
531 /* skip header */
532 buf += 16;
533 len -= 16;
534
535 switch (type) {
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);
540 }
541 return -EINVAL;
542 }
543