summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDSP/Keyboard.cpp
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-05-13 10:13:14 +0200
committerLinus Groh <mail@linusgroh.de>2022-05-26 10:24:43 +0100
commit37b340a6984c7f76d0a26319a1f164b82a7de037 (patch)
treee482e2c2b24902debcfedcc37d5739a6b58dfeef /Userland/Libraries/LibDSP/Keyboard.cpp
parenta54b68114940d070e309c0b6c0e0691fec4c4cb3 (diff)
downloadserenity-37b340a6984c7f76d0a26319a1f164b82a7de037.zip
LibDSP: Introduce the Keyboard
This is a class for handling user MIDI input, which is combined by the Track with roll note data if applicable.
Diffstat (limited to 'Userland/Libraries/LibDSP/Keyboard.cpp')
-rw-r--r--Userland/Libraries/LibDSP/Keyboard.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/Userland/Libraries/LibDSP/Keyboard.cpp b/Userland/Libraries/LibDSP/Keyboard.cpp
new file mode 100644
index 0000000000..38483243a2
--- /dev/null
+++ b/Userland/Libraries/LibDSP/Keyboard.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "Keyboard.h"
+#include "Music.h"
+#include <AK/Error.h>
+#include <AK/NumericLimits.h>
+
+namespace LibDSP {
+
+void Keyboard::set_keyboard_note(u8 pitch, Keyboard::Switch note_switch)
+{
+ VERIFY(pitch < note_frequencies.size());
+
+ if (note_switch == Switch::Off) {
+ m_pressed_notes.remove(pitch);
+ return;
+ }
+
+ auto fake_note = RollNote {
+ .on_sample = m_transport->time(),
+ .off_sample = NumericLimits<u32>::max(),
+ .pitch = pitch,
+ .velocity = NumericLimits<i8>::max(),
+ };
+
+ m_pressed_notes.set(pitch, fake_note);
+}
+void Keyboard::set_keyboard_note_in_active_octave(i8 octave_offset, Switch note_switch)
+{
+ u8 real_note = octave_offset + (m_virtual_keyboard_octave - 1) * notes_per_octave;
+ set_keyboard_note(real_note, note_switch);
+}
+
+void Keyboard::change_virtual_keyboard_octave(Direction direction)
+{
+ if (direction == Direction::Up) {
+ if (m_virtual_keyboard_octave < octave_max)
+ ++m_virtual_keyboard_octave;
+ } else {
+ if (m_virtual_keyboard_octave > octave_min)
+ --m_virtual_keyboard_octave;
+ }
+}
+
+ErrorOr<void> Keyboard::set_virtual_keyboard_octave(u8 octave)
+{
+ if (octave <= octave_max && octave >= octave_min) {
+ m_virtual_keyboard_octave = octave;
+ return {};
+ }
+ return Error::from_string_literal("Octave out of range");
+}
+
+bool Keyboard::is_pressed(u8 pitch) const
+{
+ return m_pressed_notes.get(pitch).has_value() && m_pressed_notes.get(pitch)->is_playing(m_transport->time());
+}
+
+bool Keyboard::is_pressed_in_active_octave(i8 octave_offset) const
+{
+ return is_pressed(octave_offset + virtual_keyboard_octave_base());
+}
+
+}