Always use "OpenAL Soft" for the short device enumeration list
[openal-soft.git] / Alc / backends / wave.c
blob5ef363d337e8a04a97cb8cebb473ad0d4e0dd8fc
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 */
67 static void fwrite16le(ALushort val, FILE *f)
69 fputc(val&0xff, f);
70 fputc((val>>8)&0xff, f);
73 static void fwrite32le(ALuint val, FILE *f)
75 fputc(val&0xff, f);
76 fputc((val>>8)&0xff, f);
77 fputc((val>>16)&0xff, f);
78 fputc((val>>24)&0xff, f);
82 static ALuint WaveProc(ALvoid *ptr)
84 ALCdevice *pDevice = (ALCdevice*)ptr;
85 wave_data *data = (wave_data*)pDevice->ExtraData;
86 ALuint frameSize;
87 ALuint now, start;
88 ALuint64 avail, done;
89 size_t fs;
90 const ALuint restTime = (ALuint64)pDevice->UpdateSize * 1000 /
91 pDevice->Frequency / 2;
93 frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
95 done = 0;
96 start = timeGetTime();
97 while(!data->killNow && pDevice->Connected)
99 now = timeGetTime();
101 avail = (ALuint64)(now-start) * pDevice->Frequency / 1000;
102 if(avail < done)
104 /* Timer wrapped. Add the remainder of the cycle to the available
105 * count and reset the number of samples done */
106 avail += (ALuint64)0xFFFFFFFFu*pDevice->Frequency/1000 - done;
107 done = 0;
109 if(avail-done < pDevice->UpdateSize)
111 Sleep(restTime);
112 continue;
115 while(avail-done >= pDevice->UpdateSize)
117 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
118 done += pDevice->UpdateSize;
120 if(!IS_LITTLE_ENDIAN)
122 ALuint bytesize = BytesFromDevFmt(pDevice->FmtType);
123 ALubyte *bytes = data->buffer;
124 ALuint i;
126 if(bytesize == 1)
128 for(i = 0;i < data->size;i++)
129 fputc(bytes[i], data->f);
131 else if(bytesize == 2)
133 for(i = 0;i < data->size;i++)
134 fputc(bytes[i^1], data->f);
136 else if(bytesize == 4)
138 for(i = 0;i < data->size;i++)
139 fputc(bytes[i^3], data->f);
142 else
143 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
144 data->f);
145 if(ferror(data->f))
147 ERR("Error writing to file\n");
148 aluHandleDisconnect(pDevice);
149 break;
154 return 0;
157 static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
159 wave_data *data;
160 const char *fname;
162 fname = GetConfigValue("wave", "file", "");
163 if(!fname[0])
164 return ALC_INVALID_VALUE;
166 if(!deviceName)
167 deviceName = waveDevice;
168 else if(strcmp(deviceName, waveDevice) != 0)
169 return ALC_INVALID_VALUE;
171 data = (wave_data*)calloc(1, sizeof(wave_data));
173 data->f = fopen(fname, "wb");
174 if(!data->f)
176 free(data);
177 ERR("Could not open file '%s': %s\n", fname, strerror(errno));
178 return ALC_INVALID_VALUE;
181 device->szDeviceName = strdup(deviceName);
182 device->ExtraData = data;
183 return ALC_NO_ERROR;
186 static void wave_close_playback(ALCdevice *device)
188 wave_data *data = (wave_data*)device->ExtraData;
190 fclose(data->f);
191 free(data);
192 device->ExtraData = NULL;
195 static ALCboolean wave_reset_playback(ALCdevice *device)
197 wave_data *data = (wave_data*)device->ExtraData;
198 ALuint channels=0, bits=0;
199 size_t val;
201 fseek(data->f, 0, SEEK_SET);
202 clearerr(data->f);
204 switch(device->FmtType)
206 case DevFmtByte:
207 device->FmtType = DevFmtUByte;
208 break;
209 case DevFmtUShort:
210 device->FmtType = DevFmtShort;
211 break;
212 case DevFmtUInt:
213 device->FmtType = DevFmtInt;
214 break;
215 case DevFmtUByte:
216 case DevFmtShort:
217 case DevFmtInt:
218 case DevFmtFloat:
219 break;
221 bits = BytesFromDevFmt(device->FmtType) * 8;
222 channels = ChannelsFromDevFmt(device->FmtChans);
224 fprintf(data->f, "RIFF");
225 fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
227 fprintf(data->f, "WAVE");
229 fprintf(data->f, "fmt ");
230 fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
232 // 16-bit val, format type id (extensible: 0xFFFE)
233 fwrite16le(0xFFFE, data->f);
234 // 16-bit val, channel count
235 fwrite16le(channels, data->f);
236 // 32-bit val, frequency
237 fwrite32le(device->Frequency, data->f);
238 // 32-bit val, bytes per second
239 fwrite32le(device->Frequency * channels * bits / 8, data->f);
240 // 16-bit val, frame size
241 fwrite16le(channels * bits / 8, data->f);
242 // 16-bit val, bits per sample
243 fwrite16le(bits, data->f);
244 // 16-bit val, extra byte count
245 fwrite16le(22, data->f);
246 // 16-bit val, valid bits per sample
247 fwrite16le(bits, data->f);
248 // 32-bit val, channel mask
249 fwrite32le(channel_masks[channels], data->f);
250 // 16 byte GUID, sub-type format
251 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
253 fprintf(data->f, "data");
254 fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
256 if(ferror(data->f))
258 ERR("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 ERR("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)
308 fwrite32le(dataLen, data->f); // 'data' header len
309 if(fseek(data->f, 4, SEEK_SET) == 0)
310 fwrite32le(size-8, data->f); // 'WAVE' header len
315 static const BackendFuncs wave_funcs = {
316 wave_open_playback,
317 wave_close_playback,
318 wave_reset_playback,
319 wave_stop_playback,
320 NULL,
321 NULL,
322 NULL,
323 NULL,
324 NULL,
325 NULL
328 ALCboolean alc_wave_init(BackendFuncs *func_list)
330 *func_list = wave_funcs;
331 return ALC_TRUE;
334 void alc_wave_deinit(void)
338 void alc_wave_probe(enum DevProbe type)
340 if(!ConfigValueExists("wave", "file"))
341 return;
343 switch(type)
345 case ALL_DEVICE_PROBE:
346 AppendAllDeviceList(waveDevice);
347 break;
348 case CAPTURE_DEVICE_PROBE:
349 break;