Fix retrieved update size from pulseaudio
[openal-soft.git] / Alc / wave.c
blob8de3a623c04d55fbd6cb884133190f658425ecda
1 /**
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
31 typedef struct {
32 FILE *f;
33 long DataStart;
35 ALvoid *buffer;
36 ALuint size;
38 volatile int killNow;
39 ALvoid *thread;
40 } wave_data;
43 static const ALCchar waveDevice[] = "Wave File Writer";
46 static ALuint WaveProc(ALvoid *ptr)
48 ALCdevice *pDevice = (ALCdevice*)ptr;
49 wave_data *data = (wave_data*)pDevice->ExtraData;
50 ALuint frameSize;
51 ALuint now, last;
52 size_t fs;
53 ALuint avail;
54 union {
55 short s;
56 char b[sizeof(short)];
57 } uSB;
59 uSB.s = 1;
60 frameSize = aluBytesFromFormat(pDevice->Format) *
61 aluChannelsFromFormat(pDevice->Format);
63 last = timeGetTime();
64 while(!data->killNow && pDevice->Connected)
66 now = timeGetTime();
68 avail = (now-last) * pDevice->Frequency / 1000;
69 if(avail < pDevice->UpdateSize)
71 Sleep(1);
72 continue;
75 while(avail >= pDevice->UpdateSize)
77 aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
79 if(uSB.b[0] != 1 && aluBytesFromFormat(pDevice->Format) > 1)
81 ALubyte *bytes = data->buffer;
82 ALuint i;
84 for(i = 0;i < data->size;i++)
85 fputc(bytes[i^1], data->f);
87 else
88 fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
89 data->f);
90 if(ferror(data->f))
92 AL_PRINT("Error writing to file\n");
93 aluHandleDisconnect(pDevice);
94 break;
97 avail -= pDevice->UpdateSize;
99 last = now;
102 return 0;
105 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
107 wave_data *data;
108 const char *fname;
110 fname = GetConfigValue("wave", "file", "");
111 if(!fname[0])
112 return ALC_FALSE;
114 if(!deviceName)
115 deviceName = waveDevice;
116 else if(strcmp(deviceName, waveDevice) != 0)
117 return ALC_FALSE;
119 data = (wave_data*)calloc(1, sizeof(wave_data));
121 data->f = fopen(fname, "wb");
122 if(!data->f)
124 free(data);
125 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
126 return ALC_FALSE;
129 device->szDeviceName = strdup(deviceName);
130 device->ExtraData = data;
131 return ALC_TRUE;
134 static void wave_close_playback(ALCdevice *device)
136 wave_data *data = (wave_data*)device->ExtraData;
138 fclose(data->f);
139 free(data);
140 device->ExtraData = NULL;
143 static ALCboolean wave_reset_playback(ALCdevice *device)
145 wave_data *data = (wave_data*)device->ExtraData;
146 ALuint channels, bits, i;
148 fseek(data->f, 0, SEEK_SET);
149 clearerr(data->f);
151 bits = aluBytesFromFormat(device->Format) * 8;
152 channels = aluChannelsFromFormat(device->Format);
153 switch(bits)
155 case 8:
156 case 16:
157 case 32:
158 if(channels == 0)
160 AL_PRINT("Unknown format?! %x\n", device->Format);
161 return ALC_FALSE;
163 break;
165 default:
166 AL_PRINT("Unknown format?! %x\n", device->Format);
167 return ALC_FALSE;
170 fprintf(data->f, "RIFF");
171 fputc(0, data->f); // 'RIFF' header len; filled in at close
172 fputc(0, data->f);
173 fputc(0, data->f);
174 fputc(0, data->f);
176 fprintf(data->f, "WAVE");
178 fprintf(data->f, "fmt ");
179 fputc(16, data->f); // 'fmt ' header len; 16 bytes for PCM
180 fputc(0, data->f);
181 fputc(0, data->f);
182 fputc(0, data->f);
183 // 16-bit val, format type id (PCM: 1)
184 fputc(1, data->f);
185 fputc(0, data->f);
186 // 16-bit val, channel count
187 fputc(channels&0xff, data->f);
188 fputc((channels>>8)&0xff, data->f);
189 // 32-bit val, frequency
190 fputc(device->Frequency&0xff, data->f);
191 fputc((device->Frequency>>8)&0xff, data->f);
192 fputc((device->Frequency>>16)&0xff, data->f);
193 fputc((device->Frequency>>24)&0xff, data->f);
194 // 32-bit val, bytes per second
195 i = device->Frequency * channels * bits / 8;
196 fputc(i&0xff, data->f);
197 fputc((i>>8)&0xff, data->f);
198 fputc((i>>16)&0xff, data->f);
199 fputc((i>>24)&0xff, data->f);
200 // 16-bit val, frame size
201 i = channels * bits / 8;
202 fputc(i&0xff, data->f);
203 fputc((i>>8)&0xff, data->f);
204 // 16-bit val, bits per sample
205 fputc(bits&0xff, data->f);
206 fputc((bits>>8)&0xff, data->f);
208 fprintf(data->f, "data");
209 fputc(0, data->f); // 'data' header len; filled in at close
210 fputc(0, data->f);
211 fputc(0, data->f);
212 fputc(0, data->f);
214 if(ferror(data->f))
216 AL_PRINT("Error writing header: %s\n", strerror(errno));
217 return ALC_FALSE;
220 data->DataStart = ftell(data->f);
222 data->size = device->UpdateSize * channels * bits / 8;
223 data->buffer = malloc(data->size);
224 if(!data->buffer)
226 AL_PRINT("buffer malloc failed\n");
227 return ALC_FALSE;
230 data->thread = StartThread(WaveProc, device);
231 if(data->thread == NULL)
233 free(data->buffer);
234 data->buffer = NULL;
235 return ALC_FALSE;
238 return ALC_TRUE;
241 static void wave_stop_playback(ALCdevice *device)
243 wave_data *data = (wave_data*)device->ExtraData;
244 ALuint dataLen;
245 long size;
247 if(!data->thread)
248 return;
250 data->killNow = 1;
251 StopThread(data->thread);
252 data->thread = NULL;
254 data->killNow = 0;
256 free(data->buffer);
257 data->buffer = NULL;
259 size = ftell(data->f);
260 if(size > 0)
262 dataLen = size - data->DataStart;
263 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
265 fputc(dataLen&0xff, data->f); // 'data' header len
266 fputc((dataLen>>8)&0xff, data->f);
267 fputc((dataLen>>16)&0xff, data->f);
268 fputc((dataLen>>24)&0xff, data->f);
270 if(fseek(data->f, 4, SEEK_SET) == 0)
272 size -= 8;
273 fputc(size&0xff, data->f); // 'WAVE' header len
274 fputc((size>>8)&0xff, data->f);
275 fputc((size>>16)&0xff, data->f);
276 fputc((size>>24)&0xff, data->f);
282 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
284 (void)pDevice;
285 (void)deviceName;
286 return ALC_FALSE;
289 static void wave_close_capture(ALCdevice *pDevice)
291 (void)pDevice;
294 static void wave_start_capture(ALCdevice *pDevice)
296 (void)pDevice;
299 static void wave_stop_capture(ALCdevice *pDevice)
301 (void)pDevice;
304 static void wave_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
306 (void)pDevice;
307 (void)pBuffer;
308 (void)lSamples;
311 static ALCuint wave_available_samples(ALCdevice *pDevice)
313 (void)pDevice;
314 return 0;
318 BackendFuncs wave_funcs = {
319 wave_open_playback,
320 wave_close_playback,
321 wave_reset_playback,
322 wave_stop_playback,
323 wave_open_capture,
324 wave_close_capture,
325 wave_start_capture,
326 wave_stop_capture,
327 wave_capture_samples,
328 wave_available_samples
331 void alc_wave_init(BackendFuncs *func_list)
333 *func_list = wave_funcs;
336 void alc_wave_deinit(void)
340 void alc_wave_probe(int type)
342 if(*(GetConfigValue("wave", "file", "")) == 0)
343 return;
345 if(type == DEVICE_PROBE)
346 AppendDeviceList(waveDevice);
347 else if(type == ALL_DEVICE_PROBE)
348 AppendAllDeviceList(waveDevice);