This commit is contained in:
Attila Uygun 2023-06-04 19:28:55 +02:00
parent e376b1bc17
commit 557eb517cb
5 changed files with 22 additions and 22 deletions

View File

@ -101,7 +101,7 @@ void AudioBus::FromInterleaved(std::unique_ptr<float[]> source_buffer,
}); });
} }
if (IsEndOfStream()) { if (EndOfStream()) {
// We are done with the resampler. // We are done with the resampler.
for (size_t i = 0; i < num_channels_; ++i) for (size_t i = 0; i < num_channels_; ++i)
resampler_[i].reset(); resampler_[i].reset();

View File

@ -19,7 +19,7 @@ class AudioBus {
virtual void Stream(bool loop) = 0; virtual void Stream(bool loop) = 0;
virtual void SwapBuffers() = 0; virtual void SwapBuffers() = 0;
virtual void ResetStream() = 0; virtual void ResetStream() = 0;
virtual bool IsEndOfStream() const = 0; virtual bool EndOfStream() const = 0;
float* GetChannelData(int channel) const { float* GetChannelData(int channel) const {
return channel_data_[channel].get(); return channel_data_[channel].get();

View File

@ -49,7 +49,7 @@ void AudioMixer::DestroyResource(uint64_t resource_id) {
} }
void AudioMixer::Play(uint64_t resource_id, void AudioMixer::Play(uint64_t resource_id,
std::shared_ptr<AudioBus> sound, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos) { bool reset_pos) {
if (!audio_enabled_) if (!audio_enabled_)
@ -64,9 +64,9 @@ void AudioMixer::Play(uint64_t resource_id,
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed); it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
if (it->second->flags.load(std::memory_order_relaxed) & kStopped) if (it->second->flags.load(std::memory_order_relaxed) & kStopped)
it->second->restart_cb = [&, resource_id, sound, amplitude, it->second->restart_cb = [&, resource_id, audio_bus, amplitude,
reset_pos]() -> void { reset_pos]() -> void {
Play(resource_id, sound, amplitude, reset_pos); Play(resource_id, audio_bus, amplitude, reset_pos);
}; };
return; return;
@ -75,14 +75,14 @@ void AudioMixer::Play(uint64_t resource_id,
if (reset_pos) { if (reset_pos) {
it->second->src_index = 0; it->second->src_index = 0;
it->second->accumulator = 0; it->second->accumulator = 0;
sound->ResetStream(); audio_bus->ResetStream();
} else if (it->second->src_index >= sound->samples_per_channel()) { } else if (it->second->src_index >= audio_bus->samples_per_channel()) {
return; return;
} }
it->second->active = true; it->second->active = true;
it->second->flags.fetch_and(~kStopped, std::memory_order_relaxed); it->second->flags.fetch_and(~kStopped, std::memory_order_relaxed);
it->second->sound = sound; it->second->audio_bus = audio_bus;
if (amplitude >= 0) if (amplitude >= 0)
it->second->amplitude = amplitude; it->second->amplitude = amplitude;
@ -177,19 +177,19 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount); memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount);
for (auto it = play_list_[1].begin(); it != play_list_[1].end();) { for (auto it = play_list_[1].begin(); it != play_list_[1].end();) {
auto sound = it->get()->sound.get(); auto audio_bus = it->get()->audio_bus.get();
unsigned flags = it->get()->flags.load(std::memory_order_relaxed); unsigned flags = it->get()->flags.load(std::memory_order_relaxed);
bool marked_for_removal = false; bool marked_for_removal = false;
if (flags & kStopped) { if (flags & kStopped) {
marked_for_removal = true; marked_for_removal = true;
} else { } else {
const float* src[2] = {sound->GetChannelData(0), const float* src[2] = {audio_bus->GetChannelData(0),
sound->GetChannelData(1)}; audio_bus->GetChannelData(1)};
if (!src[1]) if (!src[1])
src[1] = src[0]; // mono. src[1] = src[0]; // mono.
size_t num_samples = sound->samples_per_channel(); size_t num_samples = audio_bus->samples_per_channel();
size_t src_index = it->get()->src_index; size_t src_index = it->get()->src_index;
size_t step = it->get()->step.load(std::memory_order_relaxed); size_t step = it->get()->step.load(std::memory_order_relaxed);
size_t accumulator = it->get()->accumulator; size_t accumulator = it->get()->accumulator;
@ -199,7 +199,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
float max_amplitude = float max_amplitude =
it->get()->max_amplitude.load(std::memory_order_relaxed); it->get()->max_amplitude.load(std::memory_order_relaxed);
size_t channel_offset = size_t channel_offset =
(flags & kSimulateStereo) ? sound->sample_rate() / 10 : 0; (flags & kSimulateStereo) ? audio_bus->sample_rate() / 10 : 0;
DCHECK(num_samples > 0); DCHECK(num_samples > 0);
@ -234,7 +234,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
if (src_index >= num_samples) { if (src_index >= num_samples) {
src_index %= num_samples; src_index %= num_samples;
if (sound->IsEndOfStream()) { if (audio_bus->EndOfStream()) {
marked_for_removal = true; marked_for_removal = true;
break; break;
} }
@ -245,12 +245,12 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
std::memory_order_relaxed); std::memory_order_relaxed);
// Swap buffers and start streaming in background. // Swap buffers and start streaming in background.
sound->SwapBuffers(); audio_bus->SwapBuffers();
src[0] = sound->GetChannelData(0); src[0] = audio_bus->GetChannelData(0);
src[1] = sound->GetChannelData(1); src[1] = audio_bus->GetChannelData(1);
if (!src[1]) if (!src[1])
src[1] = src[0]; // mono. src[1] = src[0]; // mono.
num_samples = sound->samples_per_channel(); num_samples = audio_bus->samples_per_channel();
ThreadPool::Get().PostTask( ThreadPool::Get().PostTask(
HERE, HERE,
@ -286,7 +286,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
} }
void AudioMixer::DoStream(std::shared_ptr<Resource> resource, bool loop) { void AudioMixer::DoStream(std::shared_ptr<Resource> resource, bool loop) {
resource->sound->Stream(loop); resource->audio_bus->Stream(loop);
resource->streaming_in_progress.store(false, std::memory_order_release); resource->streaming_in_progress.store(false, std::memory_order_release);
} }

View File

@ -31,7 +31,7 @@ class AudioMixer : public AudioSink::Delegate {
void DestroyResource(uint64_t resource_id); void DestroyResource(uint64_t resource_id);
void Play(uint64_t resource_id, void Play(uint64_t resource_id,
std::shared_ptr<AudioBus> sound, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos); bool reset_pos);
void Stop(uint64_t resource_id); void Stop(uint64_t resource_id);
@ -62,7 +62,7 @@ class AudioMixer : public AudioSink::Delegate {
base::Closure restart_cb; base::Closure restart_cb;
// Initialized by main thread, used by audio thread. // Initialized by main thread, used by audio thread.
std::shared_ptr<AudioBus> sound; std::shared_ptr<AudioBus> audio_bus;
size_t src_index = 0; size_t src_index = 0;
size_t accumulator = 0; size_t accumulator = 0;
float amplitude = 1.0f; float amplitude = 1.0f;

View File

@ -26,7 +26,7 @@ class Sound final : public AudioBus {
void Stream(bool loop) final; void Stream(bool loop) final;
void SwapBuffers() final; void SwapBuffers() final;
void ResetStream() final; void ResetStream() final;
bool IsEndOfStream() const final { return eos_; } bool EndOfStream() const final { return eos_; }
private: private:
// Buffer holding decoded audio. // Buffer holding decoded audio.