summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio
diff options
context:
space:
mode:
authorkleines Filmröllchen <malu.bertsch@gmail.com>2021-08-19 00:13:26 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-08-27 23:35:27 +0430
commitd049626f402f50720a1ccc4452676a56e22debbd (patch)
tree3f362d216b825318a82661e0874261c68cf6f24e /Userland/Libraries/LibAudio
parent9880a5c48150fd03e516c6f74ff0bd0cea5768d5 (diff)
downloadserenity-d049626f402f50720a1ccc4452676a56e22debbd.zip
Userland+LibAudio: Make audio applications support dynamic sample rate
All audio applications (aplay, Piano, Sound Player) respect the ability of the system to have theoretically any sample rate. Therefore, they resample their own audio into the system sample rate. LibAudio previously had its loaders resample their own audio, even though they expose their sample rate. This is now changed. The loaders output audio data in their file's sample rate, which the user has to query and resample appropriately. Resampling code from Buffer, WavLoader and FlacLoader is removed. Note that these applications only check the sample rate at startup, which is reasonable (the user has to restart applications when changing the sample rate). Fully dynamic adaptation could both lead to errors and will require another IPC interface. This seems to be enough for now.
Diffstat (limited to 'Userland/Libraries/LibAudio')
-rw-r--r--Userland/Libraries/LibAudio/Buffer.cpp43
-rw-r--r--Userland/Libraries/LibAudio/Buffer.h12
-rw-r--r--Userland/Libraries/LibAudio/FlacLoader.cpp6
-rw-r--r--Userland/Libraries/LibAudio/FlacLoader.h1
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.cpp5
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.h8
6 files changed, 35 insertions, 40 deletions
diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp
index 1039f5bb3f..4f401b4f28 100644
--- a/Userland/Libraries/LibAudio/Buffer.cpp
+++ b/Userland/Libraries/LibAudio/Buffer.cpp
@@ -45,7 +45,7 @@ i32 Buffer::allocate_id()
}
template<typename SampleReader>
-static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Frame>& samples, ResampleHelper<double>& resampler, int num_channels)
+static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Frame>& samples, int num_channels)
{
double norm_l = 0;
double norm_r = 0;
@@ -53,29 +53,23 @@ static void read_samples_from_stream(InputMemoryStream& stream, SampleReader rea
switch (num_channels) {
case 1:
for (;;) {
- while (resampler.read_sample(norm_l, norm_r)) {
- samples.append(Frame(norm_l));
- }
norm_l = read_sample(stream);
+ samples.append(Frame(norm_l));
if (stream.handle_any_error()) {
break;
}
- resampler.process_sample(norm_l, norm_r);
}
break;
case 2:
for (;;) {
- while (resampler.read_sample(norm_l, norm_r)) {
- samples.append(Frame(norm_l, norm_r));
- }
norm_l = read_sample(stream);
norm_r = read_sample(stream);
+ samples.append(Frame(norm_l, norm_r));
if (stream.handle_any_error()) {
break;
}
- resampler.process_sample(norm_l, norm_r);
}
break;
default:
@@ -128,32 +122,32 @@ static double read_norm_sample_8(InputMemoryStream& stream)
return double(sample) / NumericLimits<u8>::max();
}
-RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper<double>& resampler, int num_channels, PcmSampleFormat sample_format)
+RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, 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));
+ return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
}
-RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper<double>& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples)
+RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
{
Vector<Frame> fdata;
fdata.ensure_capacity(num_samples);
switch (sample_format) {
case PcmSampleFormat::Uint8:
- read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
+ read_samples_from_stream(stream, read_norm_sample_8, fdata, num_channels);
break;
case PcmSampleFormat::Int16:
- read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
+ read_samples_from_stream(stream, read_norm_sample_16, fdata, num_channels);
break;
case PcmSampleFormat::Int24:
- read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
+ read_samples_from_stream(stream, read_norm_sample_24, fdata, num_channels);
break;
case PcmSampleFormat::Float32:
- read_samples_from_stream(stream, read_float_sample_32, fdata, resampler, num_channels);
+ read_samples_from_stream(stream, read_float_sample_32, fdata, num_channels);
break;
case PcmSampleFormat::Float64:
- read_samples_from_stream(stream, read_float_sample_64, fdata, resampler, num_channels);
+ read_samples_from_stream(stream, read_float_sample_64, fdata, num_channels);
break;
default:
VERIFY_NOT_REACHED();
@@ -193,6 +187,21 @@ Vector<SampleType> ResampleHelper<SampleType>::resample(Vector<SampleType> to_re
template Vector<i32> ResampleHelper<i32>::resample(Vector<i32>);
template Vector<double> ResampleHelper<double>::resample(Vector<double>);
+NonnullRefPtr<Buffer> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample)
+{
+ Vector<Frame> resampled;
+ resampled.ensure_capacity(to_resample.sample_count() * ceil_div(resampler.source(), resampler.target()));
+ for (size_t i = 0; i < static_cast<size_t>(to_resample.sample_count()); ++i) {
+ auto sample = to_resample.samples()[i];
+ resampler.process_sample(sample.left, sample.right);
+
+ while (resampler.read_sample(sample.left, sample.right))
+ resampled.append(sample);
+ }
+
+ return Buffer::create_with_samples(move(resampled));
+}
+
template<typename SampleType>
void ResampleHelper<SampleType>::process_sample(SampleType sample_l, SampleType sample_r)
{
diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h
index 6bf6377b96..b00a533dc3 100644
--- a/Userland/Libraries/LibAudio/Buffer.h
+++ b/Userland/Libraries/LibAudio/Buffer.h
@@ -105,6 +105,9 @@ public:
void reset();
+ u32 source() const { return m_source; }
+ u32 target() const { return m_target; }
+
private:
const u32 m_source;
const u32 m_target;
@@ -113,11 +116,11 @@ private:
SampleType m_last_sample_r;
};
-// A buffer of audio samples, normalized to 44100hz.
+// A buffer of audio samples.
class Buffer : public RefCounted<Buffer> {
public:
- 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 RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
+ static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, 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)));
@@ -157,4 +160,7 @@ private:
const int m_sample_count;
};
+// This only works for double resamplers, and therefore cannot be part of the class
+NonnullRefPtr<Buffer> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample);
+
}
diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp
index 019633c446..1ac3f62cf7 100644
--- a/Userland/Libraries/LibAudio/FlacLoader.cpp
+++ b/Userland/Libraries/LibAudio/FlacLoader.cpp
@@ -40,8 +40,6 @@ FlacLoaderPlugin::FlacLoaderPlugin(const StringView& path)
reset();
if (!m_valid)
return;
-
- m_resampler = make<ResampleHelper<i32>>(m_sample_rate, 44100);
}
FlacLoaderPlugin::FlacLoaderPlugin(const ByteBuffer& buffer)
@@ -58,8 +56,6 @@ FlacLoaderPlugin::FlacLoaderPlugin(const ByteBuffer& buffer)
reset();
if (!m_valid)
return;
-
- m_resampler = make<ResampleHelper<i32>>(m_sample_rate, 44100);
}
bool FlacLoaderPlugin::sniff()
@@ -348,8 +344,6 @@ void FlacLoaderPlugin::next_frame()
FlacSubframeHeader new_subframe = next_subframe_header(bit_stream, i);
CHECK_ERROR_STRING;
Vector<i32> subframe_samples = parse_subframe(new_subframe, bit_stream);
- m_resampler->reset();
- subframe_samples = m_resampler->resample(subframe_samples);
CHECK_ERROR_STRING;
current_subframes.append(move(subframe_samples));
}
diff --git a/Userland/Libraries/LibAudio/FlacLoader.h b/Userland/Libraries/LibAudio/FlacLoader.h
index b593804385..915bd44b5c 100644
--- a/Userland/Libraries/LibAudio/FlacLoader.h
+++ b/Userland/Libraries/LibAudio/FlacLoader.h
@@ -124,7 +124,6 @@ private:
bool m_valid { false };
RefPtr<Core::File> m_file;
String m_error_string;
- OwnPtr<ResampleHelper<i32>> m_resampler;
// Data obtained directly from the FLAC metadata: many values have specific bit counts
u32 m_sample_rate { 0 }; // 20 bit
diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp
index 2734ae4109..d0e305fe88 100644
--- a/Userland/Libraries/LibAudio/WavLoader.cpp
+++ b/Userland/Libraries/LibAudio/WavLoader.cpp
@@ -29,8 +29,6 @@ WavLoaderPlugin::WavLoaderPlugin(const StringView& path)
valid = parse_header();
if (!valid)
return;
-
- m_resampler = make<ResampleHelper<double>>(m_sample_rate, m_device_sample_rate);
}
WavLoaderPlugin::WavLoaderPlugin(const ByteBuffer& buffer)
@@ -45,8 +43,6 @@ WavLoaderPlugin::WavLoaderPlugin(const ByteBuffer& buffer)
valid = parse_header();
if (!valid)
return;
-
- m_resampler = make<ResampleHelper<double>>(m_sample_rate, m_device_sample_rate);
}
RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
@@ -81,7 +77,6 @@ RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_i
RefPtr<Buffer> buffer = Buffer::from_pcm_data(
sample_data.bytes(),
- *m_resampler,
m_num_channels,
m_sample_format);
diff --git a/Userland/Libraries/LibAudio/WavLoader.h b/Userland/Libraries/LibAudio/WavLoader.h
index edef9ed1d3..832d4066d7 100644
--- a/Userland/Libraries/LibAudio/WavLoader.h
+++ b/Userland/Libraries/LibAudio/WavLoader.h
@@ -67,19 +67,11 @@ private:
AK::InputMemoryStream* m_memory_stream;
String m_error_string;
- // TODO: We should probably move resampling into the audio server.
- //
- // It would avoid duplicate resampling code and would allow clients
- // to be agnostic of the destination audio device's sample rate.
- OwnPtr<ResampleHelper<double>> m_resampler;
-
u32 m_sample_rate { 0 };
u16 m_num_channels { 0 };
PcmSampleFormat m_sample_format;
size_t m_byte_offset_of_data_samples { 0 };
- // FIXME: Get this value from the audio server
- int m_device_sample_rate { 44100 };
int m_loaded_samples { 0 };
int m_total_samples { 0 };
};