Make audio::stream::played() atomic
This commit is contained in:
parent
d19c93af33
commit
3890fa0b02
8 changed files with 20 additions and 45 deletions
|
|
@ -17,7 +17,6 @@ namespace psemek::audio
|
||||||
virtual std::size_t read(float * data, std::size_t sample_count) = 0;
|
virtual std::size_t read(float * data, std::size_t sample_count) = 0;
|
||||||
|
|
||||||
// The number of samples already played from this stream
|
// The number of samples already played from this stream
|
||||||
// Must be called from mixing thread
|
|
||||||
virtual std::size_t played() const = 0;
|
virtual std::size_t played() const = 0;
|
||||||
|
|
||||||
virtual ~stream() {}
|
virtual ~stream() {}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
#include <psemek/audio/stream.hpp>
|
#include <psemek/audio/stream.hpp>
|
||||||
#include <psemek/audio/constants.hpp>
|
#include <psemek/audio/constants.hpp>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace psemek::audio
|
namespace psemek::audio
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -21,7 +23,7 @@ namespace psemek::audio
|
||||||
|
|
||||||
std::size_t played() const override
|
std::size_t played() const override
|
||||||
{
|
{
|
||||||
return played_;
|
return played_.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t read(float * data, std::size_t sample_count) override
|
std::size_t read(float * data, std::size_t sample_count) override
|
||||||
|
|
@ -33,13 +35,19 @@ namespace psemek::audio
|
||||||
*p++ = v;
|
*p++ = v;
|
||||||
*p++ = v;
|
*p++ = v;
|
||||||
}
|
}
|
||||||
played_ += sample_count;
|
played_.fetch_add(sample_count);
|
||||||
return sample_count;
|
return sample_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Func func_;
|
Func func_;
|
||||||
std::size_t played_ = 0;
|
std::atomic<std::size_t> played_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Func>
|
||||||
|
auto make_generator(Func && func)
|
||||||
|
{
|
||||||
|
return std::make_shared<generator_stream<std::decay_t<Func>>>(std::forward<Func>(func));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ namespace psemek::audio
|
||||||
|
|
||||||
std::size_t played() const override
|
std::size_t played() const override
|
||||||
{
|
{
|
||||||
return played_;
|
return played_.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -37,7 +37,7 @@ namespace psemek::audio
|
||||||
std::mutex new_channels_mutex_;
|
std::mutex new_channels_mutex_;
|
||||||
std::vector<channel_ptr> new_channels_;
|
std::vector<channel_ptr> new_channels_;
|
||||||
|
|
||||||
std::size_t played_ = 0;
|
std::atomic<std::size_t> played_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
channel_ptr mixer_impl::add(stream_ptr stream)
|
channel_ptr mixer_impl::add(stream_ptr stream)
|
||||||
|
|
@ -96,7 +96,7 @@ namespace psemek::audio
|
||||||
std::swap(channels_, alive_channels_);
|
std::swap(channels_, alive_channels_);
|
||||||
alive_channels_.clear();
|
alive_channels_.clear();
|
||||||
|
|
||||||
played_ += sample_count;
|
played_.fetch_add(sample_count);
|
||||||
|
|
||||||
return sample_count;
|
return sample_count;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include <psemek/audio/wave/sawtooth.hpp>
|
#include <psemek/audio/wave/sawtooth.hpp>
|
||||||
#include <psemek/audio/wave/generator.hpp>
|
#include <psemek/audio/wave/generator.hpp>
|
||||||
#include <psemek/audio/oscillator.hpp>
|
#include <psemek/audio/oscillator.hpp>
|
||||||
#include <psemek/util/to_shared.hpp>
|
|
||||||
#include <psemek/log/log.hpp>
|
#include <psemek/log/log.hpp>
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
@ -16,7 +15,7 @@ namespace psemek::audio
|
||||||
return (2.f / geom::pi) * (- std::atan2(z.real() + 1.f, z.imag()));
|
return (2.f / geom::pi) * (- std::atan2(z.real() + 1.f, z.imag()));
|
||||||
};
|
};
|
||||||
|
|
||||||
return util::to_shared(generator_stream(func));
|
return make_generator(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,12 @@
|
||||||
#include <psemek/audio/wave/silence.hpp>
|
#include <psemek/audio/wave/silence.hpp>
|
||||||
|
#include <psemek/audio/wave/generator.hpp>
|
||||||
|
|
||||||
namespace psemek::audio
|
namespace psemek::audio
|
||||||
{
|
{
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
|
|
||||||
struct silence_impl
|
|
||||||
: stream
|
|
||||||
{
|
|
||||||
std::size_t read(float * data, std::size_t sample_count) override
|
|
||||||
{
|
|
||||||
std::fill(data, data + sample_count, 0.f);
|
|
||||||
played_ += sample_count;
|
|
||||||
return sample_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::size_t> length() const override
|
|
||||||
{
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t played() const override
|
|
||||||
{
|
|
||||||
return played_;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::size_t played_ = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
stream_ptr silence()
|
stream_ptr silence()
|
||||||
{
|
{
|
||||||
return std::make_shared<silence_impl>();
|
return make_generator([]{ return 0.f; });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include <psemek/audio/wave/sine.hpp>
|
#include <psemek/audio/wave/sine.hpp>
|
||||||
#include <psemek/audio/wave/generator.hpp>
|
#include <psemek/audio/wave/generator.hpp>
|
||||||
#include <psemek/audio/oscillator.hpp>
|
#include <psemek/audio/oscillator.hpp>
|
||||||
#include <psemek/util/to_shared.hpp>
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <complex>
|
#include <complex>
|
||||||
|
|
@ -15,7 +14,7 @@ namespace psemek::audio
|
||||||
return o.next().imag();
|
return o.next().imag();
|
||||||
};
|
};
|
||||||
|
|
||||||
return util::to_shared(generator_stream(func));
|
return make_generator(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include <psemek/audio/wave/square.hpp>
|
#include <psemek/audio/wave/square.hpp>
|
||||||
#include <psemek/audio/wave/generator.hpp>
|
#include <psemek/audio/wave/generator.hpp>
|
||||||
#include <psemek/audio/oscillator.hpp>
|
#include <psemek/audio/oscillator.hpp>
|
||||||
#include <psemek/util/to_shared.hpp>
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
@ -14,7 +13,7 @@ namespace psemek::audio
|
||||||
return o.next().imag() > 0.f ? 1.f : -1.f;
|
return o.next().imag() > 0.f ? 1.f : -1.f;
|
||||||
};
|
};
|
||||||
|
|
||||||
return util::to_shared(generator_stream(func));
|
return make_generator(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include <psemek/audio/wave/triangle.hpp>
|
#include <psemek/audio/wave/triangle.hpp>
|
||||||
#include <psemek/audio/wave/generator.hpp>
|
#include <psemek/audio/wave/generator.hpp>
|
||||||
#include <psemek/audio/oscillator.hpp>
|
#include <psemek/audio/oscillator.hpp>
|
||||||
#include <psemek/util/to_shared.hpp>
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
@ -16,7 +15,7 @@ namespace psemek::audio
|
||||||
return 4.f * std::abs(t - std::floor(t + 0.5f)) - 1.f;
|
return 4.f * std::abs(t - std::floor(t + 0.5f)) - 1.f;
|
||||||
};
|
};
|
||||||
|
|
||||||
return util::to_shared(generator_stream(func));
|
return make_generator(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue