Don't use deprecated macros
[openal-soft.git] / Alc / wave.c
blobee969a68640cfc109c9e3bc2269887cc22890be6
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();
84 while(!data->killNow && pDevice->Connected)
86 now = timeGetTime();
88 avail = (now-last) * pDevice->Frequency / 1000;
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) == 2)
106 for(i = 0;i < data->size;i++)
107 fputc(bytes[i^1], data->f);
109 else if(aluBytesFromFormat(pDevice->Format) == 4)
111 for(i = 0;i < data->size;i++)
112 fputc(bytes[i^3], data->f);
115 else
116 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
117 data->f);
118 if(ferror(data->f))
120 AL_PRINT("Error writing to file\n");
121 aluHandleDisconnect(pDevice);
122 break;
125 avail -= pDevice->UpdateSize;
127 last = now;
130 return 0;
133 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
135 wave_data *data;
136 const char *fname;
138 fname = GetConfigValue("wave", "file", "");
139 if(!fname[0])
140 return ALC_FALSE;
142 if(!deviceName)
143 deviceName = waveDevice;
144 else if(strcmp(deviceName, waveDevice) != 0)
145 return ALC_FALSE;
147 data = (wave_data*)calloc(1, sizeof(wave_data));
149 data->f = fopen(fname, "wb");
150 if(!data->f)
152 free(data);
153 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
154 return ALC_FALSE;
157 device->szDeviceName = strdup(deviceName);
158 device->ExtraData = data;
159 return ALC_TRUE;
162 static void wave_close_playback(ALCdevice *device)
164 wave_data *data = (wave_data*)device->ExtraData;
166 fclose(data->f);
167 free(data);
168 device->ExtraData = NULL;
171 static ALCboolean wave_reset_playback(ALCdevice *device)
173 wave_data *data = (wave_data*)device->ExtraData;
174 ALuint channels, bits, i;
175 size_t val;
177 fseek(data->f, 0, SEEK_SET);
178 clearerr(data->f);
180 bits = aluBytesFromFormat(device->Format) * 8;
181 channels = aluChannelsFromFormat(device->Format);
183 /* 7.1 max */
184 if(channels > 8)
186 if(bits == 8)
187 device->Format = AL_FORMAT_71CHN8;
188 else if(bits == 16)
189 device->Format = AL_FORMAT_71CHN16;
190 else
192 device->Format = AL_FORMAT_71CHN32;
193 bits = 32;
195 channels = 8;
198 fprintf(data->f, "RIFF");
199 fputc(0xFF, data->f); // 'RIFF' header len; filled in at close
200 fputc(0xFF, data->f);
201 fputc(0xFF, data->f);
202 fputc(0xFF, data->f);
204 fprintf(data->f, "WAVE");
206 fprintf(data->f, "fmt ");
207 fputc(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIVLE
208 fputc(0, data->f);
209 fputc(0, data->f);
210 fputc(0, data->f);
211 // 16-bit val, format type id (extensible: 0xFFFE)
212 fputc(0xFE, data->f);
213 fputc(0xFF, data->f);
214 // 16-bit val, channel count
215 fputc(channels&0xff, data->f);
216 fputc((channels>>8)&0xff, data->f);
217 // 32-bit val, frequency
218 fputc(device->Frequency&0xff, data->f);
219 fputc((device->Frequency>>8)&0xff, data->f);
220 fputc((device->Frequency>>16)&0xff, data->f);
221 fputc((device->Frequency>>24)&0xff, data->f);
222 // 32-bit val, bytes per second
223 i = device->Frequency * channels * bits / 8;
224 fputc(i&0xff, data->f);
225 fputc((i>>8)&0xff, data->f);
226 fputc((i>>16)&0xff, data->f);
227 fputc((i>>24)&0xff, data->f);
228 // 16-bit val, frame size
229 i = channels * bits / 8;
230 fputc(i&0xff, data->f);
231 fputc((i>>8)&0xff, data->f);
232 // 16-bit val, bits per sample
233 fputc(bits&0xff, data->f);
234 fputc((bits>>8)&0xff, data->f);
235 // 16-bit val, extra byte count
236 fputc(22, data->f);
237 fputc(0, data->f);
238 // 16-bit val, valid bits per sample
239 fputc(bits&0xff, data->f);
240 fputc((bits>>8)&0xff, data->f);
241 // 32-bit val, channel mask
242 i = channel_masks[channels];
243 fputc(i&0xff, data->f);
244 fputc((i>>8)&0xff, data->f);
245 fputc((i>>16)&0xff, data->f);
246 fputc((i>>24)&0xff, data->f);
247 // 16 byte GUID, sub-type format
248 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
250 fprintf(data->f, "data");
251 fputc(0xFF, data->f); // 'data' header len; filled in at close
252 fputc(0xFF, data->f);
253 fputc(0xFF, data->f);
254 fputc(0xFF, data->f);
256 if(ferror(data->f))
258 AL_PRINT("Error writing header: %s\n", strerror(errno));
259 return ALC_FALSE;
262 data->DataStart = ftell(data->f);
264 data->size = device->UpdateSize * channels * bits / 8;
265 data->buffer = malloc(data->size);
266 if(!data->buffer)
268 AL_PRINT("buffer malloc failed\n");
269 return ALC_FALSE;
272 SetDefaultWFXChannelOrder(device);
274 data->thread = StartThread(WaveProc, device);
275 if(data->thread == NULL)
277 free(data->buffer);
278 data->buffer = NULL;
279 return ALC_FALSE;
282 return ALC_TRUE;
285 static void wave_stop_playback(ALCdevice *device)
287 wave_data *data = (wave_data*)device->ExtraData;
288 ALuint dataLen;
289 long size;
291 if(!data->thread)
292 return;
294 data->killNow = 1;
295 StopThread(data->thread);
296 data->thread = NULL;
298 data->killNow = 0;
300 free(data->buffer);
301 data->buffer = NULL;
303 size = ftell(data->f);
304 if(size > 0)
306 dataLen = size - data->DataStart;
307 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
309 fputc(dataLen&0xff, data->f); // 'data' header len
310 fputc((dataLen>>8)&0xff, data->f);
311 fputc((dataLen>>16)&0xff, data->f);
312 fputc((dataLen>>24)&0xff, data->f);
314 if(fseek(data->f, 4, SEEK_SET) == 0)
316 size -= 8;
317 fputc(size&0xff, data->f); // 'WAVE' header len
318 fputc((size>>8)&0xff, data->f);
319 fputc((size>>16)&0xff, data->f);
320 fputc((size>>24)&0xff, data->f);
326 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
328 (void)pDevice;
329 (void)deviceName;
330 return ALC_FALSE;
334 BackendFuncs wave_funcs = {
335 wave_open_playback,
336 wave_close_playback,
337 wave_reset_playback,
338 wave_stop_playback,
339 wave_open_capture,
340 NULL,
341 NULL,
342 NULL,
343 NULL,
344 NULL
347 void alc_wave_init(BackendFuncs *func_list)
349 *func_list = wave_funcs;
352 void alc_wave_deinit(void)
356 void alc_wave_probe(int type)
358 if(!ConfigValueExists("wave", "file"))
359 return;
361 if(type == DEVICE_PROBE)
362 AppendDeviceList(waveDevice);
363 else if(type == ALL_DEVICE_PROBE)
364 AppendAllDeviceList(waveDevice);