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 ALCchar
*waveDevice
;
46 static ALuint
WaveProc(ALvoid
*ptr
)
48 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
49 wave_data
*data
= (wave_data
*)pDevice
->ExtraData
;
57 char b
[sizeof(short)];
61 frameSize
= aluBytesFromFormat(pDevice
->Format
) *
62 aluChannelsFromFormat(pDevice
->Format
);
69 avail
= (now
-last
) * pDevice
->Frequency
/ 1000;
70 if(avail
< pDevice
->UpdateSize
)
79 WriteCnt
= min(data
->size
, avail
);
80 aluMixData(pDevice
->Context
, data
->buffer
, WriteCnt
* frameSize
,
84 if(uSB
.b
[0] != 1 && aluBytesFromFormat(pDevice
->Format
) > 1)
86 ALubyte
*bytes
= data
->buffer
;
89 for(i
= 0;i
< WriteCnt
*frameSize
;i
++)
90 fputc(bytes
[i
^1], data
->f
);
93 fs
= fwrite(data
->buffer
, frameSize
, WriteCnt
, data
->f
);
96 AL_PRINT("Error writing to file\n");
109 static ALCboolean
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
114 fname
= GetConfigValue("wave", "file", "");
120 if(strcmp(deviceName
, waveDevice
) != 0)
122 device
->szDeviceName
= waveDevice
;
125 device
->szDeviceName
= waveDevice
;
127 data
= (wave_data
*)calloc(1, sizeof(wave_data
));
129 data
->f
= fopen(fname
, "wb");
133 AL_PRINT("Could not open file '%s': %s\n", fname
, strerror(errno
));
137 device
->ExtraData
= data
;
141 static void wave_close_playback(ALCdevice
*device
)
143 wave_data
*data
= (wave_data
*)device
->ExtraData
;
148 StopThread(data
->thread
);
150 size
= ftell(data
->f
);
153 dataLen
= size
- data
->DataStart
;
154 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
156 fputc(dataLen
&0xff, data
->f
); // 'data' header len
157 fputc((dataLen
>>8)&0xff, data
->f
);
158 fputc((dataLen
>>16)&0xff, data
->f
);
159 fputc((dataLen
>>24)&0xff, data
->f
);
161 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
164 fputc(size
&0xff, data
->f
); // 'WAVE' header len
165 fputc((size
>>8)&0xff, data
->f
);
166 fputc((size
>>16)&0xff, data
->f
);
167 fputc((size
>>24)&0xff, data
->f
);
173 device
->ExtraData
= NULL
;
176 static ALCboolean
wave_start_context(ALCdevice
*device
, ALCcontext
*Context
)
178 wave_data
*data
= (wave_data
*)device
->ExtraData
;
179 ALuint channels
, bits
, i
;
182 fseek(data
->f
, 0, SEEK_SET
);
185 bits
= aluBytesFromFormat(device
->Format
) * 8;
186 channels
= aluChannelsFromFormat(device
->Format
);
194 AL_PRINT("Unknown format?! %x\n", device
->Format
);
200 AL_PRINT("Unknown format?! %x\n", device
->Format
);
204 fprintf(data
->f
, "RIFF");
205 fputc(0, data
->f
); // 'RIFF' header len; filled in at close
210 fprintf(data
->f
, "WAVE");
212 fprintf(data
->f
, "fmt ");
213 fputc(16, data
->f
); // 'fmt ' header len; 16 bytes for PCM
217 // 16-bit val, format type id (PCM: 1)
220 // 16-bit val, channel count
221 fputc(channels
&0xff, data
->f
);
222 fputc((channels
>>8)&0xff, data
->f
);
223 // 32-bit val, frequency
224 fputc(device
->Frequency
&0xff, data
->f
);
225 fputc((device
->Frequency
>>8)&0xff, data
->f
);
226 fputc((device
->Frequency
>>16)&0xff, data
->f
);
227 fputc((device
->Frequency
>>24)&0xff, data
->f
);
228 // 32-bit val, bytes per second
229 i
= device
->Frequency
* channels
* bits
/ 8;
230 fputc(i
&0xff, data
->f
);
231 fputc((i
>>8)&0xff, data
->f
);
232 fputc((i
>>16)&0xff, data
->f
);
233 fputc((i
>>24)&0xff, data
->f
);
234 // 16-bit val, frame size
235 i
= channels
* bits
/ 8;
236 fputc(i
&0xff, data
->f
);
237 fputc((i
>>8)&0xff, data
->f
);
238 // 16-bit val, bits per sample
239 fputc(bits
&0xff, data
->f
);
240 fputc((bits
>>8)&0xff, data
->f
);
242 fprintf(data
->f
, "data");
243 fputc(0, data
->f
); // 'data' header len; filled in at close
250 AL_PRINT("Error writing header: %s\n", strerror(errno
));
254 data
->DataStart
= ftell(data
->f
);
256 device
->UpdateSize
= device
->BufferSize
/ 4;
258 data
->size
= device
->UpdateSize
;
259 data
->buffer
= malloc(data
->size
* channels
* bits
/ 8);
262 AL_PRINT("buffer malloc failed\n");
266 data
->thread
= StartThread(WaveProc
, device
);
267 if(data
->thread
== NULL
)
277 static void wave_stop_context(ALCdevice
*device
, ALCcontext
*Context
)
279 wave_data
*data
= (wave_data
*)device
->ExtraData
;
288 StopThread(data
->thread
);
294 size
= ftell(data
->f
);
297 dataLen
= size
- data
->DataStart
;
298 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
300 fputc(dataLen
&0xff, data
->f
); // 'data' header len
301 fputc((dataLen
>>8)&0xff, data
->f
);
302 fputc((dataLen
>>16)&0xff, data
->f
);
303 fputc((dataLen
>>24)&0xff, data
->f
);
305 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
308 fputc(size
&0xff, data
->f
); // 'WAVE' header len
309 fputc((size
>>8)&0xff, data
->f
);
310 fputc((size
>>16)&0xff, data
->f
);
311 fputc((size
>>24)&0xff, data
->f
);
317 static ALCboolean
wave_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
324 static void wave_close_capture(ALCdevice
*pDevice
)
329 static void wave_start_capture(ALCdevice
*pDevice
)
334 static void wave_stop_capture(ALCdevice
*pDevice
)
339 static void wave_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
346 static ALCuint
wave_available_samples(ALCdevice
*pDevice
)
353 BackendFuncs wave_funcs
= {
362 wave_capture_samples
,
363 wave_available_samples
366 void alc_wave_init(BackendFuncs
*func_list
)
368 *func_list
= wave_funcs
;
370 waveDevice
= AppendDeviceList("Wave File Writer");
371 AppendAllDeviceList(waveDevice
);