Don't clamp the wave writer's update size
[openal-soft.git] / Alc / wave.c
blob6074f241d06f4cc5d2b3f8fa1ab3e5c2d28124d6
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 ALCchar *waveDevice;
46 static ALuint WaveProc(ALvoid *ptr)
48 ALCdevice *pDevice = (ALCdevice*)ptr;
49 wave_data *data = (wave_data*)pDevice->ExtraData;
50 ALuint frameSize;
51 ALuint now, last;
52 size_t WriteCnt;
53 size_t fs;
54 ALuint avail;
55 union {
56 short s;
57 char b[sizeof(short)];
58 } uSB;
60 uSB.s = 1;
61 frameSize = aluBytesFromFormat(pDevice->Format) *
62 aluChannelsFromFormat(pDevice->Format);
64 last = timeGetTime();
65 while(!data->killNow)
67 now = timeGetTime();
69 avail = (now-last) * pDevice->Frequency / 1000;
70 if(avail < pDevice->UpdateSize)
72 Sleep(1);
73 continue;
76 while(avail > 0)
78 SuspendContext(NULL);
79 WriteCnt = min(data->size, avail);
80 aluMixData(pDevice->Context, data->buffer, WriteCnt * frameSize,
81 pDevice->Format);
82 ProcessContext(NULL);
84 if(uSB.b[0] != 1 && aluBytesFromFormat(pDevice->Format) > 1)
86 ALubyte *bytes = data->buffer;
87 ALuint i;
89 for(i = 0;i < WriteCnt*frameSize;i++)
90 fputc(bytes[i^1], data->f);
92 else
93 fs = fwrite(data->buffer, frameSize, WriteCnt, data->f);
94 if(ferror(data->f))
96 AL_PRINT("Error writing to file\n");
97 data->killNow = 1;
98 break;
101 avail -= WriteCnt;
103 last = now;
106 return 0;
109 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
111 wave_data *data;
112 const char *fname;
114 fname = GetConfigValue("wave", "file", "");
115 if(!fname[0])
116 return ALC_FALSE;
118 if(deviceName)
120 if(strcmp(deviceName, waveDevice) != 0)
121 return ALC_FALSE;
122 device->szDeviceName = waveDevice;
124 else
125 device->szDeviceName = waveDevice;
127 data = (wave_data*)calloc(1, sizeof(wave_data));
129 data->f = fopen(fname, "wb");
130 if(!data->f)
132 free(data);
133 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
134 return ALC_FALSE;
137 device->ExtraData = data;
138 return ALC_TRUE;
141 static void wave_close_playback(ALCdevice *device)
143 wave_data *data = (wave_data*)device->ExtraData;
144 ALuint dataLen;
145 long size;
147 data->killNow = 1;
148 StopThread(data->thread);
150 size = ftell(data->f);
151 if(size > 0)
153 dataLen = size - data->DataStart;
154 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
156 fputc(dataLen&0xff, data->f); // 'data' header len
157 fputc((dataLen>>8)&0xff, data->f);
158 fputc((dataLen>>16)&0xff, data->f);
159 fputc((dataLen>>24)&0xff, data->f);
161 if(fseek(data->f, 4, SEEK_SET) == 0)
163 size -= 8;
164 fputc(size&0xff, data->f); // 'WAVE' header len
165 fputc((size>>8)&0xff, data->f);
166 fputc((size>>16)&0xff, data->f);
167 fputc((size>>24)&0xff, data->f);
171 fclose(data->f);
172 free(data);
173 device->ExtraData = NULL;
176 static ALCboolean wave_start_context(ALCdevice *device, ALCcontext *Context)
178 wave_data *data = (wave_data*)device->ExtraData;
179 ALuint channels, bits, i;
180 (void)Context;
182 fseek(data->f, 0, SEEK_SET);
183 clearerr(data->f);
185 bits = aluBytesFromFormat(device->Format) * 8;
186 channels = aluChannelsFromFormat(device->Format);
187 switch(bits)
189 case 8:
190 case 16:
191 if(channels == 0)
193 AL_PRINT("Unknown format?! %x\n", device->Format);
194 return ALC_FALSE;
196 break;
198 default:
199 AL_PRINT("Unknown format?! %x\n", device->Format);
200 return ALC_FALSE;
203 fprintf(data->f, "RIFF");
204 fputc(0, data->f); // 'RIFF' header len; filled in at close
205 fputc(0, data->f);
206 fputc(0, data->f);
207 fputc(0, data->f);
209 fprintf(data->f, "WAVE");
211 fprintf(data->f, "fmt ");
212 fputc(16, data->f); // 'fmt ' header len; 16 bytes for PCM
213 fputc(0, data->f);
214 fputc(0, data->f);
215 fputc(0, data->f);
216 // 16-bit val, format type id (PCM: 1)
217 fputc(1, data->f);
218 fputc(0, data->f);
219 // 16-bit val, channel count
220 fputc(channels&0xff, data->f);
221 fputc((channels>>8)&0xff, data->f);
222 // 32-bit val, frequency
223 fputc(device->Frequency&0xff, data->f);
224 fputc((device->Frequency>>8)&0xff, data->f);
225 fputc((device->Frequency>>16)&0xff, data->f);
226 fputc((device->Frequency>>24)&0xff, data->f);
227 // 32-bit val, bytes per second
228 i = device->Frequency * channels * bits / 8;
229 fputc(i&0xff, data->f);
230 fputc((i>>8)&0xff, data->f);
231 fputc((i>>16)&0xff, data->f);
232 fputc((i>>24)&0xff, data->f);
233 // 16-bit val, frame size
234 i = channels * bits / 8;
235 fputc(i&0xff, data->f);
236 fputc((i>>8)&0xff, data->f);
237 // 16-bit val, bits per sample
238 fputc(bits&0xff, data->f);
239 fputc((bits>>8)&0xff, data->f);
241 fprintf(data->f, "data");
242 fputc(0, data->f); // 'data' header len; filled in at close
243 fputc(0, data->f);
244 fputc(0, data->f);
245 fputc(0, data->f);
247 if(ferror(data->f))
249 AL_PRINT("Error writing header: %s\n", strerror(errno));
250 return ALC_FALSE;
253 data->DataStart = ftell(data->f);
255 device->UpdateSize = device->BufferSize / 4;
257 data->size = device->UpdateSize;
258 data->buffer = malloc(data->size * channels * bits / 8);
259 if(!data->buffer)
261 AL_PRINT("buffer malloc failed\n");
262 return ALC_FALSE;
265 data->thread = StartThread(WaveProc, device);
266 if(data->thread == NULL)
268 free(data->buffer);
269 data->buffer = NULL;
270 return ALC_FALSE;
273 return ALC_TRUE;
276 static void wave_stop_context(ALCdevice *device, ALCcontext *Context)
278 wave_data *data = (wave_data*)device->ExtraData;
279 ALuint dataLen;
280 long size;
281 (void)Context;
283 if(!data->thread)
284 return;
286 data->killNow = 1;
287 StopThread(data->thread);
288 data->thread = NULL;
290 free(data->buffer);
291 data->buffer = NULL;
293 size = ftell(data->f);
294 if(size > 0)
296 dataLen = size - data->DataStart;
297 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
299 fputc(dataLen&0xff, data->f); // 'data' header len
300 fputc((dataLen>>8)&0xff, data->f);
301 fputc((dataLen>>16)&0xff, data->f);
302 fputc((dataLen>>24)&0xff, data->f);
304 if(fseek(data->f, 4, SEEK_SET) == 0)
306 size -= 8;
307 fputc(size&0xff, data->f); // 'WAVE' header len
308 fputc((size>>8)&0xff, data->f);
309 fputc((size>>16)&0xff, data->f);
310 fputc((size>>24)&0xff, data->f);
316 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
318 (void)pDevice;
319 (void)deviceName;
320 (void)frequency;
321 (void)format;
322 (void)SampleSize;
323 return ALC_FALSE;
326 static void wave_close_capture(ALCdevice *pDevice)
328 (void)pDevice;
331 static void wave_start_capture(ALCdevice *pDevice)
333 (void)pDevice;
336 static void wave_stop_capture(ALCdevice *pDevice)
338 (void)pDevice;
341 static void wave_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
343 (void)pDevice;
344 (void)pBuffer;
345 (void)lSamples;
348 static ALCuint wave_available_samples(ALCdevice *pDevice)
350 (void)pDevice;
351 return 0;
355 BackendFuncs wave_funcs = {
356 wave_open_playback,
357 wave_close_playback,
358 wave_start_context,
359 wave_stop_context,
360 wave_open_capture,
361 wave_close_capture,
362 wave_start_capture,
363 wave_stop_capture,
364 wave_capture_samples,
365 wave_available_samples
368 void alc_wave_init(BackendFuncs *func_list)
370 *func_list = wave_funcs;
372 waveDevice = AppendDeviceList("Wave File Writer");
373 AppendAllDeviceList(waveDevice);