wave.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #include "alMain.h"
  26. #include "alu.h"
  27. #include "threads.h"
  28. #include "compat.h"
  29. #include "backends/base.h"
  30. static const ALCchar waveDevice[] = "Wave File Writer";
  31. static const ALubyte SUBTYPE_PCM[] = {
  32. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
  33. 0x00, 0x38, 0x9b, 0x71
  34. };
  35. static const ALubyte SUBTYPE_FLOAT[] = {
  36. 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
  37. 0x00, 0x38, 0x9b, 0x71
  38. };
  39. static const ALubyte SUBTYPE_BFORMAT_PCM[] = {
  40. 0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
  41. 0xca, 0x00, 0x00, 0x00
  42. };
  43. static const ALubyte SUBTYPE_BFORMAT_FLOAT[] = {
  44. 0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
  45. 0xca, 0x00, 0x00, 0x00
  46. };
  47. static void fwrite16le(ALushort val, FILE *f)
  48. {
  49. fputc(val&0xff, f);
  50. fputc((val>>8)&0xff, f);
  51. }
  52. static void fwrite32le(ALuint val, FILE *f)
  53. {
  54. fputc(val&0xff, f);
  55. fputc((val>>8)&0xff, f);
  56. fputc((val>>16)&0xff, f);
  57. fputc((val>>24)&0xff, f);
  58. }
  59. typedef struct ALCwaveBackend {
  60. DERIVE_FROM_TYPE(ALCbackend);
  61. FILE *mFile;
  62. long mDataStart;
  63. ALvoid *mBuffer;
  64. ALuint mSize;
  65. volatile int killNow;
  66. althrd_t thread;
  67. } ALCwaveBackend;
  68. static int ALCwaveBackend_mixerProc(void *ptr);
  69. static void ALCwaveBackend_Construct(ALCwaveBackend *self, ALCdevice *device);
  70. static DECLARE_FORWARD(ALCwaveBackend, ALCbackend, void, Destruct)
  71. static ALCenum ALCwaveBackend_open(ALCwaveBackend *self, const ALCchar *name);
  72. static void ALCwaveBackend_close(ALCwaveBackend *self);
  73. static ALCboolean ALCwaveBackend_reset(ALCwaveBackend *self);
  74. static ALCboolean ALCwaveBackend_start(ALCwaveBackend *self);
  75. static void ALCwaveBackend_stop(ALCwaveBackend *self);
  76. static DECLARE_FORWARD2(ALCwaveBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  77. static DECLARE_FORWARD(ALCwaveBackend, ALCbackend, ALCuint, availableSamples)
  78. static DECLARE_FORWARD(ALCwaveBackend, ALCbackend, ALint64, getLatency)
  79. static DECLARE_FORWARD(ALCwaveBackend, ALCbackend, void, lock)
  80. static DECLARE_FORWARD(ALCwaveBackend, ALCbackend, void, unlock)
  81. DECLARE_DEFAULT_ALLOCATORS(ALCwaveBackend)
  82. DEFINE_ALCBACKEND_VTABLE(ALCwaveBackend);
  83. static void ALCwaveBackend_Construct(ALCwaveBackend *self, ALCdevice *device)
  84. {
  85. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  86. SET_VTABLE2(ALCwaveBackend, ALCbackend, self);
  87. self->mFile = NULL;
  88. self->mDataStart = -1;
  89. self->mBuffer = NULL;
  90. self->mSize = 0;
  91. self->killNow = 1;
  92. }
  93. static int ALCwaveBackend_mixerProc(void *ptr)
  94. {
  95. ALCwaveBackend *self = (ALCwaveBackend*)ptr;
  96. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  97. struct timespec now, start;
  98. ALint64 avail, done;
  99. ALuint frameSize;
  100. size_t fs;
  101. const long restTime = (long)((ALuint64)device->UpdateSize * 1000000000 /
  102. device->Frequency / 2);
  103. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  104. frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  105. done = 0;
  106. if(altimespec_get(&start, AL_TIME_UTC) != AL_TIME_UTC)
  107. {
  108. ERR("Failed to get starting time\n");
  109. return 1;
  110. }
  111. while(!self->killNow && device->Connected)
  112. {
  113. if(altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
  114. {
  115. ERR("Failed to get current time\n");
  116. return 1;
  117. }
  118. avail = (now.tv_sec - start.tv_sec) * device->Frequency;
  119. avail += (ALint64)(now.tv_nsec - start.tv_nsec) * device->Frequency / 1000000000;
  120. if(avail < done)
  121. {
  122. /* Oops, time skipped backwards. Reset the number of samples done
  123. * with one update available since we (likely) just came back from
  124. * sleeping. */
  125. done = avail - device->UpdateSize;
  126. }
  127. if(avail-done < device->UpdateSize)
  128. al_nssleep(restTime);
  129. else while(avail-done >= device->UpdateSize)
  130. {
  131. aluMixData(device, self->mBuffer, device->UpdateSize);
  132. done += device->UpdateSize;
  133. if(!IS_LITTLE_ENDIAN)
  134. {
  135. ALuint bytesize = BytesFromDevFmt(device->FmtType);
  136. ALubyte *bytes = self->mBuffer;
  137. ALuint i;
  138. if(bytesize == 1)
  139. {
  140. for(i = 0;i < self->mSize;i++)
  141. fputc(bytes[i], self->mFile);
  142. }
  143. else if(bytesize == 2)
  144. {
  145. for(i = 0;i < self->mSize;i++)
  146. fputc(bytes[i^1], self->mFile);
  147. }
  148. else if(bytesize == 4)
  149. {
  150. for(i = 0;i < self->mSize;i++)
  151. fputc(bytes[i^3], self->mFile);
  152. }
  153. }
  154. else
  155. {
  156. fs = fwrite(self->mBuffer, frameSize, device->UpdateSize,
  157. self->mFile);
  158. (void)fs;
  159. }
  160. if(ferror(self->mFile))
  161. {
  162. ERR("Error writing to file\n");
  163. ALCdevice_Lock(device);
  164. aluHandleDisconnect(device);
  165. ALCdevice_Unlock(device);
  166. break;
  167. }
  168. }
  169. }
  170. return 0;
  171. }
  172. static ALCenum ALCwaveBackend_open(ALCwaveBackend *self, const ALCchar *name)
  173. {
  174. ALCdevice *device;
  175. const char *fname;
  176. fname = GetConfigValue(NULL, "wave", "file", "");
  177. if(!fname[0]) return ALC_INVALID_VALUE;
  178. if(!name)
  179. name = waveDevice;
  180. else if(strcmp(name, waveDevice) != 0)
  181. return ALC_INVALID_VALUE;
  182. self->mFile = al_fopen(fname, "wb");
  183. if(!self->mFile)
  184. {
  185. ERR("Could not open file '%s': %s\n", fname, strerror(errno));
  186. return ALC_INVALID_VALUE;
  187. }
  188. device = STATIC_CAST(ALCbackend, self)->mDevice;
  189. al_string_copy_cstr(&device->DeviceName, name);
  190. return ALC_NO_ERROR;
  191. }
  192. static void ALCwaveBackend_close(ALCwaveBackend *self)
  193. {
  194. if(self->mFile)
  195. fclose(self->mFile);
  196. self->mFile = NULL;
  197. }
  198. static ALCboolean ALCwaveBackend_reset(ALCwaveBackend *self)
  199. {
  200. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  201. ALuint channels=0, bits=0, chanmask=0;
  202. int isbformat = 0;
  203. size_t val;
  204. fseek(self->mFile, 0, SEEK_SET);
  205. clearerr(self->mFile);
  206. if(GetConfigValueBool(NULL, "wave", "bformat", 0))
  207. device->FmtChans = DevFmtBFormat3D;
  208. switch(device->FmtType)
  209. {
  210. case DevFmtByte:
  211. device->FmtType = DevFmtUByte;
  212. break;
  213. case DevFmtUShort:
  214. device->FmtType = DevFmtShort;
  215. break;
  216. case DevFmtUInt:
  217. device->FmtType = DevFmtInt;
  218. break;
  219. case DevFmtUByte:
  220. case DevFmtShort:
  221. case DevFmtInt:
  222. case DevFmtFloat:
  223. break;
  224. }
  225. switch(device->FmtChans)
  226. {
  227. case DevFmtMono: chanmask = 0x04; break;
  228. case DevFmtStereo: chanmask = 0x01 | 0x02; break;
  229. case DevFmtQuad: chanmask = 0x01 | 0x02 | 0x10 | 0x20; break;
  230. case DevFmtX51: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x200 | 0x400; break;
  231. case DevFmtX51Rear: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020; break;
  232. case DevFmtX61: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x100 | 0x200 | 0x400; break;
  233. case DevFmtX71: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400; break;
  234. case DevFmtBFormat3D:
  235. isbformat = 1;
  236. chanmask = 0;
  237. break;
  238. }
  239. bits = BytesFromDevFmt(device->FmtType) * 8;
  240. channels = ChannelsFromDevFmt(device->FmtChans);
  241. fprintf(self->mFile, "RIFF");
  242. fwrite32le(0xFFFFFFFF, self->mFile); // 'RIFF' header len; filled in at close
  243. fprintf(self->mFile, "WAVE");
  244. fprintf(self->mFile, "fmt ");
  245. fwrite32le(40, self->mFile); // 'fmt ' header len; 40 bytes for EXTENSIBLE
  246. // 16-bit val, format type id (extensible: 0xFFFE)
  247. fwrite16le(0xFFFE, self->mFile);
  248. // 16-bit val, channel count
  249. fwrite16le(channels, self->mFile);
  250. // 32-bit val, frequency
  251. fwrite32le(device->Frequency, self->mFile);
  252. // 32-bit val, bytes per second
  253. fwrite32le(device->Frequency * channels * bits / 8, self->mFile);
  254. // 16-bit val, frame size
  255. fwrite16le(channels * bits / 8, self->mFile);
  256. // 16-bit val, bits per sample
  257. fwrite16le(bits, self->mFile);
  258. // 16-bit val, extra byte count
  259. fwrite16le(22, self->mFile);
  260. // 16-bit val, valid bits per sample
  261. fwrite16le(bits, self->mFile);
  262. // 32-bit val, channel mask
  263. fwrite32le(chanmask, self->mFile);
  264. // 16 byte GUID, sub-type format
  265. val = fwrite(((bits==32) ? (isbformat ? SUBTYPE_BFORMAT_FLOAT : SUBTYPE_FLOAT) :
  266. (isbformat ? SUBTYPE_BFORMAT_PCM : SUBTYPE_PCM)), 1, 16, self->mFile);
  267. (void)val;
  268. fprintf(self->mFile, "data");
  269. fwrite32le(0xFFFFFFFF, self->mFile); // 'data' header len; filled in at close
  270. if(ferror(self->mFile))
  271. {
  272. ERR("Error writing header: %s\n", strerror(errno));
  273. return ALC_FALSE;
  274. }
  275. self->mDataStart = ftell(self->mFile);
  276. SetDefaultWFXChannelOrder(device);
  277. return ALC_TRUE;
  278. }
  279. static ALCboolean ALCwaveBackend_start(ALCwaveBackend *self)
  280. {
  281. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  282. self->mSize = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  283. self->mBuffer = malloc(self->mSize);
  284. if(!self->mBuffer)
  285. {
  286. ERR("Buffer malloc failed\n");
  287. return ALC_FALSE;
  288. }
  289. self->killNow = 0;
  290. if(althrd_create(&self->thread, ALCwaveBackend_mixerProc, self) != althrd_success)
  291. {
  292. free(self->mBuffer);
  293. self->mBuffer = NULL;
  294. self->mSize = 0;
  295. return ALC_FALSE;
  296. }
  297. return ALC_TRUE;
  298. }
  299. static void ALCwaveBackend_stop(ALCwaveBackend *self)
  300. {
  301. ALuint dataLen;
  302. long size;
  303. int res;
  304. if(self->killNow)
  305. return;
  306. self->killNow = 1;
  307. althrd_join(self->thread, &res);
  308. free(self->mBuffer);
  309. self->mBuffer = NULL;
  310. size = ftell(self->mFile);
  311. if(size > 0)
  312. {
  313. dataLen = size - self->mDataStart;
  314. if(fseek(self->mFile, self->mDataStart-4, SEEK_SET) == 0)
  315. fwrite32le(dataLen, self->mFile); // 'data' header len
  316. if(fseek(self->mFile, 4, SEEK_SET) == 0)
  317. fwrite32le(size-8, self->mFile); // 'WAVE' header len
  318. }
  319. }
  320. typedef struct ALCwaveBackendFactory {
  321. DERIVE_FROM_TYPE(ALCbackendFactory);
  322. } ALCwaveBackendFactory;
  323. #define ALCWAVEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCwaveBackendFactory, ALCbackendFactory) } }
  324. ALCbackendFactory *ALCwaveBackendFactory_getFactory(void);
  325. static ALCboolean ALCwaveBackendFactory_init(ALCwaveBackendFactory *self);
  326. static DECLARE_FORWARD(ALCwaveBackendFactory, ALCbackendFactory, void, deinit)
  327. static ALCboolean ALCwaveBackendFactory_querySupport(ALCwaveBackendFactory *self, ALCbackend_Type type);
  328. static void ALCwaveBackendFactory_probe(ALCwaveBackendFactory *self, enum DevProbe type);
  329. static ALCbackend* ALCwaveBackendFactory_createBackend(ALCwaveBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  330. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCwaveBackendFactory);
  331. ALCbackendFactory *ALCwaveBackendFactory_getFactory(void)
  332. {
  333. static ALCwaveBackendFactory factory = ALCWAVEBACKENDFACTORY_INITIALIZER;
  334. return STATIC_CAST(ALCbackendFactory, &factory);
  335. }
  336. static ALCboolean ALCwaveBackendFactory_init(ALCwaveBackendFactory* UNUSED(self))
  337. {
  338. return ALC_TRUE;
  339. }
  340. static ALCboolean ALCwaveBackendFactory_querySupport(ALCwaveBackendFactory* UNUSED(self), ALCbackend_Type type)
  341. {
  342. if(type == ALCbackend_Playback)
  343. return !!ConfigValueExists(NULL, "wave", "file");
  344. return ALC_FALSE;
  345. }
  346. static void ALCwaveBackendFactory_probe(ALCwaveBackendFactory* UNUSED(self), enum DevProbe type)
  347. {
  348. switch(type)
  349. {
  350. case ALL_DEVICE_PROBE:
  351. AppendAllDevicesList(waveDevice);
  352. break;
  353. case CAPTURE_DEVICE_PROBE:
  354. break;
  355. }
  356. }
  357. static ALCbackend* ALCwaveBackendFactory_createBackend(ALCwaveBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  358. {
  359. if(type == ALCbackend_Playback)
  360. {
  361. ALCwaveBackend *backend;
  362. NEW_OBJ(backend, ALCwaveBackend)(device);
  363. if(!backend) return NULL;
  364. return STATIC_CAST(ALCbackend, backend);
  365. }
  366. return NULL;
  367. }