wave.c 14 KB

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