Simplify ALSA PCM parameter retrieval
[openal-soft.git] / Alc / wave.c
blobcba9fb132bc2b8b019972686218410bde875437b
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 ALCchar *waveDevice;
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 WriteCnt;
53 size_t fs;
54 ALuint avail;
55 union {
56 short s;
57 char b[sizeof(short)];
58 } uSB;
60 uSB.s = 1;
61 frameSize = aluBytesFromFormat(pDevice->Format) *
62 aluChannelsFromFormat(pDevice->Format);
64 last = timeGetTime();
65 while(!data->killNow)
67 now = timeGetTime();
69 avail = (now-last) * pDevice->Frequency / 1000;
70 if(avail < pDevice->UpdateSize/4)
72 Sleep(1);
73 continue;
76 while(avail > 0)
78 SuspendContext(NULL);
79 WriteCnt = min(data->size, avail);
80 aluMixData(pDevice->Context, data->buffer, WriteCnt * frameSize,
81 pDevice->Format);
82 ProcessContext(NULL);
84 if(uSB.b[0] != 1 && aluBytesFromFormat(pDevice->Format) > 1)
86 ALubyte *bytes = data->buffer;
87 ALuint i;
89 for(i = 0;i < WriteCnt*frameSize;i++)
90 fputc(bytes[i^1], data->f);
92 else
93 fs = fwrite(data->buffer, frameSize, WriteCnt, data->f);
94 if(ferror(data->f))
96 AL_PRINT("Error writing to file\n");
97 data->killNow = 1;
98 break;
101 avail -= WriteCnt;
103 last = now;
106 return 0;
109 static ALCboolean wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
111 wave_data *data;
112 ALuint channels;
113 ALuint bits;
114 const char *fname;
115 int i;
117 fname = GetConfigValue("wave", "file", "");
118 if(!fname[0])
119 return ALC_FALSE;
121 if(deviceName)
123 if(strcmp(deviceName, waveDevice) != 0)
124 return ALC_FALSE;
125 device->szDeviceName = waveDevice;
127 else
128 device->szDeviceName = waveDevice;
130 data = (wave_data*)calloc(1, sizeof(wave_data));
132 data->f = fopen(fname, "wb");
133 if(!data->f)
135 free(data);
136 AL_PRINT("Could not open file '%s': %s\n", fname, strerror(errno));
137 return ALC_FALSE;
140 bits = aluBytesFromFormat(device->Format) * 8;
141 channels = aluChannelsFromFormat(device->Format);
142 switch(bits)
144 case 8:
145 case 16:
146 if(channels == 0)
148 AL_PRINT("Unknown format?! %x\n", device->Format);
149 fclose(data->f);
150 free(data);
151 return ALC_FALSE;
153 break;
155 default:
156 AL_PRINT("Unknown format?! %x\n", device->Format);
157 fclose(data->f);
158 free(data);
159 return ALC_FALSE;
162 fprintf(data->f, "RIFF");
163 fputc(0, data->f); // 'RIFF' header len; filled in at close
164 fputc(0, data->f);
165 fputc(0, data->f);
166 fputc(0, data->f);
168 fprintf(data->f, "WAVE");
170 fprintf(data->f, "fmt ");
171 fputc(16, data->f); // 'fmt ' header len; 16 bytes for PCM
172 fputc(0, data->f);
173 fputc(0, data->f);
174 fputc(0, data->f);
175 // 16-bit val, format type id (PCM: 1)
176 fputc(1, data->f);
177 fputc(0, data->f);
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
202 fputc(0, data->f);
203 fputc(0, data->f);
204 fputc(0, data->f);
206 if(ferror(data->f))
208 AL_PRINT("Error writing header: %s\n", strerror(errno));
209 fclose(data->f);
210 free(data);
211 return ALC_FALSE;
214 data->DataStart = ftell(data->f);
216 device->UpdateSize = max(device->UpdateSize, 2048);
218 data->size = device->UpdateSize;
219 data->buffer = malloc(data->size * channels * bits / 8);
220 if(!data->buffer)
222 AL_PRINT("buffer malloc failed\n");
223 fclose(data->f);
224 free(data);
225 return ALC_FALSE;
228 device->ExtraData = data;
229 data->thread = StartThread(WaveProc, device);
230 if(data->thread == NULL)
232 device->ExtraData = NULL;
233 fclose(data->f);
234 free(data->buffer);
235 free(data);
236 return ALC_FALSE;
239 return ALC_TRUE;
242 static void wave_close_playback(ALCdevice *device)
244 wave_data *data = (wave_data*)device->ExtraData;
245 ALuint dataLen;
246 long size;
248 data->killNow = 1;
249 StopThread(data->thread);
251 size = ftell(data->f);
252 if(size > 0)
254 dataLen = size - data->DataStart;
255 if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
257 fputc(dataLen&0xff, data->f); // 'data' header len
258 fputc((dataLen>>8)&0xff, data->f);
259 fputc((dataLen>>16)&0xff, data->f);
260 fputc((dataLen>>24)&0xff, data->f);
262 if(fseek(data->f, 4, SEEK_SET) == 0)
264 size -= 8;
265 fputc(size&0xff, data->f); // 'WAVE' header len
266 fputc((size>>8)&0xff, data->f);
267 fputc((size>>16)&0xff, data->f);
268 fputc((size>>24)&0xff, data->f);
272 fclose(data->f);
273 free(data->buffer);
274 free(data);
275 device->ExtraData = NULL;
279 static ALCboolean wave_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
281 (void)pDevice;
282 (void)deviceName;
283 (void)frequency;
284 (void)format;
285 (void)SampleSize;
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_open_capture,
322 wave_close_capture,
323 wave_start_capture,
324 wave_stop_capture,
325 wave_capture_samples,
326 wave_available_samples
329 void alc_wave_init(BackendFuncs *func_list)
331 *func_list = wave_funcs;
333 waveDevice = AppendDeviceList("Wave File Writer");
334 AppendAllDeviceList(waveDevice);