1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * synth callback routines for Emu10k1
5 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
8 #include <linux/export.h>
9 #include "emu10k1_synth_local.h"
10 #include <sound/asoundef.h>
14 V_FREE=0, V_OFF, V_RELEASED, V_PLAYING, V_END
17 /* Keeps track of what we are finding */
26 static void lookup_voices(struct snd_emux *emux, struct snd_emu10k1 *hw,
27 struct best_voice *best, int active_only);
28 static struct snd_emux_voice *get_voice(struct snd_emux *emux,
29 struct snd_emux_port *port);
30 static int start_voice(struct snd_emux_voice *vp);
31 static void trigger_voice(struct snd_emux_voice *vp);
32 static void release_voice(struct snd_emux_voice *vp);
33 static void update_voice(struct snd_emux_voice *vp, int update);
34 static void terminate_voice(struct snd_emux_voice *vp);
35 static void free_voice(struct snd_emux_voice *vp);
36 static u32 make_fmmod(struct snd_emux_voice *vp);
37 static u32 make_fm2frq2(struct snd_emux_voice *vp);
38 static int get_pitch_shift(struct snd_emux *emu);
41 * Ensure a value is between two points
42 * macro evaluates its args more than once, so changed to upper-case.
44 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
45 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
51 static const struct snd_emux_operators emu10k1_ops = {
53 .get_voice = get_voice,
54 .prepare = start_voice,
55 .trigger = trigger_voice,
56 .release = release_voice,
57 .update = update_voice,
58 .terminate = terminate_voice,
59 .free_voice = free_voice,
60 .sample_new = snd_emu10k1_sample_new,
61 .sample_free = snd_emu10k1_sample_free,
62 .get_pitch_shift = get_pitch_shift,
66 snd_emu10k1_ops_setup(struct snd_emux *emux)
68 emux->ops = emu10k1_ops;
73 * get more voice for pcm
75 * terminate most inactive voice and give it as a pcm voice.
77 * voice_lock is already held.
80 snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
83 struct snd_emux_voice *vp;
84 struct best_voice best[V_END];
89 lookup_voices(emu, hw, best, 1); /* no OFF voices */
90 for (i = 0; i < V_END; i++) {
91 if (best[i].voice >= 0) {
93 vp = &emu->voices[best[i].voice];
97 dev_warn(emu->card->dev,
98 "synth_get_voice: ch < 0 (%d) ??", i);
102 vp->emu->num_voices--;
104 vp->state = SNDRV_EMUX_ST_OFF;
115 * turn off the voice (not terminated)
118 release_voice(struct snd_emux_voice *vp)
120 struct snd_emu10k1 *hw;
123 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
124 DCYSUSM, (unsigned char)vp->reg.parm.modrelease | DCYSUSM_PHASE1_MASK,
125 DCYSUSV, (unsigned char)vp->reg.parm.volrelease | DCYSUSV_PHASE1_MASK | DCYSUSV_CHANNELENABLE_MASK,
131 * terminate the voice
134 terminate_voice(struct snd_emux_voice *vp)
136 struct snd_emu10k1 *hw;
141 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
143 VTFT, VTFT_FILTERTARGET_MASK,
144 CVCF, CVCF_CURRENTFILTER_MASK,
149 struct snd_emu10k1_memblk *emem;
150 emem = (struct snd_emu10k1_memblk *)vp->block;
151 if (emem->map_locked > 0)
157 * release the voice to system
160 free_voice(struct snd_emux_voice *vp)
162 struct snd_emu10k1 *hw;
165 /* FIXME: emu10k1_synth is broken. */
166 /* This can get called with hw == 0 */
167 /* Problem apparent on plug, unplug then plug */
168 /* on the Audigy 2 ZS Notebook. */
169 if (hw && (vp->ch >= 0)) {
170 snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
171 vp->emu->num_voices--;
181 update_voice(struct snd_emux_voice *vp, int update)
183 struct snd_emu10k1 *hw;
186 if (update & SNDRV_EMUX_UPDATE_VOLUME)
187 snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
188 if (update & SNDRV_EMUX_UPDATE_PITCH)
189 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
190 if (update & SNDRV_EMUX_UPDATE_PAN) {
191 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
192 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
194 if (update & SNDRV_EMUX_UPDATE_FMMOD)
195 snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, make_fmmod(vp));
196 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
197 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
198 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
199 snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, make_fm2frq2(vp));
200 if (update & SNDRV_EMUX_UPDATE_Q)
201 snd_emu10k1_ptr_write(hw, CCCA_RESONANCE, vp->ch, vp->reg.parm.filterQ);
206 * look up voice table - get the best voice in order of preference
210 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
211 struct best_voice *best, int active_only)
213 struct snd_emux_voice *vp;
214 struct best_voice *bp;
217 for (i = 0; i < V_END; i++) {
218 best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */
223 * Go through them all and get a best one to use.
224 * NOTE: could also look at volume and pick the quietest one.
226 for (i = 0; i < emu->max_voices; i++) {
229 vp = &emu->voices[i];
231 if (state == SNDRV_EMUX_ST_OFF) {
239 else if (state == SNDRV_EMUX_ST_RELEASED ||
240 state == SNDRV_EMUX_ST_PENDING) {
241 bp = best + V_RELEASED;
243 val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
248 else if (state == SNDRV_EMUX_ST_STANDBY)
250 else if (state & SNDRV_EMUX_ST_ON)
251 bp = best + V_PLAYING;
255 /* check if sample is finished playing (non-looping only) */
256 if (bp != best + V_OFF && bp != best + V_FREE &&
257 (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
258 val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch) - 64;
259 if (val >= vp->reg.loopstart)
263 if (vp->time < bp->time) {
273 * emu->voice_lock is already held.
275 static struct snd_emux_voice *
276 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
278 struct snd_emu10k1 *hw;
279 struct snd_emux_voice *vp;
280 struct best_voice best[V_END];
285 lookup_voices(emu, hw, best, 0);
286 for (i = 0; i < V_END; i++) {
287 if (best[i].voice >= 0) {
288 vp = &emu->voices[best[i].voice];
290 /* allocate a voice */
291 struct snd_emu10k1_voice *hwvoice;
292 if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, 1, NULL, &hwvoice) < 0)
294 vp->ch = hwvoice->number;
306 * prepare envelopes and LFOs
309 start_voice(struct snd_emux_voice *vp)
313 u32 psst, dsl, map, ccca, vtarget;
314 unsigned int addr, mapped_offset;
315 struct snd_midi_channel *chan;
316 struct snd_emu10k1 *hw;
317 struct snd_emu10k1_memblk *emem;
321 if (snd_BUG_ON(ch < 0))
325 emem = (struct snd_emu10k1_memblk *)vp->block;
329 if (snd_emu10k1_memblk_map(hw, emem) < 0) {
330 /* dev_err(hw->card->devK, "emu: cannot map!\n"); */
333 mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
334 vp->reg.start += mapped_offset;
335 vp->reg.end += mapped_offset;
336 vp->reg.loopstart += mapped_offset;
337 vp->reg.loopend += mapped_offset;
339 /* set channel routing */
340 /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
342 temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
343 (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
344 snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
346 temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
347 (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
348 snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
351 temp = vp->reg.parm.reverb;
352 temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
354 addr = vp->reg.loopstart;
355 psst = (temp << 24) | addr;
357 addr = vp->reg.loopend;
358 temp = vp->reg.parm.chorus;
359 temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
361 dsl = (temp << 24) | addr;
363 map = (hw->silent_page.addr << hw->address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
365 addr = vp->reg.start + 64;
366 temp = vp->reg.parm.filterQ;
367 ccca = (temp << 28) | addr;
368 if (vp->apitch < 0xe400)
369 ccca |= CCCA_INTERPROM_0;
371 unsigned int shift = (vp->apitch - 0xe000) >> 10;
374 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
375 ccca |= CCCA_8BITSELECT;
377 vtarget = (unsigned int)vp->vtarget << 16;
379 snd_emu10k1_ptr_write_multiple(hw, ch,
380 /* channel to be silent and idle */
382 VTFT, VTFT_FILTERTARGET_MASK,
383 CVCF, CVCF_CURRENTFILTER_MASK,
387 /* set pitch offset */
390 /* set envelope parameters */
391 ENVVAL, vp->reg.parm.moddelay,
392 ATKHLDM, vp->reg.parm.modatkhld,
393 DCYSUSM, vp->reg.parm.moddcysus,
394 ENVVOL, vp->reg.parm.voldelay,
395 ATKHLDV, vp->reg.parm.volatkhld,
396 /* decay/sustain parameter for volume envelope is used
397 for triggerg the voice */
399 /* cutoff and volume */
400 IFATN, (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol,
402 /* modulation envelope heights */
403 PEFE, vp->reg.parm.pefe,
406 LFOVAL1, vp->reg.parm.lfo1delay,
407 LFOVAL2, vp->reg.parm.lfo2delay,
409 /* lfo1 pitch & cutoff shift */
410 FMMOD, make_fmmod(vp),
411 /* lfo1 volume & freq */
412 TREMFRQ, vp->reg.parm.tremfrq,
413 /* lfo2 pitch & freq */
414 FM2FRQ2, make_fm2frq2(vp),
416 /* reverb and loop start (reverb 8bit, MSB) */
419 /* chorus & loop end (chorus 8bit, MSB) */
422 /* clear filter delay memory */
426 /* invalidate maps */
430 /* Q & current address (Q 4bit value, MSB) */
434 CCR, REG_VAL_PUT(CCR_CACHEINVALIDSIZE, 64),
437 VTFT, vtarget | vp->ftarget,
438 CVCF, vtarget | CVCF_CURRENTFILTER_MASK,
442 hw->voices[ch].dirty = 1;
450 trigger_voice(struct snd_emux_voice *vp)
452 unsigned int ptarget;
453 struct snd_emu10k1 *hw;
454 struct snd_emu10k1_memblk *emem;
458 emem = (struct snd_emu10k1_memblk *)vp->block;
459 if (! emem || emem->mapped_page < 0)
460 return; /* not mapped */
463 ptarget = (unsigned int)vp->ptarget << 16;
465 ptarget = IP_TO_CP(vp->apitch);
467 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
468 /* set pitch target and pan (volume) */
469 PTRX, ptarget | (vp->apan << 8) | vp->aaux,
471 /* current pitch and fractional address */
474 /* enable envelope engine */
475 DCYSUSV, vp->reg.parm.voldcysus | DCYSUSV_CHANNELENABLE_MASK,
482 /* calculate lfo1 modulation height and cutoff register */
484 make_fmmod(struct snd_emux_voice *vp)
487 unsigned char cutoff;
490 pitch = (char)(vp->reg.parm.fmmod>>8);
491 cutoff = (vp->reg.parm.fmmod & 0xff);
492 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
493 pitch += (MOD_SENSE * modulation) / 1200;
494 LIMITVALUE(pitch, -128, 127);
495 return ((unsigned char)pitch << 8) | cutoff;
498 /* calculate set lfo2 pitch & frequency register */
500 make_fm2frq2(struct snd_emux_voice *vp)
506 pitch = (char)(vp->reg.parm.fm2frq2>>8);
507 freq = vp->reg.parm.fm2frq2 & 0xff;
508 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
509 pitch += (MOD_SENSE * modulation) / 1200;
510 LIMITVALUE(pitch, -128, 127);
511 return ((unsigned char)pitch << 8) | freq;
514 static int get_pitch_shift(struct snd_emux *emu)
516 struct snd_emu10k1 *hw = emu->hw;
518 return (hw->card_capabilities->emu_model &&
519 hw->emu1010.word_clock == 44100) ? 0 : -501;