wave.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. const ALuint restTime = (ALuint64)pDevice->UpdateSize * 1000 /
  76. pDevice->Frequency / 2;
  77. frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
  78. done = 0;
  79. start = timeGetTime();
  80. while(!data->killNow && pDevice->Connected)
  81. {
  82. now = timeGetTime();
  83. avail = (ALuint64)(now-start) * pDevice->Frequency / 1000;
  84. if(avail < done)
  85. {
  86. /* Timer wrapped (50 days???). Add the remainder of the cycle to
  87. * the available count and reset the number of samples done */
  88. avail += ((ALuint64)1<<32)*pDevice->Frequency/1000 - done;
  89. done = 0;
  90. }
  91. if(avail-done < pDevice->UpdateSize)
  92. {
  93. Sleep(restTime);
  94. continue;
  95. }
  96. while(avail-done >= pDevice->UpdateSize)
  97. {
  98. aluMixData(pDevice, data->buffer, pDevice->UpdateSize);
  99. done += pDevice->UpdateSize;
  100. if(!IS_LITTLE_ENDIAN)
  101. {
  102. ALuint bytesize = BytesFromDevFmt(pDevice->FmtType);
  103. ALubyte *bytes = data->buffer;
  104. ALuint i;
  105. if(bytesize == 1)
  106. {
  107. for(i = 0;i < data->size;i++)
  108. fputc(bytes[i], data->f);
  109. }
  110. else if(bytesize == 2)
  111. {
  112. for(i = 0;i < data->size;i++)
  113. fputc(bytes[i^1], data->f);
  114. }
  115. else if(bytesize == 4)
  116. {
  117. for(i = 0;i < data->size;i++)
  118. fputc(bytes[i^3], data->f);
  119. }
  120. }
  121. else
  122. fs = fwrite(data->buffer, frameSize, pDevice->UpdateSize,
  123. data->f);
  124. if(ferror(data->f))
  125. {
  126. ERR("Error writing to file\n");
  127. aluHandleDisconnect(pDevice);
  128. break;
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. static ALCenum wave_open_playback(ALCdevice *device, const ALCchar *deviceName)
  135. {
  136. wave_data *data;
  137. const char *fname;
  138. fname = GetConfigValue("wave", "file", "");
  139. if(!fname[0])
  140. return ALC_INVALID_VALUE;
  141. if(!deviceName)
  142. deviceName = waveDevice;
  143. else if(strcmp(deviceName, waveDevice) != 0)
  144. return ALC_INVALID_VALUE;
  145. data = (wave_data*)calloc(1, sizeof(wave_data));
  146. data->f = fopen(fname, "wb");
  147. if(!data->f)
  148. {
  149. free(data);
  150. ERR("Could not open file '%s': %s\n", fname, strerror(errno));
  151. return ALC_INVALID_VALUE;
  152. }
  153. device->szDeviceName = strdup(deviceName);
  154. device->ExtraData = data;
  155. return ALC_NO_ERROR;
  156. }
  157. static void wave_close_playback(ALCdevice *device)
  158. {
  159. wave_data *data = (wave_data*)device->ExtraData;
  160. fclose(data->f);
  161. free(data);
  162. device->ExtraData = NULL;
  163. }
  164. static ALCboolean wave_reset_playback(ALCdevice *device)
  165. {
  166. wave_data *data = (wave_data*)device->ExtraData;
  167. ALuint channels=0, bits=0;
  168. size_t val;
  169. fseek(data->f, 0, SEEK_SET);
  170. clearerr(data->f);
  171. switch(device->FmtType)
  172. {
  173. case DevFmtByte:
  174. device->FmtType = DevFmtUByte;
  175. break;
  176. case DevFmtUShort:
  177. device->FmtType = DevFmtShort;
  178. break;
  179. case DevFmtUInt:
  180. device->FmtType = DevFmtInt;
  181. break;
  182. case DevFmtUByte:
  183. case DevFmtShort:
  184. case DevFmtInt:
  185. case DevFmtFloat:
  186. break;
  187. }
  188. bits = BytesFromDevFmt(device->FmtType) * 8;
  189. channels = ChannelsFromDevFmt(device->FmtChans);
  190. fprintf(data->f, "RIFF");
  191. fwrite32le(0xFFFFFFFF, data->f); // 'RIFF' header len; filled in at close
  192. fprintf(data->f, "WAVE");
  193. fprintf(data->f, "fmt ");
  194. fwrite32le(40, data->f); // 'fmt ' header len; 40 bytes for EXTENSIBLE
  195. // 16-bit val, format type id (extensible: 0xFFFE)
  196. fwrite16le(0xFFFE, data->f);
  197. // 16-bit val, channel count
  198. fwrite16le(channels, data->f);
  199. // 32-bit val, frequency
  200. fwrite32le(device->Frequency, data->f);
  201. // 32-bit val, bytes per second
  202. fwrite32le(device->Frequency * channels * bits / 8, data->f);
  203. // 16-bit val, frame size
  204. fwrite16le(channels * bits / 8, data->f);
  205. // 16-bit val, bits per sample
  206. fwrite16le(bits, data->f);
  207. // 16-bit val, extra byte count
  208. fwrite16le(22, data->f);
  209. // 16-bit val, valid bits per sample
  210. fwrite16le(bits, data->f);
  211. // 32-bit val, channel mask
  212. fwrite32le(channel_masks[channels], data->f);
  213. // 16 byte GUID, sub-type format
  214. val = fwrite(((bits==32) ? SUBTYPE_FLOAT : SUBTYPE_PCM), 1, 16, data->f);
  215. fprintf(data->f, "data");
  216. fwrite32le(0xFFFFFFFF, data->f); // 'data' header len; filled in at close
  217. if(ferror(data->f))
  218. {
  219. ERR("Error writing header: %s\n", strerror(errno));
  220. return ALC_FALSE;
  221. }
  222. data->DataStart = ftell(data->f);
  223. SetDefaultWFXChannelOrder(device);
  224. return ALC_TRUE;
  225. }
  226. static ALCboolean wave_start_playback(ALCdevice *device)
  227. {
  228. wave_data *data = (wave_data*)device->ExtraData;
  229. data->size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  230. data->buffer = malloc(data->size);
  231. if(!data->buffer)
  232. {
  233. ERR("Buffer malloc failed\n");
  234. return ALC_FALSE;
  235. }
  236. data->thread = StartThread(WaveProc, device);
  237. if(data->thread == NULL)
  238. {
  239. free(data->buffer);
  240. data->buffer = NULL;
  241. return ALC_FALSE;
  242. }
  243. return ALC_TRUE;
  244. }
  245. static void wave_stop_playback(ALCdevice *device)
  246. {
  247. wave_data *data = (wave_data*)device->ExtraData;
  248. ALuint dataLen;
  249. long size;
  250. if(!data->thread)
  251. return;
  252. data->killNow = 1;
  253. StopThread(data->thread);
  254. data->thread = NULL;
  255. data->killNow = 0;
  256. free(data->buffer);
  257. data->buffer = NULL;
  258. size = ftell(data->f);
  259. if(size > 0)
  260. {
  261. dataLen = size - data->DataStart;
  262. if(fseek(data->f, data->DataStart-4, SEEK_SET) == 0)
  263. fwrite32le(dataLen, data->f); // 'data' header len
  264. if(fseek(data->f, 4, SEEK_SET) == 0)
  265. fwrite32le(size-8, data->f); // 'WAVE' header len
  266. }
  267. }
  268. static const BackendFuncs wave_funcs = {
  269. wave_open_playback,
  270. wave_close_playback,
  271. wave_reset_playback,
  272. wave_start_playback,
  273. wave_stop_playback,
  274. NULL,
  275. NULL,
  276. NULL,
  277. NULL,
  278. NULL,
  279. NULL
  280. };
  281. ALCboolean alc_wave_init(BackendFuncs *func_list)
  282. {
  283. *func_list = wave_funcs;
  284. return ALC_TRUE;
  285. }
  286. void alc_wave_deinit(void)
  287. {
  288. }
  289. void alc_wave_probe(enum DevProbe type)
  290. {
  291. if(!ConfigValueExists("wave", "file"))
  292. return;
  293. switch(type)
  294. {
  295. case ALL_DEVICE_PROBE:
  296. AppendAllDeviceList(waveDevice);
  297. break;
  298. case CAPTURE_DEVICE_PROBE:
  299. break;
  300. }
  301. }