summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/Buffer.h
diff options
context:
space:
mode:
authorkleines Filmröllchen <malu.bertsch@gmail.com>2021-06-25 13:37:38 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-06-25 20:48:14 +0430
commit184a9e7e67542faccd6a054b5c4d50f2916cd0d4 (patch)
tree585fd80bd90cb116849a12497bf6fc1e15c9249a /Userland/Libraries/LibAudio/Buffer.h
parent2e0015527582c04dc0dd7b81d2af62923db90686 (diff)
downloadserenity-184a9e7e67542faccd6a054b5c4d50f2916cd0d4.zip
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.
Diffstat (limited to 'Userland/Libraries/LibAudio/Buffer.h')
-rw-r--r--Userland/Libraries/LibAudio/Buffer.h20
1 files changed, 14 insertions, 6 deletions
diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h
index 88d0fa0c56..3619111384 100644
--- a/Userland/Libraries/LibAudio/Buffer.h
+++ b/Userland/Libraries/LibAudio/Buffer.h
@@ -88,25 +88,33 @@ String sample_format_name(PcmSampleFormat format);
// Small helper to resample from one playback rate to another
// This isn't really "smart", in that we just insert (or drop) samples.
// Should do better...
+template<typename SampleType>
class ResampleHelper {
public:
ResampleHelper(double source, double target);
- void process_sample(double sample_l, double sample_r);
- bool read_sample(double& next_l, double& next_r);
+ // To be used as follows:
+ // while the resampler doesn't need a new sample, read_sample(current) and store the resulting samples.
+ // as long as the resampler needs a new sample, process_sample(current)
+
+ // Stores a new sample
+ void process_sample(SampleType sample_l, SampleType sample_r);
+ // Assigns the given sample to its correct value and returns false if there is a new sample required
+ bool read_sample(SampleType& next_l, SampleType& next_r);
+ Vector<SampleType> resample(Vector<SampleType> to_resample);
private:
const double m_ratio;
double m_current_ratio { 0 };
- double m_last_sample_l { 0 };
- double m_last_sample_r { 0 };
+ SampleType m_last_sample_l;
+ SampleType m_last_sample_r;
};
// A buffer of audio samples, normalized to 44100hz.
class Buffer : public RefCounted<Buffer> {
public:
- static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format);
- static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples);
+ static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, ResampleHelper<double>& resampler, int num_channels, PcmSampleFormat sample_format);
+ static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, ResampleHelper<double>& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples);
static NonnullRefPtr<Buffer> create_with_samples(Vector<Frame>&& samples)
{
return adopt_ref(*new Buffer(move(samples)));