Add comments

This commit is contained in:
Attila Uygun 2023-06-04 11:13:22 +02:00
parent 7b637a95d9
commit a39efa5d3c
2 changed files with 21 additions and 13 deletions

View File

@ -11,28 +11,28 @@ namespace eng {
namespace {
template <typename T>
std::array<std::unique_ptr<T[]>, 2> Deinterleave(size_t num_channels,
std::array<std::unique_ptr<T[]>, 2> Deinterleave(
size_t num_channels,
size_t num_samples,
float* input_buffer) {
std::unique_ptr<float[]> source_buffer) {
std::array<std::unique_ptr<T[]>, 2> channels;
if (num_channels == 1) {
// Single channel.
if constexpr (std::is_same<float, T>::value) {
channels[0] = std::make_unique<T[]>(num_samples);
memcpy(channels[0].get(), input_buffer, num_samples * sizeof(float));
// Passthrough
channels[0] = std::move(source_buffer);
} else {
channels[0] = std::make_unique<T[]>(num_samples);
for (int i = 0; i < num_samples; ++i)
channels[0].get()[i] = static_cast<T>(input_buffer[i]);
channels[0].get()[i] = static_cast<T>(source_buffer.get()[i]);
}
} else {
// Deinterleave into separate channels.
channels[0] = std::make_unique<T[]>(num_samples);
channels[1] = std::make_unique<T[]>(num_samples);
for (size_t i = 0, j = 0; i < num_samples * 2; i += 2) {
channels[0].get()[j] = static_cast<T>(input_buffer[i]);
channels[1].get()[j++] = static_cast<T>(input_buffer[i + 1]);
channels[0].get()[j] = static_cast<T>(source_buffer.get()[i]);
channels[1].get()[j++] = static_cast<T>(source_buffer.get()[i + 1]);
}
}
@ -59,15 +59,15 @@ void AudioBus::SetAudioConfig(size_t num_channels, size_t sample_rate) {
sample_rate_ = sample_rate;
}
void AudioBus::FromInterleaved(std::unique_ptr<float[]> input_buffer,
void AudioBus::FromInterleaved(std::unique_ptr<float[]> source_buffer,
size_t samples_per_channel) {
auto channels = Deinterleave<float>(num_channels_, samples_per_channel,
input_buffer.get());
std::move(source_buffer));
size_t hw_sample_rate = Engine::Get().GetAudioHardwareSampleRate();
if (hw_sample_rate == sample_rate_) {
// No need for resmapling.
// Passthrough
channel_data_[0] = std::move(channels[0]);
if (num_channels_ == 2)
channel_data_[1] = std::move(channels[1]);

View File

@ -7,6 +7,10 @@ namespace eng {
class SincResampler;
// Represents a sequence of audio samples for each channels. The data layout is
// planar as opposed to interleaved. The memory for the data is allocated and
// owned by the AudioBus. Max two channels are supported. An AudioBus with one
// channel is mono, with two channels is stereo.
class AudioBus {
public:
AudioBus();
@ -28,7 +32,11 @@ class AudioBus {
protected:
void SetAudioConfig(size_t num_channels, size_t sample_rate);
void FromInterleaved(std::unique_ptr<float[]> input_buffer,
// Overwrites the sample values stored in this AudioBus instance with values
// from a given interleaved source_buffer. The expected layout of the
// source_buffer is [ch0, ch1, ch0, ch1, ...]. A sample-rate conversion to the
// system sample-rate will be made if it doesn't match.
void FromInterleaved(std::unique_ptr<float[]> source_buffer,
size_t samples_per_channel);
private: