wave.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <memory.h>
  24. #include <errno.h>
  25. #ifdef HAVE_WINDOWS_H
  26. #include <windows.h>
  27. #endif
  28. #include "alMain.h"
  29. #include "alu.h"
  30. #include "threads.h"
  31. #include "compat.h"
  32. typedef struct {
  33. FILE *f;
  34. long DataStart;
  35. ALvoid *buffer;
  36. ALuint size;
  37. volatile int killNow;
  38. althread_t thread;
  39. } wave_data;
  40. static const ALCchar waveDevice[] = "Wave File Writer";
  41. static const ALubyte SUBTYPE_PCM[] = {
  42. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
  43. 0x00, 0x38, 0x9b, 0x71
  44. };
  45. static const ALubyte SUBTYPE_FLOAT[] = {
  46. 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
  47. 0x00, 0x38, 0x9b, 0x71
  48. };
  49. static const ALuint channel_masks[] = {
  50. 0, /* invalid */
  51. 0x4, /* Mono */
  52. 0x1 | 0x2, /* Stereo */
  53. 0, /* 3 channel */
  54. 0x1 | 0x2 | 0x10 | 0x20, /* Quad */
  55. 0, /* 5 channel */
  56. 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20, /* 5.1 */
  57. 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400, /* 6.1 */
  58. 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400, /* 7.1 */
  59. };
  60. static void fwrite16le(ALushort val, FILE *f)
  61. {
  62. fputc(val&0xff, f);
  63. fputc((val>>8)&0xff, f);
  64. }
  65. static void fwrite32le(ALuint val, FILE *f)
  66. {
  67. fputc(val&0xff, f);
  68. fputc((val>>8)&0xff, f);
  69. fputc((val>>16)&0xff, f);
  70. fputc((val>>24)&0xff, f);
  71. }
  72. static ALuint WaveProc(ALvoid *ptr)
  73. {
  74. ALCdevice *Device = (ALCdevice*)ptr;
  75. wave_data *data = (wave_data*)Device->ExtraData;
  76. ALuint frameSize;
  77. ALuint now, start;
  78. ALuint64 avail, done;
  79. size_t fs;
  80. const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
  81. Device->Frequency / 2;
  82. SetThreadName(MIXER_THREAD_NAME);
  83. frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
  84. done = 0;
  85. start = timeGetTime();
  86. while(!data->killNow && Device->Connected)
  87. {
  88. now = timeGetTime();
  89. avail = (ALuint64)(now-start) * Device->Frequency / 1000;
  90. if(avail < done)
  91. {
  92. /* Timer wrapped (50 days???). Add the remainder of the cycle to
  93. * the available count and reset the number of samples done */
  94. avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done;
  95. done = 0;
  96. }
  97. if(avail-done < Device->UpdateSize)
  98. {
  99. Sleep(restTime);
  100. continue;
  101. }
  102. while(avail-done >= Device->UpdateSize)
  103. {
  104. aluMixData(Device, data->buffer, Device->UpdateSize);
  105. done += Device->UpdateSize;
  106. if(!IS_LITTLE_ENDIAN)
  107. {
  108. ALuint bytesize = BytesFromDevFmt(Device->FmtType);
  109. ALubyte *bytes = data->buffer;
  110. ALuint i;
  111. if(bytesize == 1)
  112. {
  113. for(i = 0;i < data->size;i++)
  114. fputc(bytes[i], data->f);
  115. }
  116. else if(bytesize == 2)
  117. {
  118. for(i = 0;i < data->size;i++)
  119. fputc(bytes[i^1], data->f);
  120. }
  121. else if(bytesize == 4)
  122. {
  123. for(i = 0;i < data->size;i++)
  124. fputc(bytes[i^3], data->f);
  125. }
  126. }
  127. else
  128. {
  129. fs = fwrite(data->buffer, frameSize, Device->UpdateSize,
  130. data->f);
  131. (void)fs;
  132. }
  133. if(ferror(data->f))
  134. {
  135. ERR("Error writing to file\n");
  136. ALCdevice_Lock(Device);
  137. aluHandleDisconnect(Device);
  138. ALCdevice_Unlock(Device);
  139. break;
  140. }
  141. }
  142. }
  143. return 0;
  144. }
  145. static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
  146. {
  147. wave_data *data;
  148. const char *fname;
  149. fname = GetConfigValue("wave", "file", "");
  150. if(!fname[0])
  151. return ALC_INVALID_VALUE;
  152. if(!deviceName)
  153. deviceName = waveDevice;
  154. else if(strcmp(deviceName, waveDevice) != 0)
  155. return ALC_INVALID_VALUE;
  156. data = (wave_data*)calloc(1, sizeof(wave_data));
  157. data->f = fopen(fname, "wb");
  158. if(!data->f)
  159. {
  160. free(data);
  161. ERR("Could not open file '%s': %s\n", fname, strerror(errno));
  162. return ALC_INVALID_VALUE;
  163. }
  164. device->DeviceName = strdup(deviceName);
  165. device->ExtraData = data;
  166. return ALC_NO_ERROR;
  167. }
  168. static void wave_close_playback(ALCdevice *device)
  169. {
  170. wave_data *data = (wave_data*)device->ExtraData;
  171. fclose(data->f);
  172. free(data);
  173. device->ExtraData = NULL;
  174. }
  175. static ALCboolean wave_reset_playback(ALCdevice *device)
  176. {
  177. wave_data *data = (wave_data*)device->ExtraData;
  178. ALuint channels=0, bits=0;
  179. size_t val;
  180. fseek(data->f, 0, SEEK_SET);
  181. clearerr(data->f);
  182. switch(device->FmtType)
  183. {
  184. case DevFmtByte:
  185. device->FmtType = DevFmtUByte;
  186. break;
  187. case DevFmtUShort:
  188. device->FmtType = DevFmtShort;
  189. break;
  190. case DevFmtUInt:
  191. device->FmtType = DevFmtInt;
  192. break;
  193. case DevFmtUByte:
  194. case DevFmtShort:
  195. case DevFmtInt:
  196. case DevFmtFloat:
  197. break;
  198. }
  199. bits = BytesFromDevFmt(device->FmtType) * 8;
  200. channels = ChannelsFromDevFmt(device->FmtChans);
  201. fprintf(data->f, "RIFF");
  202. fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
  203. fprintf(data->f, "WAVE");
  204. fprintf(data->f, "fmt ");
  205. fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
  206. // 16-bit val, format type id (extensible: 0xFFFE)
  207. fwrite16le(0xFFFE, data->f);
  208. // 16-bit val, channel count
  209. fwrite16le(channels, data->f);
  210. // 32-bit val, frequency
  211. fwrite32le(device->Frequency, data->f);
  212. // 32-bit val, bytes per second
  213. fwrite32le(device->Frequency * channels * bits / 8, data->f);
  214. // 16-bit val, frame size
  215. fwrite16le(channels * bits / 8, data->f);
  216. // 16-bit val, bits per sample
  217. fwrite16le(bits, data->f);
  218. // 16-bit val, extra byte count
  219. fwrite16le(22, data->f);
  220. // 16-bit val, valid bits per sample
  221. fwrite16le(bits, data->f);
  222. // 32-bit val, channel mask
  223. fwrite32le(channel_masks[channels], data->f);
  224. // 16 byte GUID, sub-type format
  225. val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
  226. (void)val;
  227. fprintf(data->f, "data");
  228. fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
  229. if(ferror(data->f))
  230. {
  231. ERR("Error writing header: %s\n", strerror(errno));
  232. return ALC_FALSE;
  233. }
  234. data->DataStart = ftell(data->f);
  235. SetDefaultWFXChannelOrder(device);
  236. return ALC_TRUE;
  237. }
  238. static ALCboolean wave_start_playback(ALCdevice *device)
  239. {
  240. wave_data *data = (wave_data*)device->ExtraData;
  241. data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  242. data->buffer = malloc(data->size);
  243. if(!data->buffer)
  244. {
  245. ERR("Buffer malloc failed\n");
  246. return ALC_FALSE;
  247. }
  248. if(!StartThread(&data->thread, WaveProc, device))
  249. {
  250. free(data->buffer);
  251. data->buffer = NULL;
  252. return ALC_FALSE;
  253. }
  254. return ALC_TRUE;
  255. }
  256. static void wave_stop_playback(ALCdevice *device)
  257. {
  258. wave_data *data = (wave_data*)device->ExtraData;
  259. ALuint dataLen;
  260. long size;
  261. if(!data->thread)
  262. return;
  263. data->killNow = 1;
  264. StopThread(data->thread);
  265. data->thread = NULL;
  266. data->killNow = 0;
  267. free(data->buffer);
  268. data->buffer = NULL;
  269. size = ftell(data->f);
  270. if(size > 0)
  271. {
  272. dataLen = size - data->DataStart;
  273. if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
  274. fwrite32le(dataLen, data->f); // 'data' header len
  275. if(fseek(data->f, 4, SEEK_SET) == 0)
  276. fwrite32le(size-8, data->f); // 'WAVE' header len
  277. }
  278. }
  279. static const BackendFuncs wave_funcs = {
  280. wave_open_playback,
  281. wave_close_playback,
  282. wave_reset_playback,
  283. wave_start_playback,
  284. wave_stop_playback,
  285. NULL,
  286. NULL,
  287. NULL,
  288. NULL,
  289. NULL,
  290. NULL,
  291. ALCdevice_GetLatencyDefault
  292. };
  293. ALCboolean alc_wave_init(BackendFuncs *func_list)
  294. {
  295. *func_list = wave_funcs;
  296. return ALC_TRUE;
  297. }
  298. void alc_wave_deinit(void)
  299. {
  300. }
  301. void alc_wave_probe(enum DevProbe type)
  302. {
  303. if(!ConfigValueExists("wave", "file"))
  304. return;
  305. switch(type)
  306. {
  307. case ALL_DEVICE_PROBE:
  308. AppendAllDevicesList(waveDevice);
  309. break;
  310. case CAPTURE_DEVICE_PROBE:
  311. break;
  312. }
  313. }