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

Piano: Use LibDSP::Keyboard for all keyboard-playing logic

The only major functional change is that the Track now needs to know
whether it's active or not, in order to listen to the keyboard (or not).

There are some bugs exposed/created by this, mainly:
* KeysWidget sometimes shows phantom notes. Those do not actually exist
  as far as debugging has revealed and do not play in the synth.
* The keyboard can lock up Piano when rapidly pressing keys. This
  appears to be a HashMap bug; I invested significant time in bugfixing
  but got nowhere.
This commit is contained in:
kleines Filmröllchen 2022-05-14 01:11:23 +02:00 committed by Linus Groh
parent a861d2b728
commit aea8a040b3
11 changed files with 83 additions and 132 deletions

View file

@ -46,7 +46,7 @@ MainWidget::MainWidget(TrackManager& track_manager, AudioPlayerLoop& loop)
m_keys_and_knobs_container->set_fixed_height(130);
m_keys_and_knobs_container->set_fill_with_background_color(true);
m_keys_widget = m_keys_and_knobs_container->add<KeysWidget>(track_manager);
m_keys_widget = m_keys_and_knobs_container->add<KeysWidget>(track_manager.keyboard());
m_knobs_widget = m_keys_and_knobs_container->add<KnobsWidget>(track_manager, *this);
@ -85,7 +85,7 @@ void MainWidget::keydown_event(GUI::KeyEvent& event)
else
m_keys_pressed[event.key()] = true;
note_key_action(event.key(), On);
note_key_action(event.key(), LibDSP::Keyboard::Switch::On);
special_key_action(event.key());
m_keys_widget->update();
}
@ -94,24 +94,26 @@ void MainWidget::keyup_event(GUI::KeyEvent& event)
{
m_keys_pressed[event.key()] = false;
note_key_action(event.key(), Off);
note_key_action(event.key(), LibDSP::Keyboard::Switch::Off);
m_keys_widget->update();
}
void MainWidget::note_key_action(int key_code, Switch switch_note)
void MainWidget::note_key_action(int key_code, LibDSP::Keyboard::Switch switch_note)
{
int key = m_keys_widget->key_code_to_key(key_code);
m_keys_widget->set_key(key, switch_note);
auto key = m_keys_widget->key_code_to_key(key_code);
if (key == -1)
return;
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(key, switch_note);
}
void MainWidget::special_key_action(int key_code)
{
switch (key_code) {
case Key_Z:
set_octave_and_ensure_note_change(Down);
set_octave_and_ensure_note_change(LibDSP::Keyboard::Direction::Down);
break;
case Key_X:
set_octave_and_ensure_note_change(Up);
set_octave_and_ensure_note_change(LibDSP::Keyboard::Direction::Up);
break;
case Key_C:
m_knobs_widget->cycle_waveform();
@ -124,36 +126,38 @@ void MainWidget::special_key_action(int key_code)
void MainWidget::turn_off_pressed_keys()
{
m_keys_widget->set_key(m_keys_widget->mouse_note(), Off);
if (m_keys_widget->mouse_note() != -1)
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(m_keys_widget->mouse_note(), LibDSP::Keyboard::Switch::Off);
for (int i = 0; i < key_code_count; ++i) {
if (m_keys_pressed[i])
note_key_action(i, Off);
note_key_action(i, LibDSP::Keyboard::Switch::Off);
}
}
void MainWidget::turn_on_pressed_keys()
{
m_keys_widget->set_key(m_keys_widget->mouse_note(), On);
if (m_keys_widget->mouse_note() != -1)
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(m_keys_widget->mouse_note(), LibDSP::Keyboard::Switch::On);
for (int i = 0; i < key_code_count; ++i) {
if (m_keys_pressed[i])
note_key_action(i, On);
note_key_action(i, LibDSP::Keyboard::Switch::On);
}
}
void MainWidget::set_octave_and_ensure_note_change(int octave)
{
turn_off_pressed_keys();
m_track_manager.set_octave(octave);
MUST(m_track_manager.keyboard()->set_virtual_keyboard_octave(octave));
turn_on_pressed_keys();
m_knobs_widget->update_knobs();
m_keys_widget->update();
}
void MainWidget::set_octave_and_ensure_note_change(Direction direction)
void MainWidget::set_octave_and_ensure_note_change(LibDSP::Keyboard::Direction direction)
{
turn_off_pressed_keys();
m_track_manager.set_octave(direction);
m_track_manager.keyboard()->change_virtual_keyboard_octave(direction);
turn_on_pressed_keys();
m_knobs_widget->update_knobs();