1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

Piano: Use a real transport in the TrackManager

This is technically only a stepping stone but needed to happen at some
point anyways. Now, there's no more integer time stored in Piano's
legacy datastructures directly.
This commit is contained in:
kleines Filmröllchen 2022-05-13 23:32:44 +02:00 committed by Linus Groh
parent 37b340a698
commit 7e04560af4
6 changed files with 29 additions and 28 deletions

View file

@ -64,7 +64,7 @@ void AudioPlayerLoop::enqueue_audio()
do { do {
m_track_manager.fill_buffer(m_buffer); m_track_manager.fill_buffer(m_buffer);
m_wav_writer.write_samples(reinterpret_cast<u8*>(m_buffer.data()), buffer_size); m_wav_writer.write_samples(reinterpret_cast<u8*>(m_buffer.data()), buffer_size);
} while (m_track_manager.time()); } while (m_track_manager.transport()->time());
m_track_manager.reset(); m_track_manager.reset();
m_track_manager.set_should_loop(true); m_track_manager.set_should_loop(true);
m_wav_writer.finalize(); m_wav_writer.finalize();

View file

@ -160,7 +160,7 @@ void RollWidget::paint_event(GUI::PaintEvent& event)
painter.draw_text(note_name_rect, String::formatted("{}", note / notes_per_octave + 1), Gfx::TextAlignment::CenterLeft); painter.draw_text(note_name_rect, String::formatted("{}", note / notes_per_octave + 1), Gfx::TextAlignment::CenterLeft);
} }
int x = m_roll_width * (static_cast<double>(m_track_manager.time()) / roll_length); int x = m_roll_width * (static_cast<double>(m_track_manager.transport()->time()) / roll_length);
if (x > x_offset && x <= x_offset + widget_inner_rect().width()) if (x > x_offset && x <= x_offset + widget_inner_rect().width())
painter.draw_line({ x, 0 }, { x, roll_height }, Gfx::Color::Black); painter.draw_line({ x, 0 }, { x, roll_height }, Gfx::Color::Black);

View file

@ -16,29 +16,26 @@
#include <LibDSP/Music.h> #include <LibDSP/Music.h>
#include <math.h> #include <math.h>
Track::Track(u32 const& time) Track::Track(NonnullRefPtr<LibDSP::Transport> transport)
: m_time(time) : m_transport(move(transport))
, m_temporary_transport(make_ref_counted<LibDSP::Transport>(120, 4)) , m_delay(make_ref_counted<LibDSP::Effects::Delay>(m_transport))
, m_delay(make_ref_counted<LibDSP::Effects::Delay>(m_temporary_transport)) , m_synth(make_ref_counted<LibDSP::Synthesizers::Classic>(m_transport))
, m_synth(make_ref_counted<LibDSP::Synthesizers::Classic>(m_temporary_transport))
{ {
set_volume(volume_max); set_volume(volume_max);
} }
void Track::fill_sample(Sample& sample) void Track::fill_sample(Sample& sample)
{ {
m_temporary_transport->set_time(m_time);
auto playing_notes = LibDSP::RollNotes {}; auto playing_notes = LibDSP::RollNotes {};
for (size_t i = 0; i < note_count; ++i) { for (size_t i = 0; i < note_count; ++i) {
auto& notes_at_pitch = m_roll_notes[i]; auto& notes_at_pitch = m_roll_notes[i];
for (auto& note : notes_at_pitch) { for (auto& note : notes_at_pitch) {
if (note.is_playing(m_time)) if (note.is_playing(m_transport->time()))
playing_notes.set(i, note); playing_notes.set(i, note);
} }
auto& key_at_pitch = m_keyboard_notes[i]; auto& key_at_pitch = m_keyboard_notes[i];
if (key_at_pitch.has_value() && key_at_pitch.value().is_playing(m_time)) if (key_at_pitch.has_value() && key_at_pitch.value().is_playing(m_transport->time()))
playing_notes.set(i, key_at_pitch.value()); playing_notes.set(i, key_at_pitch.value());
// No need to keep non-playing keyboard notes around. // No need to keep non-playing keyboard notes around.
else else
@ -70,7 +67,7 @@ void Track::reset()
void Track::sync_roll(int note) void Track::sync_roll(int note)
{ {
auto it = m_roll_notes[note].find_if([&](auto& roll_note) { return roll_note.off_sample > m_time; }); auto it = m_roll_notes[note].find_if([&](auto& roll_note) { return roll_note.off_sample > m_transport->time(); });
if (it.is_end()) if (it.is_end())
m_roll_iterators[note] = m_roll_notes[note].begin(); m_roll_iterators[note] = m_roll_notes[note].begin();
else else
@ -115,15 +112,15 @@ void Track::set_keyboard_note(int note, Switch state)
// If the note is playing, we need to start releasing it, otherwise just delete // If the note is playing, we need to start releasing it, otherwise just delete
if (auto& maybe_roll_note = m_keyboard_notes[note]; maybe_roll_note.has_value()) { if (auto& maybe_roll_note = m_keyboard_notes[note]; maybe_roll_note.has_value()) {
auto& roll_note = maybe_roll_note.value(); auto& roll_note = maybe_roll_note.value();
if (roll_note.is_playing(m_time)) if (roll_note.is_playing(m_transport->time()))
roll_note.off_sample = m_time; roll_note.off_sample = m_transport->time();
else else
m_keyboard_notes[note] = {}; m_keyboard_notes[note] = {};
} }
} else } else
// FIXME: The end time needs to be far in the future. // FIXME: The end time needs to be far in the future.
m_keyboard_notes[note] m_keyboard_notes[note]
= RollNote { m_time, m_time + static_cast<u32>(sample_rate) * 10'000, static_cast<u8>(note), 0 }; = RollNote { m_transport->time(), m_transport->time() + static_cast<u32>(sample_rate) * 10'000, static_cast<u8>(note), 0 };
} }
void Track::set_volume(int volume) void Track::set_volume(int volume)

View file

@ -16,6 +16,7 @@
#include <LibDSP/Effects.h> #include <LibDSP/Effects.h>
#include <LibDSP/Music.h> #include <LibDSP/Music.h>
#include <LibDSP/Synthesizers.h> #include <LibDSP/Synthesizers.h>
#include <LibDSP/Transport.h>
using LibDSP::RollNote; using LibDSP::RollNote;
using RollIter = AK::SinglyLinkedListIterator<SinglyLinkedList<RollNote>, RollNote>; using RollIter = AK::SinglyLinkedListIterator<SinglyLinkedList<RollNote>, RollNote>;
@ -25,7 +26,7 @@ class Track {
AK_MAKE_NONMOVABLE(Track); AK_MAKE_NONMOVABLE(Track);
public: public:
explicit Track(u32 const& time); Track(NonnullRefPtr<LibDSP::Transport>);
~Track() = default; ~Track() = default;
Vector<Audio::Sample> const& recorded_sample() const { return m_recorded_sample; } Vector<Audio::Sample> const& recorded_sample() const { return m_recorded_sample; }
@ -50,9 +51,7 @@ private:
int m_volume; int m_volume;
u32 const& m_time; NonnullRefPtr<LibDSP::Transport> m_transport;
NonnullRefPtr<LibDSP::Transport> m_temporary_transport;
NonnullRefPtr<LibDSP::Effects::Delay> m_delay; NonnullRefPtr<LibDSP::Effects::Delay> m_delay;
NonnullRefPtr<LibDSP::Synthesizers::Classic> m_synth; NonnullRefPtr<LibDSP::Synthesizers::Classic> m_synth;

View file

@ -9,20 +9,22 @@
#include "TrackManager.h" #include "TrackManager.h"
#include "Applications/Piano/Music.h" #include "Applications/Piano/Music.h"
#include <AK/NonnullRefPtr.h>
TrackManager::TrackManager() TrackManager::TrackManager()
: m_transport(make_ref_counted<LibDSP::Transport>(120, 4))
{ {
add_track(); add_track();
} }
void TrackManager::time_forward(int amount) void TrackManager::time_forward(int amount)
{ {
int new_value = (static_cast<int>(m_time) + amount) % roll_length; int new_value = (static_cast<int>(m_transport->time()) + amount) % roll_length;
if (new_value < 0) { // If the new time value is negative add roll_length to wrap around if (new_value < 0) { // If the new time value is negative add roll_length to wrap around
m_time = roll_length + new_value; m_transport->set_time(roll_length + new_value);
} else { } else {
m_time = new_value; m_transport->set_time(new_value);
} }
} }
@ -34,8 +36,10 @@ void TrackManager::fill_buffer(Span<Sample> buffer)
for (auto& track : m_tracks) for (auto& track : m_tracks)
track->fill_sample(buffer[i]); track->fill_sample(buffer[i]);
if (++m_time >= roll_length) { m_transport->set_time(m_transport->time() + 1);
m_time = 0; // FIXME: This should be handled automatically by Transport.
if (m_transport->time() >= roll_length) {
m_transport->set_time(0);
if (!m_should_loop) if (!m_should_loop)
break; break;
} }
@ -53,7 +57,7 @@ void TrackManager::reset()
m_current_front_buffer = m_front_buffer.span(); m_current_front_buffer = m_front_buffer.span();
m_current_back_buffer = m_back_buffer.span(); m_current_back_buffer = m_back_buffer.span();
m_time = 0; m_transport->set_time(0);
for (auto& track : m_tracks) for (auto& track : m_tracks)
track->reset(); track->reset();
@ -84,7 +88,7 @@ void TrackManager::set_octave(int octave)
void TrackManager::add_track() void TrackManager::add_track()
{ {
m_tracks.append(make<Track>(m_time)); m_tracks.append(make<Track>(m_transport));
} }
int TrackManager::next_track_index() int TrackManager::next_track_index()

View file

@ -35,7 +35,8 @@ public:
m_current_track = track_index; m_current_track = track_index;
} }
int time() const { return m_time; } NonnullRefPtr<LibDSP::Transport> transport() const { return m_transport; }
// Legacy API, do not add new users.
void time_forward(int amount); void time_forward(int amount);
void fill_buffer(Span<Sample>); void fill_buffer(Span<Sample>);
@ -58,7 +59,7 @@ private:
int m_octave { 4 }; int m_octave { 4 };
u32 m_time { 0 }; NonnullRefPtr<LibDSP::Transport> m_transport;
bool m_should_loop { true }; bool m_should_loop { true };
}; };