Calculate gain steps once during a source mix
[openal-soft.git] / Alc / wave.c
blobf24fc867d3e58a9e78f91ce6674cdb14c52bf26b
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
31 typedef struct {
32 FILE *f;
33 long DataStart;
35 ALvoid *buffer;
36 ALuint size;
38 ALuint startTime;
39 ALuint64 baseTime;
41 volatile int killNow;
42 ALvoid *thread;
43 } wave_data;
46 static const ALCchar waveDevice[] = "Wave File Writer";
48 static const ALubyte SUBTYPE_PCM[] = {
49 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
50 0x00, 0x38, 0x9b, 0x71
52 static const ALubyte SUBTYPE_FLOAT[] = {
53 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
54 0x00, 0x38, 0x9b, 0x71
57 static const ALuint channel_masks[] = {
58 0, /* invalid */
59 0x4, /* Mono */
60 0x1 | 0x2, /* Stereo */
61 0, /* 3 channel */
62 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
63 0, /* 5 channel */
64 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
65 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
66 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
69 static ALuint WaveProc(ALvoid *ptr)
71 ALCdevice *pDevice = (ALCdevice*)ptr;
72 wave_data *data = (wave_data*)pDevice->ExtraData;
73 ALuint frameSize;
74 ALuint now, start;
75 ALuint64 avail, done;
76 size_t fs;
77 union {
78 short s;
79 char b[sizeof(short)];
80 } uSB;
81 const ALuint restTime = (ALuint64)pDevice->UpdateSize * 1000 /
82 pDevice->Frequency / 2;
84 uSB.s = 1;
85 frameSize = aluFrameSizeFromFormat(pDevice->Format);
87 done = 0;
88 start = data->startTime;
89 while(!data->killNow && pDevice->Connected)
91 now = timeGetTime();
93 avail = (ALuint64)(now-start) * pDevice->Frequency / 1000;
94 if(avail < done)
96 AL_PRINT("Timer wrapped\n");
97 aluHandleDisconnect(pDevice);
98 break;
100 if(avail-done < pDevice->UpdateSize)
102 Sleep(restTime);
103 continue;
106 while(avail-done >= pDevice->UpdateSize)
108 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
109 done += pDevice->UpdateSize;
111 if(uSB.b[0] != 1)
113 ALubyte *bytes = data->buffer;
114 ALuint i;
116 if(aluBytesFromFormat(pDevice->Format) == 1)
118 for(i = 0;i < data->size;i++)
119 fputc(bytes[i], data->f);
121 else if(aluBytesFromFormat(pDevice->Format) == 2)
123 for(i = 0;i < data->size;i++)
124 fputc(bytes[i^1], data->f);
126 else if(aluBytesFromFormat(pDevice->Format) == 4)
128 for(i = 0;i < data->size;i++)
129 fputc(bytes[i^3], data->f);
132 else
133 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
134 data->f);
135 if(ferror(data->f))
137 AL_PRINT("Error writing to file\n");
138 aluHandleDisconnect(pDevice);
139 break;
144 return 0;
147 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
149 wave_data *data;
150 const char *fname;
152 fname = GetConfigValue("wave", "file", "");
153 if(!fname[0])
154 return ALC_FALSE;
156 if(!deviceName)
157 deviceName = waveDevice;
158 else if(strcmp(deviceName, waveDevice) != 0)
159 return ALC_FALSE;
161 data = (wave_data*)calloc(1, sizeof(wave_data));
163 data->f = fopen(fname, "wb");
164 if(!data->f)
166 free(data);
167 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
168 return ALC_FALSE;
171 device->szDeviceName = strdup(deviceName);
172 device->ExtraData = data;
173 return ALC_TRUE;
176 static void wave_close_playback(ALCdevice *device)
178 wave_data *data = (wave_data*)device->ExtraData;
180 fclose(data->f);
181 free(data);
182 device->ExtraData = NULL;
185 static ALCboolean wave_reset_playback(ALCdevice *device)
187 wave_data *data = (wave_data*)device->ExtraData;
188 ALuint channels, bits, i;
189 size_t val;
191 fseek(data->f, 0, SEEK_SET);
192 clearerr(data->f);
194 bits = aluBytesFromFormat(device->Format) * 8;
195 channels = aluChannelsFromFormat(device->Format);
197 /* 7.1 max */
198 if(channels > 8)
200 if(bits == 8)
201 device->Format = AL_FORMAT_71CHN8;
202 else if(bits == 16)
203 device->Format = AL_FORMAT_71CHN16;
204 else
206 device->Format = AL_FORMAT_71CHN32;
207 bits = 32;
209 channels = 8;
212 fprintf(data->f, "RIFF");
213 fputc(0xFF, data->f); // 'RIFF' header len; filled in at close
214 fputc(0xFF, data->f);
215 fputc(0xFF, data->f);
216 fputc(0xFF, data->f);
218 fprintf(data->f, "WAVE");
220 fprintf(data->f, "fmt ");
221 fputc(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
222 fputc(0, data->f);
223 fputc(0, data->f);
224 fputc(0, data->f);
225 // 16-bit val, format type id (extensible: 0xFFFE)
226 fputc(0xFE, data->f);
227 fputc(0xFF, data->f);
228 // 16-bit val, channel count
229 fputc(channels&0xff, data->f);
230 fputc((channels>>8)&0xff, data->f);
231 // 32-bit val, frequency
232 fputc(device->Frequency&0xff, data->f);
233 fputc((device->Frequency>>8)&0xff, data->f);
234 fputc((device->Frequency>>16)&0xff, data->f);
235 fputc((device->Frequency>>24)&0xff, data->f);
236 // 32-bit val, bytes per second
237 i = device->Frequency * channels * bits / 8;
238 fputc(i&0xff, data->f);
239 fputc((i>>8)&0xff, data->f);
240 fputc((i>>16)&0xff, data->f);
241 fputc((i>>24)&0xff, data->f);
242 // 16-bit val, frame size
243 i = channels * bits / 8;
244 fputc(i&0xff, data->f);
245 fputc((i>>8)&0xff, data->f);
246 // 16-bit val, bits per sample
247 fputc(bits&0xff, data->f);
248 fputc((bits>>8)&0xff, data->f);
249 // 16-bit val, extra byte count
250 fputc(22, data->f);
251 fputc(0, data->f);
252 // 16-bit val, valid bits per sample
253 fputc(bits&0xff, data->f);
254 fputc((bits>>8)&0xff, data->f);
255 // 32-bit val, channel mask
256 i = channel_masks[channels];
257 fputc(i&0xff, data->f);
258 fputc((i>>8)&0xff, data->f);
259 fputc((i>>16)&0xff, data->f);
260 fputc((i>>24)&0xff, data->f);
261 // 16 byte GUID, sub-type format
262 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
264 fprintf(data->f, "data");
265 fputc(0xFF, data->f); // 'data' header len; filled in at close
266 fputc(0xFF, data->f);
267 fputc(0xFF, data->f);
268 fputc(0xFF, data->f);
270 if(ferror(data->f))
272 AL_PRINT("Error writing header: %s\n", strerror(errno));
273 return ALC_FALSE;
276 data->DataStart = ftell(data->f);
278 data->size = device->UpdateSize * channels * bits / 8;
279 data->buffer = malloc(data->size);
280 if(!data->buffer)
282 AL_PRINT("buffer malloc failed\n");
283 return ALC_FALSE;
286 SetDefaultWFXChannelOrder(device);
287 device->TimeRes = 1000000;
289 data->startTime = timeGetTime();
290 data->thread = StartThread(WaveProc, device);
291 if(data->thread == NULL)
293 free(data->buffer);
294 data->buffer = NULL;
295 return ALC_FALSE;
298 return ALC_TRUE;
301 static void wave_stop_playback(ALCdevice *device)
303 wave_data *data = (wave_data*)device->ExtraData;
304 ALuint dataLen;
305 ALuint ext;
306 long size;
308 if(!data->thread)
309 return;
311 data->killNow = 1;
312 StopThread(data->thread);
313 data->thread = NULL;
315 data->killNow = 0;
317 ext = timeGetTime() - data->startTime;
318 data->baseTime += (ALuint64)ext * 1000000;
320 free(data->buffer);
321 data->buffer = NULL;
323 size = ftell(data->f);
324 if(size > 0)
326 dataLen = size - data->DataStart;
327 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
329 fputc(dataLen&0xff, data->f); // 'data' header len
330 fputc((dataLen>>8)&0xff, data->f);
331 fputc((dataLen>>16)&0xff, data->f);
332 fputc((dataLen>>24)&0xff, data->f);
334 if(fseek(data->f, 4, SEEK_SET) == 0)
336 size -= 8;
337 fputc(size&0xff, data->f); // 'WAVE' header len
338 fputc((size>>8)&0xff, data->f);
339 fputc((size>>16)&0xff, data->f);
340 fputc((size>>24)&0xff, data->f);
346 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
348 (void)pDevice;
349 (void)deviceName;
350 return ALC_FALSE;
353 static ALuint64 wave_get_time(ALCdevice *Device)
355 wave_data *data = (wave_data*)Device->ExtraData;
356 ALuint ext = 0;
357 if(data->thread)
358 ext = timeGetTime() - data->startTime;
359 return data->baseTime + ((ALuint64)ext * 1000000);
363 BackendFuncs wave_funcs = {
364 wave_open_playback,
365 wave_close_playback,
366 wave_reset_playback,
367 wave_stop_playback,
368 wave_open_capture,
369 NULL,
370 NULL,
371 NULL,
372 NULL,
373 NULL,
374 wave_get_time
377 void alc_wave_init(BackendFuncs *func_list)
379 *func_list = wave_funcs;
382 void alc_wave_deinit(void)
386 void alc_wave_probe(int type)
388 if(!ConfigValueExists("wave", "file"))
389 return;
391 if(type == DEVICE_PROBE)
392 AppendDeviceList(waveDevice);
393 else if(type == ALL_DEVICE_PROBE)
394 AppendAllDeviceList(waveDevice);