zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wave.c
Go to the documentation of this file.
1 
21 #include "config.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #ifdef HAVE_WINDOWS_H
27 #include <windows.h>
28 #endif
29 
30 #include "alMain.h"
31 #include "alu.h"
32 
33 
34 typedef struct {
35  FILE *f;
36  long DataStart;
37 
38  ALvoid *buffer;
39  ALuint size;
40 
41  volatile int killNow;
42  ALvoid *thread;
43 } wave_data;
44 
45 
46 static const ALCchar waveDevice[] = "Wave File Writer";
47 
48 static const ALubyte SUBTYPE_PCM[] = {
49  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
50  0x00, 0x38, 0x9b, 0x71
51 };
52 static const ALubyte SUBTYPE_FLOAT[] = {
53  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
54  0x00, 0x38, 0x9b, 0x71
55 };
56 
57 static const ALuint channel_masks[] = {
58  0, /* invalid */
59  0x4, /* Mono */
60  0x1 | 0x2, /* Stereo */
61  0, /* 3 channel */
62  0x1 | 0x2 | 0x10 | 0x20, /* Quad */
63  0, /* 5 channel */
64  0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
65  0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
66  0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
67 };
68 
69 
70 static void fwrite16le(ALushort val, FILE *f)
71 {
72  fputc(val&0xff, f);
73  fputc((val>>8)&0xff, f);
74 }
75 
76 static void fwrite32le(ALuint val, FILE *f)
77 {
78  fputc(val&0xff, f);
79  fputc((val>>8)&0xff, f);
80  fputc((val>>16)&0xff, f);
81  fputc((val>>24)&0xff, f);
82 }
83 
84 
85 static ALuint WaveProc(ALvoid *ptr)
86 {
87  ALCdevice *Device = (ALCdevice*)ptr;
88  wave_data *data = (wave_data*)Device->ExtraData;
89  ALuint frameSize;
90  ALuint now, start;
91  ALuint64 avail, done;
92  size_t fs;
93  const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
94  Device->Frequency / 2;
95 
96  frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
97 
98  done = 0;
99  start = timeGetTime();
100  while(!data->killNow && Device->Connected)
101  {
102  now = timeGetTime();
103 
104  avail = (ALuint64)(now-start) * Device->Frequency / 1000;
105  if(avail < done)
106  {
107  /* Timer wrapped (50 days???). Add the remainder of the cycle to
108  * the available count and reset the number of samples done */
109  avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done;
110  done = 0;
111  }
112  if(avail-done < Device->UpdateSize)
113  {
114  Sleep(restTime);
115  continue;
116  }
117 
118  while(avail-done >= Device->UpdateSize)
119  {
120  aluMixData(Device, data->buffer, Device->UpdateSize);
121  done += Device->UpdateSize;
122 
123  if(!IS_LITTLE_ENDIAN)
124  {
125  ALuint bytesize = BytesFromDevFmt(Device->FmtType);
126  ALubyte *bytes = data->buffer;
127  ALuint i;
128 
129  if(bytesize == 1)
130  {
131  for(i = 0;i < data->size;i++)
132  fputc(bytes[i], data->f);
133  }
134  else if(bytesize == 2)
135  {
136  for(i = 0;i < data->size;i++)
137  fputc(bytes[i^1], data->f);
138  }
139  else if(bytesize == 4)
140  {
141  for(i = 0;i < data->size;i++)
142  fputc(bytes[i^3], data->f);
143  }
144  }
145  else
146  {
147  fs = fwrite(data->buffer, frameSize, Device->UpdateSize,
148  data->f);
149  fs = fs;
150  }
151  if(ferror(data->f))
152  {
153  ERR("Error writing to file\n");
154  ALCdevice_Lock(Device);
155  aluHandleDisconnect(Device);
156  ALCdevice_Unlock(Device);
157  break;
158  }
159  }
160  }
161 
162  return 0;
163 }
164 
165 static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
166 {
167  wave_data *data;
168  const char *fname;
169 
170  fname = GetConfigValue("wave", "file", "");
171  if(!fname[0])
172  return ALC_INVALID_VALUE;
173 
174  if(!deviceName)
175  deviceName = waveDevice;
176  else if(strcmp(deviceName, waveDevice) != 0)
177  return ALC_INVALID_VALUE;
178 
179  data = (wave_data*)calloc(1, sizeof(wave_data));
180 
181  data->f = fopen(fname, "wb");
182  if(!data->f)
183  {
184  free(data);
185  ERR("Could not open file '%s': %s\n", fname, strerror(errno));
186  return ALC_INVALID_VALUE;
187  }
188 
189  device->DeviceName = strdup(deviceName);
190  device->ExtraData = data;
191  return ALC_NO_ERROR;
192 }
193 
194 static void wave_close_playback(ALCdevice *device)
195 {
196  wave_data *data = (wave_data*)device->ExtraData;
197 
198  fclose(data->f);
199  free(data);
200  device->ExtraData = NULL;
201 }
202 
204 {
205  wave_data *data = (wave_data*)device->ExtraData;
206  ALuint channels=0, bits=0;
207  size_t val;
208 
209  fseek(data->f, 0, SEEK_SET);
210  clearerr(data->f);
211 
212  switch(device->FmtType)
213  {
214  case DevFmtByte:
215  device->FmtType = DevFmtUByte;
216  break;
217  case DevFmtUShort:
218  device->FmtType = DevFmtShort;
219  break;
220  case DevFmtUInt:
221  device->FmtType = DevFmtInt;
222  break;
223  case DevFmtUByte:
224  case DevFmtShort:
225  case DevFmtInt:
226  case DevFmtFloat:
227  break;
228  }
229  bits = BytesFromDevFmt(device->FmtType) * 8;
230  channels = ChannelsFromDevFmt(device->FmtChans);
231 
232  fprintf(data->f, "RIFF");
233  fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
234 
235  fprintf(data->f, "WAVE");
236 
237  fprintf(data->f, "fmt ");
238  fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
239 
240  // 16-bit val, format type id (extensible: 0xFFFE)
241  fwrite16le(0xFFFE, data->f);
242  // 16-bit val, channel count
243  fwrite16le(channels, data->f);
244  // 32-bit val, frequency
245  fwrite32le(device->Frequency, data->f);
246  // 32-bit val, bytes per second
247  fwrite32le(device->Frequency * channels * bits / 8, data->f);
248  // 16-bit val, frame size
249  fwrite16le(channels * bits / 8, data->f);
250  // 16-bit val, bits per sample
251  fwrite16le(bits, data->f);
252  // 16-bit val, extra byte count
253  fwrite16le(22, data->f);
254  // 16-bit val, valid bits per sample
255  fwrite16le(bits, data->f);
256  // 32-bit val, channel mask
257  fwrite32le(channel_masks[channels], data->f);
258  // 16 byte GUID, sub-type format
259  val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
260  val = val;
261 
262  fprintf(data->f, "data");
263  fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
264 
265  if(ferror(data->f))
266  {
267  ERR("Error writing header: %s\n", strerror(errno));
268  return ALC_FALSE;
269  }
270  data->DataStart = ftell(data->f);
271 
273 
274  return ALC_TRUE;
275 }
276 
278 {
279  wave_data *data = (wave_data*)device->ExtraData;
280 
281  data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
282  data->buffer = malloc(data->size);
283  if(!data->buffer)
284  {
285  ERR("Buffer malloc failed\n");
286  return ALC_FALSE;
287  }
288 
289  data->thread = StartThread(WaveProc, device);
290  if(data->thread == NULL)
291  {
292  free(data->buffer);
293  data->buffer = NULL;
294  return ALC_FALSE;
295  }
296 
297  return ALC_TRUE;
298 }
299 
300 static void wave_stop_playback(ALCdevice *device)
301 {
302  wave_data *data = (wave_data*)device->ExtraData;
303  ALuint dataLen;
304  long size;
305 
306  if(!data->thread)
307  return;
308 
309  data->killNow = 1;
310  StopThread(data->thread);
311  data->thread = NULL;
312 
313  data->killNow = 0;
314 
315  free(data->buffer);
316  data->buffer = NULL;
317 
318  size = ftell(data->f);
319  if(size > 0)
320  {
321  dataLen = size - data->DataStart;
322  if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
323  fwrite32le(dataLen, data->f); // 'data' header len
324  if(fseek(data->f, 4, SEEK_SET) == 0)
325  fwrite32le(size-8, data->f); // 'WAVE' header len
326  }
327 }
328 
329 
330 static const BackendFuncs wave_funcs = {
336  NULL,
337  NULL,
338  NULL,
339  NULL,
340  NULL,
341  NULL,
345 };
346 
348 {
349  *func_list = wave_funcs;
350  return ALC_TRUE;
351 }
352 
353 void alc_wave_deinit(void)
354 {
355 }
356 
358 {
359  if(!ConfigValueExists("wave", "file"))
360  return;
361 
362  switch(type)
363  {
364  case ALL_DEVICE_PROBE:
366  break;
368  break;
369  }
370 }
unsigned char ALubyte
Definition: al.h:47
ALCboolean Connected
Definition: alMain.h:564
#define IS_LITTLE_ENDIAN
Definition: alMain.h:61
GLuint const GLfloat * val
Definition: glew.h:2715
void ALvoid
Definition: al.h:74
static const BackendFuncs wave_funcs
Definition: wave.c:330
#define ALC_TRUE
Definition: alc.h:84
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
char * strdup(const char *inStr)
Definition: strdup.c:6
#define NULL
Definition: ftobjs.h:61
GLuint start
Definition: glew.h:1239
GLclampf f
Definition: glew.h:3390
static void fwrite32le(ALuint val, FILE *f)
Definition: wave.c:76
ALuint timeGetTime(void)
Definition: helpers.c:376
SDL_EventEntry * free
Definition: SDL_events.c:80
ALuint Frequency
Definition: alMain.h:569
ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
Definition: ALu.c:970
ALint64 ALCdevice_GetLatencyDefault(ALCdevice *device)
Definition: ALc.c:1285
int ConfigValueExists(const char *blockName, const char *keyName)
Definition: alcConfig.c:310
const char * GetConfigValue(const char *blockName, const char *keyName, const char *def)
Definition: alcConfig.c:278
#define calloc
Definition: SDL_malloc.c:636
ALvoid aluHandleDisconnect(ALCdevice *device)
Definition: ALu.c:1176
char ALCchar
Definition: alc.h:42
#define Sleep(x)
Definition: allatency.c:34
#define ALCdevice_Unlock(a)
Definition: alMain.h:647
static const ALubyte SUBTYPE_FLOAT[]
Definition: wave.c:52
static const ALCchar waveDevice[]
Definition: wave.c:46
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
void * ExtraData
Definition: alMain.h:630
#define ALC_FALSE
Definition: alc.h:81
static void wave_close_playback(ALCdevice *device)
Definition: wave.c:194
static void wave_stop_playback(ALCdevice *device)
Definition: wave.c:300
void alc_wave_deinit(void)
Definition: wave.c:353
ALuint StopThread(ALvoid *thread)
Definition: alcThread.c:131
void SetDefaultWFXChannelOrder(ALCdevice *device)
Definition: ALc.c:1295
unsigned short ALushort
Definition: al.h:53
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
ALCboolean alc_wave_init(BackendFuncs *func_list)
Definition: wave.c:347
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: SDL_opengl.h:10449
unsigned int ALuint
Definition: al.h:59
static ALuint WaveProc(ALvoid *ptr)
Definition: wave.c:85
static const ALubyte SUBTYPE_PCM[]
Definition: wave.c:48
enum DevFmtChannels FmtChans
Definition: alMain.h:572
#define SEEK_SET
Definition: zconf.h:249
static SDL_Thread * thread
#define ALC_NO_ERROR
Definition: alc.h:102
void ALCdevice_LockDefault(ALCdevice *device)
Definition: ALc.c:1277
#define malloc
Definition: SDL_malloc.c:635
char ALCboolean
Definition: alc.h:39
#define ERR(...)
Definition: alMain.h:816
ALuint UpdateSize
Definition: alMain.h:570
static const ALuint channel_masks[]
Definition: wave.c:57
enum DevFmtType FmtType
Definition: alMain.h:573
static ALCboolean wave_reset_playback(ALCdevice *device)
Definition: wave.c:203
ALCchar * DeviceName
Definition: alMain.h:575
#define ALC_INVALID_VALUE
Definition: alc.h:114
static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
Definition: wave.c:165
int i
Definition: pngrutil.c:1377
static void fwrite16le(ALushort val, FILE *f)
Definition: wave.c:70
void alc_wave_probe(enum DevProbe type)
Definition: wave.c:357
int ALCenum
Definition: alc.h:66
ALuint BytesFromDevFmt(enum DevFmtType type)
Definition: ALc.c:1165
void ALCdevice_UnlockDefault(ALCdevice *device)
Definition: ALc.c:1281
ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
Definition: ALc.c:1179
#define ALCdevice_Lock(a)
Definition: alMain.h:646
static ALCboolean wave_start_playback(ALCdevice *device)
Definition: wave.c:277
void AppendAllDevicesList(const ALCchar *name)
ALvoid * StartThread(ALuint(*func)(ALvoid *), ALvoid *ptr)
Definition: alcThread.c:100
DevProbe
Definition: alMain.h:383
static __inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
Definition: alMain.h:523
GLsizei size
Definition: gl2ext.h:1467