wave.c 8.9 KB

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