Use channel class for main audio output

This commit is contained in:
Nikita Lisitsa 2022-10-05 22:51:10 +03:00
parent 26b44a9fa3
commit 9b3afc9865
4 changed files with 13 additions and 7 deletions

View file

@ -68,7 +68,7 @@ struct audio_app
left_volume_ = audio::volume(dup1, 0.5f, 0.1f);
right_volume_ = audio::volume(dup2, 0.5f, 0.1f);
auto result = audio::stereo(left_volume_, right_volume_);
engine_.output(result);
engine_.output()->stream(result);
}
void on_key_down(SDL_Keycode key) override
@ -90,7 +90,8 @@ struct audio_app
if (channels_.contains(key))
{
auto & ch = channels_[key];
ch->stream(audio::fade_out(ch->stream(), 0.01f));
if (auto s = ch->stream())
ch->stream(audio::fade_out(s, 0.01f));
channels_.erase(key);
}
}

View file

@ -10,6 +10,8 @@ namespace psemek::audio
struct channel
{
channel() = default;
channel(stream_ptr stream)
: stream_(std::move(stream))
{}

View file

@ -2,6 +2,7 @@
#include <psemek/audio/stream.hpp>
#include <psemek/audio/track.hpp>
#include <psemek/audio/channel.hpp>
#include <psemek/util/pimpl.hpp>
#include <psemek/util/span.hpp>
@ -21,7 +22,7 @@ namespace psemek::audio
track_ptr load(float const * data, std::size_t sample_count, bool copy = true);
track_ptr load(util::span<float> data, bool copy = true);
stream_ptr output(stream_ptr stream);
channel_ptr output();
private:
psemek_declare_pimpl

View file

@ -23,7 +23,7 @@ namespace psemek::audio
std::vector<float> buffer;
bool thread_registered = false;
stream_ptr output;
channel_ptr output;
impl();
~impl();
@ -49,6 +49,8 @@ namespace psemek::audio
SDL_PauseAudioDevice(device, 0);
log::info() << "Initialized audio: " << static_cast<int>(obtained.channels) << " channels, " << obtained.freq << " Hz, " << obtained.samples << " samples";
output = std::make_shared<channel>();
}
engine::impl::~impl()
@ -59,7 +61,7 @@ namespace psemek::audio
void engine::impl::callback(void * userdata, std::uint8_t * dst_u8, int len)
{
auto self = static_cast<impl *>(userdata);
stream_ptr output = std::atomic_load(&(self->output));
stream_ptr output = self->output->stream();
std::int16_t * dst = reinterpret_cast<std::int16_t *>(dst_u8);
if (!self->thread_registered)
@ -101,9 +103,9 @@ namespace psemek::audio
return load(data.data(), data.size(), copy);
}
stream_ptr engine::output(stream_ptr stream)
channel_ptr engine::output()
{
return std::atomic_exchange(&(impl().output), std::move(stream));
return impl().output;
}
}