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
49 static const ALCchar waveDevice
[] = "Wave File Writer";
51 static const ALubyte SUBTYPE_PCM
[] = {
52 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
53 0x00, 0x38, 0x9b, 0x71
55 static const ALubyte SUBTYPE_FLOAT
[] = {
56 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
57 0x00, 0x38, 0x9b, 0x71
60 static const ALuint channel_masks
[] = {
63 0x1 | 0x2, /* Stereo */
65 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
67 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
68 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
69 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
73 static void fwrite16le(ALushort val
, FILE *f
)
76 fputc((val
>>8)&0xff, f
);
79 static void fwrite32le(ALuint val
, FILE *f
)
82 fputc((val
>>8)&0xff, f
);
83 fputc((val
>>16)&0xff, f
);
84 fputc((val
>>24)&0xff, f
);
88 static int WaveProc(void *ptr
)
90 ALCdevice
*device
= (ALCdevice
*)ptr
;
91 wave_data
*data
= (wave_data
*)device
->ExtraData
;
92 struct timespec now
, start
;
96 const long restTime
= (long)((ALuint64
)device
->UpdateSize
* 1000000000 /
97 device
->Frequency
/ 2);
99 althrd_setname(althrd_current(), MIXER_THREAD_NAME
);
101 frameSize
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
104 if(altimespec_get(&start
, AL_TIME_UTC
) != AL_TIME_UTC
)
106 ERR("Failed to get starting time\n");
109 while(!data
->killNow
&& device
->Connected
)
111 if(altimespec_get(&now
, AL_TIME_UTC
) != AL_TIME_UTC
)
113 ERR("Failed to get current time\n");
117 avail
= (now
.tv_sec
- start
.tv_sec
) * device
->Frequency
;
118 avail
+= (ALint64
)(now
.tv_nsec
- start
.tv_nsec
) * device
->Frequency
/ 1000000000;
121 /* Oops, time skipped backwards. Reset the number of samples done
122 * with one update available since we (likely) just came back from
124 done
= avail
- device
->UpdateSize
;
127 if(avail
-done
< device
->UpdateSize
)
128 al_nssleep(0, restTime
);
129 else while(avail
-done
>= device
->UpdateSize
)
131 aluMixData(device
, data
->buffer
, device
->UpdateSize
);
132 done
+= device
->UpdateSize
;
134 if(!IS_LITTLE_ENDIAN
)
136 ALuint bytesize
= BytesFromDevFmt(device
->FmtType
);
137 ALubyte
*bytes
= data
->buffer
;
142 for(i
= 0;i
< data
->size
;i
++)
143 fputc(bytes
[i
], data
->f
);
145 else if(bytesize
== 2)
147 for(i
= 0;i
< data
->size
;i
++)
148 fputc(bytes
[i
^1], data
->f
);
150 else if(bytesize
== 4)
152 for(i
= 0;i
< data
->size
;i
++)
153 fputc(bytes
[i
^3], data
->f
);
158 fs
= fwrite(data
->buffer
, frameSize
, device
->UpdateSize
,
164 ERR("Error writing to file\n");
165 ALCdevice_Lock(device
);
166 aluHandleDisconnect(device
);
167 ALCdevice_Unlock(device
);
176 static ALCenum
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
181 fname
= GetConfigValue("wave", "file", "");
183 return ALC_INVALID_VALUE
;
186 deviceName
= waveDevice
;
187 else if(strcmp(deviceName
, waveDevice
) != 0)
188 return ALC_INVALID_VALUE
;
190 data
= (wave_data
*)calloc(1, sizeof(wave_data
));
192 data
->f
= al_fopen(fname
, "wb");
196 ERR("Could not open file '%s': %s\n", fname
, strerror(errno
));
197 return ALC_INVALID_VALUE
;
200 al_string_copy_cstr(&device
->DeviceName
, deviceName
);
201 device
->ExtraData
= data
;
205 static void wave_close_playback(ALCdevice
*device
)
207 wave_data
*data
= (wave_data
*)device
->ExtraData
;
211 device
->ExtraData
= NULL
;
214 static ALCboolean
wave_reset_playback(ALCdevice
*device
)
216 wave_data
*data
= (wave_data
*)device
->ExtraData
;
217 ALuint channels
=0, bits
=0;
220 fseek(data
->f
, 0, SEEK_SET
);
223 switch(device
->FmtType
)
226 device
->FmtType
= DevFmtUByte
;
229 device
->FmtType
= DevFmtShort
;
232 device
->FmtType
= DevFmtInt
;
240 bits
= BytesFromDevFmt(device
->FmtType
) * 8;
241 channels
= ChannelsFromDevFmt(device
->FmtChans
);
243 fprintf(data
->f
, "RIFF");
244 fwrite32le(0xFFFFFFFF, data
->f
); // 'RIFF' header len; filled in at close
246 fprintf(data
->f
, "WAVE");
248 fprintf(data
->f
, "fmt ");
249 fwrite32le(40, data
->f
); // 'fmt ' header len; 40 bytes for EXTENSIBLE
251 // 16-bit val, format type id (extensible: 0xFFFE)
252 fwrite16le(0xFFFE, data
->f
);
253 // 16-bit val, channel count
254 fwrite16le(channels
, data
->f
);
255 // 32-bit val, frequency
256 fwrite32le(device
->Frequency
, data
->f
);
257 // 32-bit val, bytes per second
258 fwrite32le(device
->Frequency
* channels
* bits
/ 8, data
->f
);
259 // 16-bit val, frame size
260 fwrite16le(channels
* bits
/ 8, data
->f
);
261 // 16-bit val, bits per sample
262 fwrite16le(bits
, data
->f
);
263 // 16-bit val, extra byte count
264 fwrite16le(22, data
->f
);
265 // 16-bit val, valid bits per sample
266 fwrite16le(bits
, data
->f
);
267 // 32-bit val, channel mask
268 fwrite32le(channel_masks
[channels
], data
->f
);
269 // 16 byte GUID, sub-type format
270 val
= fwrite(((bits
==32) ? SUBTYPE_FLOAT
: SUBTYPE_PCM
), 1, 16, data
->f
);
273 fprintf(data
->f
, "data");
274 fwrite32le(0xFFFFFFFF, data
->f
); // 'data' header len; filled in at close
278 ERR("Error writing header: %s\n", strerror(errno
));
281 data
->DataStart
= ftell(data
->f
);
283 SetDefaultWFXChannelOrder(device
);
288 static ALCboolean
wave_start_playback(ALCdevice
*device
)
290 wave_data
*data
= (wave_data
*)device
->ExtraData
;
292 data
->size
= device
->UpdateSize
* FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
293 data
->buffer
= malloc(data
->size
);
296 ERR("Buffer malloc failed\n");
301 if(althrd_create(&data
->thread
, WaveProc
, device
) != althrd_success
)
311 static void wave_stop_playback(ALCdevice
*device
)
313 wave_data
*data
= (wave_data
*)device
->ExtraData
;
322 althrd_join(data
->thread
, &res
);
327 size
= ftell(data
->f
);
330 dataLen
= size
- data
->DataStart
;
331 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
332 fwrite32le(dataLen
, data
->f
); // 'data' header len
333 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
334 fwrite32le(size
-8, data
->f
); // 'WAVE' header len
339 static const BackendFuncs wave_funcs
= {
351 ALCdevice_GetLatencyDefault
354 ALCboolean
alc_wave_init(BackendFuncs
*func_list
)
356 *func_list
= wave_funcs
;
360 void alc_wave_deinit(void)
364 void alc_wave_probe(enum DevProbe type
)
366 if(!ConfigValueExists("wave", "file"))
371 case ALL_DEVICE_PROBE
:
372 AppendAllDevicesList(waveDevice
);
374 case CAPTURE_DEVICE_PROBE
: