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
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
[] = {
60 0x1 | 0x2, /* Stereo */
62 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
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 */
69 static ALuint
WaveProc(ALvoid
*ptr
)
71 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
72 wave_data
*data
= (wave_data
*)pDevice
->ExtraData
;
79 char b
[sizeof(short)];
81 const ALuint restTime
= (ALuint64
)pDevice
->UpdateSize
* 1000 /
82 pDevice
->Frequency
/ 2;
85 frameSize
= aluFrameSizeFromFormat(pDevice
->Format
);
88 start
= data
->startTime
;
89 while(!data
->killNow
&& pDevice
->Connected
)
93 avail
= (ALuint64
)(now
-start
) * pDevice
->Frequency
/ 1000;
96 AL_PRINT("Timer wrapped\n");
97 aluHandleDisconnect(pDevice
);
100 if(avail
-done
< pDevice
->UpdateSize
)
106 while(avail
-done
>= pDevice
->UpdateSize
)
108 aluMixData(pDevice
, data
->buffer
, pDevice
->UpdateSize
);
109 done
+= pDevice
->UpdateSize
;
113 ALubyte
*bytes
= data
->buffer
;
116 if(aluBytesFromFormat(pDevice
->Format
) == 1)
118 for(i
= 0;i
< data
->size
;i
++)
119 fputc(bytes
[i
], data
->f
);
121 else if(aluBytesFromFormat(pDevice
->Format
) == 2)
123 for(i
= 0;i
< data
->size
;i
++)
124 fputc(bytes
[i
^1], data
->f
);
126 else if(aluBytesFromFormat(pDevice
->Format
) == 4)
128 for(i
= 0;i
< data
->size
;i
++)
129 fputc(bytes
[i
^3], data
->f
);
133 fs
= fwrite(data
->buffer
, frameSize
, pDevice
->UpdateSize
,
137 AL_PRINT("Error writing to file\n");
138 aluHandleDisconnect(pDevice
);
147 static ALCboolean
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
152 fname
= GetConfigValue("wave", "file", "");
157 deviceName
= waveDevice
;
158 else if(strcmp(deviceName
, waveDevice
) != 0)
161 data
= (wave_data
*)calloc(1, sizeof(wave_data
));
163 data
->f
= fopen(fname
, "wb");
167 AL_PRINT("Could not open file '%s': %s\n", fname
, strerror(errno
));
171 device
->szDeviceName
= strdup(deviceName
);
172 device
->ExtraData
= data
;
176 static void wave_close_playback(ALCdevice
*device
)
178 wave_data
*data
= (wave_data
*)device
->ExtraData
;
182 device
->ExtraData
= NULL
;
185 static ALCboolean
wave_reset_playback(ALCdevice
*device
)
187 wave_data
*data
= (wave_data
*)device
->ExtraData
;
188 ALuint channels
, bits
, i
;
191 fseek(data
->f
, 0, SEEK_SET
);
194 bits
= aluBytesFromFormat(device
->Format
) * 8;
195 channels
= aluChannelsFromFormat(device
->Format
);
201 device
->Format
= AL_FORMAT_71CHN8
;
203 device
->Format
= AL_FORMAT_71CHN16
;
206 device
->Format
= AL_FORMAT_71CHN32
;
212 fprintf(data
->f
, "RIFF");
213 fputc(0xFF, data
->f
); // 'RIFF' header len; filled in at close
214 fputc(0xFF, data
->f
);
215 fputc(0xFF, data
->f
);
216 fputc(0xFF, data
->f
);
218 fprintf(data
->f
, "WAVE");
220 fprintf(data
->f
, "fmt ");
221 fputc(40, data
->f
); // 'fmt ' header len; 40 bytes for EXTENSIBLE
225 // 16-bit val, format type id (extensible: 0xFFFE)
226 fputc(0xFE, data
->f
);
227 fputc(0xFF, data
->f
);
228 // 16-bit val, channel count
229 fputc(channels
&0xff, data
->f
);
230 fputc((channels
>>8)&0xff, data
->f
);
231 // 32-bit val, frequency
232 fputc(device
->Frequency
&0xff, data
->f
);
233 fputc((device
->Frequency
>>8)&0xff, data
->f
);
234 fputc((device
->Frequency
>>16)&0xff, data
->f
);
235 fputc((device
->Frequency
>>24)&0xff, data
->f
);
236 // 32-bit val, bytes per second
237 i
= device
->Frequency
* channels
* bits
/ 8;
238 fputc(i
&0xff, data
->f
);
239 fputc((i
>>8)&0xff, data
->f
);
240 fputc((i
>>16)&0xff, data
->f
);
241 fputc((i
>>24)&0xff, data
->f
);
242 // 16-bit val, frame size
243 i
= channels
* bits
/ 8;
244 fputc(i
&0xff, data
->f
);
245 fputc((i
>>8)&0xff, data
->f
);
246 // 16-bit val, bits per sample
247 fputc(bits
&0xff, data
->f
);
248 fputc((bits
>>8)&0xff, data
->f
);
249 // 16-bit val, extra byte count
252 // 16-bit val, valid bits per sample
253 fputc(bits
&0xff, data
->f
);
254 fputc((bits
>>8)&0xff, data
->f
);
255 // 32-bit val, channel mask
256 i
= channel_masks
[channels
];
257 fputc(i
&0xff, data
->f
);
258 fputc((i
>>8)&0xff, data
->f
);
259 fputc((i
>>16)&0xff, data
->f
);
260 fputc((i
>>24)&0xff, data
->f
);
261 // 16 byte GUID, sub-type format
262 val
= fwrite(((bits
==32) ? SUBTYPE_FLOAT
: SUBTYPE_PCM
), 1, 16, data
->f
);
264 fprintf(data
->f
, "data");
265 fputc(0xFF, data
->f
); // 'data' header len; filled in at close
266 fputc(0xFF, data
->f
);
267 fputc(0xFF, data
->f
);
268 fputc(0xFF, data
->f
);
272 AL_PRINT("Error writing header: %s\n", strerror(errno
));
276 data
->DataStart
= ftell(data
->f
);
278 data
->size
= device
->UpdateSize
* channels
* bits
/ 8;
279 data
->buffer
= malloc(data
->size
);
282 AL_PRINT("buffer malloc failed\n");
286 SetDefaultWFXChannelOrder(device
);
287 device
->TimeRes
= 1000000;
289 data
->startTime
= timeGetTime();
290 data
->thread
= StartThread(WaveProc
, device
);
291 if(data
->thread
== NULL
)
301 static void wave_stop_playback(ALCdevice
*device
)
303 wave_data
*data
= (wave_data
*)device
->ExtraData
;
312 StopThread(data
->thread
);
317 ext
= timeGetTime() - data
->startTime
;
318 data
->baseTime
+= (ALuint64
)ext
* 1000000;
323 size
= ftell(data
->f
);
326 dataLen
= size
- data
->DataStart
;
327 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
329 fputc(dataLen
&0xff, data
->f
); // 'data' header len
330 fputc((dataLen
>>8)&0xff, data
->f
);
331 fputc((dataLen
>>16)&0xff, data
->f
);
332 fputc((dataLen
>>24)&0xff, data
->f
);
334 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
337 fputc(size
&0xff, data
->f
); // 'WAVE' header len
338 fputc((size
>>8)&0xff, data
->f
);
339 fputc((size
>>16)&0xff, data
->f
);
340 fputc((size
>>24)&0xff, data
->f
);
346 static ALCboolean
wave_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
353 static ALuint64
wave_get_time(ALCdevice
*Device
)
355 wave_data
*data
= (wave_data
*)Device
->ExtraData
;
358 ext
= timeGetTime() - data
->startTime
;
359 return data
->baseTime
+ ((ALuint64
)ext
* 1000000);
363 BackendFuncs wave_funcs
= {
377 void alc_wave_init(BackendFuncs
*func_list
)
379 *func_list
= wave_funcs
;
382 void alc_wave_deinit(void)
386 void alc_wave_probe(int type
)
388 if(!ConfigValueExists("wave", "file"))
391 if(type
== DEVICE_PROBE
)
392 AppendDeviceList(waveDevice
);
393 else if(type
== ALL_DEVICE_PROBE
)
394 AppendAllDeviceList(waveDevice
);