Pass the frame count to aluMixData
[openal-soft.git] / Alc / wave.c
blobbd6c408aeaa8bab45f9ca3ea17c92125e581ab20
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";
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 fs;
53 ALuint avail;
54 union {
55 short s;
56 char b[sizeof(short)];
57 } uSB;
59 uSB.s = 1;
60 frameSize = aluBytesFromFormat(pDevice->Format) *
61 aluChannelsFromFormat(pDevice->Format);
63 last = timeGetTime();
64 while(!data->killNow && pDevice->Connected)
66 now = timeGetTime();
68 avail = (now-last) * pDevice->Frequency / 1000;
69 if(avail < pDevice->UpdateSize)
71 Sleep(1);
72 continue;
75 while(avail >= pDevice->UpdateSize)
77 SuspendContext(NULL);
78 aluMixData(pDevice->Context, data->buffer, pDevice->UpdateSize,
79 pDevice->Format);
80 ProcessContext(NULL);
82 if(uSB.b[0] != 1 && aluBytesFromFormat(pDevice->Format) > 1)
84 ALubyte *bytes = data->buffer;
85 ALuint i;
87 for(i = 0;i < data->size;i++)
88 fputc(bytes[i^1], data->f);
90 else
91 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
92 data->f);
93 if(ferror(data->f))
95 AL_PRINT("Error writing to file\n");
96 aluHandleDisconnect(pDevice);
97 break;
100 avail -= pDevice->UpdateSize;
102 last = now;
105 return 0;
108 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
110 wave_data *data;
111 const char *fname;
113 fname = GetConfigValue("wave", "file", "");
114 if(!fname[0])
115 return ALC_FALSE;
117 if(!deviceName)
118 deviceName = waveDevice;
119 else if(strcmp(deviceName, waveDevice) != 0)
120 return ALC_FALSE;
122 data = (wave_data*)calloc(1, sizeof(wave_data));
124 data->f = fopen(fname, "wb");
125 if(!data->f)
127 free(data);
128 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
129 return ALC_FALSE;
132 device->szDeviceName = strdup(deviceName);
133 device->ExtraData = data;
134 return ALC_TRUE;
137 static void wave_close_playback(ALCdevice *device)
139 wave_data *data = (wave_data*)device->ExtraData;
141 fclose(data->f);
142 free(data);
143 device->ExtraData = NULL;
146 static ALCboolean wave_start_context(ALCdevice *device, ALCcontext *Context)
148 wave_data *data = (wave_data*)device->ExtraData;
149 ALuint channels, bits, i;
150 (void)Context;
152 fseek(data->f, 0, SEEK_SET);
153 clearerr(data->f);
155 bits = aluBytesFromFormat(device->Format) * 8;
156 channels = aluChannelsFromFormat(device->Format);
157 switch(bits)
159 case 8:
160 case 16:
161 case 32:
162 if(channels == 0)
164 AL_PRINT("Unknown format?! %x\n", device->Format);
165 return ALC_FALSE;
167 break;
169 default:
170 AL_PRINT("Unknown format?! %x\n", device->Format);
171 return ALC_FALSE;
174 fprintf(data->f, "RIFF");
175 fputc(0, data->f); // 'RIFF' header len; filled in at close
176 fputc(0, data->f);
177 fputc(0, data->f);
178 fputc(0, data->f);
180 fprintf(data->f, "WAVE");
182 fprintf(data->f, "fmt ");
183 fputc(16, data->f); // 'fmt ' header len; 16 bytes for PCM
184 fputc(0, data->f);
185 fputc(0, data->f);
186 fputc(0, data->f);
187 // 16-bit val, format type id (PCM: 1)
188 fputc(1, data->f);
189 fputc(0, data->f);
190 // 16-bit val, channel count
191 fputc(channels&0xff, data->f);
192 fputc((channels>>8)&0xff, data->f);
193 // 32-bit val, frequency
194 fputc(device->Frequency&0xff, data->f);
195 fputc((device->Frequency>>8)&0xff, data->f);
196 fputc((device->Frequency>>16)&0xff, data->f);
197 fputc((device->Frequency>>24)&0xff, data->f);
198 // 32-bit val, bytes per second
199 i = device->Frequency * channels * bits / 8;
200 fputc(i&0xff, data->f);
201 fputc((i>>8)&0xff, data->f);
202 fputc((i>>16)&0xff, data->f);
203 fputc((i>>24)&0xff, data->f);
204 // 16-bit val, frame size
205 i = channels * bits / 8;
206 fputc(i&0xff, data->f);
207 fputc((i>>8)&0xff, data->f);
208 // 16-bit val, bits per sample
209 fputc(bits&0xff, data->f);
210 fputc((bits>>8)&0xff, data->f);
212 fprintf(data->f, "data");
213 fputc(0, data->f); // 'data' header len; filled in at close
214 fputc(0, data->f);
215 fputc(0, data->f);
216 fputc(0, data->f);
218 if(ferror(data->f))
220 AL_PRINT("Error writing header: %s\n", strerror(errno));
221 return ALC_FALSE;
224 data->DataStart = ftell(data->f);
226 device->UpdateSize = device->BufferSize / 4;
228 data->size = device->UpdateSize * channels * bits / 8;
229 data->buffer = malloc(data->size);
230 if(!data->buffer)
232 AL_PRINT("buffer malloc failed\n");
233 return ALC_FALSE;
236 data->thread = StartThread(WaveProc, device);
237 if(data->thread == NULL)
239 free(data->buffer);
240 data->buffer = NULL;
241 return ALC_FALSE;
244 return ALC_TRUE;
247 static void wave_stop_context(ALCdevice *device, ALCcontext *Context)
249 wave_data *data = (wave_data*)device->ExtraData;
250 ALuint dataLen;
251 long size;
252 (void)Context;
254 if(!data->thread)
255 return;
257 data->killNow = 1;
258 StopThread(data->thread);
259 data->thread = NULL;
261 free(data->buffer);
262 data->buffer = NULL;
264 size = ftell(data->f);
265 if(size > 0)
267 dataLen = size - data->DataStart;
268 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
270 fputc(dataLen&0xff, data->f); // 'data' header len
271 fputc((dataLen>>8)&0xff, data->f);
272 fputc((dataLen>>16)&0xff, data->f);
273 fputc((dataLen>>24)&0xff, data->f);
275 if(fseek(data->f, 4, SEEK_SET) == 0)
277 size -= 8;
278 fputc(size&0xff, data->f); // 'WAVE' header len
279 fputc((size>>8)&0xff, data->f);
280 fputc((size>>16)&0xff, data->f);
281 fputc((size>>24)&0xff, data->f);
287 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
289 (void)pDevice;
290 (void)deviceName;
291 return ALC_FALSE;
294 static void wave_close_capture(ALCdevice *pDevice)
296 (void)pDevice;
299 static void wave_start_capture(ALCdevice *pDevice)
301 (void)pDevice;
304 static void wave_stop_capture(ALCdevice *pDevice)
306 (void)pDevice;
309 static void wave_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
311 (void)pDevice;
312 (void)pBuffer;
313 (void)lSamples;
316 static ALCuint wave_available_samples(ALCdevice *pDevice)
318 (void)pDevice;
319 return 0;
323 BackendFuncs wave_funcs = {
324 wave_open_playback,
325 wave_close_playback,
326 wave_start_context,
327 wave_stop_context,
328 wave_open_capture,
329 wave_close_capture,
330 wave_start_capture,
331 wave_stop_capture,
332 wave_capture_samples,
333 wave_available_samples
336 void alc_wave_init(BackendFuncs *func_list)
338 *func_list = wave_funcs;
341 void alc_wave_deinit(void)
345 void alc_wave_probe(int type)
347 if(type == DEVICE_PROBE)
348 AppendDeviceList(waveDevice);
349 else if(type == ALL_DEVICE_PROBE)
350 AppendAllDeviceList(waveDevice);