Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard logic rework #1

Merged
merged 14 commits into from
Apr 21, 2023
Merged

Keyboard logic rework #1

merged 14 commits into from
Apr 21, 2023

Conversation

Mangoshi
Copy link
Owner

@Mangoshi Mangoshi commented Apr 21, 2023

Why?

The previous logic was full of bugs 🐛🐛🐛
The most annoying of these was "sticky" keys, where notes would stay triggered when octave/detune was change while playing.
This was due to the webaudio-controls library event handling, as well as the nature of focus in the browser.
So I needed to create my own global keyboard listening instead, bypassing the native logic.

New Logic

Each input type now uses the same logic to cut down on duplicate code.
Each key on the computer keyboard acts like a MIDI keyboard key.
Keyboard listening is now active no matter what is focused on the page (except text inputs).
Octave/detune controls now update notes in real-time, no more sticky keys! 😄
Note: The webaudio-controls library had to be modified directly to deactivate the native keyboard logic.

Global Octave Modifier

MS24 now includes a global octave slider, positioned beside the GUI keyboard.
This increases/decreases all octaves at once, allowing for 4 extra octaves to be reached.

Arpeggiator

Arpeggiator functionality is now enabled!
This is done with three Tone.js Pattern Objects, one per synth.
GUI now includes switches for each oscillator's arpeggiator, as well as sliders for arp controls
Tone.Transport is now in use so that the arpeggiators have a timeline to play along.
GUI now includes a BPM knob beside the master gain knob, to adjust the Transport BPM.

Extras / Other

Loads of code cleaned up & rearranged.
Lots of comments added/updated.
GUI keyboard keys increased from 49 (4 octaves + 1 semitone) to 61 (5 octaves + 1 semitone).
Fixed double keyDown bug.

Known Bugs 🐛

While holding keys, if the octave is changed while the arp is off, and then arp is turned on, the arp will play the old octave.

# Why?
- The previous logic was full of bugs 🐛🐛
- Most annoying of these was "sticky" keys
- This was due to the event handling of web-audio-controls
- As well as the nature of the focus event..
- So I needed to create global keyboard listening instead

# New Logic
- Computer keyboard now emulates MIDI
- Each key is handled the same way
- Works no matter what is focused on the page
- Octave/detune controls now update notes in real-time
- This fixes the sticky note bug! 😄

# Global Octave Modifier
- GUI now includes global octave slider
- This increases/decreases all octaves
- Allowing for 4 extra octaves to be reached

# Arpeggiator
- Now 3xTone.Patterns, one per synth
- GUI now includes ARP switches per-oscillator
- As well as sliders for ARP controls
- Tone.Transport now in use (no BPM adjust yet)

# Extras / Other
- TODO list updated
- Code cleaned up & rearranged
- SYNTH Object created for all stateful data
- PRESET Object updated with new data
- Key maps updated to C3 root note
- Arpeggiator key maps added

# Known Bugs 🐛
- Changing note modifiers with arpeggiator enabled
- Preset loading doesn't work with arpeggiator or global octave
- Activating old logic with mouse duplicates keys
- Switching octaves while playing doesn't update GUI keys
- Arpeggiator doesn't update GUI keys
- Input boxes can't be typed in (likely due to preventDefault)
- Large arp patterns can cause glitches
- Quickly inputting Keydown-A->Keydown-B->Keyup-A glitches, causing a phantom Keydown-B to fire, which triggers an extra voice
# Apreggiator Bug
- Changing note modifier while arp was enabled wasn't handled correctly
- Now determines target arpeggiator & its state
- Before running a minified startArp() and stopArp()
- Allowing for note modification during arp playback

# Extra
- New stopArp() logic reduces possibility of sticky bugs

# Known Bugs
- Changing note modifiers per-oscillator can cause that arpeggiator to desync if playing
- Input boxes stopped working after new key logic was implemented
- This was mainly due to preventDefault being applied to all key events
- Another issue was notes triggering while typing in input boxes
- Keyboard event listening is now disabled while inputs are focused
# Logic
- Removed old GUI keyboard event listening logic
- Replaced with slightly modified handleNote trigger
- Over 100 lines of code reduced to three!

# Variables
- physicalKeyboardActive moved to SYNTH.STATE
- octave / subOctave maps incremented by one

# Other
- Code rearranged
- Old comments updated
- New comments added
- Now using local script file instead of CDN URL
- This is so I can edit the library to fix a bug
- Commented-out all relevant "keydown" event handling
- This disables native keyboard events
- Allowing my new logic to run without any conflicts
- This new control adjusts the Tone.Transport BPM
- Allowing for the arpeggiator's base timing to be adjusted
# Extra / Other
- Added/updated comments
- Mostly fixed newly introduced BPM change bugs
- These caused arpeggiators to break if changing while playing
- Seems to work fine now, except for when double-key bug occurs
- This is due to keysHeld ending up with an incorrect value
- Started fixing, hence new lastEvents array
- Will try to fix this for next commit, as it is the last major key bug
- It couldn't be bundled as it wasn't a module
- So it had to be moved to /public
# The bug:
- The sequence: keyDown_a->keyDown_b->keyUp_a
- Would fire a phantom keyDown_b while held
- Leading to two tones being audible instead of one
- As well as bugs with keysHeld Tone.Transport logic

# The fix:
- Store last keyDown events in an array
- Only fire keyDown logic if key does not exist in array
- The event bug definitely still happens, but it doesn't affect the app anymore
- Renamed SYNTH.STATE.p5_enabled to SYNTH.STATE.enabled
- Now only handles notes when synth state is enabled
- Deactivates when returning to welcome screen
- Then activates again without duplicating the p5 instance
- Now seamlessly switches between arp on/off states

# Known bugs:
- Aggressively toggling arp/osc switches while playing causes note duplication
- Arrays look clean so it must be duplicate triggers
- Will look into it!
- 65-key isn't a very common keyboard size... Oops!
- 61-key is the usual size-up from a 49-key!
@vercel
Copy link

vercel bot commented Apr 21, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ms24 ✅ Ready (Inspect) Visit Preview Apr 21, 2023 7:10pm

@Mangoshi Mangoshi added the enhancement New feature or request label Apr 21, 2023
@Mangoshi Mangoshi self-assigned this Apr 21, 2023
@Mangoshi Mangoshi merged commit 7a4f7d3 into master Apr 21, 2023
@Mangoshi Mangoshi deleted the keyboard_logic_rework branch April 21, 2023 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant