1/*
2 *  Midi synth routines for the Emu8k/Emu10k1
3 *
4 *  Copyright (C) 1999 Steve Ratcliffe
5 *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
6 *
7 *  Contains code based on awe_wave.c by Takashi Iwai
8 *
9 *   This program is free software; you can redistribute it and/or modify
10 *   it under the terms of the GNU General Public License as published by
11 *   the Free Software Foundation; either version 2 of the License, or
12 *   (at your option) any later version.
13 *
14 *   This program is distributed in the hope that it will be useful,
15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *   GNU General Public License for more details.
18 *
19 *   You should have received a copy of the GNU General Public License
20 *   along with this program; if not, write to the Free Software
21 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22 *
23 */
24
25#include "emux_voice.h"
26#include <sound/asoundef.h>
27
28/*
29 * Prototypes
30 */
31
32/*
33 * Ensure a value is between two points
34 * macro evaluates its args more than once, so changed to upper-case.
35 */
36#define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
37#define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
38
39static int get_zone(struct snd_emux *emu, struct snd_emux_port *port,
40		    int *notep, int vel, struct snd_midi_channel *chan,
41		    struct snd_sf_zone **table);
42static int get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan);
43static void terminate_note1(struct snd_emux *emu, int note,
44			    struct snd_midi_channel *chan, int free);
45static void exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port,
46			       int exclass);
47static void terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free);
48static void update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update);
49static void setup_voice(struct snd_emux_voice *vp);
50static int calc_pan(struct snd_emux_voice *vp);
51static int calc_volume(struct snd_emux_voice *vp);
52static int calc_pitch(struct snd_emux_voice *vp);
53
54
55/*
56 * Start a note.
57 */
58void
59snd_emux_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
60{
61	struct snd_emux *emu;
62	int i, key, nvoices;
63	struct snd_emux_voice *vp;
64	struct snd_sf_zone *table[SNDRV_EMUX_MAX_MULTI_VOICES];
65	unsigned long flags;
66	struct snd_emux_port *port;
67
68	port = p;
69	snd_assert(port != NULL && chan != NULL, return);
70
71	emu = port->emu;
72	snd_assert(emu != NULL, return);
73	snd_assert(emu->ops.get_voice != NULL, return);
74	snd_assert(emu->ops.trigger != NULL, return);
75
76	key = note; /* remember the original note */
77	nvoices = get_zone(emu, port, &note, vel, chan, table);
78	if (! nvoices)
79		return;
80
81	/* exclusive note off */
82	for (i = 0; i < nvoices; i++) {
83		struct snd_sf_zone *zp = table[i];
84		if (zp && zp->v.exclusiveClass)
85			exclusive_note_off(emu, port, zp->v.exclusiveClass);
86	}
87
88
89	spin_lock_irqsave(&emu->voice_lock, flags);
90	for (i = 0; i < nvoices; i++) {
91
92		/* set up each voice parameter */
93		/* at this stage, we don't trigger the voice yet. */
94
95		if (table[i] == NULL)
96			continue;
97
98		vp = emu->ops.get_voice(emu, port);
99		if (vp == NULL || vp->ch < 0)
100			continue;
101		if (STATE_IS_PLAYING(vp->state))
102			emu->ops.terminate(vp);
103
104		vp->time = emu->use_time++;
105		vp->chan = chan;
106		vp->port = port;
107		vp->key = key;
108		vp->note = note;
109		vp->velocity = vel;
110		vp->zone = table[i];
111		if (vp->zone->sample)
112			vp->block = vp->zone->sample->block;
113		else
114			vp->block = NULL;
115
116		setup_voice(vp);
117
118		vp->state = SNDRV_EMUX_ST_STANDBY;
119		if (emu->ops.prepare) {
120			vp->state = SNDRV_EMUX_ST_OFF;
121			if (emu->ops.prepare(vp) >= 0)
122				vp->state = SNDRV_EMUX_ST_STANDBY;
123		}
124	}
125
126	/* start envelope now */
127	for (i = 0; i < emu->max_voices; i++) {
128		vp = &emu->voices[i];
129		if (vp->state == SNDRV_EMUX_ST_STANDBY &&
130		    vp->chan == chan) {
131			emu->ops.trigger(vp);
132			vp->state = SNDRV_EMUX_ST_ON;
133			vp->ontime = jiffies; /* remember the trigger timing */
134		}
135	}
136	spin_unlock_irqrestore(&emu->voice_lock, flags);
137
138#ifdef SNDRV_EMUX_USE_RAW_EFFECT
139	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
140		/* clear voice position for the next note on this channel */
141		struct snd_emux_effect_table *fx = chan->private;
142		if (fx) {
143			fx->flag[EMUX_FX_SAMPLE_START] = 0;
144			fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
145		}
146	}
147#endif
148}
149
150/*
151 * Release a note in response to a midi note off.
152 */
153void
154snd_emux_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
155{
156	int ch;
157	struct snd_emux *emu;
158	struct snd_emux_voice *vp;
159	unsigned long flags;
160	struct snd_emux_port *port;
161
162	port = p;
163	snd_assert(port != NULL && chan != NULL, return);
164
165	emu = port->emu;
166	snd_assert(emu != NULL, return);
167	snd_assert(emu->ops.release != NULL, return);
168
169	spin_lock_irqsave(&emu->voice_lock, flags);
170	for (ch = 0; ch < emu->max_voices; ch++) {
171		vp = &emu->voices[ch];
172		if (STATE_IS_PLAYING(vp->state) &&
173		    vp->chan == chan && vp->key == note) {
174			vp->state = SNDRV_EMUX_ST_RELEASED;
175			if (vp->ontime == jiffies) {
176				/* if note-off is sent too shortly after
177				 * note-on, emuX engine cannot produce the sound
178				 * correctly.  so we'll release this note
179				 * a bit later via timer callback.
180				 */
181				vp->state = SNDRV_EMUX_ST_PENDING;
182				if (! emu->timer_active) {
183					emu->tlist.expires = jiffies + 1;
184					add_timer(&emu->tlist);
185					emu->timer_active = 1;
186				}
187			} else
188				/* ok now release the note */
189				emu->ops.release(vp);
190		}
191	}
192	spin_unlock_irqrestore(&emu->voice_lock, flags);
193}
194
195/*
196 * timer callback
197 *
198 * release the pending note-offs
199 */
200void snd_emux_timer_callback(unsigned long data)
201{
202	struct snd_emux *emu = (struct snd_emux *) data;
203	struct snd_emux_voice *vp;
204	unsigned long flags;
205	int ch, do_again = 0;
206
207	spin_lock_irqsave(&emu->voice_lock, flags);
208	for (ch = 0; ch < emu->max_voices; ch++) {
209		vp = &emu->voices[ch];
210		if (vp->state == SNDRV_EMUX_ST_PENDING) {
211			if (vp->ontime == jiffies)
212				do_again++; /* release this at the next interrupt */
213			else {
214				emu->ops.release(vp);
215				vp->state = SNDRV_EMUX_ST_RELEASED;
216			}
217		}
218	}
219	if (do_again) {
220		emu->tlist.expires = jiffies + 1;
221		add_timer(&emu->tlist);
222		emu->timer_active = 1;
223	} else
224		emu->timer_active = 0;
225	spin_unlock_irqrestore(&emu->voice_lock, flags);
226}
227
228/*
229 * key pressure change
230 */
231void
232snd_emux_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
233{
234	int ch;
235	struct snd_emux *emu;
236	struct snd_emux_voice *vp;
237	unsigned long flags;
238	struct snd_emux_port *port;
239
240	port = p;
241	snd_assert(port != NULL && chan != NULL, return);
242
243	emu = port->emu;
244	snd_assert(emu != NULL, return);
245	snd_assert(emu->ops.update != NULL, return);
246
247	spin_lock_irqsave(&emu->voice_lock, flags);
248	for (ch = 0; ch < emu->max_voices; ch++) {
249		vp = &emu->voices[ch];
250		if (vp->state == SNDRV_EMUX_ST_ON &&
251		    vp->chan == chan && vp->key == note) {
252			vp->velocity = vel;
253			update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
254		}
255	}
256	spin_unlock_irqrestore(&emu->voice_lock, flags);
257}
258
259
260/*
261 * Modulate the voices which belong to the channel
262 */
263void
264snd_emux_update_channel(struct snd_emux_port *port, struct snd_midi_channel *chan, int update)
265{
266	struct snd_emux *emu;
267	struct snd_emux_voice *vp;
268	int i;
269	unsigned long flags;
270
271	if (! update)
272		return;
273
274	emu = port->emu;
275	snd_assert(emu != NULL, return);
276	snd_assert(emu->ops.update != NULL, return);
277
278	spin_lock_irqsave(&emu->voice_lock, flags);
279	for (i = 0; i < emu->max_voices; i++) {
280		vp = &emu->voices[i];
281		if (vp->chan == chan)
282			update_voice(emu, vp, update);
283	}
284	spin_unlock_irqrestore(&emu->voice_lock, flags);
285}
286
287/*
288 * Modulate all the voices which belong to the port.
289 */
290void
291snd_emux_update_port(struct snd_emux_port *port, int update)
292{
293	struct snd_emux *emu;
294	struct snd_emux_voice *vp;
295	int i;
296	unsigned long flags;
297
298	if (! update)
299		return;
300
301	emu = port->emu;
302	snd_assert(emu != NULL, return);
303	snd_assert(emu->ops.update != NULL, return);
304
305	spin_lock_irqsave(&emu->voice_lock, flags);
306	for (i = 0; i < emu->max_voices; i++) {
307		vp = &emu->voices[i];
308		if (vp->port == port)
309			update_voice(emu, vp, update);
310	}
311	spin_unlock_irqrestore(&emu->voice_lock, flags);
312}
313
314
315/*
316 * Deal with a controler type event.  This includes all types of
317 * control events, not just the midi controllers
318 */
319void
320snd_emux_control(void *p, int type, struct snd_midi_channel *chan)
321{
322	struct snd_emux_port *port;
323
324	port = p;
325	snd_assert(port != NULL && chan != NULL, return);
326
327	switch (type) {
328	case MIDI_CTL_MSB_MAIN_VOLUME:
329	case MIDI_CTL_MSB_EXPRESSION:
330		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
331		break;
332
333	case MIDI_CTL_MSB_PAN:
334		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
335		break;
336
337	case MIDI_CTL_SOFT_PEDAL:
338#ifdef SNDRV_EMUX_USE_RAW_EFFECT
339		snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
340				     EMUX_FX_FLAG_ADD);
341#endif
342		break;
343
344	case MIDI_CTL_PITCHBEND:
345		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
346		break;
347
348	case MIDI_CTL_MSB_MODWHEEL:
349	case MIDI_CTL_CHAN_PRESSURE:
350		snd_emux_update_channel(port, chan,
351					SNDRV_EMUX_UPDATE_FMMOD |
352					SNDRV_EMUX_UPDATE_FM2FRQ2);
353		break;
354
355	}
356
357	if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
358		snd_emux_xg_control(port, chan, type);
359	}
360}
361
362
363/*
364 * terminate note - if free flag is true, free the terminated voice
365 */
366static void
367terminate_note1(struct snd_emux *emu, int note, struct snd_midi_channel *chan, int free)
368{
369	int  i;
370	struct snd_emux_voice *vp;
371	unsigned long flags;
372
373	spin_lock_irqsave(&emu->voice_lock, flags);
374	for (i = 0; i < emu->max_voices; i++) {
375		vp = &emu->voices[i];
376		if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
377		    vp->key == note)
378			terminate_voice(emu, vp, free);
379	}
380	spin_unlock_irqrestore(&emu->voice_lock, flags);
381}
382
383
384/*
385 * terminate note - exported for midi emulation
386 */
387void
388snd_emux_terminate_note(void *p, int note, struct snd_midi_channel *chan)
389{
390	struct snd_emux *emu;
391	struct snd_emux_port *port;
392
393	port = p;
394	snd_assert(port != NULL && chan != NULL, return);
395
396	emu = port->emu;
397	snd_assert(emu != NULL, return);
398	snd_assert(emu->ops.terminate != NULL, return);
399
400	terminate_note1(emu, note, chan, 1);
401}
402
403
404/*
405 * Terminate all the notes
406 */
407void
408snd_emux_terminate_all(struct snd_emux *emu)
409{
410	int i;
411	struct snd_emux_voice *vp;
412	unsigned long flags;
413
414	spin_lock_irqsave(&emu->voice_lock, flags);
415	for (i = 0; i < emu->max_voices; i++) {
416		vp = &emu->voices[i];
417		if (STATE_IS_PLAYING(vp->state))
418			terminate_voice(emu, vp, 0);
419		if (vp->state == SNDRV_EMUX_ST_OFF) {
420			if (emu->ops.free_voice)
421				emu->ops.free_voice(vp);
422			if (emu->ops.reset)
423				emu->ops.reset(emu, i);
424		}
425		vp->time = 0;
426	}
427	/* initialize allocation time */
428	emu->use_time = 0;
429	spin_unlock_irqrestore(&emu->voice_lock, flags);
430}
431
432EXPORT_SYMBOL(snd_emux_terminate_all);
433
434/*
435 * Terminate all voices associated with the given port
436 */
437void
438snd_emux_sounds_off_all(struct snd_emux_port *port)
439{
440	int i;
441	struct snd_emux *emu;
442	struct snd_emux_voice *vp;
443	unsigned long flags;
444
445	snd_assert(port != NULL, return);
446	emu = port->emu;
447	snd_assert(emu != NULL, return);
448	snd_assert(emu->ops.terminate != NULL, return);
449
450	spin_lock_irqsave(&emu->voice_lock, flags);
451	for (i = 0; i < emu->max_voices; i++) {
452		vp = &emu->voices[i];
453		if (STATE_IS_PLAYING(vp->state) &&
454		    vp->port == port)
455			terminate_voice(emu, vp, 0);
456		if (vp->state == SNDRV_EMUX_ST_OFF) {
457			if (emu->ops.free_voice)
458				emu->ops.free_voice(vp);
459			if (emu->ops.reset)
460				emu->ops.reset(emu, i);
461		}
462	}
463	spin_unlock_irqrestore(&emu->voice_lock, flags);
464}
465
466
467/*
468 * Terminate all voices that have the same exclusive class.  This
469 * is mainly for drums.
470 */
471static void
472exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port, int exclass)
473{
474	struct snd_emux_voice *vp;
475	int  i;
476	unsigned long flags;
477
478	spin_lock_irqsave(&emu->voice_lock, flags);
479	for (i = 0; i < emu->max_voices; i++) {
480		vp = &emu->voices[i];
481		if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
482		    vp->reg.exclusiveClass == exclass) {
483			terminate_voice(emu, vp, 0);
484		}
485	}
486	spin_unlock_irqrestore(&emu->voice_lock, flags);
487}
488
489/*
490 * terminate a voice
491 * if free flag is true, call free_voice after termination
492 */
493static void
494terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free)
495{
496	emu->ops.terminate(vp);
497	vp->time = emu->use_time++;
498	vp->chan = NULL;
499	vp->port = NULL;
500	vp->zone = NULL;
501	vp->block = NULL;
502	vp->state = SNDRV_EMUX_ST_OFF;
503	if (free && emu->ops.free_voice)
504		emu->ops.free_voice(vp);
505}
506
507
508/*
509 * Modulate the voice
510 */
511static void
512update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update)
513{
514	if (!STATE_IS_PLAYING(vp->state))
515		return;
516
517	if (vp->chan == NULL || vp->port == NULL)
518		return;
519	if (update & SNDRV_EMUX_UPDATE_VOLUME)
520		calc_volume(vp);
521	if (update & SNDRV_EMUX_UPDATE_PITCH)
522		calc_pitch(vp);
523	if (update & SNDRV_EMUX_UPDATE_PAN) {
524		if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
525			return;
526	}
527	emu->ops.update(vp, update);
528}
529
530
531
532#define LO_BYTE(v)	((v) & 0xff)
533#define HI_BYTE(v)	(((v) >> 8) & 0xff)
534
535/*
536 * Sets up the voice structure by calculating some values that
537 * will be needed later.
538 */
539static void
540setup_voice(struct snd_emux_voice *vp)
541{
542	struct soundfont_voice_parm *parm;
543	int pitch;
544
545	/* copy the original register values */
546	vp->reg = vp->zone->v;
547
548#ifdef SNDRV_EMUX_USE_RAW_EFFECT
549	snd_emux_setup_effect(vp);
550#endif
551
552	/* reset status */
553	vp->apan = -1;
554	vp->avol = -1;
555	vp->apitch = -1;
556
557	calc_volume(vp);
558	calc_pitch(vp);
559	calc_pan(vp);
560
561	parm = &vp->reg.parm;
562
563	/* compute filter target and correct modulation parameters */
564	if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
565		parm->moddelay = 0xbfff;
566		pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
567		if (pitch > 0xffff)
568			pitch = 0xffff;
569		/* calculate filter target */
570		vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
571		LIMITVALUE(vp->ftarget, 0, 255);
572		vp->ftarget <<= 8;
573	} else {
574		vp->ftarget = parm->cutoff;
575		vp->ftarget <<= 8;
576		pitch = vp->apitch;
577	}
578
579	/* compute pitch target */
580	if (pitch != 0xffff) {
581		vp->ptarget = 1 << (pitch >> 12);
582		if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
583		if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
584		if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
585		vp->ptarget += (vp->ptarget >> 1);
586		if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
587	} else
588		vp->ptarget = 0xffff;
589
590	if (LO_BYTE(parm->modatkhld) >= 0x80) {
591		parm->modatkhld &= ~0xff;
592		parm->modatkhld |= 0x7f;
593	}
594
595	/* compute volume target and correct volume parameters */
596	vp->vtarget = 0;
597
598	if (LO_BYTE(parm->volatkhld) >= 0x80) {
599		parm->volatkhld &= ~0xff;
600		parm->volatkhld |= 0x7f;
601	}
602}
603
604/*
605 * calculate pitch parameter
606 */
607static unsigned char pan_volumes[256] = {
6080x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
6090x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
6100x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
6110x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
6120x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
6130xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
6140xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
6150xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
6160xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
6170xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
6180xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
6190xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
6200xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
6210xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
6220xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
6230xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
624};
625
626static int
627calc_pan(struct snd_emux_voice *vp)
628{
629	struct snd_midi_channel *chan = vp->chan;
630	int pan;
631
632	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
633	if (vp->reg.fixpan > 0)	/* 0-127 */
634		pan = 255 - (int)vp->reg.fixpan * 2;
635	else {
636		pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
637		if (vp->reg.pan >= 0) /* 0-127 */
638			pan += vp->reg.pan - 64;
639		pan = 127 - (int)pan * 2;
640	}
641	LIMITVALUE(pan, 0, 255);
642
643	if (vp->emu->linear_panning) {
644		/* assuming linear volume */
645		if (pan != vp->apan) {
646			vp->apan = pan;
647			if (pan == 0)
648				vp->aaux = 0xff;
649			else
650				vp->aaux = (-pan) & 0xff;
651			return 1;
652		} else
653			return 0;
654	} else {
655		/* using volume table */
656		if (vp->apan != (int)pan_volumes[pan]) {
657			vp->apan = pan_volumes[pan];
658			vp->aaux = pan_volumes[255 - pan];
659			return 1;
660		}
661		return 0;
662	}
663}
664
665
666/*
667 * calculate volume attenuation
668 *
669 * Voice volume is controlled by volume attenuation parameter.
670 * So volume becomes maximum when avol is 0 (no attenuation), and
671 * minimum when 255 (-96dB or silence).
672 */
673
674/* tables for volume->attenuation calculation */
675static unsigned char voltab1[128] = {
676   0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
677   0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
678   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
679   0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
680   0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
681   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
682   0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
683   0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
684   0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
685   0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
686   0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
687   0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
688   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
689};
690
691static unsigned char voltab2[128] = {
692   0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
693   0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
694   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
695   0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
696   0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
697   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
698   0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
699   0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
700   0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
701   0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
702   0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
703   0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
704   0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
705};
706
707static unsigned char expressiontab[128] = {
708   0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
709   0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
710   0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
711   0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
712   0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
713   0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
714   0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
715   0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
716   0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
717   0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
718   0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
719   0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
720   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
721};
722
723/*
724 * Magic to calculate the volume (actually attenuation) from all the
725 * voice and channels parameters.
726 */
727static int
728calc_volume(struct snd_emux_voice *vp)
729{
730	int vol;
731	int main_vol, expression_vol, master_vol;
732	struct snd_midi_channel *chan = vp->chan;
733	struct snd_emux_port *port = vp->port;
734
735	expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
736	LIMITMAX(vp->velocity, 127);
737	LIMITVALUE(expression_vol, 0, 127);
738	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
739		/* 0 - 127 */
740		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
741		vol = (vp->velocity * main_vol * expression_vol) / (127*127);
742		vol = vol * vp->reg.amplitude / 127;
743
744		LIMITVALUE(vol, 0, 127);
745
746		/* calc to attenuation */
747		vol = snd_sf_vol_table[vol];
748
749	} else {
750		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
751		LIMITVALUE(main_vol, 0, 127);
752
753		vol = voltab1[main_vol] + voltab2[vp->velocity];
754		vol = (vol * 8) / 3;
755		vol += vp->reg.attenuation;
756		vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
757	}
758
759	master_vol = port->chset.gs_master_volume;
760	LIMITVALUE(master_vol, 0, 127);
761	vol += snd_sf_vol_table[master_vol];
762	vol += port->volume_atten;
763
764#ifdef SNDRV_EMUX_USE_RAW_EFFECT
765	if (chan->private) {
766		struct snd_emux_effect_table *fx = chan->private;
767		vol += fx->val[EMUX_FX_ATTEN];
768	}
769#endif
770
771	LIMITVALUE(vol, 0, 255);
772	if (vp->avol == vol)
773		return 0; /* value unchanged */
774
775	vp->avol = vol;
776	if (!SF_IS_DRUM_BANK(get_bank(port, chan))
777	    && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
778		int atten;
779		if (vp->velocity < 70)
780			atten = 70;
781		else
782			atten = vp->velocity;
783		vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
784	} else {
785		vp->acutoff = vp->reg.parm.cutoff;
786	}
787
788	return 1; /* value changed */
789}
790
791/*
792 * calculate pitch offset
793 *
794 * 0xE000 is no pitch offset at 44100Hz sample.
795 * Every 4096 is one octave.
796 */
797
798static int
799calc_pitch(struct snd_emux_voice *vp)
800{
801	struct snd_midi_channel *chan = vp->chan;
802	int offset;
803
804	/* calculate offset */
805	if (vp->reg.fixkey >= 0) {
806		offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
807	} else {
808		offset = (vp->note - vp->reg.root) * 4096 / 12;
809	}
810	offset = (offset * vp->reg.scaleTuning) / 100;
811	offset += vp->reg.tune * 4096 / 1200;
812	if (chan->midi_pitchbend != 0) {
813		/* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
814		offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
815	}
816
817	/* tuning via RPN:
818	 *   coarse = -8192 to 8192 (100 cent per 128)
819	 *   fine = -8192 to 8192 (max=100cent)
820	 */
821	/* 4096 = 1200 cents in emu8000 parameter */
822	offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
823	offset += chan->gm_rpn_fine_tuning / 24;
824
825#ifdef SNDRV_EMUX_USE_RAW_EFFECT
826	/* add initial pitch correction */
827	if (chan->private) {
828		struct snd_emux_effect_table *fx = chan->private;
829		if (fx->flag[EMUX_FX_INIT_PITCH])
830			offset += fx->val[EMUX_FX_INIT_PITCH];
831	}
832#endif
833
834	/* 0xe000: root pitch */
835	offset += 0xe000 + vp->reg.rate_offset;
836	offset += vp->emu->pitch_shift;
837	LIMITVALUE(offset, 0, 0xffff);
838	if (offset == vp->apitch)
839		return 0; /* unchanged */
840	vp->apitch = offset;
841	return 1; /* value changed */
842}
843
844/*
845 * Get the bank number assigned to the channel
846 */
847static int
848get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan)
849{
850	int val;
851
852	switch (port->chset.midi_mode) {
853	case SNDRV_MIDI_MODE_XG:
854		val = chan->control[MIDI_CTL_MSB_BANK];
855		if (val == 127)
856			return 128; /* return drum bank */
857		return chan->control[MIDI_CTL_LSB_BANK];
858
859	case SNDRV_MIDI_MODE_GS:
860		if (chan->drum_channel)
861			return 128;
862		/* ignore LSB (bank map) */
863		return chan->control[MIDI_CTL_MSB_BANK];
864
865	default:
866		if (chan->drum_channel)
867			return 128;
868		return chan->control[MIDI_CTL_MSB_BANK];
869	}
870}
871
872
873/* Look for the zones matching with the given note and velocity.
874 * The resultant zones are stored on table.
875 */
876static int
877get_zone(struct snd_emux *emu, struct snd_emux_port *port,
878	 int *notep, int vel, struct snd_midi_channel *chan,
879	 struct snd_sf_zone **table)
880{
881	int preset, bank, def_preset, def_bank;
882
883	bank = get_bank(port, chan);
884	preset = chan->midi_program;
885
886	if (SF_IS_DRUM_BANK(bank)) {
887		def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
888		def_bank = bank;
889	} else {
890		def_preset = preset;
891		def_bank = port->ctrls[EMUX_MD_DEF_BANK];
892	}
893
894	return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
895					 def_preset, def_bank,
896					 table, SNDRV_EMUX_MAX_MULTI_VOICES);
897}
898
899/*
900 */
901void
902snd_emux_init_voices(struct snd_emux *emu)
903{
904	struct snd_emux_voice *vp;
905	int i;
906	unsigned long flags;
907
908	spin_lock_irqsave(&emu->voice_lock, flags);
909	for (i = 0; i < emu->max_voices; i++) {
910		vp = &emu->voices[i];
911		vp->ch = -1; /* not used */
912		vp->state = SNDRV_EMUX_ST_OFF;
913		vp->chan = NULL;
914		vp->port = NULL;
915		vp->time = 0;
916		vp->emu = emu;
917		vp->hw = emu->hw;
918	}
919	spin_unlock_irqrestore(&emu->voice_lock, flags);
920}
921
922/*
923 */
924void snd_emux_lock_voice(struct snd_emux *emu, int voice)
925{
926	unsigned long flags;
927
928	spin_lock_irqsave(&emu->voice_lock, flags);
929	if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
930		emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
931	else
932		snd_printk("invalid voice for lock %d (state = %x)\n",
933			   voice, emu->voices[voice].state);
934	spin_unlock_irqrestore(&emu->voice_lock, flags);
935}
936
937EXPORT_SYMBOL(snd_emux_lock_voice);
938
939/*
940 */
941void snd_emux_unlock_voice(struct snd_emux *emu, int voice)
942{
943	unsigned long flags;
944
945	spin_lock_irqsave(&emu->voice_lock, flags);
946	if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
947		emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
948	else
949		snd_printk("invalid voice for unlock %d (state = %x)\n",
950			   voice, emu->voices[voice].state);
951	spin_unlock_irqrestore(&emu->voice_lock, flags);
952}
953
954EXPORT_SYMBOL(snd_emux_unlock_voice);
955