Make sure the effectslot map is properly initialized
[openal-soft/android/lowlatency.git] / Alc / wave.c
blob62c3ffc21e0249ee934c097fabe6a44014e130ff
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 */
66 static ALuint WaveProc(ALvoid *ptr)
68 ALCdevice *pDevice = (ALCdevice*)ptr;
69 wave_data *data = (wave_data*)pDevice->ExtraData;
70 ALuint frameSize;
71 ALuint now, last;
72 size_t fs;
73 ALuint avail;
74 union {
75 short s;
76 char b[sizeof(short)];
77 } uSB;
79 uSB.s = 1;
80 frameSize = aluFrameSizeFromFormat(pDevice->Format);
82 last = timeGetTime()<<8;
83 while(!data->killNow && pDevice->Connected)
85 now = timeGetTime()<<8;
87 avail = (ALuint64)(now-last) * pDevice->Frequency / (1000<<8);
88 if(avail < pDevice->UpdateSize)
90 Sleep(1);
91 continue;
94 while(avail >= pDevice->UpdateSize)
96 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
98 if(uSB.b[0] != 1)
100 ALubyte *bytes = data->buffer;
101 ALuint i;
103 if(aluBytesFromFormat(pDevice->Format) == 1)
105 for(i = 0;i < data->size;i++)
106 fputc(bytes[i], data->f);
108 else if(aluBytesFromFormat(pDevice->Format) == 2)
110 for(i = 0;i < data->size;i++)
111 fputc(bytes[i^1], data->f);
113 else if(aluBytesFromFormat(pDevice->Format) == 4)
115 for(i = 0;i < data->size;i++)
116 fputc(bytes[i^3], data->f);
119 else
120 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
121 data->f);
122 if(ferror(data->f))
124 AL_PRINT("Error writing to file\n");
125 aluHandleDisconnect(pDevice);
126 break;
129 avail -= pDevice->UpdateSize;
130 last += (ALuint64)pDevice->UpdateSize * (1000<<8) / pDevice->Frequency;
134 return 0;
137 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
139 wave_data *data;
140 const char *fname;
142 fname = GetConfigValue("wave", "file", "");
143 if(!fname[0])
144 return ALC_FALSE;
146 if(!deviceName)
147 deviceName = waveDevice;
148 else if(strcmp(deviceName, waveDevice) != 0)
149 return ALC_FALSE;
151 data = (wave_data*)calloc(1, sizeof(wave_data));
153 data->f = fopen(fname, "wb");
154 if(!data->f)
156 free(data);
157 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
158 return ALC_FALSE;
161 device->szDeviceName = strdup(deviceName);
162 device->ExtraData = data;
163 return ALC_TRUE;
166 static void wave_close_playback(ALCdevice *device)
168 wave_data *data = (wave_data*)device->ExtraData;
170 fclose(data->f);
171 free(data);
172 device->ExtraData = NULL;
175 static ALCboolean wave_reset_playback(ALCdevice *device)
177 wave_data *data = (wave_data*)device->ExtraData;
178 ALuint channels, bits, i;
179 size_t val;
181 fseek(data->f, 0, SEEK_SET);
182 clearerr(data->f);
184 bits = aluBytesFromFormat(device->Format) * 8;
185 channels = aluChannelsFromFormat(device->Format);
187 /* 7.1 max */
188 if(channels > 8)
190 if(bits == 8)
191 device->Format = AL_FORMAT_71CHN8;
192 else if(bits == 16)
193 device->Format = AL_FORMAT_71CHN16;
194 else
196 device->Format = AL_FORMAT_71CHN32;
197 bits = 32;
199 channels = 8;
202 fprintf(data->f, "RIFF");
203 fputc(0xFF, data->f); // 'RIFF' header len; filled in at close
204 fputc(0xFF, data->f);
205 fputc(0xFF, data->f);
206 fputc(0xFF, data->f);
208 fprintf(data->f, "WAVE");
210 fprintf(data->f, "fmt ");
211 fputc(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
212 fputc(0, data->f);
213 fputc(0, data->f);
214 fputc(0, data->f);
215 // 16-bit val, format type id (extensible: 0xFFFE)
216 fputc(0xFE, data->f);
217 fputc(0xFF, data->f);
218 // 16-bit val, channel count
219 fputc(channels&0xff, data->f);
220 fputc((channels>>8)&0xff, data->f);
221 // 32-bit val, frequency
222 fputc(device->Frequency&0xff, data->f);
223 fputc((device->Frequency>>8)&0xff, data->f);
224 fputc((device->Frequency>>16)&0xff, data->f);
225 fputc((device->Frequency>>24)&0xff, data->f);
226 // 32-bit val, bytes per second
227 i = device->Frequency * channels * bits / 8;
228 fputc(i&0xff, data->f);
229 fputc((i>>8)&0xff, data->f);
230 fputc((i>>16)&0xff, data->f);
231 fputc((i>>24)&0xff, data->f);
232 // 16-bit val, frame size
233 i = channels * bits / 8;
234 fputc(i&0xff, data->f);
235 fputc((i>>8)&0xff, data->f);
236 // 16-bit val, bits per sample
237 fputc(bits&0xff, data->f);
238 fputc((bits>>8)&0xff, data->f);
239 // 16-bit val, extra byte count
240 fputc(22, data->f);
241 fputc(0, data->f);
242 // 16-bit val, valid bits per sample
243 fputc(bits&0xff, data->f);
244 fputc((bits>>8)&0xff, data->f);
245 // 32-bit val, channel mask
246 i = channel_masks[channels];
247 fputc(i&0xff, data->f);
248 fputc((i>>8)&0xff, data->f);
249 fputc((i>>16)&0xff, data->f);
250 fputc((i>>24)&0xff, data->f);
251 // 16 byte GUID, sub-type format
252 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
254 fprintf(data->f, "data");
255 fputc(0xFF, data->f); // 'data' header len; filled in at close
256 fputc(0xFF, data->f);
257 fputc(0xFF, data->f);
258 fputc(0xFF, data->f);
260 if(ferror(data->f))
262 AL_PRINT("Error writing header: %s\n", strerror(errno));
263 return ALC_FALSE;
266 data->DataStart = ftell(data->f);
268 data->size = device->UpdateSize * channels * bits / 8;
269 data->buffer = malloc(data->size);
270 if(!data->buffer)
272 AL_PRINT("buffer malloc failed\n");
273 return ALC_FALSE;
276 SetDefaultWFXChannelOrder(device);
278 data->thread = StartThread(WaveProc, device);
279 if(data->thread == NULL)
281 free(data->buffer);
282 data->buffer = NULL;
283 return ALC_FALSE;
286 return ALC_TRUE;
289 static void wave_stop_playback(ALCdevice *device)
291 wave_data *data = (wave_data*)device->ExtraData;
292 ALuint dataLen;
293 long size;
295 if(!data->thread)
296 return;
298 data->killNow = 1;
299 StopThread(data->thread);
300 data->thread = NULL;
302 data->killNow = 0;
304 free(data->buffer);
305 data->buffer = NULL;
307 size = ftell(data->f);
308 if(size > 0)
310 dataLen = size - data->DataStart;
311 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
313 fputc(dataLen&0xff, data->f); // 'data' header len
314 fputc((dataLen>>8)&0xff, data->f);
315 fputc((dataLen>>16)&0xff, data->f);
316 fputc((dataLen>>24)&0xff, data->f);
318 if(fseek(data->f, 4, SEEK_SET) == 0)
320 size -= 8;
321 fputc(size&0xff, data->f); // 'WAVE' header len
322 fputc((size>>8)&0xff, data->f);
323 fputc((size>>16)&0xff, data->f);
324 fputc((size>>24)&0xff, data->f);
330 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
332 (void)pDevice;
333 (void)deviceName;
334 return ALC_FALSE;
338 BackendFuncs wave_funcs = {
339 wave_open_playback,
340 wave_close_playback,
341 wave_reset_playback,
342 wave_stop_playback,
343 wave_open_capture,
344 NULL,
345 NULL,
346 NULL,
347 NULL,
348 NULL
351 void alc_wave_init(BackendFuncs *func_list)
353 *func_list = wave_funcs;
356 void alc_wave_deinit(void)
360 void alc_wave_probe(int type)
362 if(!ConfigValueExists("wave", "file"))
363 return;
365 if(type == DEVICE_PROBE)
366 AppendDeviceList(waveDevice);
367 else if(type == ALL_DEVICE_PROBE)
368 AppendAllDeviceList(waveDevice);