mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:37:35 +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:
parent
a861d2b728
commit
aea8a040b3
11 changed files with 83 additions and 132 deletions
|
@ -7,53 +7,32 @@
|
|||
*/
|
||||
|
||||
#include "KeysWidget.h"
|
||||
#include "LibDSP/Keyboard.h"
|
||||
#include "TrackManager.h"
|
||||
#include <AK/Array.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
|
||||
KeysWidget::KeysWidget(TrackManager& track_manager)
|
||||
: m_track_manager(track_manager)
|
||||
KeysWidget::KeysWidget(NonnullRefPtr<LibDSP::Keyboard> keyboard)
|
||||
: m_keyboard(move(keyboard))
|
||||
{
|
||||
set_fill_with_background_color(true);
|
||||
}
|
||||
|
||||
int KeysWidget::mouse_note() const
|
||||
{
|
||||
if (m_mouse_down && m_mouse_note + m_track_manager.octave_base() < note_count)
|
||||
if (m_mouse_down && m_mouse_note + m_keyboard->virtual_keyboard_octave_base() < note_count)
|
||||
return m_mouse_note; // Can be -1.
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void KeysWidget::set_key(int key, Switch switch_key)
|
||||
void KeysWidget::set_key(i8 key, LibDSP::Keyboard::Switch switch_note)
|
||||
{
|
||||
if (key == -1 || key + m_track_manager.octave_base() >= note_count)
|
||||
return;
|
||||
|
||||
if (switch_key == On) {
|
||||
++m_key_on[key];
|
||||
} else {
|
||||
if (m_key_on[key] >= 1)
|
||||
--m_key_on[key];
|
||||
}
|
||||
VERIFY(m_key_on[key] <= 2);
|
||||
|
||||
m_track_manager.set_keyboard_note(key + m_track_manager.octave_base(), switch_key);
|
||||
m_keyboard->set_keyboard_note_in_active_octave(key, switch_note);
|
||||
}
|
||||
|
||||
bool KeysWidget::note_is_set(int note) const
|
||||
{
|
||||
if (note < m_track_manager.octave_base())
|
||||
return false;
|
||||
|
||||
if (note >= m_track_manager.octave_base() + note_count)
|
||||
return false;
|
||||
|
||||
return m_key_on[note - m_track_manager.octave_base()] != 0;
|
||||
}
|
||||
|
||||
int KeysWidget::key_code_to_key(int key_code) const
|
||||
i8 KeysWidget::key_code_to_key(int key_code)
|
||||
{
|
||||
switch (key_code) {
|
||||
case Key_A:
|
||||
|
@ -170,7 +149,7 @@ void KeysWidget::paint_event(GUI::PaintEvent& event)
|
|||
int i = 0;
|
||||
for (;;) {
|
||||
Gfx::IntRect rect(x, 0, white_key_width, frame_inner_rect().height());
|
||||
painter.fill_rect(rect, m_key_on[note] ? note_pressed_color : Color::White);
|
||||
painter.fill_rect(rect, m_keyboard->is_pressed_in_active_octave(note) ? note_pressed_color : Color::White);
|
||||
painter.draw_rect(rect, Color::Black);
|
||||
if (i < white_key_labels_count) {
|
||||
rect.set_height(rect.height() * 1.5);
|
||||
|
@ -181,7 +160,7 @@ void KeysWidget::paint_event(GUI::PaintEvent& event)
|
|||
x += white_key_width;
|
||||
++i;
|
||||
|
||||
if (note + m_track_manager.octave_base() >= note_count)
|
||||
if (note + m_keyboard->virtual_keyboard_octave_base() >= note_count)
|
||||
break;
|
||||
if (x >= frame_inner_rect().width())
|
||||
break;
|
||||
|
@ -192,7 +171,7 @@ void KeysWidget::paint_event(GUI::PaintEvent& event)
|
|||
i = 0;
|
||||
for (;;) {
|
||||
Gfx::IntRect rect(x, 0, black_key_width, black_key_height);
|
||||
painter.fill_rect(rect, m_key_on[note] ? note_pressed_color : Color::Black);
|
||||
painter.fill_rect(rect, m_keyboard->is_pressed_in_active_octave(note) ? note_pressed_color : Color::Black);
|
||||
painter.draw_rect(rect, Color::Black);
|
||||
if (i < black_key_labels_count) {
|
||||
rect.set_height(rect.height() * 1.5);
|
||||
|
@ -203,7 +182,7 @@ void KeysWidget::paint_event(GUI::PaintEvent& event)
|
|||
x += black_key_offsets[i % black_keys_per_octave];
|
||||
++i;
|
||||
|
||||
if (note + m_track_manager.octave_base() >= note_count)
|
||||
if (note + m_keyboard->virtual_keyboard_octave_base() >= note_count)
|
||||
break;
|
||||
if (x >= frame_inner_rect().width())
|
||||
break;
|
||||
|
@ -274,7 +253,7 @@ void KeysWidget::mousedown_event(GUI::MouseEvent& event)
|
|||
|
||||
m_mouse_note = note_for_event_position(event.position());
|
||||
|
||||
set_key(m_mouse_note, On);
|
||||
set_key(m_mouse_note, LibDSP::Keyboard::Switch::On);
|
||||
update();
|
||||
}
|
||||
|
||||
|
@ -285,7 +264,7 @@ void KeysWidget::mouseup_event(GUI::MouseEvent& event)
|
|||
|
||||
m_mouse_down = false;
|
||||
|
||||
set_key(m_mouse_note, Off);
|
||||
set_key(m_mouse_note, LibDSP::Keyboard::Switch::Off);
|
||||
update();
|
||||
}
|
||||
|
||||
|
@ -299,8 +278,8 @@ void KeysWidget::mousemove_event(GUI::MouseEvent& event)
|
|||
if (m_mouse_note == new_mouse_note)
|
||||
return;
|
||||
|
||||
set_key(m_mouse_note, Off);
|
||||
set_key(new_mouse_note, On);
|
||||
set_key(m_mouse_note, LibDSP::Keyboard::Switch::Off);
|
||||
set_key(new_mouse_note, LibDSP::Keyboard::Switch::On);
|
||||
update();
|
||||
|
||||
m_mouse_note = new_mouse_note;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue