1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:57:44 +00:00

LibDSP: Improve const correctness

This commit is contained in:
kleines Filmröllchen 2022-05-11 21:49:41 +02:00 committed by Linus Groh
parent 4a6ebb8beb
commit bcb331b862
8 changed files with 23 additions and 22 deletions

View file

@ -47,7 +47,7 @@ class NoteClip final : public Clip {
public: public:
void set_note(RollNote note); void set_note(RollNote note);
Array<SinglyLinkedList<RollNote>, note_count>& notes() { return m_notes; } Array<SinglyLinkedList<RollNote>, note_count> const& notes() const { return m_notes; }
private: private:
Array<SinglyLinkedList<RollNote>, note_count> m_notes; Array<SinglyLinkedList<RollNote>, note_count> m_notes;

View file

@ -15,10 +15,9 @@
namespace LibDSP { namespace LibDSP {
// FIXME: Audio::Frame is 64-bit float, which is quite large for long clips.
using Sample = Audio::Sample; using Sample = Audio::Sample;
Sample const SAMPLE_OFF = { 0.0, 0.0 }; constexpr Sample const SAMPLE_OFF = { 0.0, 0.0 };
struct RollNote { struct RollNote {
constexpr u32 length() const { return (off_sample - on_sample) + 1; } constexpr u32 length() const { return (off_sample - on_sample) + 1; }
@ -28,7 +27,7 @@ struct RollNote {
u8 pitch; u8 pitch;
i8 velocity; i8 velocity;
Envelope to_envelope(u32 time, u32 attack_samples, u32 decay_samples, u32 release_samples) constexpr Envelope to_envelope(u32 time, u32 attack_samples, u32 decay_samples, u32 release_samples) const
{ {
i64 time_since_end = static_cast<i64>(time) - static_cast<i64>(off_sample); i64 time_since_end = static_cast<i64>(time) - static_cast<i64>(off_sample);
// We're before the end of this note. // We're before the end of this note.
@ -58,7 +57,7 @@ struct RollNote {
return Envelope::from_release(static_cast<double>(time_since_end) / static_cast<double>(release_samples)); return Envelope::from_release(static_cast<double>(time_since_end) / static_cast<double>(release_samples));
} }
constexpr bool is_playing(u32 time) { return on_sample <= time && time <= off_sample; } constexpr bool is_playing(u32 time) const { return on_sample <= time && time <= off_sample; }
}; };
enum class SignalType : u8 { enum class SignalType : u8 {
@ -178,8 +177,8 @@ constexpr double note_frequencies[] = {
3729.3100921447249, 3729.3100921447249,
3951.0664100489994, 3951.0664100489994,
}; };
constexpr size_t note_count = array_size(note_frequencies); constexpr size_t const note_count = array_size(note_frequencies);
constexpr double middle_c = note_frequencies[36]; constexpr double const middle_c = note_frequencies[36];
} }

View file

@ -34,6 +34,7 @@ public:
SignalType input_type() const { return m_input_type; } SignalType input_type() const { return m_input_type; }
SignalType output_type() const { return m_output_type; } SignalType output_type() const { return m_output_type; }
Vector<ProcessorParameter&>& parameters() { return m_parameters; } Vector<ProcessorParameter&>& parameters() { return m_parameters; }
Vector<ProcessorParameter&> const& parameters() const { return m_parameters; }
private: private:
SignalType const m_input_type; SignalType const m_input_type;

View file

@ -66,7 +66,7 @@ Signal Classic::process_impl(Signal const& input_signal)
} }
// Linear ADSR envelope with no peak adjustment. // Linear ADSR envelope with no peak adjustment.
double Classic::volume_from_envelope(Envelope const& envelope) double Classic::volume_from_envelope(Envelope const& envelope) const
{ {
switch (static_cast<EnvelopeState>(envelope)) { switch (static_cast<EnvelopeState>(envelope)) {
case EnvelopeState::Off: case EnvelopeState::Off:
@ -102,12 +102,12 @@ double Classic::wave_position(u8 note)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
double Classic::samples_per_cycle(u8 note) double Classic::samples_per_cycle(u8 note) const
{ {
return m_transport->sample_rate() / note_frequencies[note]; return m_transport->sample_rate() / note_frequencies[note];
} }
double Classic::sin_position(u8 note) double Classic::sin_position(u8 note) const
{ {
double spc = samples_per_cycle(note); double spc = samples_per_cycle(note);
double cycle_pos = m_transport->time() / spc; double cycle_pos = m_transport->time() / spc;
@ -115,14 +115,14 @@ double Classic::sin_position(u8 note)
} }
// Absolute value of the saw wave "flips" the negative portion into the positive, creating a ramp up and down. // Absolute value of the saw wave "flips" the negative portion into the positive, creating a ramp up and down.
double Classic::triangle_position(u8 note) double Classic::triangle_position(u8 note) const
{ {
double saw = saw_position(note); double saw = saw_position(note);
return AK::fabs(saw) * 2 - 1; return AK::fabs(saw) * 2 - 1;
} }
// The first half of the cycle period is 1, the other half -1. // The first half of the cycle period is 1, the other half -1.
double Classic::square_position(u8 note) double Classic::square_position(u8 note) const
{ {
double spc = samples_per_cycle(note); double spc = samples_per_cycle(note);
double progress = AK::fmod(static_cast<double>(m_transport->time()), spc) / spc; double progress = AK::fmod(static_cast<double>(m_transport->time()), spc) / spc;
@ -130,7 +130,7 @@ double Classic::square_position(u8 note)
} }
// Modulus creates inverse saw, which we need to flip and scale. // Modulus creates inverse saw, which we need to flip and scale.
double Classic::saw_position(u8 note) double Classic::saw_position(u8 note) const
{ {
double spc = samples_per_cycle(note); double spc = samples_per_cycle(note);
double unscaled = spc - AK::fmod(static_cast<double>(m_transport->time()), spc); double unscaled = spc - AK::fmod(static_cast<double>(m_transport->time()), spc);

View file

@ -49,13 +49,13 @@ public:
private: private:
virtual Signal process_impl(Signal const&) override; virtual Signal process_impl(Signal const&) override;
double volume_from_envelope(Envelope const&); double volume_from_envelope(Envelope const&) const;
double wave_position(u8 note); double wave_position(u8 note);
double samples_per_cycle(u8 note); double samples_per_cycle(u8 note) const;
double sin_position(u8 note); double sin_position(u8 note) const;
double triangle_position(u8 note); double triangle_position(u8 note) const;
double square_position(u8 note); double square_position(u8 note) const;
double saw_position(u8 note); double saw_position(u8 note) const;
double noise_position(u8 note); double noise_position(u8 note);
double get_random_from_seed(u64 note); double get_random_from_seed(u64 note);

View file

@ -79,8 +79,8 @@ void NoteTrack::compute_current_clips_signal()
return; return;
// FIXME: performance? // FIXME: performance?
for (auto& note_list : playing_clip->notes()) { for (auto const& note_list : playing_clip->notes()) {
for (auto& note : note_list) { for (auto const& note : note_list) {
if (note.on_sample >= time && note.off_sample >= time) if (note.on_sample >= time && note.off_sample >= time)
break; break;
if (note.on_sample <= time && note.off_sample >= time) if (note.on_sample <= time && note.off_sample >= time)

View file

@ -27,7 +27,7 @@ public:
Sample current_signal(); Sample current_signal();
NonnullRefPtrVector<Processor> const& processor_chain() const { return m_processor_chain; } NonnullRefPtrVector<Processor> const& processor_chain() const { return m_processor_chain; }
NonnullRefPtr<Transport> const transport() const { return m_transport; } NonnullRefPtr<Transport const> transport() const { return m_transport; }
protected: protected:
Track(NonnullRefPtr<Transport> transport) Track(NonnullRefPtr<Transport> transport)

View file

@ -16,6 +16,7 @@ namespace LibDSP {
class Transport final : public RefCounted<Transport> { class Transport final : public RefCounted<Transport> {
public: public:
constexpr u32& time() { return m_time; } constexpr u32& time() { return m_time; }
constexpr u32 time() const { return m_time; }
constexpr u16 beats_per_minute() const { return m_beats_per_minute; } constexpr u16 beats_per_minute() const { return m_beats_per_minute; }
constexpr double current_second() const { return static_cast<double>(m_time) / m_sample_rate; } constexpr double current_second() const { return static_cast<double>(m_time) / m_sample_rate; }
constexpr double samples_per_measure() const { return (1.0 / m_beats_per_minute) * 60.0 * m_sample_rate; } constexpr double samples_per_measure() const { return (1.0 / m_beats_per_minute) * 60.0 * m_sample_rate; }