Add audio stereo aggregator
This commit is contained in:
parent
d346f7b50d
commit
6c213f9c45
2 changed files with 63 additions and 0 deletions
12
libs/audio/include/psemek/audio/stereo.hpp
Normal file
12
libs/audio/include/psemek/audio/stereo.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/audio/stream.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
stream_ptr stereo(stream_ptr left, stream_ptr right);
|
||||
|
||||
}
|
||||
51
libs/audio/source/stereo.cpp
Normal file
51
libs/audio/source/stereo.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <psemek/audio/stereo.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct stereo_impl
|
||||
: stream
|
||||
{
|
||||
stereo_impl(stream_ptr left, stream_ptr right)
|
||||
: left_(std::move(left))
|
||||
, right_(std::move(right))
|
||||
{}
|
||||
|
||||
std::size_t read(float * data, std::size_t sample_count) override
|
||||
{
|
||||
if (buffer_.size() < sample_count)
|
||||
buffer_.resize(sample_count);
|
||||
|
||||
auto right_result = right_->read(data, sample_count);
|
||||
auto left_result = left_->read(buffer_.data(), sample_count);
|
||||
|
||||
auto result = std::min(right_result, left_result);
|
||||
|
||||
{
|
||||
auto begin = buffer_.data();
|
||||
auto end = buffer_.data() + result;
|
||||
auto dst = data;
|
||||
for (; begin < end; begin += 2, dst += 2)
|
||||
*dst = *begin;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
stream_ptr left_, right_;
|
||||
std::vector<float> buffer_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
stream_ptr stereo(stream_ptr left, stream_ptr right)
|
||||
{
|
||||
return std::make_shared<stereo_impl>(std::move(left), std::move(right));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue