wave.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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, 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. 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. ALuint i;
  135. if(bytesize == 2)
  136. {
  137. ALushort *samples = self->mBuffer;
  138. ALuint len = self->mSize / 2;
  139. for(i = 0;i < len;i++)
  140. {
  141. ALushort samp = samples[i];
  142. samples[i] = (samp>>8) | (samp<<8);
  143. }
  144. }
  145. else if(bytesize == 4)
  146. {
  147. ALuint *samples = self->mBuffer;
  148. ALuint len = self->mSize / 4;
  149. for(i = 0;i < len;i++)
  150. {
  151. ALuint samp = samples[i];
  152. samples[i] = (samp>>24) | ((samp>>8)&0x0000ff00) |
  153. ((samp<<8)&0x00ff0000) | (samp<<24);
  154. }
  155. }
  156. }
  157. fs = fwrite(self->mBuffer, frameSize, device->UpdateSize, self->mFile);
  158. (void)fs;
  159. if(ferror(self->mFile))
  160. {
  161. ERR("Error writing to file\n");
  162. ALCdevice_Lock(device);
  163. aluHandleDisconnect(device);
  164. ALCdevice_Unlock(device);
  165. break;
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. static ALCenum ALCwaveBackend_open(ALCwaveBackend *self, const ALCchar *name)
  172. {
  173. ALCdevice *device;
  174. const char *fname;
  175. fname = GetConfigValue(NULL, "wave", "file", "");
  176. if(!fname[0]) return ALC_INVALID_VALUE;
  177. if(!name)
  178. name = waveDevice;
  179. else if(strcmp(name, waveDevice) != 0)
  180. return ALC_INVALID_VALUE;
  181. self->mFile = al_fopen(fname, "wb");
  182. if(!self->mFile)
  183. {
  184. ERR("Could not open file '%s': %s\n", fname, strerror(errno));
  185. return ALC_INVALID_VALUE;
  186. }
  187. device = STATIC_CAST(ALCbackend, self)->mDevice;
  188. al_string_copy_cstr(&device->DeviceName, name);
  189. return ALC_NO_ERROR;
  190. }
  191. static void ALCwaveBackend_close(ALCwaveBackend *self)
  192. {
  193. if(self->mFile)
  194. fclose(self->mFile);
  195. self->mFile = NULL;
  196. }
  197. static ALCboolean ALCwaveBackend_reset(ALCwaveBackend *self)
  198. {
  199. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  200. ALuint channels=0, bits=0, chanmask=0;
  201. int isbformat = 0;
  202. size_t val;
  203. fseek(self->mFile, 0, SEEK_SET);
  204. clearerr(self->mFile);
  205. if(GetConfigValueBool(NULL, "wave", "bformat", 0))
  206. device->FmtChans = DevFmtAmbi1;
  207. switch(device->FmtType)
  208. {
  209. case DevFmtByte:
  210. device->FmtType = DevFmtUByte;
  211. break;
  212. case DevFmtUShort:
  213. device->FmtType = DevFmtShort;
  214. break;
  215. case DevFmtUInt:
  216. device->FmtType = DevFmtInt;
  217. break;
  218. case DevFmtUByte:
  219. case DevFmtShort:
  220. case DevFmtInt:
  221. case DevFmtFloat:
  222. break;
  223. }
  224. switch(device->FmtChans)
  225. {
  226. case DevFmtMono: chanmask = 0x04; break;
  227. case DevFmtStereo: chanmask = 0x01 | 0x02; break;
  228. case DevFmtQuad: chanmask = 0x01 | 0x02 | 0x10 | 0x20; break;
  229. case DevFmtX51: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x200 | 0x400; break;
  230. case DevFmtX51Rear: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020; break;
  231. case DevFmtX61: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x100 | 0x200 | 0x400; break;
  232. case DevFmtX71: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400; break;
  233. case DevFmtAmbi1:
  234. case DevFmtAmbi2:
  235. case DevFmtAmbi3:
  236. /* .amb output requires FuMa */
  237. device->AmbiFmt = AmbiFormat_FuMa;
  238. isbformat = 1;
  239. chanmask = 0;
  240. break;
  241. }
  242. bits = BytesFromDevFmt(device->FmtType) * 8;
  243. channels = ChannelsFromDevFmt(device->FmtChans);
  244. fprintf(self->mFile, "RIFF");
  245. fwrite32le(0xFFFFFFFF, self->mFile); // 'RIFF' header len; filled in at close
  246. fprintf(self->mFile, "WAVE");
  247. fprintf(self->mFile, "fmt ");
  248. fwrite32le(40, self->mFile); // 'fmt ' header len; 40 bytes for EXTENSIBLE
  249. // 16-bit val, format type id (extensible: 0xFFFE)
  250. fwrite16le(0xFFFE, self->mFile);
  251. // 16-bit val, channel count
  252. fwrite16le(channels, self->mFile);
  253. // 32-bit val, frequency
  254. fwrite32le(device->Frequency, self->mFile);
  255. // 32-bit val, bytes per second
  256. fwrite32le(device->Frequency * channels * bits / 8, self->mFile);
  257. // 16-bit val, frame size
  258. fwrite16le(channels * bits / 8, self->mFile);
  259. // 16-bit val, bits per sample
  260. fwrite16le(bits, self->mFile);
  261. // 16-bit val, extra byte count
  262. fwrite16le(22, self->mFile);
  263. // 16-bit val, valid bits per sample
  264. fwrite16le(bits, self->mFile);
  265. // 32-bit val, channel mask
  266. fwrite32le(chanmask, self->mFile);
  267. // 16 byte GUID, sub-type format
  268. val = fwrite(((bits==32) ? (isbformat ? SUBTYPE_BFORMAT_FLOAT : SUBTYPE_FLOAT) :
  269. (isbformat ? SUBTYPE_BFORMAT_PCM : SUBTYPE_PCM)), 1, 16, self->mFile);
  270. (void)val;
  271. fprintf(self->mFile, "data");
  272. fwrite32le(0xFFFFFFFF, self->mFile); // 'data' header len; filled in at close
  273. if(ferror(self->mFile))
  274. {
  275. ERR("Error writing header: %s\n", strerror(errno));
  276. return ALC_FALSE;
  277. }
  278. self->mDataStart = ftell(self->mFile);
  279. SetDefaultWFXChannelOrder(device);
  280. return ALC_TRUE;
  281. }
  282. static ALCboolean ALCwaveBackend_start(ALCwaveBackend *self)
  283. {
  284. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  285. self->mSize = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  286. self->mBuffer = malloc(self->mSize);
  287. if(!self->mBuffer)
  288. {
  289. ERR("Buffer malloc failed\n");
  290. return ALC_FALSE;
  291. }
  292. self->killNow = 0;
  293. if(althrd_create(&self->thread, ALCwaveBackend_mixerProc, self) != althrd_success)
  294. {
  295. free(self->mBuffer);
  296. self->mBuffer = NULL;
  297. self->mSize = 0;
  298. return ALC_FALSE;
  299. }
  300. return ALC_TRUE;
  301. }
  302. static void ALCwaveBackend_stop(ALCwaveBackend *self)
  303. {
  304. ALuint dataLen;
  305. long size;
  306. int res;
  307. if(self->killNow)
  308. return;
  309. self->killNow = 1;
  310. althrd_join(self->thread, &res);
  311. free(self->mBuffer);
  312. self->mBuffer = NULL;
  313. size = ftell(self->mFile);
  314. if(size > 0)
  315. {
  316. dataLen = size - self->mDataStart;
  317. if(fseek(self->mFile, self->mDataStart-4, SEEK_SET) == 0)
  318. fwrite32le(dataLen, self->mFile); // 'data' header len
  319. if(fseek(self->mFile, 4, SEEK_SET) == 0)
  320. fwrite32le(size-8, self->mFile); // 'WAVE' header len
  321. }
  322. }
  323. typedef struct ALCwaveBackendFactory {
  324. DERIVE_FROM_TYPE(ALCbackendFactory);
  325. } ALCwaveBackendFactory;
  326. #define ALCWAVEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCwaveBackendFactory, ALCbackendFactory) } }
  327. ALCbackendFactory *ALCwaveBackendFactory_getFactory(void);
  328. static ALCboolean ALCwaveBackendFactory_init(ALCwaveBackendFactory *self);
  329. static DECLARE_FORWARD(ALCwaveBackendFactory, ALCbackendFactory, void, deinit)
  330. static ALCboolean ALCwaveBackendFactory_querySupport(ALCwaveBackendFactory *self, ALCbackend_Type type);
  331. static void ALCwaveBackendFactory_probe(ALCwaveBackendFactory *self, enum DevProbe type);
  332. static ALCbackend* ALCwaveBackendFactory_createBackend(ALCwaveBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  333. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCwaveBackendFactory);
  334. ALCbackendFactory *ALCwaveBackendFactory_getFactory(void)
  335. {
  336. static ALCwaveBackendFactory factory = ALCWAVEBACKENDFACTORY_INITIALIZER;
  337. return STATIC_CAST(ALCbackendFactory, &factory);
  338. }
  339. static ALCboolean ALCwaveBackendFactory_init(ALCwaveBackendFactory* UNUSED(self))
  340. {
  341. return ALC_TRUE;
  342. }
  343. static ALCboolean ALCwaveBackendFactory_querySupport(ALCwaveBackendFactory* UNUSED(self), ALCbackend_Type type)
  344. {
  345. if(type == ALCbackend_Playback)
  346. return !!ConfigValueExists(NULL, "wave", "file");
  347. return ALC_FALSE;
  348. }
  349. static void ALCwaveBackendFactory_probe(ALCwaveBackendFactory* UNUSED(self), enum DevProbe type)
  350. {
  351. switch(type)
  352. {
  353. case ALL_DEVICE_PROBE:
  354. AppendAllDevicesList(waveDevice);
  355. break;
  356. case CAPTURE_DEVICE_PROBE:
  357. break;
  358. }
  359. }
  360. static ALCbackend* ALCwaveBackendFactory_createBackend(ALCwaveBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  361. {
  362. if(type == ALCbackend_Playback)
  363. {
  364. ALCwaveBackend *backend;
  365. NEW_OBJ(backend, ALCwaveBackend)(device);
  366. if(!backend) return NULL;
  367. return STATIC_CAST(ALCbackend, backend);
  368. }
  369. return NULL;
  370. }