From 184a9e7e67542faccd6a054b5c4d50f2916cd0d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 25 Jun 2021 13:37:38 +0200 Subject: LibAudio: Make ResampleHelper templated for different sample types Previously, ResampleHelper was fixed on handling double's, which makes it unsuitable for the upcoming FLAC loader that needs to resample integers. For this reason, ResampleHelper is templated to support theoretically any type of sample, though only the necessary i32 and double are templated right now. The ResampleHelper implementations are moved from WavLoader.cpp to Buffer.cpp. This also improves some imports in the WavLoader files. --- Userland/Libraries/LibAudio/Buffer.cpp | 58 +++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) (limited to 'Userland/Libraries/LibAudio/Buffer.cpp') diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp index 86a51aa7ac..efe6945a5f 100644 --- a/Userland/Libraries/LibAudio/Buffer.cpp +++ b/Userland/Libraries/LibAudio/Buffer.cpp @@ -5,10 +5,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "Buffer.h" #include #include #include -#include namespace Audio { @@ -44,7 +44,7 @@ i32 Buffer::allocate_id() } template -static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector& samples, ResampleHelper& resampler, int num_channels) +static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector& samples, ResampleHelper& resampler, int num_channels) { double norm_l = 0; double norm_r = 0; @@ -127,13 +127,13 @@ static double read_norm_sample_8(InputMemoryStream& stream) return double(sample) / NumericLimits::max(); } -RefPtr Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format) +RefPtr Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format) { InputMemoryStream stream { data }; return from_pcm_stream(stream, resampler, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8)); } -RefPtr Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples) +RefPtr Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples) { Vector fdata; fdata.ensure_capacity(num_samples); @@ -166,4 +166,54 @@ RefPtr Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper return Buffer::create_with_samples(move(fdata)); } +template +ResampleHelper::ResampleHelper(double source, double target) + : m_ratio(source / target) +{ +} +template ResampleHelper::ResampleHelper(double, double); +template ResampleHelper::ResampleHelper(double, double); + +template +Vector ResampleHelper::resample(Vector to_resample) +{ + Vector resampled; + resampled.ensure_capacity(to_resample.size() * m_ratio); + for (auto sample : to_resample) { + process_sample(sample, sample); + + while (read_sample(sample, sample)) + resampled.unchecked_append(sample); + } + + return resampled; +} +template Vector ResampleHelper::resample(Vector); +template Vector ResampleHelper::resample(Vector); + +template +void ResampleHelper::process_sample(SampleType sample_l, SampleType sample_r) +{ + m_last_sample_l = sample_l; + m_last_sample_r = sample_r; + m_current_ratio += 1; +} +template void ResampleHelper::process_sample(i32, i32); +template void ResampleHelper::process_sample(double, double); + +template +bool ResampleHelper::read_sample(SampleType& next_l, SampleType& next_r) +{ + if (m_current_ratio > 0) { + m_current_ratio -= m_ratio; + next_l = m_last_sample_l; + next_r = m_last_sample_r; + return true; + } + + return false; +} +template bool ResampleHelper::read_sample(i32&, i32&); +template bool ResampleHelper::read_sample(double&, double&); + } -- cgit v1.2.3