|
|
@@ -1,125 +1,306 @@
|
|
|
#include "SDLAudioDriver.h"
|
|
|
#include "WallpaperEngine/Logging/Log.h"
|
|
|
|
|
|
-// a sound starts at the next callback, so keep it short (WE's OpenAL Soft updates every 20 ms)
|
|
|
+#include <algorithm>
|
|
|
+#include <cfloat>
|
|
|
+#include <cstring>
|
|
|
+
|
|
|
+// how much SDL asks for at once, the mixing itself runs in WE's update size (m_updateFrames)
|
|
|
#define SDL_AUDIO_BUFFER_SIZE 1024
|
|
|
-#define MAX_AUDIO_FRAME_SIZE 192000
|
|
|
|
|
|
using namespace WallpaperEngine::Audio;
|
|
|
using namespace WallpaperEngine::Audio::Drivers;
|
|
|
|
|
|
namespace {
|
|
|
-template <typename T>
|
|
|
-void scaleSamples (uint8_t* data, int bytes, int channels, int firstChannel, float left, float right) {
|
|
|
- auto* samples = reinterpret_cast<T*> (data);
|
|
|
- const int count = bytes / static_cast<int> (sizeof (T));
|
|
|
+/** a stopping voice without a buffer fades over at most this many samples (voice.cpp Voice::mix) */
|
|
|
+constexpr unsigned StopFadeSamples = 64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Per channel gain ramp of one update, OpenAL's MixSamples: from the current gains to the target over counter frames,
|
|
|
+ * the target from there on. A step no bigger than FLT_EPSILON goes straight to the target
|
|
|
+ */
|
|
|
+struct GainRamp {
|
|
|
+ float left, right;
|
|
|
+ float targetLeft, targetRight;
|
|
|
+ int counter;
|
|
|
+
|
|
|
+ [[nodiscard]] float at (int channel, int frame) const {
|
|
|
+ if (channel > 1) {
|
|
|
+ return (this->at (0, frame) + this->at (1, frame)) * 0.5f;
|
|
|
+ }
|
|
|
+
|
|
|
+ const float gain = channel == 0 ? this->left : this->right;
|
|
|
+ const float target = channel == 0 ? this->targetLeft : this->targetRight;
|
|
|
+ const float step = (target - gain) * (this->counter > 0 ? 1.0f / static_cast<float> (this->counter) : 0.0f);
|
|
|
+
|
|
|
+ if (!(std::abs (step) > FLT_EPSILON) || frame >= this->counter) {
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ return gain + step * static_cast<float> (frame);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+void audio_callback (void* userdata, uint8_t* streamData, int length) {
|
|
|
+ auto* driver = static_cast<SDLAudioDriver*> (userdata);
|
|
|
|
|
|
- for (int i = 0; i < count; i++) {
|
|
|
- const int channel = (firstChannel + i) % channels;
|
|
|
- const float gain = channel == 0 ? left : channel == 1 ? right : (left + right) * 0.5f;
|
|
|
+ memset (streamData, 0, length);
|
|
|
|
|
|
- samples[i] = static_cast<T> (static_cast<float> (samples[i]) * gain);
|
|
|
+ // if audio is playing do not do anything here!
|
|
|
+ if (driver->getAudioDetector ().anythingPlaying ()) {
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ driver->render (
|
|
|
+ reinterpret_cast<float*> (streamData), length / static_cast<int> (sizeof (float)) / driver->getChannels ()
|
|
|
+ );
|
|
|
}
|
|
|
+} // namespace
|
|
|
+
|
|
|
+void SDLAudioDriver::render (float* out, int frames) {
|
|
|
+ const int channels = this->m_audioSpec.channels;
|
|
|
+ const size_t wanted = static_cast<size_t> (frames) * channels;
|
|
|
|
|
|
-/** Copies a chunk of decoded audio with each channel scaled, false when the device format isn't handled */
|
|
|
-bool scaleChunk (
|
|
|
- std::vector<uint8_t>& out, const uint8_t* data, int bytes, const SDL_AudioSpec& spec, int byteOffset, float left,
|
|
|
- float right
|
|
|
-) {
|
|
|
- const int sampleSize = SDL_AUDIO_BITSIZE (spec.format) / 8;
|
|
|
- const int firstChannel = (byteOffset / sampleSize) % spec.channels;
|
|
|
+ SDL_LockMutex (this->m_streamListMutex);
|
|
|
|
|
|
- out.assign (data, data + bytes);
|
|
|
+ // the WASAPI mixer thread renders an update once that much buffer is free, in chunks of at most BufferLineSize
|
|
|
+ while (this->m_pending.size () < wanted && this->getApplicationContext ().state.general.keepRunning) {
|
|
|
+ for (int done = 0; done < this->m_updateFrames;) {
|
|
|
+ const int count = std::min (this->m_updateFrames - done, static_cast<int> (Hrtf::BufferLineSize));
|
|
|
+ const size_t offset = this->m_pending.size ();
|
|
|
|
|
|
- if (SDL_AUDIO_ISBIGENDIAN (spec.format) != (SDL_BYTEORDER == SDL_BIG_ENDIAN)) {
|
|
|
- return false;
|
|
|
+ this->m_pending.resize (offset + static_cast<size_t> (count) * channels, 0.0f);
|
|
|
+ this->mixUpdate (this->m_pending.data () + offset, count);
|
|
|
+ done += count;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (SDL_AUDIO_ISFLOAT (spec.format) && sampleSize == 4) {
|
|
|
- scaleSamples<float> (out.data (), bytes, spec.channels, firstChannel, left, right);
|
|
|
- } else if (SDL_AUDIO_ISSIGNED (spec.format) && sampleSize == 2) {
|
|
|
- scaleSamples<int16_t> (out.data (), bytes, spec.channels, firstChannel, left, right);
|
|
|
- } else if (SDL_AUDIO_ISSIGNED (spec.format) && sampleSize == 4) {
|
|
|
- scaleSamples<int32_t> (out.data (), bytes, spec.channels, firstChannel, left, right);
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+ const size_t available = std::min (wanted, this->m_pending.size ());
|
|
|
+
|
|
|
+ for (size_t i = 0; i < available; i++) {
|
|
|
+ out[i] = std::clamp (this->m_pending[i], -1.0f, 1.0f);
|
|
|
}
|
|
|
|
|
|
- return true;
|
|
|
+ this->m_pending.erase (this->m_pending.begin (), this->m_pending.begin () + static_cast<long> (available));
|
|
|
+
|
|
|
+ // TODO: do we also need to lock while audio is playing, or wait until the stream is unused?
|
|
|
+ SDL_UnlockMutex (this->m_streamListMutex);
|
|
|
}
|
|
|
-} // namespace
|
|
|
|
|
|
-void audio_callback (void* userdata, uint8_t* streamData, int length) {
|
|
|
- auto* driver = static_cast<SDLAudioDriver*> (userdata);
|
|
|
+void SDLAudioDriver::mixUpdate (float* out, int frames) {
|
|
|
+ const int channels = this->m_audioSpec.channels;
|
|
|
+ Hrtf::Mixer* hrtf = channels >= 2 ? this->getHrtfMixer () : nullptr;
|
|
|
+ auto& update = this->m_update;
|
|
|
|
|
|
- memset (streamData, 0, length);
|
|
|
+ this->m_hrtfVoices.clear ();
|
|
|
|
|
|
- // if audio is playing do not do anything here!
|
|
|
- if (driver->getAudioDetector ().anythingPlaying ()) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ for (auto* buffer : this->m_streams | std::views::values) {
|
|
|
+ if (!buffer->stream->isInitialized () || buffer->finished.load (std::memory_order_relaxed)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- SDL_LockMutex (driver->getStreamMutex ());
|
|
|
+ // OpenAL's Stopping state: alSourceStop and alSourcePause both mix one more update with the gain going to 0,
|
|
|
+ // and so does a voice whose file ran out in the last update
|
|
|
+ const bool stopping = buffer->stopping.load (std::memory_order_acquire);
|
|
|
+ const bool pausing = !stopping && buffer->paused.load (std::memory_order_relaxed);
|
|
|
+ const bool runOut = !stopping && !pausing && buffer->stream->ended ();
|
|
|
|
|
|
- for (const auto& buffer : driver->getStreams () | std::views::values) {
|
|
|
- uint8_t* streamDataPointer = streamData;
|
|
|
- int streamLength = length;
|
|
|
+ if (buffer->over || (stopping && buffer->pauseFaded)) {
|
|
|
+ if (stopping) {
|
|
|
+ buffer->finished.store (true, std::memory_order_release);
|
|
|
+ }
|
|
|
|
|
|
- if (!buffer->stream->isInitialized () || buffer->paused.load (std::memory_order_relaxed)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- while (streamLength > 0 && driver->getApplicationContext ().state.general.keepRunning) {
|
|
|
- if (buffer->audio_buf_index >= buffer->audio_buf_size) {
|
|
|
- int audio_size = buffer->stream->decodeFrame (buffer->audio_buf, sizeof (buffer->audio_buf));
|
|
|
+ if (pausing && buffer->pauseFaded) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ buffer->pauseFaded = false;
|
|
|
+
|
|
|
+ // a stopping voice that never mixed has no gains to fade from (Counter 0), it goes silent right away
|
|
|
+ if ((stopping || runOut) && !buffer->mixed) {
|
|
|
+ if (stopping) {
|
|
|
+ buffer->finished.store (true, std::memory_order_release);
|
|
|
+ } else {
|
|
|
+ buffer->over = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ const bool ending = stopping || pausing || runOut;
|
|
|
+ // without a buffer the fade is cut short, a paused voice keeps its buffer and fades over the whole update
|
|
|
+ const bool detached = stopping || runOut;
|
|
|
+ bool viaHrtf = false;
|
|
|
+
|
|
|
+ if (hrtf != nullptr) {
|
|
|
+ std::lock_guard lock (buffer->hrtfMutex);
|
|
|
+
|
|
|
+ if (buffer->hrtfSet) {
|
|
|
+ buffer->hrtfMixing = buffer->hrtf;
|
|
|
+ viaHrtf = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ update.assign (static_cast<size_t> (frames) * channels, 0.0f);
|
|
|
+
|
|
|
+ // a resumed voice replays audio it already read, its passes are not known any more
|
|
|
+ if (viaHrtf) {
|
|
|
+ buffer->hrtfPasses
|
|
|
+ = !detached && !buffer->replay.empty () ? std::vector { frames } : buffer->stream->passes (frames);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (detached) {
|
|
|
+ // alSourceStop detaches the buffer before the update (ProcessVoiceChanges), so the voice plays out what it
|
|
|
+ // loaded last time instead
|
|
|
+ buffer->stream->stopTail (update.data (), frames);
|
|
|
+ } else {
|
|
|
+ // this update's audio: what a pause held back first, then newly decoded data
|
|
|
+ const size_t replayed = std::min (buffer->replay.size (), update.size ());
|
|
|
+
|
|
|
+ std::copy_n (buffer->replay.begin (), replayed, update.begin ());
|
|
|
+ buffer->replay.erase (buffer->replay.begin (), buffer->replay.begin () + static_cast<long> (replayed));
|
|
|
+
|
|
|
+ int filled = static_cast<int> (replayed) / channels;
|
|
|
+
|
|
|
+ while (filled < frames && this->getApplicationContext ().state.general.keepRunning) {
|
|
|
+ const int read
|
|
|
+ = buffer->stream->read (update.data () + static_cast<size_t> (filled) * channels, frames - filled);
|
|
|
|
|
|
// 0: nothing decoded yet, wake the reader
|
|
|
- if (audio_size <= 0) {
|
|
|
- if (audio_size == 0) {
|
|
|
+ if (read <= 0) {
|
|
|
+ if (read == 0) {
|
|
|
SDL_CondSignal (buffer->stream->getWaitCondition ());
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- buffer->audio_buf_size = audio_size;
|
|
|
- buffer->audio_buf_index = 0;
|
|
|
+ filled += read;
|
|
|
+ }
|
|
|
+
|
|
|
+ // a stopping voice doesn't advance, a resumed one plays this update again
|
|
|
+ if (pausing) {
|
|
|
+ buffer->replay.insert (
|
|
|
+ buffer->replay.begin (), update.begin (), update.begin () + static_cast<long> (filled) * channels
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // mix the audio, using this stream's own volume override if it has one
|
|
|
+ const int streamVolume = buffer->volume.load (std::memory_order_relaxed);
|
|
|
+ const int volume = streamVolume >= 0 ? streamVolume : this->getApplicationContext ().state.audio.volume;
|
|
|
+ const float scale = static_cast<float> (volume) / static_cast<float> (SDL_MIX_MAXVOLUME);
|
|
|
+ const int counter = detached ? std::min (frames, static_cast<int> (StopFadeSamples)) : frames;
|
|
|
+
|
|
|
+ if (viaHrtf) {
|
|
|
+ for (int channel = 0; channel < buffer->hrtfMixing.channels; channel++) {
|
|
|
+ auto& samples = buffer->hrtfSamples[channel];
|
|
|
+
|
|
|
+ samples.resize (frames);
|
|
|
+
|
|
|
+ for (int i = 0; i < frames; i++) {
|
|
|
+ samples[i] = update[static_cast<size_t> (i) * channels + channel] * scale;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ending) {
|
|
|
+ buffer->hrtfMixing.filters[channel].gain = 0.0f;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- int len1 = buffer->audio_buf_size - buffer->audio_buf_index;
|
|
|
+ buffer->hrtfCounter = counter;
|
|
|
+ this->m_hrtfVoices.push_back (buffer);
|
|
|
+ } else {
|
|
|
+ const float targetLeft = ending ? 0.0f : buffer->gainLeft.load (std::memory_order_relaxed);
|
|
|
+ const float targetRight = ending ? 0.0f : buffer->gainRight.load (std::memory_order_relaxed);
|
|
|
|
|
|
- if (len1 > streamLength) {
|
|
|
- len1 = streamLength;
|
|
|
+ // a voice's first update starts at its target gains, the later ones ramp over the update
|
|
|
+ if (!buffer->mixed) {
|
|
|
+ buffer->currentLeft = targetLeft;
|
|
|
+ buffer->currentRight = targetRight;
|
|
|
}
|
|
|
|
|
|
- // mix the audio, using this stream's own volume override if it has one
|
|
|
- const int streamVolume = buffer->volume.load (std::memory_order_relaxed);
|
|
|
- const float left = buffer->gainLeft.load (std::memory_order_relaxed);
|
|
|
- const float right = buffer->gainRight.load (std::memory_order_relaxed);
|
|
|
- const uint8_t* source = &buffer->audio_buf[buffer->audio_buf_index];
|
|
|
- static std::vector<uint8_t> scaled;
|
|
|
-
|
|
|
- if ((left != 1.0f || right != 1.0f)
|
|
|
- && scaleChunk (
|
|
|
- scaled, source, len1, driver->getSpec (), static_cast<int> (buffer->audio_buf_index), left, right
|
|
|
- )) {
|
|
|
- source = scaled.data ();
|
|
|
+ const GainRamp ramp { buffer->currentLeft, buffer->currentRight, targetLeft, targetRight, counter };
|
|
|
+
|
|
|
+ buffer->currentLeft = targetLeft;
|
|
|
+ buffer->currentRight = targetRight;
|
|
|
+
|
|
|
+ for (int i = 0; i < frames; i++) {
|
|
|
+ for (int channel = 0; channel < channels; channel++) {
|
|
|
+ const size_t index = static_cast<size_t> (i) * channels + channel;
|
|
|
+
|
|
|
+ out[index] += update[index] * ramp.at (channel, i) * scale;
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- SDL_MixAudioFormat (
|
|
|
- streamDataPointer, source, driver->getSpec ().format, len1,
|
|
|
- streamVolume >= 0 ? streamVolume : driver->getApplicationContext ().state.audio.volume
|
|
|
- );
|
|
|
+ buffer->mixed = true;
|
|
|
|
|
|
- streamLength -= len1;
|
|
|
- streamDataPointer += len1;
|
|
|
- buffer->audio_buf_index += len1;
|
|
|
+ if (pausing) {
|
|
|
+ buffer->pauseFaded = true;
|
|
|
+ } else if (stopping) {
|
|
|
+ buffer->finished.store (true, std::memory_order_release);
|
|
|
+ } else if (runOut) {
|
|
|
+ buffer->over = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // TODO: do we also need to lock while audio is playing, or wait until the stream is unused?
|
|
|
- SDL_UnlockMutex (driver->getStreamMutex ());
|
|
|
+ // the accumulator carries the tail of the last updates, so it runs even with no voice left
|
|
|
+ if (hrtf != nullptr) {
|
|
|
+ this->mixHrtf (this->m_hrtfVoices, out, frames);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SDLAudioDriver::mixHrtf (const std::vector<SDLAudioBuffer*>& voices, float* out, int frames) {
|
|
|
+ Hrtf::Mixer& mixer = *this->m_hrtfMixer;
|
|
|
+ const unsigned generation = this->getHrtfGeneration ();
|
|
|
+ const int channels = this->m_audioSpec.channels;
|
|
|
+
|
|
|
+ if (this->m_hrtfMixerGeneration != generation) {
|
|
|
+ this->m_hrtfMixerGeneration = generation;
|
|
|
+ mixer.clear ();
|
|
|
+ }
|
|
|
+
|
|
|
+ auto& [left, right] = this->m_hrtfOutput;
|
|
|
+
|
|
|
+ left.assign (frames, 0.0f);
|
|
|
+ right.assign (frames, 0.0f);
|
|
|
+
|
|
|
+ for (auto* voice : voices) {
|
|
|
+ if (voice->hrtfGeneration != generation) {
|
|
|
+ voice->hrtfGeneration = generation;
|
|
|
+ voice->hrtfChannels = {};
|
|
|
+ voice->hrtfFading = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ unsigned counter = voice->hrtfFading ? static_cast<unsigned> (voice->hrtfCounter) : 0;
|
|
|
+ unsigned outPos = 0;
|
|
|
+
|
|
|
+ for (const int pass : voice->hrtfPasses) {
|
|
|
+ const auto count = static_cast<unsigned> (pass);
|
|
|
+
|
|
|
+ for (int channel = 0; channel < voice->hrtfMixing.channels; channel++) {
|
|
|
+ mixer.mix (
|
|
|
+ voice->hrtfChannels[channel], voice->hrtfMixing.filters[channel],
|
|
|
+ voice->hrtfSamples[channel].data () + outPos, count, counter, outPos
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ outPos += count;
|
|
|
+ counter = std::max (count, counter) - count;
|
|
|
+ }
|
|
|
+
|
|
|
+ voice->hrtfFading = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ mixer.finish (left.data (), right.data (), static_cast<unsigned> (frames));
|
|
|
+
|
|
|
+ for (int i = 0; i < frames; i++) {
|
|
|
+ out[static_cast<size_t> (i) * channels] += left[i];
|
|
|
+ out[static_cast<size_t> (i) * channels + 1] += right[i];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
SDLAudioDriver::SDLAudioDriver (
|
|
|
@@ -142,17 +323,29 @@ SDLAudioDriver::SDLAudioDriver (
|
|
|
.callback = audio_callback,
|
|
|
.userdata = this };
|
|
|
|
|
|
- this->m_deviceID
|
|
|
- = SDL_OpenAudioDevice (nullptr, false, &requestedSpec, &this->m_audioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE);
|
|
|
+ // mixing is done in float like OpenAL's, SDL converts to whatever the device takes
|
|
|
+ this->m_deviceID = SDL_OpenAudioDevice (
|
|
|
+ nullptr, false, &requestedSpec, &this->m_audioSpec,
|
|
|
+ SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE
|
|
|
+ );
|
|
|
|
|
|
if (this->m_deviceID == 0) {
|
|
|
sLog.error ("SDL_OpenAudioDevice: ", SDL_GetError ());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- SDL_PauseAudioDevice (this->m_deviceID, 0);
|
|
|
+ // WasapiPlayback::resetProxy: OpenAL's 20 ms default update in 100 ns units, rounded to a multiple of the
|
|
|
+ // endpoint's default period (Windows' shared mode default, 10 ms), in frames at the device rate
|
|
|
+ constexpr int64_t updateTime = 10000000LL * 882 / 44100;
|
|
|
+ constexpr int64_t periodTime = 100000;
|
|
|
+ constexpr int64_t periods = std::max<int64_t> ((updateTime + periodTime / 2) / periodTime, 1);
|
|
|
+
|
|
|
+ this->m_updateFrames = static_cast<int> ((periodTime * periods * this->m_audioSpec.freq + 5000000) / 10000000);
|
|
|
|
|
|
this->m_initialized = true;
|
|
|
+ this->updateHrtf ();
|
|
|
+
|
|
|
+ SDL_PauseAudioDevice (this->m_deviceID, 0);
|
|
|
}
|
|
|
|
|
|
SDLAudioDriver::~SDLAudioDriver () {
|
|
|
@@ -164,6 +357,13 @@ SDLAudioDriver::~SDLAudioDriver () {
|
|
|
SDL_CloseAudioDevice (this->m_deviceID);
|
|
|
}
|
|
|
|
|
|
+ // streams still fading out belong to the driver
|
|
|
+ for (const auto* buffer : this->m_streams | std::views::values) {
|
|
|
+ delete buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ this->m_streams.clear ();
|
|
|
+
|
|
|
SDL_QuitSubSystem (SDL_INIT_AUDIO);
|
|
|
}
|
|
|
|
|
|
@@ -224,33 +424,130 @@ void SDLAudioDriver::setStreamPaused (int streamId, bool paused) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-const std::map<int, SDLAudioBuffer*>& SDLAudioDriver::getStreams () { return this->m_streams; }
|
|
|
-
|
|
|
-AVSampleFormat SDLAudioDriver::getFormat () const {
|
|
|
- switch (this->m_audioSpec.format) {
|
|
|
- case AUDIO_U8:
|
|
|
- case AUDIO_S8:
|
|
|
- return AV_SAMPLE_FMT_U8;
|
|
|
- case AUDIO_U16MSB:
|
|
|
- case AUDIO_U16LSB:
|
|
|
- case AUDIO_S16LSB:
|
|
|
- case AUDIO_S16MSB:
|
|
|
- return AV_SAMPLE_FMT_S16;
|
|
|
- case AUDIO_S32LSB:
|
|
|
- case AUDIO_S32MSB:
|
|
|
- return AV_SAMPLE_FMT_S32;
|
|
|
- case AUDIO_F32LSB:
|
|
|
- case AUDIO_F32MSB:
|
|
|
- return AV_SAMPLE_FMT_FLT;
|
|
|
- default:
|
|
|
- sLog.exception ("Cannot convert from SDL format to ffmpeg format, aborting...");
|
|
|
+void SDLAudioDriver::setStreamHrtf (int streamId, const StreamHrtf* hrtf) {
|
|
|
+ // same reasoning as setStreamVolume for the lookup, the filters themselves are copied under the stream's lock
|
|
|
+ if (const auto it = this->m_streams.find (streamId); it != this->m_streams.end ()) {
|
|
|
+ std::lock_guard lock (it->second->hrtfMutex);
|
|
|
+
|
|
|
+ it->second->hrtfSet = hrtf != nullptr;
|
|
|
+
|
|
|
+ if (hrtf != nullptr) {
|
|
|
+ it->second->hrtf = *hrtf;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-int SDLAudioDriver::getSampleRate () const { return this->m_audioSpec.freq; }
|
|
|
+const Hrtf::Store* SDLAudioDriver::getHrtf () const {
|
|
|
+ return this->m_hrtfEnabled.load (std::memory_order_acquire) ? this->m_hrtfStore.get () : nullptr;
|
|
|
+}
|
|
|
|
|
|
-int SDLAudioDriver::getChannels () const { return this->m_audioSpec.channels; }
|
|
|
+Hrtf::Mixer* SDLAudioDriver::getHrtfMixer () const {
|
|
|
+ return this->m_hrtfEnabled.load (std::memory_order_acquire) ? this->m_hrtfMixer.get () : nullptr;
|
|
|
+}
|
|
|
|
|
|
-const SDL_AudioSpec& SDLAudioDriver::getSpec () const { return this->m_audioSpec; }
|
|
|
+unsigned SDLAudioDriver::getHrtfGeneration () const { return this->m_hrtfGeneration.load (std::memory_order_acquire); }
|
|
|
|
|
|
-SDL_mutex* SDLAudioDriver::getStreamMutex () const { return this->m_streamListMutex; }
|
|
|
+void SDLAudioDriver::update (float dt) {
|
|
|
+ AudioDriver::update (dt);
|
|
|
+ this->releaseFinishedStreams ();
|
|
|
+ this->updateHrtf ();
|
|
|
+}
|
|
|
+
|
|
|
+void SDLAudioDriver::updateHrtf () {
|
|
|
+ if (!this->m_initialized || this->m_hrtfFailed) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const auto mode = this->getApplicationContext ().settings.audio.hrtf;
|
|
|
+
|
|
|
+ // created the first time auto is the mode, from the command line or a control file
|
|
|
+ if (mode == Application::ApplicationContext::HRTF_AUTO && this->m_headphonesDetector == nullptr
|
|
|
+ && this->getApplicationContext ().settings.audio.enabled) {
|
|
|
+ this->m_headphonesDetector = std::make_unique<Detectors::HeadphonesDetector> ();
|
|
|
+ }
|
|
|
+
|
|
|
+ const bool headphones = this->m_headphonesDetector != nullptr && this->m_headphonesDetector->headphones ();
|
|
|
+ // OpenAL Soft only renders HRTF on stereo output
|
|
|
+ const bool wanted = this->m_audioSpec.channels == 2
|
|
|
+ && (mode == Application::ApplicationContext::HRTF_ON
|
|
|
+ || (mode == Application::ApplicationContext::HRTF_AUTO && headphones));
|
|
|
+
|
|
|
+ if (wanted == this->m_hrtfEnabled.load (std::memory_order_relaxed)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (wanted && this->m_hrtfStore == nullptr) {
|
|
|
+ this->m_hrtfStore = Hrtf::loadDefault (static_cast<unsigned> (this->m_audioSpec.freq));
|
|
|
+
|
|
|
+ if (this->m_hrtfStore == nullptr) {
|
|
|
+ this->m_hrtfFailed = true;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this->m_hrtfMixer = std::make_unique<Hrtf::Mixer> (this->m_hrtfStore->irSize);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (wanted) {
|
|
|
+ this->m_hrtfGeneration.fetch_add (1, std::memory_order_release);
|
|
|
+ }
|
|
|
+
|
|
|
+ this->m_hrtfEnabled.store (wanted, std::memory_order_release);
|
|
|
+
|
|
|
+ sLog.out (
|
|
|
+ "Sound objects: HRTF ", wanted ? "on" : "off",
|
|
|
+ mode == Application::ApplicationContext::HRTF_AUTO ? (headphones ? " (headphones)" : " (speakers)")
|
|
|
+ : " (forced)"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+void SDLAudioDriver::stopStream (int streamId, std::unique_ptr<AudioStream> stream) {
|
|
|
+ SDL_LockMutex (this->m_streamListMutex);
|
|
|
+
|
|
|
+ if (const auto it = this->m_streams.find (streamId); it != this->m_streams.end () && this->m_initialized) {
|
|
|
+ it->second->owned = std::move (stream);
|
|
|
+ it->second->stopping.store (true, std::memory_order_release);
|
|
|
+ }
|
|
|
+
|
|
|
+ SDL_UnlockMutex (this->m_streamListMutex);
|
|
|
+
|
|
|
+ // not registered (or no device): nothing to fade, the stream goes right away
|
|
|
+ if (stream != nullptr) {
|
|
|
+ this->removeStream (streamId);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void SDLAudioDriver::releaseFinishedStreams () {
|
|
|
+ std::vector<SDLAudioBuffer*> finished;
|
|
|
+
|
|
|
+ // only mixUpdate marks a stopping voice finished; while automute holds the callback back or the device is gone
|
|
|
+ // nothing mixes, and there is no fade to hear anyway
|
|
|
+ const bool mixing = SDL_GetAudioDeviceStatus (this->m_deviceID) == SDL_AUDIO_PLAYING
|
|
|
+ && !this->getAudioDetector ().anythingPlaying ();
|
|
|
+
|
|
|
+ SDL_LockMutex (this->m_streamListMutex);
|
|
|
+
|
|
|
+ for (auto it = this->m_streams.begin (); it != this->m_streams.end ();) {
|
|
|
+ const bool done = it->second->finished.load (std::memory_order_acquire)
|
|
|
+ || (!mixing && it->second->stopping.load (std::memory_order_acquire));
|
|
|
+
|
|
|
+ if (done && it->second->owned != nullptr) {
|
|
|
+ finished.push_back (it->second);
|
|
|
+ it = this->m_streams.erase (it);
|
|
|
+ } else {
|
|
|
+ ++it;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SDL_UnlockMutex (this->m_streamListMutex);
|
|
|
+
|
|
|
+ // outside the lock, an AudioStream waits for its reader thread
|
|
|
+ for (const auto* buffer : finished) {
|
|
|
+ delete buffer;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+AVSampleFormat SDLAudioDriver::getFormat () const { return AV_SAMPLE_FMT_FLT; }
|
|
|
+
|
|
|
+int SDLAudioDriver::getSampleRate () const { return this->m_audioSpec.freq; }
|
|
|
+
|
|
|
+int SDLAudioDriver::getChannels () const { return this->m_audioSpec.channels; }
|