2 * OpenAL Callback-based Stream Example
4 * Copyright (c) 2020 by Chris Robinson <chris.kcat@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* This file contains a streaming audio player using a callback buffer. */
39 #include <string_view>
51 #include "common/alhelpers.h"
53 #include "win_main_utf8.h"
58 using std::chrono::seconds
;
59 using std::chrono::nanoseconds
;
61 LPALBUFFERCALLBACKSOFT alBufferCallbackSOFT
;
64 /* A lockless ring-buffer (supports single-provider, single-consumer
67 std::vector
<std::byte
> mBufferData
;
68 std::atomic
<size_t> mReadPos
{0};
69 std::atomic
<size_t> mWritePos
{0};
70 size_t mSamplesPerBlock
{1};
71 size_t mBytesPerBlock
{1};
73 enum class SampleType
{
74 Int16
, Float
, IMA4
, MSADPCM
76 SampleType mSampleFormat
{SampleType::Int16
};
78 /* The buffer to get the callback, and source to play with. */
79 ALuint mBuffer
{0}, mSource
{0};
80 size_t mStartOffset
{0};
82 /* Handle for the audio file to decode. */
83 SNDFILE
*mSndfile
{nullptr};
85 size_t mDecoderOffset
{0};
87 /* The format of the callback samples. */
92 alGenBuffers(1, &mBuffer
);
93 if(alGetError() != AL_NO_ERROR
)
94 throw std::runtime_error
{"alGenBuffers failed"};
95 alGenSources(1, &mSource
);
96 if(alGetError() != AL_NO_ERROR
)
98 alDeleteBuffers(1, &mBuffer
);
99 throw std::runtime_error
{"alGenSources failed"};
104 alDeleteSources(1, &mSource
);
105 alDeleteBuffers(1, &mBuffer
);
112 if(mSamplesPerBlock
> 1)
113 alBufferi(mBuffer
, AL_UNPACK_BLOCK_ALIGNMENT_SOFT
, 0);
117 alSourceRewind(mSource
);
118 alSourcei(mSource
, AL_BUFFER
, 0);
124 bool open(const std::string
&filename
)
128 /* Open the file and figure out the OpenAL format. */
129 mSndfile
= sf_open(filename
.c_str(), SFM_READ
, &mSfInfo
);
132 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
.c_str(),
133 sf_strerror(mSndfile
));
137 switch((mSfInfo
.format
&SF_FORMAT_SUBMASK
))
139 case SF_FORMAT_PCM_24
:
140 case SF_FORMAT_PCM_32
:
141 case SF_FORMAT_FLOAT
:
142 case SF_FORMAT_DOUBLE
:
143 case SF_FORMAT_VORBIS
:
145 case SF_FORMAT_ALAC_20
:
146 case SF_FORMAT_ALAC_24
:
147 case SF_FORMAT_ALAC_32
:
148 case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
149 case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
150 case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
151 if(alIsExtensionPresent("AL_EXT_FLOAT32"))
152 mSampleFormat
= SampleType::Float
;
154 case SF_FORMAT_IMA_ADPCM
:
155 if(mSfInfo
.channels
<= 2 && (mSfInfo
.format
&SF_FORMAT_TYPEMASK
) == SF_FORMAT_WAV
156 && alIsExtensionPresent("AL_EXT_IMA4")
157 && alIsExtensionPresent("AL_SOFT_block_alignment"))
158 mSampleFormat
= SampleType::IMA4
;
160 case SF_FORMAT_MS_ADPCM
:
161 if(mSfInfo
.channels
<= 2 && (mSfInfo
.format
&SF_FORMAT_TYPEMASK
) == SF_FORMAT_WAV
162 && alIsExtensionPresent("AL_SOFT_MSADPCM")
163 && alIsExtensionPresent("AL_SOFT_block_alignment"))
164 mSampleFormat
= SampleType::MSADPCM
;
168 int splblocksize
{}, byteblocksize
{};
169 if(mSampleFormat
== SampleType::IMA4
|| mSampleFormat
== SampleType::MSADPCM
)
171 SF_CHUNK_INFO inf
{ "fmt ", 4, 0, nullptr };
172 SF_CHUNK_ITERATOR
*iter
= sf_get_chunk_iterator(mSndfile
, &inf
);
173 if(!iter
|| sf_get_chunk_size(iter
, &inf
) != SF_ERR_NO_ERROR
|| inf
.datalen
< 14)
174 mSampleFormat
= SampleType::Int16
;
177 auto fmtbuf
= std::vector
<ALubyte
>(inf
.datalen
);
178 inf
.data
= fmtbuf
.data();
179 if(sf_get_chunk_data(iter
, &inf
) != SF_ERR_NO_ERROR
)
180 mSampleFormat
= SampleType::Int16
;
183 byteblocksize
= fmtbuf
[12] | (fmtbuf
[13]<<8u);
184 if(mSampleFormat
== SampleType::IMA4
)
186 splblocksize
= (byteblocksize
/mSfInfo
.channels
- 4)/4*8 + 1;
188 || ((splblocksize
-1)/2 + 4)*mSfInfo
.channels
!= byteblocksize
)
189 mSampleFormat
= SampleType::Int16
;
193 splblocksize
= (byteblocksize
/mSfInfo
.channels
- 7)*2 + 2;
195 || ((splblocksize
-2)/2 + 7)*mSfInfo
.channels
!= byteblocksize
)
196 mSampleFormat
= SampleType::Int16
;
202 if(mSampleFormat
== SampleType::Int16
)
204 mSamplesPerBlock
= 1;
205 mBytesPerBlock
= static_cast<size_t>(mSfInfo
.channels
) * 2;
207 else if(mSampleFormat
== SampleType::Float
)
209 mSamplesPerBlock
= 1;
210 mBytesPerBlock
= static_cast<size_t>(mSfInfo
.channels
) * 4;
214 mSamplesPerBlock
= static_cast<size_t>(splblocksize
);
215 mBytesPerBlock
= static_cast<size_t>(byteblocksize
);
219 if(mSfInfo
.channels
== 1)
221 if(mSampleFormat
== SampleType::Int16
)
222 mFormat
= AL_FORMAT_MONO16
;
223 else if(mSampleFormat
== SampleType::Float
)
224 mFormat
= AL_FORMAT_MONO_FLOAT32
;
225 else if(mSampleFormat
== SampleType::IMA4
)
226 mFormat
= AL_FORMAT_MONO_IMA4
;
227 else if(mSampleFormat
== SampleType::MSADPCM
)
228 mFormat
= AL_FORMAT_MONO_MSADPCM_SOFT
;
230 else if(mSfInfo
.channels
== 2)
232 if(mSampleFormat
== SampleType::Int16
)
233 mFormat
= AL_FORMAT_STEREO16
;
234 else if(mSampleFormat
== SampleType::Float
)
235 mFormat
= AL_FORMAT_STEREO_FLOAT32
;
236 else if(mSampleFormat
== SampleType::IMA4
)
237 mFormat
= AL_FORMAT_STEREO_IMA4
;
238 else if(mSampleFormat
== SampleType::MSADPCM
)
239 mFormat
= AL_FORMAT_STEREO_MSADPCM_SOFT
;
241 else if(mSfInfo
.channels
== 3)
243 if(sf_command(mSndfile
, SFC_WAVEX_GET_AMBISONIC
, nullptr, 0) == SF_AMBISONIC_B_FORMAT
)
245 if(mSampleFormat
== SampleType::Int16
)
246 mFormat
= AL_FORMAT_BFORMAT2D_16
;
247 else if(mSampleFormat
== SampleType::Float
)
248 mFormat
= AL_FORMAT_BFORMAT2D_FLOAT32
;
251 else if(mSfInfo
.channels
== 4)
253 if(sf_command(mSndfile
, SFC_WAVEX_GET_AMBISONIC
, nullptr, 0) == SF_AMBISONIC_B_FORMAT
)
255 if(mSampleFormat
== SampleType::Int16
)
256 mFormat
= AL_FORMAT_BFORMAT3D_16
;
257 else if(mSampleFormat
== SampleType::Float
)
258 mFormat
= AL_FORMAT_BFORMAT3D_FLOAT32
;
263 fprintf(stderr
, "Unsupported channel count: %d\n", mSfInfo
.channels
);
270 /* Set a 1s ring buffer size. */
271 size_t numblocks
{(static_cast<ALuint
>(mSfInfo
.samplerate
) + mSamplesPerBlock
-1)
273 mBufferData
.resize(static_cast<ALuint
>(numblocks
* mBytesPerBlock
));
274 mReadPos
.store(0, std::memory_order_relaxed
);
275 mWritePos
.store(0, std::memory_order_relaxed
);
281 /* The actual C-style callback just forwards to the non-static method. Not
282 * strictly needed and the compiler will optimize it to a normal function,
283 * but it allows the callback implementation to have a nice 'this' pointer
284 * with normal member access.
286 static ALsizei AL_APIENTRY
bufferCallbackC(void *userptr
, void *data
, ALsizei size
) noexcept
287 { return static_cast<StreamPlayer
*>(userptr
)->bufferCallback(data
, size
); }
288 ALsizei
bufferCallback(void *data
, ALsizei size
) noexcept
290 const auto output
= al::span
{static_cast<std::byte
*>(data
), static_cast<ALuint
>(size
)};
291 auto dst
= output
.begin();
293 /* NOTE: The callback *MUST* be real-time safe! That means no blocking,
294 * no allocations or deallocations, no I/O, no page faults, or calls to
295 * functions that could do these things (this includes calling to
296 * libraries like SDL_sound, libsndfile, ffmpeg, etc). Nothing should
297 * unexpectedly stall this call since the audio has to get to the
301 size_t roffset
{mReadPos
.load(std::memory_order_acquire
)};
302 while(const auto remaining
= static_cast<size_t>(std::distance(dst
, output
.end())))
304 /* If the write offset == read offset, there's nothing left in the
305 * ring-buffer. Break from the loop and give what has been written.
307 const size_t woffset
{mWritePos
.load(std::memory_order_relaxed
)};
308 if(woffset
== roffset
) break;
310 /* If the write offset is behind the read offset, the readable
311 * portion wrapped around. Just read up to the end of the buffer in
312 * that case, otherwise read up to the write offset. Also limit the
313 * amount to copy given how much is remaining to write.
315 size_t todo
{((woffset
< roffset
) ? mBufferData
.size() : woffset
) - roffset
};
316 todo
= std::min(todo
, remaining
);
318 /* Copy from the ring buffer to the provided output buffer. Wrap
319 * the resulting read offset if it reached the end of the ring-
322 const auto input
= al::span
{mBufferData
}.subspan(roffset
, todo
);
323 dst
= std::copy_n(input
.begin(), input
.size(), dst
);
326 if(roffset
== mBufferData
.size())
329 /* Finally, store the updated read offset, and return how many bytes
332 mReadPos
.store(roffset
, std::memory_order_release
);
334 return static_cast<ALsizei
>(std::distance(output
.begin(), dst
));
339 if(mSamplesPerBlock
> 1)
340 alBufferi(mBuffer
, AL_UNPACK_BLOCK_ALIGNMENT_SOFT
, static_cast<int>(mSamplesPerBlock
));
341 alBufferCallbackSOFT(mBuffer
, mFormat
, mSfInfo
.samplerate
, bufferCallbackC
, this);
342 alSourcei(mSource
, AL_BUFFER
, static_cast<ALint
>(mBuffer
));
343 if(ALenum err
{alGetError()})
345 fprintf(stderr
, "Failed to set callback: %s (0x%04x)\n", alGetString(err
), err
);
355 alGetSourcei(mSource
, AL_SAMPLE_OFFSET
, &pos
);
356 alGetSourcei(mSource
, AL_SOURCE_STATE
, &state
);
358 size_t woffset
{mWritePos
.load(std::memory_order_acquire
)};
359 if(state
!= AL_INITIAL
)
361 const size_t roffset
{mReadPos
.load(std::memory_order_relaxed
)};
362 const size_t readable
{((woffset
>= roffset
) ? woffset
: (mBufferData
.size()+woffset
)) -
364 /* For a stopped (underrun) source, the current playback offset is
365 * the current decoder offset excluding the readable buffered data.
366 * For a playing/paused source, it's the source's offset including
367 * the playback offset the source was started with.
369 const auto curtime
= ((state
== AL_STOPPED
)
370 ? (mDecoderOffset
-readable
) / mBytesPerBlock
* mSamplesPerBlock
371 : (size_t{static_cast<ALuint
>(pos
)} + mStartOffset
))
372 / static_cast<ALuint
>(mSfInfo
.samplerate
);
373 printf("\r %zum%02zus (%3zu%% full)", curtime
/60, curtime
%60,
374 readable
* 100 / mBufferData
.size());
377 fputs("Starting...", stdout
);
380 while(!sf_error(mSndfile
))
382 const auto roffset
= mReadPos
.load(std::memory_order_relaxed
);
383 auto get_writable
= [this,roffset
,woffset
]() noexcept
-> size_t
385 if(roffset
> woffset
)
387 /* Note that the ring buffer's writable space is one byte
388 * less than the available area because the write offset
389 * ending up at the read offset would be interpreted as
390 * being empty instead of full.
392 return roffset
- woffset
- 1;
395 /* If the read offset is at or behind the write offset, the
396 * writeable area (might) wrap around. Make sure the sample
397 * data can fit, and calculate how much can go in front before
400 return mBufferData
.size() - (!roffset
? woffset
+1 : woffset
);
403 const auto writable
= get_writable() / mBytesPerBlock
;
406 auto read_bytes
= size_t{};
407 if(mSampleFormat
== SampleType::Int16
)
409 const auto num_frames
= sf_readf_short(mSndfile
,
410 reinterpret_cast<short*>(&mBufferData
[woffset
]),
411 static_cast<sf_count_t
>(writable
*mSamplesPerBlock
));
412 if(num_frames
< 1) break;
413 read_bytes
= static_cast<size_t>(num_frames
) * mBytesPerBlock
;
415 else if(mSampleFormat
== SampleType::Float
)
417 const auto num_frames
= sf_readf_float(mSndfile
,
418 reinterpret_cast<float*>(&mBufferData
[woffset
]),
419 static_cast<sf_count_t
>(writable
*mSamplesPerBlock
));
420 if(num_frames
< 1) break;
421 read_bytes
= static_cast<size_t>(num_frames
) * mBytesPerBlock
;
425 const auto numbytes
= sf_read_raw(mSndfile
, &mBufferData
[woffset
],
426 static_cast<sf_count_t
>(writable
*mBytesPerBlock
));
427 if(numbytes
< 1) break;
428 read_bytes
= static_cast<size_t>(numbytes
);
431 woffset
+= read_bytes
;
432 if(woffset
== mBufferData
.size())
435 mWritePos
.store(woffset
, std::memory_order_release
);
436 mDecoderOffset
+= read_bytes
;
439 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
441 /* If the source is not playing or paused, it either underrun
442 * (AL_STOPPED) or is just getting started (AL_INITIAL). If the
443 * ring buffer is empty, it's done, otherwise play the source with
446 const auto roffset
= mReadPos
.load(std::memory_order_relaxed
);
447 const auto readable
= ((woffset
< roffset
) ? mBufferData
.size()+woffset
: woffset
) -
452 /* Store the playback offset that the source will start reading
453 * from, so it can be tracked during playback.
455 mStartOffset
= (mDecoderOffset
-readable
) / mBytesPerBlock
* mSamplesPerBlock
;
456 alSourcePlay(mSource
);
457 if(alGetError() != AL_NO_ERROR
)
464 int main(al::span
<std::string_view
> args
)
466 /* Print out usage if no arguments were specified */
469 fprintf(stderr
, "Usage: %.*s [-device <name>] <filenames...>\n", al::sizei(args
[0]),
473 args
= args
.subspan(1);
475 if(InitAL(args
) != 0)
476 throw std::runtime_error
{"Failed to initialize OpenAL"};
477 /* A simple RAII container for automating OpenAL shutdown. */
478 struct AudioManager
{
479 AudioManager() = default;
480 AudioManager(const AudioManager
&) = delete;
481 auto operator=(const AudioManager
&) -> AudioManager
& = delete;
482 ~AudioManager() { CloseAL(); }
486 if(!alIsExtensionPresent("AL_SOFT_callback_buffer"))
488 fprintf(stderr
, "AL_SOFT_callback_buffer extension not available\n");
492 alBufferCallbackSOFT
= reinterpret_cast<LPALBUFFERCALLBACKSOFT
>(
493 alGetProcAddress("alBufferCallbackSOFT"));
496 alcGetIntegerv(alcGetContextsDevice(alcGetCurrentContext()), ALC_REFRESH
, 1, &refresh
);
498 auto player
= std::make_unique
<StreamPlayer
>();
500 /* Play each file listed on the command line */
501 for(size_t i
{0};i
< args
.size();++i
)
503 if(!player
->open(std::string
{args
[i
]}))
506 /* Get the name portion, without the path, for display. */
507 auto namepart
= args
[i
];
508 if(auto sep
= namepart
.rfind('/'); sep
< namepart
.size())
509 namepart
= namepart
.substr(sep
+1);
510 else if(sep
= namepart
.rfind('\\'); sep
< namepart
.size())
511 namepart
= namepart
.substr(sep
+1);
513 printf("Playing: %.*s (%s, %dhz)\n", al::sizei(namepart
), namepart
.data(),
514 FormatName(player
->mFormat
), player
->mSfInfo
.samplerate
);
517 if(!player
->prepare())
523 while(player
->update())
524 std::this_thread::sleep_for(nanoseconds
{seconds
{1}} / refresh
);
527 /* All done with this file. Close it and go to the next */
538 int main(int argc
, char **argv
)
541 auto args
= std::vector
<std::string_view
>(static_cast<unsigned int>(argc
));
542 std::copy_n(argv
, args
.size(), args
.begin());
543 return main(al::span
{args
});