mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:27:45 +00:00
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.
This commit is contained in:
parent
9880a5c481
commit
d049626f40
12 changed files with 56 additions and 41 deletions
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue