Add a device method to retrieve the active latency
[openal-soft.git] / Alc / backends / wave.c
blobfb118ee856991077d73be21101e4f2fece2e37b3
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 *Device = (ALCdevice*)ptr;
85 wave_data *data = (wave_data*)Device->ExtraData;
86 ALuint frameSize;
87 ALuint now, start;
88 ALuint64 avail, done;
89 size_t fs;
90 const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
91 Device->Frequency / 2;
93 frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
95 done = 0;
96 start = timeGetTime();
97 while(!data->killNow && Device->Connected)
99 now = timeGetTime();
101 avail = (ALuint64)(now-start) * Device->Frequency / 1000;
102 if(avail < done)
104 /* Timer wrapped (50 days???). Add the remainder of the cycle to
105 * the available count and reset the number of samples done */
106 avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done;
107 done = 0;
109 if(avail-done < Device->UpdateSize)
111 Sleep(restTime);
112 continue;
115 while(avail-done >= Device->UpdateSize)
117 aluMixData(Device, data->buffer, Device->UpdateSize);
118 done += Device->UpdateSize;
120 if(!IS_LITTLE_ENDIAN)
122 ALuint bytesize = BytesFromDevFmt(Device->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
144 fs = fwrite(data->buffer, frameSize, Device->UpdateSize,
145 data->f);
146 fs = fs;
148 if(ferror(data->f))
150 ERR("Error writing to file\n");
151 aluHandleDisconnect(Device);
152 break;
157 return 0;
160 static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
162 wave_data *data;
163 const char *fname;
165 fname = GetConfigValue("wave", "file", "");
166 if(!fname[0])
167 return ALC_INVALID_VALUE;
169 if(!deviceName)
170 deviceName = waveDevice;
171 else if(strcmp(deviceName, waveDevice) != 0)
172 return ALC_INVALID_VALUE;
174 data = (wave_data*)calloc(1, sizeof(wave_data));
176 data->f = fopen(fname, "wb");
177 if(!data->f)
179 free(data);
180 ERR("Could not open file '%s': %s\n", fname, strerror(errno));
181 return ALC_INVALID_VALUE;
184 device->DeviceName = strdup(deviceName);
185 device->ExtraData = data;
186 return ALC_NO_ERROR;
189 static void wave_close_playback(ALCdevice *device)
191 wave_data *data = (wave_data*)device->ExtraData;
193 fclose(data->f);
194 free(data);
195 device->ExtraData = NULL;
198 static ALCboolean wave_reset_playback(ALCdevice *device)
200 wave_data *data = (wave_data*)device->ExtraData;
201 ALuint channels=0, bits=0;
202 size_t val;
204 fseek(data->f, 0, SEEK_SET);
205 clearerr(data->f);
207 switch(device->FmtType)
209 case DevFmtByte:
210 device->FmtType = DevFmtUByte;
211 break;
212 case DevFmtUShort:
213 device->FmtType = DevFmtShort;
214 break;
215 case DevFmtUInt:
216 device->FmtType = DevFmtInt;
217 break;
218 case DevFmtUByte:
219 case DevFmtShort:
220 case DevFmtInt:
221 case DevFmtFloat:
222 break;
224 bits = BytesFromDevFmt(device->FmtType) * 8;
225 channels = ChannelsFromDevFmt(device->FmtChans);
227 fprintf(data->f, "RIFF");
228 fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
230 fprintf(data->f, "WAVE");
232 fprintf(data->f, "fmt ");
233 fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
235 // 16-bit val, format type id (extensible: 0xFFFE)
236 fwrite16le(0xFFFE, data->f);
237 // 16-bit val, channel count
238 fwrite16le(channels, data->f);
239 // 32-bit val, frequency
240 fwrite32le(device->Frequency, data->f);
241 // 32-bit val, bytes per second
242 fwrite32le(device->Frequency * channels * bits / 8, data->f);
243 // 16-bit val, frame size
244 fwrite16le(channels * bits / 8, data->f);
245 // 16-bit val, bits per sample
246 fwrite16le(bits, data->f);
247 // 16-bit val, extra byte count
248 fwrite16le(22, data->f);
249 // 16-bit val, valid bits per sample
250 fwrite16le(bits, data->f);
251 // 32-bit val, channel mask
252 fwrite32le(channel_masks[channels], data->f);
253 // 16 byte GUID, sub-type format
254 val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
255 val = val;
257 fprintf(data->f, "data");
258 fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
260 if(ferror(data->f))
262 ERR("Error writing header: %s\n", strerror(errno));
263 return ALC_FALSE;
265 data->DataStart = ftell(data->f);
267 SetDefaultWFXChannelOrder(device);
269 return ALC_TRUE;
272 static ALCboolean wave_start_playback(ALCdevice *device)
274 wave_data *data = (wave_data*)device->ExtraData;
276 data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
277 data->buffer = malloc(data->size);
278 if(!data->buffer)
280 ERR("Buffer malloc failed\n");
281 return ALC_FALSE;
284 data->thread = StartThread(WaveProc, device);
285 if(data->thread == NULL)
287 free(data->buffer);
288 data->buffer = NULL;
289 return ALC_FALSE;
292 return ALC_TRUE;
295 static void wave_stop_playback(ALCdevice *device)
297 wave_data *data = (wave_data*)device->ExtraData;
298 ALuint dataLen;
299 long size;
301 if(!data->thread)
302 return;
304 data->killNow = 1;
305 StopThread(data->thread);
306 data->thread = NULL;
308 data->killNow = 0;
310 free(data->buffer);
311 data->buffer = NULL;
313 size = ftell(data->f);
314 if(size > 0)
316 dataLen = size - data->DataStart;
317 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
318 fwrite32le(dataLen, data->f); // 'data' header len
319 if(fseek(data->f, 4, SEEK_SET) == 0)
320 fwrite32le(size-8, data->f); // 'WAVE' header len
325 static ALint64 wave_get_latency(ALCdevice *device)
327 /* FIXME: time until next update + "safe" offset */
328 (void)device;
329 return 0;
333 static const BackendFuncs wave_funcs = {
334 wave_open_playback,
335 wave_close_playback,
336 wave_reset_playback,
337 wave_start_playback,
338 wave_stop_playback,
339 NULL,
340 NULL,
341 NULL,
342 NULL,
343 NULL,
344 NULL,
345 wave_get_latency
348 ALCboolean alc_wave_init(BackendFuncs *func_list)
350 *func_list = wave_funcs;
351 return ALC_TRUE;
354 void alc_wave_deinit(void)
358 void alc_wave_probe(enum DevProbe type)
360 if(!ConfigValueExists("wave", "file"))
361 return;
363 switch(type)
365 case ALL_DEVICE_PROBE:
366 AppendAllDevicesList(waveDevice);
367 break;
368 case CAPTURE_DEVICE_PROBE:
369 break;