Remove 'v' from the source enum names
[openal-soft.git] / Alc / backends / wave.c
blobbe528c9a0404c8b1712934036261af39b4d67d2c
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 ALCdevice_Lock(Device);
155 aluHandleDisconnect(Device);
156 ALCdevice_Unlock(Device);
157 break;
162 return 0;
165 static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
167 wave_data *data;
168 const char *fname;
170 fname = GetConfigValue("wave", "file", "");
171 if(!fname[0])
172 return ALC_INVALID_VALUE;
174 if(!deviceName)
175 deviceName = waveDevice;
176 else if(strcmp(deviceName, waveDevice) != 0)
177 return ALC_INVALID_VALUE;
179 data = (wave_data*)calloc(1, sizeof(wave_data));
181 data->f = fopen(fname, "wb");
182 if(!data->f)
184 free(data);
185 ERR("Could not open file '%s': %s\n", fname, strerror(errno));
186 return ALC_INVALID_VALUE;
189 device->DeviceName = strdup(deviceName);
190 device->ExtraData = data;
191 return ALC_NO_ERROR;
194 static void wave_close_playback(ALCdevice *device)
196 wave_data *data = (wave_data*)device->ExtraData;
198 fclose(data->f);
199 free(data);
200 device->ExtraData = NULL;
203 static ALCboolean wave_reset_playback(ALCdevice *device)
205 wave_data *data = (wave_data*)device->ExtraData;
206 ALuint channels=0, bits=0;
207 size_t val;
209 fseek(data->f, 0, SEEK_SET);
210 clearerr(data->f);
212 switch(device->FmtType)
214 case DevFmtByte:
215 device->FmtType = DevFmtUByte;
216 break;
217 case DevFmtUShort:
218 device->FmtType = DevFmtShort;
219 break;
220 case DevFmtUInt:
221 device->FmtType = DevFmtInt;
222 break;
223 case DevFmtUByte:
224 case DevFmtShort:
225 case DevFmtInt:
226 case DevFmtFloat:
227 break;
229 bits = BytesFromDevFmt(device->FmtType) * 8;
230 channels = ChannelsFromDevFmt(device->FmtChans);
232 fprintf(data->f, "RIFF");
233 fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
235 fprintf(data->f, "WAVE");
237 fprintf(data->f, "fmt ");
238 fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
240 // 16-bit val, format type id (extensible: 0xFFFE)
241 fwrite16le(0xFFFE, data->f);
242 // 16-bit val, channel count
243 fwrite16le(channels, data->f);
244 // 32-bit val, frequency
245 fwrite32le(device->Frequency, data->f);
246 // 32-bit val, bytes per second
247 fwrite32le(device->Frequency * channels * bits / 8, data->f);
248 // 16-bit val, frame size
249 fwrite16le(channels * bits / 8, data->f);
250 // 16-bit val, bits per sample
251 fwrite16le(bits, data->f);
252 // 16-bit val, extra byte count
253 fwrite16le(22, data->f);
254 // 16-bit val, valid bits per sample
255 fwrite16le(bits, data->f);
256 // 32-bit val, channel mask
257 fwrite32le(channel_masks[channels], data->f);
258 // 16 byte GUID, sub-type format
259 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
260 val = val;
262 fprintf(data->f, "data");
263 fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
265 if(ferror(data->f))
267 ERR("Error writing header: %s\n", strerror(errno));
268 return ALC_FALSE;
270 data->DataStart = ftell(data->f);
272 SetDefaultWFXChannelOrder(device);
274 return ALC_TRUE;
277 static ALCboolean wave_start_playback(ALCdevice *device)
279 wave_data *data = (wave_data*)device->ExtraData;
281 data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
282 data->buffer = malloc(data->size);
283 if(!data->buffer)
285 ERR("Buffer malloc failed\n");
286 return ALC_FALSE;
289 data->thread = StartThread(WaveProc, device);
290 if(data->thread == NULL)
292 free(data->buffer);
293 data->buffer = NULL;
294 return ALC_FALSE;
297 return ALC_TRUE;
300 static void wave_stop_playback(ALCdevice *device)
302 wave_data *data = (wave_data*)device->ExtraData;
303 ALuint dataLen;
304 long size;
306 if(!data->thread)
307 return;
309 data->killNow = 1;
310 StopThread(data->thread);
311 data->thread = NULL;
313 data->killNow = 0;
315 free(data->buffer);
316 data->buffer = NULL;
318 size = ftell(data->f);
319 if(size > 0)
321 dataLen = size - data->DataStart;
322 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
323 fwrite32le(dataLen, data->f); // 'data' header len
324 if(fseek(data->f, 4, SEEK_SET) == 0)
325 fwrite32le(size-8, data->f); // 'WAVE' header len
330 static const BackendFuncs wave_funcs = {
331 wave_open_playback,
332 wave_close_playback,
333 wave_reset_playback,
334 wave_start_playback,
335 wave_stop_playback,
336 NULL,
337 NULL,
338 NULL,
339 NULL,
340 NULL,
341 NULL,
342 ALCdevice_LockDefault,
343 ALCdevice_UnlockDefault,
344 ALCdevice_GetLatencyDefault
347 ALCboolean alc_wave_init(BackendFuncs *func_list)
349 *func_list = wave_funcs;
350 return ALC_TRUE;
353 void alc_wave_deinit(void)
357 void alc_wave_probe(enum DevProbe type)
359 if(!ConfigValueExists("wave", "file"))
360 return;
362 switch(type)
364 case ALL_DEVICE_PROBE:
365 AppendAllDevicesList(waveDevice);
366 break;
367 case CAPTURE_DEVICE_PROBE:
368 break;