wave.c 13 KB

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