Don't needlessly expose a variable for the backends
[openal-soft/android/lowlatency.git] / Alc / wave.c
blob4ed120bb1afdd65a362f391430607fe5fc77b437
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 = aluBytesFromFormat(pDevice->Format) *
81 aluChannelsFromFormat(pDevice->Format);
83 last = timeGetTime()<<8;
84 while(!data->killNow && pDevice->Connected)
86 now = timeGetTime()<<8;
88 avail = (ALuint64)(now-last) * pDevice->Frequency / (1000<<8);
89 if(avail < pDevice->UpdateSize)
91 Sleep(1);
92 continue;
95 while(avail >= pDevice->UpdateSize)
97 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
99 if(uSB.b[0] != 1)
101 ALubyte *bytes = data->buffer;
102 ALuint i;
104 if(aluBytesFromFormat(pDevice->Format) == 1)
106 for(i = 0;i < data->size;i++)
107 fputc(bytes[i], data->f);
109 else if(aluBytesFromFormat(pDevice->Format) == 2)
111 for(i = 0;i < data->size;i++)
112 fputc(bytes[i^1], data->f);
114 else if(aluBytesFromFormat(pDevice->Format) == 4)
116 for(i = 0;i < data->size;i++)
117 fputc(bytes[i^3], data->f);
120 else
121 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
122 data->f);
123 if(ferror(data->f))
125 AL_PRINT("Error writing to file\n");
126 aluHandleDisconnect(pDevice);
127 break;
130 avail -= pDevice->UpdateSize;
131 last += (ALuint64)pDevice->UpdateSize * (1000<<8) / pDevice->Frequency;
135 return 0;
138 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
140 wave_data *data;
141 const char *fname;
143 fname = GetConfigValue("wave", "file", "");
144 if(!fname[0])
145 return ALC_FALSE;
147 if(!deviceName)
148 deviceName = waveDevice;
149 else if(strcmp(deviceName, waveDevice) != 0)
150 return ALC_FALSE;
152 data = (wave_data*)calloc(1, sizeof(wave_data));
154 data->f = fopen(fname, "wb");
155 if(!data->f)
157 free(data);
158 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
159 return ALC_FALSE;
162 device->szDeviceName = strdup(deviceName);
163 device->ExtraData = data;
164 return ALC_TRUE;
167 static void wave_close_playback(ALCdevice *device)
169 wave_data *data = (wave_data*)device->ExtraData;
171 fclose(data->f);
172 free(data);
173 device->ExtraData = NULL;
176 static ALCboolean wave_reset_playback(ALCdevice *device)
178 wave_data *data = (wave_data*)device->ExtraData;
179 ALuint channels, bits, i;
180 size_t val;
182 fseek(data->f, 0, SEEK_SET);
183 clearerr(data->f);
185 bits = aluBytesFromFormat(device->Format) * 8;
186 channels = aluChannelsFromFormat(device->Format);
188 /* 7.1 max */
189 if(channels > 8)
191 if(bits == 8)
192 device->Format = AL_FORMAT_71CHN8;
193 else if(bits == 16)
194 device->Format = AL_FORMAT_71CHN16;
195 else
197 device->Format = AL_FORMAT_71CHN32;
198 bits = 32;
200 channels = 8;
203 fprintf(data->f, "RIFF");
204 fputc(0xFF, data->f); // 'RIFF' header len; filled in at close
205 fputc(0xFF, data->f);
206 fputc(0xFF, data->f);
207 fputc(0xFF, data->f);
209 fprintf(data->f, "WAVE");
211 fprintf(data->f, "fmt ");
212 fputc(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
213 fputc(0, data->f);
214 fputc(0, data->f);
215 fputc(0, data->f);
216 // 16-bit val, format type id (extensible: 0xFFFE)
217 fputc(0xFE, data->f);
218 fputc(0xFF, 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);
240 // 16-bit val, extra byte count
241 fputc(22, data->f);
242 fputc(0, data->f);
243 // 16-bit val, valid bits per sample
244 fputc(bits&0xff, data->f);
245 fputc((bits>>8)&0xff, data->f);
246 // 32-bit val, channel mask
247 i = channel_masks[channels];
248 fputc(i&0xff, data->f);
249 fputc((i>>8)&0xff, data->f);
250 fputc((i>>16)&0xff, data->f);
251 fputc((i>>24)&0xff, 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 fputc(0xFF, data->f); // 'data' header len; filled in at close
257 fputc(0xFF, data->f);
258 fputc(0xFF, data->f);
259 fputc(0xFF, data->f);
261 if(ferror(data->f))
263 AL_PRINT("Error writing header: %s\n", strerror(errno));
264 return ALC_FALSE;
267 data->DataStart = ftell(data->f);
269 data->size = device->UpdateSize * channels * bits / 8;
270 data->buffer = malloc(data->size);
271 if(!data->buffer)
273 AL_PRINT("buffer malloc failed\n");
274 return ALC_FALSE;
277 SetDefaultWFXChannelOrder(device);
279 data->thread = StartThread(WaveProc, device);
280 if(data->thread == NULL)
282 free(data->buffer);
283 data->buffer = NULL;
284 return ALC_FALSE;
287 return ALC_TRUE;
290 static void wave_stop_playback(ALCdevice *device)
292 wave_data *data = (wave_data*)device->ExtraData;
293 ALuint dataLen;
294 long size;
296 if(!data->thread)
297 return;
299 data->killNow = 1;
300 StopThread(data->thread);
301 data->thread = NULL;
303 data->killNow = 0;
305 free(data->buffer);
306 data->buffer = NULL;
308 size = ftell(data->f);
309 if(size > 0)
311 dataLen = size - data->DataStart;
312 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
314 fputc(dataLen&0xff, data->f); // 'data' header len
315 fputc((dataLen>>8)&0xff, data->f);
316 fputc((dataLen>>16)&0xff, data->f);
317 fputc((dataLen>>24)&0xff, data->f);
319 if(fseek(data->f, 4, SEEK_SET) == 0)
321 size -= 8;
322 fputc(size&0xff, data->f); // 'WAVE' header len
323 fputc((size>>8)&0xff, data->f);
324 fputc((size>>16)&0xff, data->f);
325 fputc((size>>24)&0xff, data->f);
331 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
333 (void)pDevice;
334 (void)deviceName;
335 return ALC_FALSE;
339 BackendFuncs wave_funcs = {
340 wave_open_playback,
341 wave_close_playback,
342 wave_reset_playback,
343 wave_stop_playback,
344 wave_open_capture,
345 NULL,
346 NULL,
347 NULL,
348 NULL,
349 NULL
352 void alc_wave_init(BackendFuncs *func_list)
354 *func_list = wave_funcs;
357 void alc_wave_deinit(void)
361 void alc_wave_probe(int type)
363 if(!ConfigValueExists("wave", "file"))
364 return;
366 if(type == DEVICE_PROBE)
367 AppendDeviceList(waveDevice);
368 else if(type == ALL_DEVICE_PROBE)
369 AppendAllDeviceList(waveDevice);