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
*pDevice
= (ALCdevice
*)ptr
;
85 wave_data
*data
= (wave_data
*)pDevice
->ExtraData
;
92 char b
[sizeof(short)];
94 const ALuint restTime
= (ALuint64
)pDevice
->UpdateSize
* 1000 /
95 pDevice
->Frequency
/ 2;
98 frameSize
= aluFrameSizeFromFormat(pDevice
->Format
);
101 start
= timeGetTime();
102 while(!data
->killNow
&& pDevice
->Connected
)
106 avail
= (ALuint64
)(now
-start
) * pDevice
->Frequency
/ 1000;
109 /* Timer wrapped. Add the remainder of the cycle to the available
110 * count and reset the number of samples done */
111 avail
+= 0xFFFFFFFFu
*pDevice
->Frequency
/1000 - done
;
114 if(avail
-done
< pDevice
->UpdateSize
)
120 while(avail
-done
>= pDevice
->UpdateSize
)
122 aluMixData(pDevice
, data
->buffer
, pDevice
->UpdateSize
);
123 done
+= pDevice
->UpdateSize
;
127 ALubyte
*bytes
= data
->buffer
;
130 if(aluBytesFromFormat(pDevice
->Format
) == 1)
132 for(i
= 0;i
< data
->size
;i
++)
133 fputc(bytes
[i
], data
->f
);
135 else if(aluBytesFromFormat(pDevice
->Format
) == 2)
137 for(i
= 0;i
< data
->size
;i
++)
138 fputc(bytes
[i
^1], data
->f
);
140 else if(aluBytesFromFormat(pDevice
->Format
) == 4)
142 for(i
= 0;i
< data
->size
;i
++)
143 fputc(bytes
[i
^3], data
->f
);
147 fs
= fwrite(data
->buffer
, frameSize
, pDevice
->UpdateSize
,
151 AL_PRINT("Error writing to file\n");
152 aluHandleDisconnect(pDevice
);
161 static ALCboolean
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
166 fname
= GetConfigValue("wave", "file", "");
171 deviceName
= waveDevice
;
172 else if(strcmp(deviceName
, waveDevice
) != 0)
175 data
= (wave_data
*)calloc(1, sizeof(wave_data
));
177 data
->f
= fopen(fname
, "wb");
181 AL_PRINT("Could not open file '%s': %s\n", fname
, strerror(errno
));
185 device
->szDeviceName
= strdup(deviceName
);
186 device
->ExtraData
= data
;
190 static void wave_close_playback(ALCdevice
*device
)
192 wave_data
*data
= (wave_data
*)device
->ExtraData
;
196 device
->ExtraData
= NULL
;
199 static ALCboolean
wave_reset_playback(ALCdevice
*device
)
201 wave_data
*data
= (wave_data
*)device
->ExtraData
;
202 ALuint channels
, bits
;
205 fseek(data
->f
, 0, SEEK_SET
);
208 bits
= aluBytesFromFormat(device
->Format
) * 8;
209 channels
= aluChannelsFromFormat(device
->Format
);
215 device
->Format
= AL_FORMAT_71CHN8
;
217 device
->Format
= AL_FORMAT_71CHN16
;
220 device
->Format
= AL_FORMAT_71CHN32
;
226 fprintf(data
->f
, "RIFF");
227 fwrite32le(0xFFFFFFFF, data
->f
); // 'RIFF' header len; filled in at close
229 fprintf(data
->f
, "WAVE");
231 fprintf(data
->f
, "fmt ");
232 fwrite32le(40, data
->f
); // 'fmt ' header len; 40 bytes for EXTENSIBLE
234 // 16-bit val, format type id (extensible: 0xFFFE)
235 fwrite16le(0xFFFE, data
->f
);
236 // 16-bit val, channel count
237 fwrite16le(channels
, data
->f
);
238 // 32-bit val, frequency
239 fwrite32le(device
->Frequency
, data
->f
);
240 // 32-bit val, bytes per second
241 fwrite32le(device
->Frequency
* channels
* bits
/ 8, data
->f
);
242 // 16-bit val, frame size
243 fwrite16le(channels
* bits
/ 8, data
->f
);
244 // 16-bit val, bits per sample
245 fwrite16le(bits
, data
->f
);
246 // 16-bit val, extra byte count
247 fwrite16le(22, data
->f
);
248 // 16-bit val, valid bits per sample
249 fwrite16le(bits
, data
->f
);
250 // 32-bit val, channel mask
251 fwrite32le(channel_masks
[channels
], data
->f
);
252 // 16 byte GUID, sub-type format
253 val
= fwrite(((bits
==32) ? SUBTYPE_FLOAT
: SUBTYPE_PCM
), 1, 16, data
->f
);
255 fprintf(data
->f
, "data");
256 fwrite32le(0xFFFFFFFF, data
->f
); // 'data' header len; filled in at close
260 AL_PRINT("Error writing header: %s\n", strerror(errno
));
264 data
->DataStart
= ftell(data
->f
);
266 data
->size
= device
->UpdateSize
* channels
* bits
/ 8;
267 data
->buffer
= malloc(data
->size
);
270 AL_PRINT("buffer malloc failed\n");
274 SetDefaultWFXChannelOrder(device
);
276 data
->thread
= StartThread(WaveProc
, device
);
277 if(data
->thread
== NULL
)
287 static void wave_stop_playback(ALCdevice
*device
)
289 wave_data
*data
= (wave_data
*)device
->ExtraData
;
297 StopThread(data
->thread
);
305 size
= ftell(data
->f
);
308 dataLen
= size
- data
->DataStart
;
309 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
310 fwrite32le(dataLen
, data
->f
); // 'data' header len
311 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
312 fwrite32le(size
-8, data
->f
); // 'WAVE' header len
317 static ALCboolean
wave_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
325 BackendFuncs wave_funcs
= {
338 void alc_wave_init(BackendFuncs
*func_list
)
340 *func_list
= wave_funcs
;
343 void alc_wave_deinit(void)
347 void alc_wave_probe(int type
)
349 if(!ConfigValueExists("wave", "file"))
352 if(type
== DEVICE_PROBE
)
353 AppendDeviceList(waveDevice
);
354 else if(type
== ALL_DEVICE_PROBE
)
355 AppendAllDeviceList(waveDevice
);