summaryrefslogtreecommitdiffstats
path: root/drivers/sound/emu10k1/voicemgr.c
blob: de94a453ecfe276ae2efc80069e4b846a1f08825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

/*
 **********************************************************************
 *     voicemgr.c - Voice manager for emu10k1 driver
 *     Copyright 1999, 2000 Creative Labs, Inc.
 *
 **********************************************************************
 *
 *     Date                 Author          Summary of changes
 *     ----                 ------          ------------------
 *     October 20, 1999     Bertrand Lee    base code release
 *
 **********************************************************************
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this program; if not, write to the Free
 *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *     USA.
 *
 **********************************************************************
 */

#include "hwaccess.h"

struct emu_voice *emu10k1_voice_alloc(struct voice_manager *voicemgr, struct voice_allocdesc *voiceallocdesc)
{
	struct emu10k1_card *card = voicemgr->card;
	struct emu_voice *voice_tmp = voicemgr->voice;
	struct emu_voice *voice = NULL;
	int i;
	unsigned long flags;

	DPF(2, "emu10k1_voice_alloc()\n");

	spin_lock_irqsave(&voicemgr->lock, flags);

	if (voiceallocdesc->flags & VOICEMGR_FLAGS_MONO) {
		for (i = 0; i < NUM_G; i++)
			if (voice_tmp[i].usage == VOICEMGR_USAGE_FREE) {
				voice_tmp[i].flags = VOICEMGR_FLAGS_VOICEMASTER | voiceallocdesc->flags;
				voice_tmp[i].usage = voiceallocdesc->usage;
				voice = &voice_tmp[i];
				break;
			}
	} else {
		for (i = 0; i < NUM_G; i += 2)
			if ((voice_tmp[i].usage == VOICEMGR_USAGE_FREE)
			    && (voice_tmp[i + 1].usage == VOICEMGR_USAGE_FREE)) {
				voice_tmp[i].linked_voice = &voice_tmp[i + 1];
				voice_tmp[i].flags = VOICEMGR_FLAGS_VOICEMASTER | voiceallocdesc->flags;
				voice_tmp[i].usage = voiceallocdesc->usage;
				voice_tmp[i + 1].flags = VOICEMGR_FLAGS_STEREOSLAVE | voiceallocdesc->flags;
				voice_tmp[i + 1].usage = voiceallocdesc->usage;
				voice = &voice_tmp[i];
				break;
			}
	}

	spin_unlock_irqrestore(&voicemgr->lock, flags);

	voice_tmp = voice;

	while (voice_tmp != NULL) {

		DPD(2, " voice allocated -> %d\n", voice_tmp->num);

		sblive_writeptr(card, IFATN, voice_tmp->num, 0xffff);
		sblive_writeptr(card, DCYSUSV, voice_tmp->num, ENV_OFF);
		sblive_writeptr(card, VTFT, voice_tmp->num, 0xffff);
		sblive_writeptr(card, PTRX, voice_tmp->num, 0);

		voice_tmp = voice_tmp->linked_voice;
	}

	return voice;
}

void emu10k1_voice_free(struct voice_manager *voicemgr, struct emu_voice *voice)
{
	struct emu10k1_card *card = voice->card;
	struct emu_voice *voice_tmp;
	unsigned dcysusv;
	u32 cra, sample;
	int i;
	unsigned long flags;

	DPF(2, "emu10k1_voice_free()\n");

	voice_tmp = voice;

	while (voice_tmp != NULL) {

		DPD(2, " voice freed -> %d\n", voice_tmp->num);

		sblive_writeptr(card, IFATN, voice_tmp->num, IFATN_FILTERCUTOFF_MASK | IFATN_ATTENUATION_MASK);
		sblive_writeptr(card, IP, voice_tmp->num, 0);

		dcysusv = sblive_readptr(card, DCYSUSV, voice_tmp->num) & (DCYSUSV_PHASE1_MASK | DCYSUSV_SUSTAINLEVEL_MASK | DCYSUSV_DECAYTIME_MASK);
		sblive_writeptr(card, DCYSUSV, voice_tmp->num, dcysusv | ENV_OFF);

		sblive_writeptr(card, VTFT, voice_tmp->num, VTFT_FILTERTARGET_MASK);
		sblive_writeptr(card, PTRX_PITCHTARGET, voice_tmp->num, 0);
		sblive_writeptr(card, CVCF, voice_tmp->num, CVCF_CURRENTFILTER_MASK);
		sblive_writeptr(card, CPF, voice_tmp->num, 0);

		sample = (voice_tmp->flags & VOICEMGR_FLAGS_16BIT) ? 0 : 0x80808080;
		cra = sblive_readptr(card, CCR, voice_tmp->num) & CCR_READADDRESS_MASK;
		sblive_writeptr(card, CCR, voice_tmp->num, cra);
		cra = (cra >> 18) & 0xf;
		sblive_writeptr(card, CD0 + cra, voice_tmp->num, sample);
		cra = (cra + 0x1) & 0xf;
		sblive_writeptr(card, CD0 + cra, voice_tmp->num, sample);

		for (i = 0; i < NUM_FXSENDS; i++)
			voice_tmp->sendhandle[i] = 0;

		voice_tmp->flags = 0;

		spin_lock_irqsave(&voicemgr->lock, flags);
		voice_tmp->usage = VOICEMGR_USAGE_FREE;

		voice_tmp = voice_tmp->linked_voice;
		voice->linked_voice = NULL;
		spin_unlock_irqrestore(&voicemgr->lock, flags);
	}

	return;
}

/*       Sets up a voices for Wave Playback */

void emu10k1_voice_playback_setup(struct emu_voice *voice)
{
	struct emu10k1_card *card = voice->card;
	u32 sample, cra = 0, start = 0;

	DPF(2, "emu10k1_voice_playback_setup()\n");

	while (voice != NULL) {
		sblive_writeptr(card, DCYSUSV, voice->num, ENV_OFF);
		sblive_writeptr(card, VTFT, voice->num, VTFT_FILTERTARGET_MASK);
		sblive_writeptr(card, CVCF, voice->num, CVCF_CURRENTFILTER_MASK);
		sblive_writeptr(card, FXRT, voice->num, (voice->flags & VOICEMGR_FLAGS_FXRT2) ? 0xd23c0000 : 0xd01c0000);

		/* Stop CA */
		/* Assumption that PT is alreadt 0 so no harm overwriting */
		sblive_writeptr(card, PTRX, voice->num, (voice->params.send_a << 8) | voice->params.send_b);

		if (voice->flags & VOICEMGR_FLAGS_VOICEMASTER) {
			if (voice->linked_voice != NULL) {
				/* Set stereo bit */
				cra = 64;
				sblive_writeptr(card, CPF, voice->num, CPF_STEREO_MASK);
				sblive_writeptr(card, CPF, voice->num + 1, CPF_STEREO_MASK);
			} else {
				cra = 32;
				sblive_writeptr(card, CPF, voice->num, 0);
			}

			if (voice->flags & VOICEMGR_FLAGS_16BIT)
				sample = 0;
			else {
				cra = cra * 2;
				sample = 0x80808080;
			}
			cra -= 4;

			if (voice->linked_voice != NULL) {
				/* CCR_READADDRESS_MASK */
				sblive_writeptr(card, CCR, voice->num, 0x3c << 16);
				sblive_writeptr(card, CCR, voice->num + 1, cra << 16);
				sblive_writeptr(card, CDE, voice->num + 1, sample);
				sblive_writeptr(card, CDF, voice->num + 1, sample);
				start = voice->params.start + cra / 2;
			} else {
				sblive_writeptr(card, CCR, voice->num, 0x1c << 16);	/* FIXME: Is 0x1c correct? */
				sblive_writeptr(card, CDE, voice->num, sample);
				sblive_writeptr(card, CDF, voice->num, sample);
				start = voice->params.start + cra;
			}

			if (start > voice->params.endloop) {
				start -= voice->params.endloop;

				if (voice->linked_voice != NULL)
					cra = (cra << 25) | 0x1bc0000 | ((cra - start) << 9);
				else
					cra = (cra << 25) | 0x11c0000 | ((cra - start) << 9);

				start += voice->params.startloop;

				if (start >= voice->params.endloop)
					start = voice->params.endloop - 1;
			} else if (voice->linked_voice != NULL)
				cra = (cra << 25) | (0x3c << 16);
			else
				cra = (cra << 25) | (0x1c << 16);

			start |= CCCA_INTERPROM_0;
		}

		/* CSL, ST, CA */
		sblive_writeptr(card, DSL, voice->num, voice->params.endloop | (voice->params.send_d << 24));
		sblive_writeptr(card, PSST, voice->num, voice->params.startloop | (voice->params.send_c << 24));

		if (voice->flags & VOICEMGR_FLAGS_16BIT)
			sblive_writeptr(card, CCCA, voice->num, start);
		else
			sblive_writeptr(card, CCCA, voice->num, start | CCCA_8BITSELECT);

		/* Clear filter delay memory */
		sblive_writeptr(card, Z1, voice->num, 0);
		sblive_writeptr(card, Z2, voice->num, 0);

		/* Invalidate maps */
		sblive_writeptr(card, MAPA, voice->num, MAP_PTI_MASK | (card->silentpage->busaddx * 2));
		sblive_writeptr(card, MAPB, voice->num, MAP_PTI_MASK | (card->silentpage->busaddx * 2));

		/* Fill cache */
		if (voice->flags & VOICEMGR_FLAGS_VOICEMASTER)
			sblive_writeptr(card, CCR, voice->num, cra);

		sblive_writeptr(card, ATKHLDV, voice->num, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
		sblive_writeptr(card, LFOVAL1, voice->num, 0x8000);
		sblive_writeptr(card, ATKHLDM, voice->num, 0);
		sblive_writeptr(card, DCYSUSM, voice->num, DCYSUSM_DECAYTIME_MASK);
		sblive_writeptr(card, LFOVAL2, voice->num, 0x8000);
		sblive_writeptr(card, IP, voice->num, voice->params.initial_pitch);
		sblive_writeptr(card, PEFE, voice->num, 0x7f);
		sblive_writeptr(card, FMMOD, voice->num, 0);
		sblive_writeptr(card, TREMFRQ, voice->num, 0);
		sblive_writeptr(card, FM2FRQ2, voice->num, 0);
		sblive_writeptr(card, ENVVAL, voice->num, 0xbfff);
		sblive_writeptr(card, ENVVOL, voice->num, 0xbfff);

#ifdef PRIVATE_PCM_VOLUME
		{
			int i;

			for (i = 0; i < MAX_PCM_CHANNELS; i++) {
				if (sblive_pcm_volume[i].channel_l == voice->num) {
					voice->params.initial_attn = (sblive_pcm_volume[i].channel_r < NUM_G) ? sblive_pcm_volume[i].attn_l :
					    // test for mono channel (reverse logic is correct here!)
					    (sblive_pcm_volume[i].attn_r >
					     sblive_pcm_volume[i].attn_l) ? sblive_pcm_volume[i].attn_l : sblive_pcm_volume[i].attn_r;
					DPD(2, "set left volume  %d\n", voice->params.initial_attn);
					break;
				} else if (sblive_pcm_volume[i].channel_r == voice->num) {
					voice->params.initial_attn = sblive_pcm_volume[i].attn_r;
					DPD(2, "set right volume  %d\n", voice->params.initial_attn);
					break;
				}
			}
		}
#endif
		sblive_writeptr(card, IFATN, voice->num, IFATN_FILTERCUTOFF_MASK | voice->params.initial_attn);

		voice->params.FC_target = 0xffff;
		voice->params.pitch_target = (u16) (IP_TO_CP(voice->params.initial_pitch) >> 16);

		voice = voice->linked_voice;
	}

	return;
}

void emu10k1_voice_start(struct emu_voice *voice)
{
	struct emu10k1_card *card = voice->card;

	DPF(2, "emu10k1_voice_start()\n");

	while (voice != NULL) {
		sblive_writeptr(card, PTRX_PITCHTARGET, voice->num, voice->params.pitch_target);

		if (voice->flags & VOICEMGR_FLAGS_VOICEMASTER)
			sblive_writeptr(card, CPF_CURRENTPITCH, voice->num, voice->params.pitch_target);

		sblive_writeptr(card, VTFT, voice->num, ((u32) voice->params.volume_target << 16)
				| voice->params.FC_target);
		sblive_writeptr(card, CVCF, voice->num, ((u32) voice->params.volume_target << 16)
				| voice->params.FC_target);
		sblive_writeptr(card, DCYSUSV, voice->num, (voice->params.byampl_env_sustain << 8)
				| ENV_ON | voice->params.byampl_env_decay);

		/* Using StopOnLoop for MIDI stops the playback
		   too early, which may cause a DC level to be played
		   until the note is released. */

		if (voice->usage == VOICEMGR_USAGE_MIDI)
			emu10k1_clear_stop_on_loop(card, voice->num);
		else {
			if (voice->params.startloop > voice->params.end)
				emu10k1_set_stop_on_loop(card, voice->num);
			else
				emu10k1_clear_stop_on_loop(card, voice->num);
		}
		voice = voice->linked_voice;
	}

	return;
}

void emu10k1_voice_stop(struct emu_voice *voice)
{
	struct emu10k1_card *card = voice->card;

	DPF(2, "emu10k1_voice_stop()\n");

	while (voice != NULL) {
		sblive_writeptr(card, IFATN, voice->num, 0xffff);
		sblive_writeptr(card, IP, voice->num, 0);
		sblive_writeptr(card, VTFT, voice->num, 0xffff);
		sblive_writeptr(card, PTRX_PITCHTARGET, voice->num, 0);
		voice = voice->linked_voice;
	}

	return;
}

void emu10k1_voice_setcontrol(struct emu_voice *voice, struct voice_cntlset *setting, u32 numparam)
{
	struct emu10k1_card *card = voice->card;
	int count;

	for (count = 0; count < numparam; count++)
		sblive_writeptr(card, setting[count].paramID, voice->num, setting[count].value);

	return;
}

void emu10k1_voice_getcontrol(struct emu_voice *voice, u32 controlid, u32 * value)
{
	struct emu10k1_card *card = voice->card;

	*value = sblive_readptr(card, controlid, voice->num);

	return;
}