1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 14:55:07 +00:00

LibAudio/Piano: Replace floats with doubles

We should default to double-precision so that clients can make the
choice to use float or double.
This commit is contained in:
William McPherson 2020-02-10 23:45:10 +11:00 committed by Andreas Kling
parent d55d2b2794
commit aa149b9330
5 changed files with 30 additions and 30 deletions

View file

@ -158,10 +158,10 @@ String AudioEngine::set_recorded_sample(const StringView& path)
m_recorded_sample.clear(); m_recorded_sample.clear();
m_recorded_sample.resize(wav_buffer->sample_count()); m_recorded_sample.resize(wav_buffer->sample_count());
float peak = 0; double peak = 0;
for (int i = 0; i < wav_buffer->sample_count(); ++i) { for (int i = 0; i < wav_buffer->sample_count(); ++i) {
float left_abs = fabs(wav_buffer->samples()[i].left); double left_abs = fabs(wav_buffer->samples()[i].left);
float right_abs = fabs(wav_buffer->samples()[i].right); double right_abs = fabs(wav_buffer->samples()[i].right);
if (left_abs > peak) if (left_abs > peak)
peak = left_abs; peak = left_abs;
if (right_abs > peak) if (right_abs > peak)
@ -228,8 +228,8 @@ Audio::Sample AudioEngine::recorded_sample(size_t note)
int t = m_pos[note]; int t = m_pos[note];
if (t >= m_recorded_sample.size()) if (t >= m_recorded_sample.size())
return 0; return 0;
float w_left = m_recorded_sample[t].left; double w_left = m_recorded_sample[t].left;
float w_right = m_recorded_sample[t].right; double w_right = m_recorded_sample[t].right;
if (t + 1 < m_recorded_sample.size()) { if (t + 1 < m_recorded_sample.size()) {
double t_fraction = m_pos[note] - t; double t_fraction = m_pos[note] - t;
w_left += (m_recorded_sample[t + 1].left - m_recorded_sample[t].left) * t_fraction; w_left += (m_recorded_sample[t + 1].left - m_recorded_sample[t].left) * t_fraction;

View file

@ -46,7 +46,7 @@ WaveEditor::~WaveEditor()
{ {
} }
int WaveEditor::sample_to_y(float percentage) const int WaveEditor::sample_to_y(double percentage) const
{ {
double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0); double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height; double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;

View file

@ -45,7 +45,7 @@ private:
virtual void paint_event(GUI::PaintEvent&) override; virtual void paint_event(GUI::PaintEvent&) override;
int sample_to_y(float percentage) const; int sample_to_y(double percentage) const;
AudioEngine& m_audio_engine; AudioEngine& m_audio_engine;
}; };

View file

@ -43,14 +43,14 @@ struct Sample {
} }
// For mono // For mono
Sample(float left) Sample(double left)
: left(left) : left(left)
, right(left) , right(left)
{ {
} }
// For stereo // For stereo
Sample(float left, float right) Sample(double left, double right)
: left(left) : left(left)
, right(right) , right(right)
{ {
@ -71,7 +71,7 @@ struct Sample {
void scale(int percent) void scale(int percent)
{ {
float pct = (float)percent / 100.0; double pct = (double)percent / 100.0;
left *= pct; left *= pct;
right *= pct; right *= pct;
} }
@ -83,8 +83,8 @@ struct Sample {
return *this; return *this;
} }
float left; double left;
float right; double right;
}; };
// Small helper to resample from one playback rate to another // Small helper to resample from one playback rate to another
@ -92,16 +92,16 @@ struct Sample {
// Should do better... // Should do better...
class ResampleHelper { class ResampleHelper {
public: public:
ResampleHelper(float source, float target); ResampleHelper(double source, double target);
void process_sample(float sample_l, float sample_r); void process_sample(double sample_l, double sample_r);
bool read_sample(float& next_l, float& next_r); bool read_sample(double& next_l, double& next_r);
private: private:
const float m_ratio; const double m_ratio;
float m_current_ratio { 0 }; double m_current_ratio { 0 };
float m_last_sample_l { 0 }; double m_last_sample_l { 0 };
float m_last_sample_r { 0 }; double m_last_sample_r { 0 };
}; };
// A buffer of audio samples, normalized to 44100hz. // A buffer of audio samples, normalized to 44100hz.

View file

@ -182,19 +182,19 @@ bool WavLoader::parse_header()
return true; return true;
} }
ResampleHelper::ResampleHelper(float source, float target) ResampleHelper::ResampleHelper(double source, double target)
: m_ratio(source / target) : m_ratio(source / target)
{ {
} }
void ResampleHelper::process_sample(float sample_l, float sample_r) void ResampleHelper::process_sample(double sample_l, double sample_r)
{ {
m_last_sample_l = sample_l; m_last_sample_l = sample_l;
m_last_sample_r = sample_r; m_last_sample_r = sample_r;
m_current_ratio += 1; m_current_ratio += 1;
} }
bool ResampleHelper::read_sample(float& next_l, float& next_r) bool ResampleHelper::read_sample(double& next_l, double& next_r)
{ {
if (m_current_ratio > 0) { if (m_current_ratio > 0) {
m_current_ratio -= m_ratio; m_current_ratio -= m_ratio;
@ -209,8 +209,8 @@ bool ResampleHelper::read_sample(float& next_l, float& next_r)
template<typename SampleReader> template<typename SampleReader>
static void read_samples_from_stream(BufferStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels) static void read_samples_from_stream(BufferStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
{ {
float norm_l = 0; double norm_l = 0;
float norm_r = 0; double norm_r = 0;
switch (num_channels) { switch (num_channels) {
case 1: case 1:
@ -245,7 +245,7 @@ static void read_samples_from_stream(BufferStream& stream, SampleReader read_sam
} }
} }
static float read_norm_sample_24(BufferStream& stream) static double read_norm_sample_24(BufferStream& stream)
{ {
u8 byte = 0; u8 byte = 0;
stream >> byte; stream >> byte;
@ -259,21 +259,21 @@ static float read_norm_sample_24(BufferStream& stream)
value = sample1 << 8; value = sample1 << 8;
value |= (sample2 << 16); value |= (sample2 << 16);
value |= (sample3 << 24); value |= (sample3 << 24);
return float(value) / std::numeric_limits<i32>::max(); return double(value) / std::numeric_limits<i32>::max();
} }
static float read_norm_sample_16(BufferStream& stream) static double read_norm_sample_16(BufferStream& stream)
{ {
i16 sample = 0; i16 sample = 0;
stream >> sample; stream >> sample;
return float(sample) / std::numeric_limits<i16>::max(); return double(sample) / std::numeric_limits<i16>::max();
} }
static float read_norm_sample_8(BufferStream& stream) static double read_norm_sample_8(BufferStream& stream)
{ {
u8 sample = 0; u8 sample = 0;
stream >> sample; stream >> sample;
return float(sample) / std::numeric_limits<u8>::max(); return double(sample) / std::numeric_limits<u8>::max();
} }
// ### can't const this because BufferStream is non-const // ### can't const this because BufferStream is non-const