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
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
[] = {
57 0x1 | 0x2, /* Stereo */
59 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
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
)
70 fputc((val
>>8)&0xff, f
);
73 static void fwrite32le(ALuint val
, FILE *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
;
90 const ALuint restTime
= (ALuint64
)Device
->UpdateSize
* 1000 /
91 Device
->Frequency
/ 2;
93 frameSize
= FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
);
96 start
= timeGetTime();
97 while(!data
->killNow
&& Device
->Connected
)
101 avail
= (ALuint64
)(now
-start
) * Device
->Frequency
/ 1000;
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
;
109 if(avail
-done
< Device
->UpdateSize
)
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
;
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
);
144 fs
= fwrite(data
->buffer
, frameSize
, Device
->UpdateSize
,
150 ERR("Error writing to file\n");
151 aluHandleDisconnect(Device
);
160 static ALCenum
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
165 fname
= GetConfigValue("wave", "file", "");
167 return ALC_INVALID_VALUE
;
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");
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
;
189 static void wave_close_playback(ALCdevice
*device
)
191 wave_data
*data
= (wave_data
*)device
->ExtraData
;
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;
204 fseek(data
->f
, 0, SEEK_SET
);
207 switch(device
->FmtType
)
210 device
->FmtType
= DevFmtUByte
;
213 device
->FmtType
= DevFmtShort
;
216 device
->FmtType
= DevFmtInt
;
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
);
257 fprintf(data
->f
, "data");
258 fwrite32le(0xFFFFFFFF, data
->f
); // 'data' header len; filled in at close
262 ERR("Error writing header: %s\n", strerror(errno
));
265 data
->DataStart
= ftell(data
->f
);
267 SetDefaultWFXChannelOrder(device
);
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
);
280 ERR("Buffer malloc failed\n");
284 data
->thread
= StartThread(WaveProc
, device
);
285 if(data
->thread
== NULL
)
295 static void wave_stop_playback(ALCdevice
*device
)
297 wave_data
*data
= (wave_data
*)device
->ExtraData
;
305 StopThread(data
->thread
);
313 size
= ftell(data
->f
);
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 const BackendFuncs wave_funcs
= {
337 ALCdevice_LockDefault
,
338 ALCdevice_UnlockDefault
,
339 ALCdevice_GetLatencyDefault
342 ALCboolean
alc_wave_init(BackendFuncs
*func_list
)
344 *func_list
= wave_funcs
;
348 void alc_wave_deinit(void)
352 void alc_wave_probe(enum DevProbe type
)
354 if(!ConfigValueExists("wave", "file"))
359 case ALL_DEVICE_PROBE
:
360 AppendAllDevicesList(waveDevice
);
362 case CAPTURE_DEVICE_PROBE
: