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
;
56 char b
[sizeof(short)];
60 frameSize
= aluBytesFromFormat(pDevice
->Format
) *
61 aluChannelsFromFormat(pDevice
->Format
);
68 avail
= (now
-last
) * pDevice
->Frequency
/ 1000;
69 if(avail
< pDevice
->UpdateFreq
/4)
78 WriteCnt
= min(data
->size
, avail
);
79 aluMixData(pDevice
->Context
, data
->buffer
, WriteCnt
* frameSize
,
83 if(uSB
.b
[0] != 1 && aluBytesFromFormat(pDevice
->Format
) > 1)
85 ALubyte
*bytes
= data
->buffer
;
88 for(i
= 0;i
< WriteCnt
*frameSize
;i
++)
89 fputc(bytes
[i
^1], data
->f
);
92 fwrite(data
->buffer
, frameSize
, WriteCnt
, data
->f
);
95 AL_PRINT("Error writing to file\n");
108 static ALCboolean
wave_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
116 strncpy(fname
, GetConfigValue("wave", "file", ""), sizeof(fname
)-1);
117 fname
[sizeof(fname
)-1] = 0;
123 if(strcmp(deviceName
, waveDevice
) != 0)
125 device
->szDeviceName
= waveDevice
;
128 device
->szDeviceName
= waveDevice
;
130 data
= (wave_data
*)calloc(1, sizeof(wave_data
));
132 data
->f
= fopen(fname
, "wb");
136 AL_PRINT("Could not open file '%s': %s\n", fname
, strerror(errno
));
140 bits
= aluBytesFromFormat(device
->Format
) * 8;
141 channels
= aluChannelsFromFormat(device
->Format
);
148 AL_PRINT("Unknown format?! %x\n", device
->Format
);
156 AL_PRINT("Unknown format?! %x\n", device
->Format
);
162 fprintf(data
->f
, "RIFF");
163 fputc(0, data
->f
); // 'RIFF' header len; filled in at close
168 fprintf(data
->f
, "WAVE");
170 fprintf(data
->f
, "fmt ");
171 fputc(16, data
->f
); // 'fmt ' header len; 16 bytes for PCM
175 // 16-bit val, format type id (PCM: 1)
178 // 16-bit val, channel count
179 fputc(channels
&0xff, data
->f
);
180 fputc((channels
>>8)&0xff, data
->f
);
181 // 32-bit val, frequency
182 fputc(device
->Frequency
&0xff, data
->f
);
183 fputc((device
->Frequency
>>8)&0xff, data
->f
);
184 fputc((device
->Frequency
>>16)&0xff, data
->f
);
185 fputc((device
->Frequency
>>24)&0xff, data
->f
);
186 // 32-bit val, bytes per second
187 i
= device
->Frequency
* channels
* bits
/ 8;
188 fputc(i
&0xff, data
->f
);
189 fputc((i
>>8)&0xff, data
->f
);
190 fputc((i
>>16)&0xff, data
->f
);
191 fputc((i
>>24)&0xff, data
->f
);
192 // 16-bit val, frame size
193 i
= channels
* bits
/ 8;
194 fputc(i
&0xff, data
->f
);
195 fputc((i
>>8)&0xff, data
->f
);
196 // 16-bit val, bits per sample
197 fputc(bits
&0xff, data
->f
);
198 fputc((bits
>>8)&0xff, data
->f
);
200 fprintf(data
->f
, "data");
201 fputc(0, data
->f
); // 'data' header len; filled in at close
208 AL_PRINT("Error writing header: %s\n", strerror(errno
));
214 data
->DataStart
= ftell(data
->f
);
216 device
->MaxNoOfSources
= 256;
217 device
->UpdateFreq
= max(device
->UpdateFreq
, 2048);
219 data
->size
= device
->UpdateFreq
;
220 data
->buffer
= malloc(data
->size
* channels
* bits
/ 8);
223 AL_PRINT("buffer malloc failed\n");
229 device
->ExtraData
= data
;
230 data
->thread
= StartThread(WaveProc
, device
);
231 if(data
->thread
== NULL
)
233 device
->ExtraData
= NULL
;
243 static void wave_close_playback(ALCdevice
*device
)
245 wave_data
*data
= (wave_data
*)device
->ExtraData
;
250 StopThread(data
->thread
);
252 size
= ftell(data
->f
);
255 dataLen
= size
- data
->DataStart
;
256 if(fseek(data
->f
, data
->DataStart
-4, SEEK_SET
) == 0)
258 fputc(dataLen
&0xff, data
->f
); // 'data' header len
259 fputc((dataLen
>>8)&0xff, data
->f
);
260 fputc((dataLen
>>16)&0xff, data
->f
);
261 fputc((dataLen
>>24)&0xff, data
->f
);
263 if(fseek(data
->f
, 4, SEEK_SET
) == 0)
266 fputc(size
&0xff, data
->f
); // 'WAVE' header len
267 fputc((size
>>8)&0xff, data
->f
);
268 fputc((size
>>16)&0xff, data
->f
);
269 fputc((size
>>24)&0xff, data
->f
);
276 device
->ExtraData
= NULL
;
280 static ALCboolean
wave_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
290 static void wave_close_capture(ALCdevice
*pDevice
)
295 static void wave_start_capture(ALCdevice
*pDevice
)
300 static void wave_stop_capture(ALCdevice
*pDevice
)
305 static void wave_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
312 static ALCuint
wave_available_samples(ALCdevice
*pDevice
)
319 BackendFuncs wave_funcs
= {
326 wave_capture_samples
,
327 wave_available_samples
330 void alc_wave_init(BackendFuncs
*func_list
)
332 *func_list
= wave_funcs
;
334 waveDevice
= AppendDeviceList("Wave File Writer");
335 AppendAllDeviceList(waveDevice
);