Add an example program showing how to apply reverb to a source
[openal-soft.git] / Alc / backends / wave.c
blob389328195d181530a501d8d423782b07901d608d
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 #ifdef HAVE_WINDOWS_H
27 #include <windows.h>
28 #endif
30 #include "alMain.h"
31 #include "alu.h"
34 typedef struct {
35 FILE *f;
36 long DataStart;
38 ALvoid *buffer;
39 ALuint size;
41 volatile int killNow;
42 ALvoid *thread;
43 } wave_data;
46 static const ALCchar waveDevice[] = "Wave File Writer";
48 static const ALubyte SUBTYPE_PCM[] = {
49 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
50 0x00, 0x38, 0x9b, 0x71
52 static const ALubyte SUBTYPE_FLOAT[] = {
53 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
54 0x00, 0x38, 0x9b, 0x71
57 static const ALuint channel_masks[] = {
58 0, /* invalid */
59 0x4, /* Mono */
60 0x1 | 0x2, /* Stereo */
61 0, /* 3 channel */
62 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
63 0, /* 5 channel */
64 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
65 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
66 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
70 static void fwrite16le(ALushort val, FILE *f)
72 fputc(val&0xff, f);
73 fputc((val>>8)&0xff, f);
76 static void fwrite32le(ALuint val, FILE *f)
78 fputc(val&0xff, f);
79 fputc((val>>8)&0xff, f);
80 fputc((val>>16)&0xff, f);
81 fputc((val>>24)&0xff, f);
85 static ALuint WaveProc(ALvoid *ptr)
87 ALCdevice *Device = (ALCdevice*)ptr;
88 wave_data *data = (wave_data*)Device->ExtraData;
89 ALuint frameSize;
90 ALuint now, start;
91 ALuint64 avail, done;
92 size_t fs;
93 const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
94 Device->Frequency / 2;
96 frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
98 done = 0;
99 start = timeGetTime();
100 while(!data->killNow && Device->Connected)
102 now = timeGetTime();
104 avail = (ALuint64)(now-start) * Device->Frequency / 1000;
105 if(avail < done)
107 /* Timer wrapped (50 days???). Add the remainder of the cycle to
108 * the available count and reset the number of samples done */
109 avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done;
110 done = 0;
112 if(avail-done < Device->UpdateSize)
114 Sleep(restTime);
115 continue;
118 while(avail-done >= Device->UpdateSize)
120 aluMixData(Device, data->buffer, Device->UpdateSize);
121 done += Device->UpdateSize;
123 if(!IS_LITTLE_ENDIAN)
125 ALuint bytesize = BytesFromDevFmt(Device->FmtType);
126 ALubyte *bytes = data->buffer;
127 ALuint i;
129 if(bytesize == 1)
131 for(i = 0;i < data->size;i++)
132 fputc(bytes[i], data->f);
134 else if(bytesize == 2)
136 for(i = 0;i < data->size;i++)
137 fputc(bytes[i^1], data->f);
139 else if(bytesize == 4)
141 for(i = 0;i < data->size;i++)
142 fputc(bytes[i^3], data->f);
145 else
147 fs = fwrite(data->buffer, frameSize, Device->UpdateSize,
148 data->f);
149 fs = fs;
151 if(ferror(data->f))
153 ERR("Error writing to file\n");
154 aluHandleDisconnect(Device);
155 break;
160 return 0;
163 static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
165 wave_data *data;
166 const char *fname;
168 fname = GetConfigValue("wave", "file", "");
169 if(!fname[0])
170 return ALC_INVALID_VALUE;
172 if(!deviceName)
173 deviceName = waveDevice;
174 else if(strcmp(deviceName, waveDevice) != 0)
175 return ALC_INVALID_VALUE;
177 data = (wave_data*)calloc(1, sizeof(wave_data));
179 data->f = fopen(fname, "wb");
180 if(!data->f)
182 free(data);
183 ERR("Could not open file '%s': %s\n", fname, strerror(errno));
184 return ALC_INVALID_VALUE;
187 device->DeviceName = strdup(deviceName);
188 device->ExtraData = data;
189 return ALC_NO_ERROR;
192 static void wave_close_playback(ALCdevice *device)
194 wave_data *data = (wave_data*)device->ExtraData;
196 fclose(data->f);
197 free(data);
198 device->ExtraData = NULL;
201 static ALCboolean wave_reset_playback(ALCdevice *device)
203 wave_data *data = (wave_data*)device->ExtraData;
204 ALuint channels=0, bits=0;
205 size_t val;
207 fseek(data->f, 0, SEEK_SET);
208 clearerr(data->f);
210 switch(device->FmtType)
212 case DevFmtByte:
213 device->FmtType = DevFmtUByte;
214 break;
215 case DevFmtUShort:
216 device->FmtType = DevFmtShort;
217 break;
218 case DevFmtUInt:
219 device->FmtType = DevFmtInt;
220 break;
221 case DevFmtUByte:
222 case DevFmtShort:
223 case DevFmtInt:
224 case DevFmtFloat:
225 break;
227 bits = BytesFromDevFmt(device->FmtType) * 8;
228 channels = ChannelsFromDevFmt(device->FmtChans);
230 fprintf(data->f, "RIFF");
231 fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
233 fprintf(data->f, "WAVE");
235 fprintf(data->f, "fmt ");
236 fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
238 // 16-bit val, format type id (extensible: 0xFFFE)
239 fwrite16le(0xFFFE, data->f);
240 // 16-bit val, channel count
241 fwrite16le(channels, data->f);
242 // 32-bit val, frequency
243 fwrite32le(device->Frequency, data->f);
244 // 32-bit val, bytes per second
245 fwrite32le(device->Frequency * channels * bits / 8, data->f);
246 // 16-bit val, frame size
247 fwrite16le(channels * bits / 8, data->f);
248 // 16-bit val, bits per sample
249 fwrite16le(bits, data->f);
250 // 16-bit val, extra byte count
251 fwrite16le(22, data->f);
252 // 16-bit val, valid bits per sample
253 fwrite16le(bits, data->f);
254 // 32-bit val, channel mask
255 fwrite32le(channel_masks[channels], data->f);
256 // 16 byte GUID, sub-type format
257 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
258 val = val;
260 fprintf(data->f, "data");
261 fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
263 if(ferror(data->f))
265 ERR("Error writing header: %s\n", strerror(errno));
266 return ALC_FALSE;
268 data->DataStart = ftell(data->f);
270 SetDefaultWFXChannelOrder(device);
272 return ALC_TRUE;
275 static ALCboolean wave_start_playback(ALCdevice *device)
277 wave_data *data = (wave_data*)device->ExtraData;
279 data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
280 data->buffer = malloc(data->size);
281 if(!data->buffer)
283 ERR("Buffer malloc failed\n");
284 return ALC_FALSE;
287 data->thread = StartThread(WaveProc, device);
288 if(data->thread == NULL)
290 free(data->buffer);
291 data->buffer = NULL;
292 return ALC_FALSE;
295 return ALC_TRUE;
298 static void wave_stop_playback(ALCdevice *device)
300 wave_data *data = (wave_data*)device->ExtraData;
301 ALuint dataLen;
302 long size;
304 if(!data->thread)
305 return;
307 data->killNow = 1;
308 StopThread(data->thread);
309 data->thread = NULL;
311 data->killNow = 0;
313 free(data->buffer);
314 data->buffer = NULL;
316 size = ftell(data->f);
317 if(size > 0)
319 dataLen = size - data->DataStart;
320 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
321 fwrite32le(dataLen, data->f); // 'data' header len
322 if(fseek(data->f, 4, SEEK_SET) == 0)
323 fwrite32le(size-8, data->f); // 'WAVE' header len
328 static const BackendFuncs wave_funcs = {
329 wave_open_playback,
330 wave_close_playback,
331 wave_reset_playback,
332 wave_start_playback,
333 wave_stop_playback,
334 NULL,
335 NULL,
336 NULL,
337 NULL,
338 NULL,
339 NULL,
340 ALCdevice_LockDefault,
341 ALCdevice_UnlockDefault,
342 ALCdevice_GetLatencyDefault
345 ALCboolean alc_wave_init(BackendFuncs *func_list)
347 *func_list = wave_funcs;
348 return ALC_TRUE;
351 void alc_wave_deinit(void)
355 void alc_wave_probe(enum DevProbe type)
357 if(!ConfigValueExists("wave", "file"))
358 return;
360 switch(type)
362 case ALL_DEVICE_PROBE:
363 AppendAllDevicesList(waveDevice);
364 break;
365 case CAPTURE_DEVICE_PROBE:
366 break;