mirror of
https://github.com/Lime3DS/Lime3DS.git
synced 2024-12-24 17:01:48 +01:00
audio_core: Simplify sink interface
This commit is contained in:
parent
761ef78408
commit
f34711219a
@ -13,13 +13,11 @@ namespace AudioCore {
|
|||||||
|
|
||||||
struct CubebSink::Impl {
|
struct CubebSink::Impl {
|
||||||
unsigned int sample_rate = 0;
|
unsigned int sample_rate = 0;
|
||||||
std::vector<std::string> device_list;
|
|
||||||
|
|
||||||
cubeb* ctx = nullptr;
|
cubeb* ctx = nullptr;
|
||||||
cubeb_stream* stream = nullptr;
|
cubeb_stream* stream = nullptr;
|
||||||
|
|
||||||
std::mutex queue_mutex;
|
std::function<void(s16*, std::size_t)> cb;
|
||||||
std::vector<s16> queue;
|
|
||||||
|
|
||||||
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
||||||
void* output_buffer, long num_frames);
|
void* output_buffer, long num_frames);
|
||||||
@ -95,45 +93,19 @@ unsigned int CubebSink::GetNativeSampleRate() const {
|
|||||||
return impl->sample_rate;
|
return impl->sample_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CubebSink::EnqueueSamples(const s16* samples, std::size_t sample_count) {
|
void CubebSink::SetCallback(std::function<void(s16*, std::size_t)> cb) {
|
||||||
if (!impl->ctx)
|
impl->cb = cb;
|
||||||
return;
|
|
||||||
|
|
||||||
std::lock_guard lock{impl->queue_mutex};
|
|
||||||
|
|
||||||
impl->queue.reserve(impl->queue.size() + sample_count * 2);
|
|
||||||
std::copy(samples, samples + sample_count * 2, std::back_inserter(impl->queue));
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t CubebSink::SamplesInQueue() const {
|
|
||||||
if (!impl->ctx)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
std::lock_guard lock{impl->queue_mutex};
|
|
||||||
return impl->queue.size() / 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long CubebSink::Impl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
long CubebSink::Impl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
||||||
void* output_buffer, long num_frames) {
|
void* output_buffer, long num_frames) {
|
||||||
Impl* impl = static_cast<Impl*>(user_data);
|
Impl* impl = static_cast<Impl*>(user_data);
|
||||||
u8* buffer = reinterpret_cast<u8*>(output_buffer);
|
s16* buffer = reinterpret_cast<s16*>(output_buffer);
|
||||||
|
|
||||||
if (!impl)
|
if (!impl || !impl->cb)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
std::lock_guard lock{impl->queue_mutex};
|
impl->cb(buffer, num_frames);
|
||||||
|
|
||||||
std::size_t frames_to_write =
|
|
||||||
std::min(impl->queue.size() / 2, static_cast<std::size_t>(num_frames));
|
|
||||||
|
|
||||||
memcpy(buffer, impl->queue.data(), frames_to_write * sizeof(s16) * 2);
|
|
||||||
impl->queue.erase(impl->queue.begin(), impl->queue.begin() + frames_to_write * 2);
|
|
||||||
|
|
||||||
if (frames_to_write < num_frames) {
|
|
||||||
// Fill the rest of the frames with silence
|
|
||||||
memset(buffer + frames_to_write * sizeof(s16) * 2, 0,
|
|
||||||
(num_frames - frames_to_write) * sizeof(s16) * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return num_frames;
|
return num_frames;
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,7 @@ public:
|
|||||||
|
|
||||||
unsigned int GetNativeSampleRate() const override;
|
unsigned int GetNativeSampleRate() const override;
|
||||||
|
|
||||||
void EnqueueSamples(const s16* samples, std::size_t sample_count) override;
|
void SetCallback(std::function<void(s16*, std::size_t)> cb) override;
|
||||||
|
|
||||||
std::size_t SamplesInQueue() const override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Impl;
|
struct Impl;
|
||||||
|
@ -12,16 +12,13 @@
|
|||||||
namespace AudioCore {
|
namespace AudioCore {
|
||||||
|
|
||||||
DspInterface::DspInterface() = default;
|
DspInterface::DspInterface() = default;
|
||||||
|
DspInterface::~DspInterface() = default;
|
||||||
DspInterface::~DspInterface() {
|
|
||||||
if (perform_time_stretching) {
|
|
||||||
FlushResidualStretcherAudio();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DspInterface::SetSink(const std::string& sink_id, const std::string& audio_device) {
|
void DspInterface::SetSink(const std::string& sink_id, const std::string& audio_device) {
|
||||||
const SinkDetails& sink_details = GetSinkDetails(sink_id);
|
const SinkDetails& sink_details = GetSinkDetails(sink_id);
|
||||||
sink = sink_details.factory(audio_device);
|
sink = sink_details.factory(audio_device);
|
||||||
|
sink->SetCallback(
|
||||||
|
[this](s16* buffer, std::size_t num_frames) { OutputCallback(buffer, num_frames); });
|
||||||
time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
|
time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,32 +48,21 @@ void DspInterface::OutputFrame(StereoFrame16& frame) {
|
|||||||
frame[i][1] = static_cast<s16>(frame[i][1] * volume_scale_factor);
|
frame[i][1] = static_cast<s16>(frame[i][1] * volume_scale_factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (perform_time_stretching) {
|
fifo.Push(frame.data(), frame.size());
|
||||||
time_stretcher.AddSamples(&frame[0][0], frame.size());
|
|
||||||
std::vector<s16> stretched_samples = time_stretcher.Process(sink->SamplesInQueue());
|
|
||||||
sink->EnqueueSamples(stretched_samples.data(), stretched_samples.size() / 2);
|
|
||||||
} else {
|
|
||||||
constexpr std::size_t maximum_sample_latency = 2048; // about 64 miliseconds
|
|
||||||
if (sink->SamplesInQueue() > maximum_sample_latency) {
|
|
||||||
// This can occur if we're running too fast and samples are starting to back up.
|
|
||||||
// Just drop the samples.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sink->EnqueueSamples(&frame[0][0], frame.size());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DspInterface::FlushResidualStretcherAudio() {
|
void DspInterface::FlushResidualStretcherAudio() {}
|
||||||
if (!sink)
|
|
||||||
return;
|
|
||||||
|
|
||||||
time_stretcher.Flush();
|
void DspInterface::OutputCallback(s16* buffer, size_t num_frames) {
|
||||||
while (true) {
|
const size_t frames_written = fifo.Pop(buffer, num_frames);
|
||||||
std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue());
|
|
||||||
if (residual_audio.empty())
|
if (frames_written > 0) {
|
||||||
break;
|
std::memcpy(&last_frame[0], buffer + 2 * (frames_written - 1), 2 * sizeof(s16));
|
||||||
sink->EnqueueSamples(residual_audio.data(), residual_audio.size() / 2);
|
}
|
||||||
|
|
||||||
|
// Hold last emitted frame; this prevents popping.
|
||||||
|
for (size_t i = frames_written; i < num_frames; i++) {
|
||||||
|
std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "audio_core/audio_types.h"
|
#include "audio_core/audio_types.h"
|
||||||
#include "audio_core/time_stretch.h"
|
#include "audio_core/time_stretch.h"
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
#include "common/ring_buffer.h"
|
||||||
#include "core/memory.h"
|
#include "core/memory.h"
|
||||||
|
|
||||||
namespace Service {
|
namespace Service {
|
||||||
@ -81,9 +82,12 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void FlushResidualStretcherAudio();
|
void FlushResidualStretcherAudio();
|
||||||
|
void OutputCallback(s16* buffer, std::size_t num_frames);
|
||||||
|
|
||||||
std::unique_ptr<Sink> sink;
|
std::unique_ptr<Sink> sink;
|
||||||
bool perform_time_stretching = false;
|
bool perform_time_stretching = false;
|
||||||
|
Common::RingBuffer<s16, 0x2000, 2> fifo;
|
||||||
|
std::array<s16, 2> last_frame{};
|
||||||
TimeStretcher time_stretcher;
|
TimeStretcher time_stretcher;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -19,11 +19,7 @@ public:
|
|||||||
return native_sample_rate;
|
return native_sample_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnqueueSamples(const s16*, std::size_t) override {}
|
void SetCallback(std::function<void(s16*, std::size_t)>) override {}
|
||||||
|
|
||||||
std::size_t SamplesInQueue() const override {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace AudioCore
|
} // namespace AudioCore
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <list>
|
#include <string>
|
||||||
#include <numeric>
|
#include <vector>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include "audio_core/audio_types.h"
|
#include "audio_core/audio_types.h"
|
||||||
#include "audio_core/sdl2_sink.h"
|
#include "audio_core/sdl2_sink.h"
|
||||||
@ -17,7 +17,7 @@ struct SDL2Sink::Impl {
|
|||||||
|
|
||||||
SDL_AudioDeviceID audio_device_id = 0;
|
SDL_AudioDeviceID audio_device_id = 0;
|
||||||
|
|
||||||
std::list<std::vector<s16>> queue;
|
std::function<void(s16*, std::size_t)> cb;
|
||||||
|
|
||||||
static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes);
|
static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes);
|
||||||
};
|
};
|
||||||
@ -74,58 +74,18 @@ unsigned int SDL2Sink::GetNativeSampleRate() const {
|
|||||||
return impl->sample_rate;
|
return impl->sample_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL2Sink::EnqueueSamples(const s16* samples, std::size_t sample_count) {
|
void SDL2Sink::SetCallback(std::function<void(s16*, std::size_t)> cb) {
|
||||||
if (impl->audio_device_id <= 0)
|
impl->cb = cb;
|
||||||
return;
|
|
||||||
|
|
||||||
SDL_LockAudioDevice(impl->audio_device_id);
|
|
||||||
impl->queue.emplace_back(samples, samples + sample_count * 2);
|
|
||||||
SDL_UnlockAudioDevice(impl->audio_device_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t SDL2Sink::SamplesInQueue() const {
|
|
||||||
if (impl->audio_device_id <= 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
SDL_LockAudioDevice(impl->audio_device_id);
|
|
||||||
|
|
||||||
std::size_t total_size =
|
|
||||||
std::accumulate(impl->queue.begin(), impl->queue.end(), static_cast<std::size_t>(0),
|
|
||||||
[](std::size_t sum, const auto& buffer) {
|
|
||||||
// Division by two because each stereo sample is made of
|
|
||||||
// two s16.
|
|
||||||
return sum + buffer.size() / 2;
|
|
||||||
});
|
|
||||||
|
|
||||||
SDL_UnlockAudioDevice(impl->audio_device_id);
|
|
||||||
|
|
||||||
return total_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) {
|
void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) {
|
||||||
Impl* impl = reinterpret_cast<Impl*>(impl_);
|
Impl* impl = reinterpret_cast<Impl*>(impl_);
|
||||||
|
if (!impl || !impl->cb)
|
||||||
|
return;
|
||||||
|
|
||||||
std::size_t remaining_size = static_cast<std::size_t>(buffer_size_in_bytes) /
|
const size_t num_frames = buffer_size_in_bytes / (2 * sizeof(s16));
|
||||||
sizeof(s16); // Keep track of size in 16-bit increments.
|
|
||||||
|
|
||||||
while (remaining_size > 0 && !impl->queue.empty()) {
|
impl->cb(reinterpret_cast<s16*>(buffer), num_frames);
|
||||||
if (impl->queue.front().size() <= remaining_size) {
|
|
||||||
memcpy(buffer, impl->queue.front().data(), impl->queue.front().size() * sizeof(s16));
|
|
||||||
buffer += impl->queue.front().size() * sizeof(s16);
|
|
||||||
remaining_size -= impl->queue.front().size();
|
|
||||||
impl->queue.pop_front();
|
|
||||||
} else {
|
|
||||||
memcpy(buffer, impl->queue.front().data(), remaining_size * sizeof(s16));
|
|
||||||
buffer += remaining_size * sizeof(s16);
|
|
||||||
impl->queue.front().erase(impl->queue.front().begin(),
|
|
||||||
impl->queue.front().begin() + remaining_size);
|
|
||||||
remaining_size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remaining_size > 0) {
|
|
||||||
memset(buffer, 0, remaining_size * sizeof(s16));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> ListSDL2SinkDevices() {
|
std::vector<std::string> ListSDL2SinkDevices() {
|
||||||
|
@ -17,9 +17,7 @@ public:
|
|||||||
|
|
||||||
unsigned int GetNativeSampleRate() const override;
|
unsigned int GetNativeSampleRate() const override;
|
||||||
|
|
||||||
void EnqueueSamples(const s16* samples, std::size_t sample_count) override;
|
void SetCallback(std::function<void(s16*, std::size_t)> cb) override;
|
||||||
|
|
||||||
std::size_t SamplesInQueue() const override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Impl;
|
struct Impl;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <functional>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
|
||||||
namespace AudioCore {
|
namespace AudioCore {
|
||||||
@ -20,19 +20,16 @@ class Sink {
|
|||||||
public:
|
public:
|
||||||
virtual ~Sink() = default;
|
virtual ~Sink() = default;
|
||||||
|
|
||||||
/// The native rate of this sink. The sink expects to be fed samples that respect this. (Units:
|
/// The native rate of this sink. The sink expects to be fed samples that respect this.
|
||||||
/// samples/sec)
|
/// (Units: samples/sec)
|
||||||
virtual unsigned int GetNativeSampleRate() const = 0;
|
virtual unsigned int GetNativeSampleRate() const = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Feed stereo samples to sink.
|
* Set callback for samples
|
||||||
* @param samples Samples in interleaved stereo PCM16 format.
|
* @param samples Samples in interleaved stereo PCM16 format.
|
||||||
* @param sample_count Number of samples.
|
* @param sample_count Number of samples.
|
||||||
*/
|
*/
|
||||||
virtual void EnqueueSamples(const s16* samples, std::size_t sample_count) = 0;
|
virtual void SetCallback(std::function<void(s16*, std::size_t)> cb) = 0;
|
||||||
|
|
||||||
/// Samples enqueued that have not been played yet.
|
|
||||||
virtual std::size_t SamplesInQueue() const = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace AudioCore
|
} // namespace AudioCore
|
||||||
|
Loading…
Reference in New Issue
Block a user