Handle the timer wrapping in the wave and null output devices
[openal-soft.git] / Alc / wave.c
blobf8921c64740ad60c593ef18771057e7d9e5e91c8
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 volatile int killNow;
39 ALvoid *thread;
40 } wave_data;
43 static const ALCchar waveDevice[] = "Wave File Writer";
45 static const ALubyte SUBTYPE_PCM[] = {
46 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
47 0x00, 0x38, 0x9b, 0x71
49 static const ALubyte SUBTYPE_FLOAT[] = {
50 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
51 0x00, 0x38, 0x9b, 0x71
54 static const ALuint channel_masks[] = {
55 0, /* invalid */
56 0x4, /* Mono */
57 0x1 | 0x2, /* Stereo */
58 0, /* 3 channel */
59 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
60 0, /* 5 channel */
61 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
62 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
63 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
67 static void fwrite16le(ALushort val, FILE *f)
69 fputc(val&0xff, f);
70 fputc((val>>8)&0xff, f);
73 static void fwrite32le(ALuint val, FILE *f)
75 fputc(val&0xff, f);
76 fputc((val>>8)&0xff, f);
77 fputc((val>>16)&0xff, f);
78 fputc((val>>24)&0xff, f);
82 static ALuint WaveProc(ALvoid *ptr)
84 ALCdevice *pDevice = (ALCdevice*)ptr;
85 wave_data *data = (wave_data*)pDevice->ExtraData;
86 ALuint frameSize;
87 ALuint now, start;
88 ALuint64 avail, done;
89 size_t fs;
90 union {
91 short s;
92 char b[sizeof(short)];
93 } uSB;
94 const ALuint restTime = (ALuint64)pDevice->UpdateSize * 1000 /
95 pDevice->Frequency / 2;
97 uSB.s = 1;
98 frameSize = aluFrameSizeFromFormat(pDevice->Format);
100 done = 0;
101 start = timeGetTime();
102 while(!data->killNow && pDevice->Connected)
104 now = timeGetTime();
106 avail = (ALuint64)(now-start) * pDevice->Frequency / 1000;
107 if(avail < done)
109 /* Timer wrapped. Add the remainder of the cycle to the available
110 * count and reset the number of samples done */
111 avail += 0xFFFFFFFFu*pDevice->Frequency/1000 - done;
112 done = 0;
114 if(avail-done < pDevice->UpdateSize)
116 Sleep(restTime);
117 continue;
120 while(avail-done >= pDevice->UpdateSize)
122 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
123 done += pDevice->UpdateSize;
125 if(uSB.b[0] != 1)
127 ALubyte *bytes = data->buffer;
128 ALuint i;
130 if(aluBytesFromFormat(pDevice->Format) == 1)
132 for(i = 0;i < data->size;i++)
133 fputc(bytes[i], data->f);
135 else if(aluBytesFromFormat(pDevice->Format) == 2)
137 for(i = 0;i < data->size;i++)
138 fputc(bytes[i^1], data->f);
140 else if(aluBytesFromFormat(pDevice->Format) == 4)
142 for(i = 0;i < data->size;i++)
143 fputc(bytes[i^3], data->f);
146 else
147 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
148 data->f);
149 if(ferror(data->f))
151 AL_PRINT("Error writing to file\n");
152 aluHandleDisconnect(pDevice);
153 break;
158 return 0;
161 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
163 wave_data *data;
164 const char *fname;
166 fname = GetConfigValue("wave", "file", "");
167 if(!fname[0])
168 return ALC_FALSE;
170 if(!deviceName)
171 deviceName = waveDevice;
172 else if(strcmp(deviceName, waveDevice) != 0)
173 return ALC_FALSE;
175 data = (wave_data*)calloc(1, sizeof(wave_data));
177 data->f = fopen(fname, "wb");
178 if(!data->f)
180 free(data);
181 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
182 return ALC_FALSE;
185 device->szDeviceName = strdup(deviceName);
186 device->ExtraData = data;
187 return ALC_TRUE;
190 static void wave_close_playback(ALCdevice *device)
192 wave_data *data = (wave_data*)device->ExtraData;
194 fclose(data->f);
195 free(data);
196 device->ExtraData = NULL;
199 static ALCboolean wave_reset_playback(ALCdevice *device)
201 wave_data *data = (wave_data*)device->ExtraData;
202 ALuint channels, bits;
203 size_t val;
205 fseek(data->f, 0, SEEK_SET);
206 clearerr(data->f);
208 bits = aluBytesFromFormat(device->Format) * 8;
209 channels = aluChannelsFromFormat(device->Format);
211 /* 7.1 max */
212 if(channels > 8)
214 if(bits == 8)
215 device->Format = AL_FORMAT_71CHN8;
216 else if(bits == 16)
217 device->Format = AL_FORMAT_71CHN16;
218 else
220 device->Format = AL_FORMAT_71CHN32;
221 bits = 32;
223 channels = 8;
226 fprintf(data->f, "RIFF");
227 fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
229 fprintf(data->f, "WAVE");
231 fprintf(data->f, "fmt ");
232 fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
234 // 16-bit val, format type id (extensible: 0xFFFE)
235 fwrite16le(0xFFFE, data->f);
236 // 16-bit val, channel count
237 fwrite16le(channels, data->f);
238 // 32-bit val, frequency
239 fwrite32le(device->Frequency, data->f);
240 // 32-bit val, bytes per second
241 fwrite32le(device->Frequency * channels * bits / 8, data->f);
242 // 16-bit val, frame size
243 fwrite16le(channels * bits / 8, data->f);
244 // 16-bit val, bits per sample
245 fwrite16le(bits, data->f);
246 // 16-bit val, extra byte count
247 fwrite16le(22, data->f);
248 // 16-bit val, valid bits per sample
249 fwrite16le(bits, data->f);
250 // 32-bit val, channel mask
251 fwrite32le(channel_masks[channels], data->f);
252 // 16 byte GUID, sub-type format
253 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
255 fprintf(data->f, "data");
256 fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
258 if(ferror(data->f))
260 AL_PRINT("Error writing header: %s\n", strerror(errno));
261 return ALC_FALSE;
264 data->DataStart = ftell(data->f);
266 data->size = device->UpdateSize * channels * bits / 8;
267 data->buffer = malloc(data->size);
268 if(!data->buffer)
270 AL_PRINT("buffer malloc failed\n");
271 return ALC_FALSE;
274 SetDefaultWFXChannelOrder(device);
276 data->thread = StartThread(WaveProc, device);
277 if(data->thread == NULL)
279 free(data->buffer);
280 data->buffer = NULL;
281 return ALC_FALSE;
284 return ALC_TRUE;
287 static void wave_stop_playback(ALCdevice *device)
289 wave_data *data = (wave_data*)device->ExtraData;
290 ALuint dataLen;
291 long size;
293 if(!data->thread)
294 return;
296 data->killNow = 1;
297 StopThread(data->thread);
298 data->thread = NULL;
300 data->killNow = 0;
302 free(data->buffer);
303 data->buffer = NULL;
305 size = ftell(data->f);
306 if(size > 0)
308 dataLen = size - data->DataStart;
309 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
310 fwrite32le(dataLen, data->f); // 'data' header len
311 if(fseek(data->f, 4, SEEK_SET) == 0)
312 fwrite32le(size-8, data->f); // 'WAVE' header len
317 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
319 (void)pDevice;
320 (void)deviceName;
321 return ALC_FALSE;
325 BackendFuncs wave_funcs = {
326 wave_open_playback,
327 wave_close_playback,
328 wave_reset_playback,
329 wave_stop_playback,
330 wave_open_capture,
331 NULL,
332 NULL,
333 NULL,
334 NULL,
335 NULL
338 void alc_wave_init(BackendFuncs *func_list)
340 *func_list = wave_funcs;
343 void alc_wave_deinit(void)
347 void alc_wave_probe(int type)
349 if(!ConfigValueExists("wave", "file"))
350 return;
352 if(type == DEVICE_PROBE)
353 AppendDeviceList(waveDevice);
354 else if(type == ALL_DEVICE_PROBE)
355 AppendAllDeviceList(waveDevice);