Vital
Loading...
Searching...
No Matches
synth_computer_keyboard.cpp
Go to the documentation of this file.
2
3#include "synth_constants.h"
4#include "sound_engine.h"
5
7 MidiKeyboardState* keyboard_state) {
8 synth_ = synth;
9 keyboard_state_ = keyboard_state;
10 computer_keyboard_offset_ = vital::kDefaultKeyboardOffset;
14}
15
18
20 // Turn off all currently active notes at the old offset
21 for (int i = 0; i < layout_.length(); ++i) {
22 int note = computer_keyboard_offset_ + i;
23 keyboard_state_->noteOff(kKeyboardMidiChannel, note, 0.5f);
24 keys_pressed_.erase(layout_[i]);
25 }
26
27 // Clamp the new offset to a valid MIDI note range
29 computer_keyboard_offset_ = vital::utils::iclamp(new_offset, 0, max);
30}
31
32bool SynthComputerKeyboard::keyPressed(const KeyPress &key, Component *origin) {
33 // This method doesn't handle note-on logic directly, it's handled in keyStateChanged.
34 // Return false to indicate the event was not consumed here.
35 return false;
36}
37
38bool SynthComputerKeyboard::keyStateChanged(bool isKeyDown, Component *origin) {
39 bool consumed = false;
40 // Check through each character in the keyboard layout
41 for (int i = 0; i < layout_.length(); ++i) {
42 int note = computer_keyboard_offset_ + i;
43 ModifierKeys modifiers = ModifierKeys::getCurrentModifiersRealtime();
44
45 // If a key mapped to a note is currently down and wasn't previously registered as pressed:
46 if (KeyPress::isKeyCurrentlyDown(layout_[i]) &&
47 !keys_pressed_.count(layout_[i]) && isKeyDown && !modifiers.isCommandDown()) {
48 keys_pressed_.insert(layout_[i]);
49 keyboard_state_->noteOn(kKeyboardMidiChannel, note, 1.0f);
50 }
51 // If a key mapped to a note was previously pressed but is now released:
52 else if (!KeyPress::isKeyCurrentlyDown(layout_[i]) && keys_pressed_.count(layout_[i])) {
53 keys_pressed_.erase(layout_[i]);
54 keyboard_state_->noteOff(kKeyboardMidiChannel, note, 0.5f);
55 }
56 consumed = true;
57 }
58
59 // Handle octave down key
60 if (KeyPress::isKeyCurrentlyDown(down_key_)) {
61 if (!keys_pressed_.count(down_key_)) {
62 keys_pressed_.insert(down_key_);
63 changeKeyboardOffset(computer_keyboard_offset_ - vital::kNotesPerOctave);
64 consumed = true;
65 }
66 }
67 else
68 keys_pressed_.erase(down_key_);
69
70 // Handle octave up key
71 if (KeyPress::isKeyCurrentlyDown(up_key_)) {
72 if (!keys_pressed_.count(up_key_)) {
73 keys_pressed_.insert(up_key_);
74 changeKeyboardOffset(computer_keyboard_offset_ + vital::kNotesPerOctave);
75 consumed = true;
76 }
77 }
78 else
79 keys_pressed_.erase(up_key_);
80
81 // Handle space key to resync synth time
82 if (KeyPress::isKeyCurrentlyDown(KeyPress::spaceKey)) {
83 if (!keys_pressed_.count(' ')) {
84 keys_pressed_.insert(' ');
85 synth_->correctToTime(0.0);
86 consumed = true;
87 }
88 }
89 else
90 keys_pressed_.erase(' ');
91
92 return consumed;
93}
bool keyStateChanged(bool isKeyDown, Component *origin) override
Called when a key state changes (pressed or released).
Definition synth_computer_keyboard.cpp:38
bool keyPressed(const KeyPress &key, Component *origin) override
Called when a key is pressed.
Definition synth_computer_keyboard.cpp:32
void changeKeyboardOffset(int new_offset)
Changes the base offset of the computer keyboard notes.
Definition synth_computer_keyboard.cpp:19
static constexpr int kKeyboardMidiChannel
The MIDI channel used for the computer keyboard input.
Definition synth_computer_keyboard.h:26
~SynthComputerKeyboard()
Destructor. Cleans up any resources.
Definition synth_computer_keyboard.cpp:16
SynthComputerKeyboard()=delete
Deleted default constructor to ensure that a SoundEngine and MidiKeyboardState are provided.
Core class responsible for handling note events, oversampling, and the main effects chain.
Definition sound_engine.h:33
void correctToTime(double seconds) override
Synchronizes all time-based modules to a given time, e.g. for timeline-based automation.
Definition sound_engine.cpp:314
int up_key_
The key code (wchar_t) for octave up.
Definition synth_types.h:185
std::wstring layout_
The mapping of keys to notes.
Definition synth_types.h:184
int down_key_
The key code (wchar_t) for octave down.
Definition synth_types.h:186
force_inline int iclamp(int value, int min_val, int max_val)
Clamps an integer between [min_val, max_val].
Definition utils.h:250
constexpr wchar_t kDefaultKeyboardOctaveUp
Default key for octave-up action in the computer keyboard layout.
Definition synth_constants.h:76
constexpr int kNotesPerOctave
Number of semitones per octave.
Definition common.h:51
const std::wstring kDefaultKeyboard
The default keyboard layout (QWERTY-based) mapping keys to notes.
Definition synth_constants.h:82
constexpr int kDefaultKeyboardOffset
Default starting octave offset for the computer keyboard layout.
Definition synth_constants.h:73
constexpr int kMidiSize
MIDI note count (0-127).
Definition common.h:44
constexpr wchar_t kDefaultKeyboardOctaveDown
Default key for octave-down action in the computer keyboard layout.
Definition synth_constants.h:79