alstreamcb.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * OpenAL Callback-based Stream Example
  3. *
  4. * Copyright (c) 2020 by Chris Robinson <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* This file contains a streaming audio player using a callback buffer. */
  25. #include <algorithm>
  26. #include <atomic>
  27. #include <cassert>
  28. #include <chrono>
  29. #include <cstddef>
  30. #include <cstdlib>
  31. #include <cstdio>
  32. #include <cstring>
  33. #include <memory>
  34. #include <stdexcept>
  35. #include <string>
  36. #include <string_view>
  37. #include <thread>
  38. #include <vector>
  39. #include "sndfile.h"
  40. #include "AL/al.h"
  41. #include "AL/alc.h"
  42. #include "AL/alext.h"
  43. #include "alnumeric.h"
  44. #include "alspan.h"
  45. #include "common/alhelpers.h"
  46. #include "fmt/core.h"
  47. #include "win_main_utf8.h"
  48. namespace {
  49. using std::chrono::seconds;
  50. using std::chrono::nanoseconds;
  51. LPALBUFFERCALLBACKSOFT alBufferCallbackSOFT;
  52. struct StreamPlayer {
  53. /* A lockless ring-buffer (supports single-provider, single-consumer
  54. * operation).
  55. */
  56. std::vector<std::byte> mBufferData;
  57. std::atomic<size_t> mReadPos{0};
  58. std::atomic<size_t> mWritePos{0};
  59. size_t mSamplesPerBlock{1};
  60. size_t mBytesPerBlock{1};
  61. enum class SampleType {
  62. Int16, Float, IMA4, MSADPCM
  63. };
  64. SampleType mSampleFormat{SampleType::Int16};
  65. /* The buffer to get the callback, and source to play with. */
  66. ALuint mBuffer{0}, mSource{0};
  67. size_t mStartOffset{0};
  68. /* Handle for the audio file to decode. */
  69. SNDFILE *mSndfile{nullptr};
  70. SF_INFO mSfInfo{};
  71. size_t mDecoderOffset{0};
  72. /* The format of the callback samples. */
  73. ALenum mFormat{};
  74. StreamPlayer()
  75. {
  76. alGenBuffers(1, &mBuffer);
  77. if(alGetError() != AL_NO_ERROR)
  78. throw std::runtime_error{"alGenBuffers failed"};
  79. alGenSources(1, &mSource);
  80. if(alGetError() != AL_NO_ERROR)
  81. {
  82. alDeleteBuffers(1, &mBuffer);
  83. throw std::runtime_error{"alGenSources failed"};
  84. }
  85. }
  86. ~StreamPlayer()
  87. {
  88. alDeleteSources(1, &mSource);
  89. alDeleteBuffers(1, &mBuffer);
  90. if(mSndfile)
  91. sf_close(mSndfile);
  92. }
  93. void close()
  94. {
  95. if(mSamplesPerBlock > 1)
  96. alBufferi(mBuffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
  97. if(mSndfile)
  98. {
  99. alSourceRewind(mSource);
  100. alSourcei(mSource, AL_BUFFER, 0);
  101. sf_close(mSndfile);
  102. mSndfile = nullptr;
  103. }
  104. }
  105. bool open(const std::string &filename)
  106. {
  107. close();
  108. /* Open the file and figure out the OpenAL format. */
  109. mSndfile = sf_open(filename.c_str(), SFM_READ, &mSfInfo);
  110. if(!mSndfile)
  111. {
  112. fmt::println(stderr, "Could not open audio in {}: {}", filename,
  113. sf_strerror(mSndfile));
  114. return false;
  115. }
  116. switch((mSfInfo.format&SF_FORMAT_SUBMASK))
  117. {
  118. case SF_FORMAT_PCM_24:
  119. case SF_FORMAT_PCM_32:
  120. case SF_FORMAT_FLOAT:
  121. case SF_FORMAT_DOUBLE:
  122. case SF_FORMAT_VORBIS:
  123. case SF_FORMAT_OPUS:
  124. case SF_FORMAT_ALAC_20:
  125. case SF_FORMAT_ALAC_24:
  126. case SF_FORMAT_ALAC_32:
  127. case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
  128. case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
  129. case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
  130. if(alIsExtensionPresent("AL_EXT_FLOAT32"))
  131. mSampleFormat = SampleType::Float;
  132. break;
  133. case SF_FORMAT_IMA_ADPCM:
  134. if(mSfInfo.channels <= 2 && (mSfInfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
  135. && alIsExtensionPresent("AL_EXT_IMA4")
  136. && alIsExtensionPresent("AL_SOFT_block_alignment"))
  137. mSampleFormat = SampleType::IMA4;
  138. break;
  139. case SF_FORMAT_MS_ADPCM:
  140. if(mSfInfo.channels <= 2 && (mSfInfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
  141. && alIsExtensionPresent("AL_SOFT_MSADPCM")
  142. && alIsExtensionPresent("AL_SOFT_block_alignment"))
  143. mSampleFormat = SampleType::MSADPCM;
  144. break;
  145. }
  146. int splblocksize{}, byteblocksize{};
  147. if(mSampleFormat == SampleType::IMA4 || mSampleFormat == SampleType::MSADPCM)
  148. {
  149. SF_CHUNK_INFO inf{ "fmt ", 4, 0, nullptr };
  150. SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(mSndfile, &inf);
  151. if(!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR || inf.datalen < 14)
  152. mSampleFormat = SampleType::Int16;
  153. else
  154. {
  155. auto fmtbuf = std::vector<ALubyte>(inf.datalen);
  156. inf.data = fmtbuf.data();
  157. if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
  158. mSampleFormat = SampleType::Int16;
  159. else
  160. {
  161. byteblocksize = fmtbuf[12] | (fmtbuf[13]<<8u);
  162. if(mSampleFormat == SampleType::IMA4)
  163. {
  164. splblocksize = (byteblocksize/mSfInfo.channels - 4)/4*8 + 1;
  165. if(splblocksize < 1
  166. || ((splblocksize-1)/2 + 4)*mSfInfo.channels != byteblocksize)
  167. mSampleFormat = SampleType::Int16;
  168. }
  169. else
  170. {
  171. splblocksize = (byteblocksize/mSfInfo.channels - 7)*2 + 2;
  172. if(splblocksize < 2
  173. || ((splblocksize-2)/2 + 7)*mSfInfo.channels != byteblocksize)
  174. mSampleFormat = SampleType::Int16;
  175. }
  176. }
  177. }
  178. }
  179. if(mSampleFormat == SampleType::Int16)
  180. {
  181. mSamplesPerBlock = 1;
  182. mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 2;
  183. }
  184. else if(mSampleFormat == SampleType::Float)
  185. {
  186. mSamplesPerBlock = 1;
  187. mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 4;
  188. }
  189. else
  190. {
  191. mSamplesPerBlock = static_cast<size_t>(splblocksize);
  192. mBytesPerBlock = static_cast<size_t>(byteblocksize);
  193. }
  194. mFormat = AL_NONE;
  195. if(mSfInfo.channels == 1)
  196. {
  197. if(mSampleFormat == SampleType::Int16)
  198. mFormat = AL_FORMAT_MONO16;
  199. else if(mSampleFormat == SampleType::Float)
  200. mFormat = AL_FORMAT_MONO_FLOAT32;
  201. else if(mSampleFormat == SampleType::IMA4)
  202. mFormat = AL_FORMAT_MONO_IMA4;
  203. else if(mSampleFormat == SampleType::MSADPCM)
  204. mFormat = AL_FORMAT_MONO_MSADPCM_SOFT;
  205. }
  206. else if(mSfInfo.channels == 2)
  207. {
  208. if(mSampleFormat == SampleType::Int16)
  209. mFormat = AL_FORMAT_STEREO16;
  210. else if(mSampleFormat == SampleType::Float)
  211. mFormat = AL_FORMAT_STEREO_FLOAT32;
  212. else if(mSampleFormat == SampleType::IMA4)
  213. mFormat = AL_FORMAT_STEREO_IMA4;
  214. else if(mSampleFormat == SampleType::MSADPCM)
  215. mFormat = AL_FORMAT_STEREO_MSADPCM_SOFT;
  216. }
  217. else if(mSfInfo.channels == 3)
  218. {
  219. if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
  220. {
  221. if(mSampleFormat == SampleType::Int16)
  222. mFormat = AL_FORMAT_BFORMAT2D_16;
  223. else if(mSampleFormat == SampleType::Float)
  224. mFormat = AL_FORMAT_BFORMAT2D_FLOAT32;
  225. }
  226. }
  227. else if(mSfInfo.channels == 4)
  228. {
  229. if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
  230. {
  231. if(mSampleFormat == SampleType::Int16)
  232. mFormat = AL_FORMAT_BFORMAT3D_16;
  233. else if(mSampleFormat == SampleType::Float)
  234. mFormat = AL_FORMAT_BFORMAT3D_FLOAT32;
  235. }
  236. }
  237. if(!mFormat)
  238. {
  239. fmt::println(stderr, "Unsupported channel count: {}", mSfInfo.channels);
  240. sf_close(mSndfile);
  241. mSndfile = nullptr;
  242. return false;
  243. }
  244. /* Set a 1s ring buffer size. */
  245. size_t numblocks{(static_cast<ALuint>(mSfInfo.samplerate) + mSamplesPerBlock-1)
  246. / mSamplesPerBlock};
  247. mBufferData.resize(static_cast<ALuint>(numblocks * mBytesPerBlock));
  248. mReadPos.store(0, std::memory_order_relaxed);
  249. mWritePos.store(0, std::memory_order_relaxed);
  250. mDecoderOffset = 0;
  251. return true;
  252. }
  253. /* The actual C-style callback just forwards to the non-static method. Not
  254. * strictly needed and the compiler will optimize it to a normal function,
  255. * but it allows the callback implementation to have a nice 'this' pointer
  256. * with normal member access.
  257. */
  258. static ALsizei AL_APIENTRY bufferCallbackC(void *userptr, void *data, ALsizei size) noexcept
  259. { return static_cast<StreamPlayer*>(userptr)->bufferCallback(data, size); }
  260. ALsizei bufferCallback(void *data, ALsizei size) noexcept
  261. {
  262. const auto output = al::span{static_cast<std::byte*>(data), static_cast<ALuint>(size)};
  263. auto dst = output.begin();
  264. /* NOTE: The callback *MUST* be real-time safe! That means no blocking,
  265. * no allocations or deallocations, no I/O, no page faults, or calls to
  266. * functions that could do these things (this includes calling to
  267. * libraries like SDL_sound, libsndfile, ffmpeg, etc). Nothing should
  268. * unexpectedly stall this call since the audio has to get to the
  269. * device on time.
  270. */
  271. size_t roffset{mReadPos.load(std::memory_order_acquire)};
  272. while(const auto remaining = static_cast<size_t>(std::distance(dst, output.end())))
  273. {
  274. /* If the write offset == read offset, there's nothing left in the
  275. * ring-buffer. Break from the loop and give what has been written.
  276. */
  277. const size_t woffset{mWritePos.load(std::memory_order_relaxed)};
  278. if(woffset == roffset) break;
  279. /* If the write offset is behind the read offset, the readable
  280. * portion wrapped around. Just read up to the end of the buffer in
  281. * that case, otherwise read up to the write offset. Also limit the
  282. * amount to copy given how much is remaining to write.
  283. */
  284. size_t todo{((woffset < roffset) ? mBufferData.size() : woffset) - roffset};
  285. todo = std::min(todo, remaining);
  286. /* Copy from the ring buffer to the provided output buffer. Wrap
  287. * the resulting read offset if it reached the end of the ring-
  288. * buffer.
  289. */
  290. const auto input = al::span{mBufferData}.subspan(roffset, todo);
  291. dst = std::copy_n(input.begin(), input.size(), dst);
  292. roffset += todo;
  293. if(roffset == mBufferData.size())
  294. roffset = 0;
  295. }
  296. /* Finally, store the updated read offset, and return how many bytes
  297. * have been written.
  298. */
  299. mReadPos.store(roffset, std::memory_order_release);
  300. return static_cast<ALsizei>(std::distance(output.begin(), dst));
  301. }
  302. bool prepare()
  303. {
  304. if(mSamplesPerBlock > 1)
  305. alBufferi(mBuffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, static_cast<int>(mSamplesPerBlock));
  306. alBufferCallbackSOFT(mBuffer, mFormat, mSfInfo.samplerate, bufferCallbackC, this);
  307. alSourcei(mSource, AL_BUFFER, static_cast<ALint>(mBuffer));
  308. if(ALenum err{alGetError()})
  309. {
  310. fmt::println(stderr, "Failed to set callback: {} ({:#x})", alGetString(err),
  311. as_unsigned(err));
  312. return false;
  313. }
  314. return true;
  315. }
  316. bool update()
  317. {
  318. ALenum state;
  319. ALint pos;
  320. alGetSourcei(mSource, AL_SAMPLE_OFFSET, &pos);
  321. alGetSourcei(mSource, AL_SOURCE_STATE, &state);
  322. size_t woffset{mWritePos.load(std::memory_order_acquire)};
  323. if(state != AL_INITIAL)
  324. {
  325. const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
  326. const size_t readable{((woffset >= roffset) ? woffset : (mBufferData.size()+woffset)) -
  327. roffset};
  328. /* For a stopped (underrun) source, the current playback offset is
  329. * the current decoder offset excluding the readable buffered data.
  330. * For a playing/paused source, it's the source's offset including
  331. * the playback offset the source was started with.
  332. */
  333. const auto curtime = ((state == AL_STOPPED)
  334. ? (mDecoderOffset-readable) / mBytesPerBlock * mSamplesPerBlock
  335. : (size_t{static_cast<ALuint>(pos)} + mStartOffset))
  336. / static_cast<ALuint>(mSfInfo.samplerate);
  337. fmt::print("\r {}m{:02}s ({:3}% full)", curtime/60, curtime%60,
  338. readable * 100 / mBufferData.size());
  339. }
  340. else
  341. fmt::println("Starting...");
  342. fflush(stdout);
  343. while(!sf_error(mSndfile))
  344. {
  345. const auto roffset = mReadPos.load(std::memory_order_relaxed);
  346. auto get_writable = [this,roffset,woffset]() noexcept -> size_t
  347. {
  348. if(roffset > woffset)
  349. {
  350. /* Note that the ring buffer's writable space is one byte
  351. * less than the available area because the write offset
  352. * ending up at the read offset would be interpreted as
  353. * being empty instead of full.
  354. */
  355. return roffset - woffset - 1;
  356. }
  357. /* If the read offset is at or behind the write offset, the
  358. * writeable area (might) wrap around. Make sure the sample
  359. * data can fit, and calculate how much can go in front before
  360. * wrapping.
  361. */
  362. return mBufferData.size() - (!roffset ? woffset+1 : woffset);
  363. };
  364. const auto writable = get_writable() / mBytesPerBlock;
  365. if(!writable) break;
  366. auto read_bytes = size_t{};
  367. if(mSampleFormat == SampleType::Int16)
  368. {
  369. const auto num_frames = sf_readf_short(mSndfile,
  370. reinterpret_cast<short*>(&mBufferData[woffset]),
  371. static_cast<sf_count_t>(writable*mSamplesPerBlock));
  372. if(num_frames < 1) break;
  373. read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
  374. }
  375. else if(mSampleFormat == SampleType::Float)
  376. {
  377. const auto num_frames = sf_readf_float(mSndfile,
  378. reinterpret_cast<float*>(&mBufferData[woffset]),
  379. static_cast<sf_count_t>(writable*mSamplesPerBlock));
  380. if(num_frames < 1) break;
  381. read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
  382. }
  383. else
  384. {
  385. const auto numbytes = sf_read_raw(mSndfile, &mBufferData[woffset],
  386. static_cast<sf_count_t>(writable*mBytesPerBlock));
  387. if(numbytes < 1) break;
  388. read_bytes = static_cast<size_t>(numbytes);
  389. }
  390. woffset += read_bytes;
  391. if(woffset == mBufferData.size())
  392. woffset = 0;
  393. mWritePos.store(woffset, std::memory_order_release);
  394. mDecoderOffset += read_bytes;
  395. }
  396. if(state != AL_PLAYING && state != AL_PAUSED)
  397. {
  398. /* If the source is not playing or paused, it either underrun
  399. * (AL_STOPPED) or is just getting started (AL_INITIAL). If the
  400. * ring buffer is empty, it's done, otherwise play the source with
  401. * what's available.
  402. */
  403. const auto roffset = mReadPos.load(std::memory_order_relaxed);
  404. const auto readable = ((woffset < roffset) ? mBufferData.size()+woffset : woffset) -
  405. roffset;
  406. if(readable == 0)
  407. return false;
  408. /* Store the playback offset that the source will start reading
  409. * from, so it can be tracked during playback.
  410. */
  411. mStartOffset = (mDecoderOffset-readable) / mBytesPerBlock * mSamplesPerBlock;
  412. alSourcePlay(mSource);
  413. if(alGetError() != AL_NO_ERROR)
  414. return false;
  415. }
  416. return true;
  417. }
  418. };
  419. int main(al::span<std::string_view> args)
  420. {
  421. /* Print out usage if no arguments were specified */
  422. if(args.size() < 2)
  423. {
  424. fmt::println(stderr, "Usage: {} [-device <name>] <filenames...>", args[0]);
  425. return 1;
  426. }
  427. args = args.subspan(1);
  428. if(InitAL(args) != 0)
  429. throw std::runtime_error{"Failed to initialize OpenAL"};
  430. /* A simple RAII container for automating OpenAL shutdown. */
  431. struct AudioManager {
  432. AudioManager() = default;
  433. AudioManager(const AudioManager&) = delete;
  434. auto operator=(const AudioManager&) -> AudioManager& = delete;
  435. ~AudioManager() { CloseAL(); }
  436. };
  437. AudioManager almgr;
  438. if(!alIsExtensionPresent("AL_SOFT_callback_buffer"))
  439. {
  440. fmt::println(stderr, "AL_SOFT_callback_buffer extension not available");
  441. return 1;
  442. }
  443. alBufferCallbackSOFT = reinterpret_cast<LPALBUFFERCALLBACKSOFT>(
  444. alGetProcAddress("alBufferCallbackSOFT"));
  445. ALCint refresh{25};
  446. alcGetIntegerv(alcGetContextsDevice(alcGetCurrentContext()), ALC_REFRESH, 1, &refresh);
  447. auto player = std::make_unique<StreamPlayer>();
  448. /* Play each file listed on the command line */
  449. for(size_t i{0};i < args.size();++i)
  450. {
  451. if(!player->open(std::string{args[i]}))
  452. continue;
  453. /* Get the name portion, without the path, for display. */
  454. const auto fpos = std::max(args[i].rfind('/')+1, args[i].rfind('\\')+1);
  455. const auto namepart = args[i].substr(fpos);
  456. fmt::println("Playing: {} ({}, {}hz)", namepart, FormatName(player->mFormat),
  457. player->mSfInfo.samplerate);
  458. fflush(stdout);
  459. if(!player->prepare())
  460. {
  461. player->close();
  462. continue;
  463. }
  464. while(player->update())
  465. std::this_thread::sleep_for(nanoseconds{seconds{1}} / refresh);
  466. putc('\n', stdout);
  467. /* All done with this file. Close it and go to the next */
  468. player->close();
  469. }
  470. /* All done. */
  471. fmt::println("Done.");
  472. return 0;
  473. }
  474. } // namespace
  475. int main(int argc, char **argv)
  476. {
  477. assert(argc >= 0);
  478. auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
  479. std::copy_n(argv, args.size(), args.begin());
  480. return main(al::span{args});
  481. }