fix: MidiBuffer should work without options. #1062
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Acknowledgment
First of all, I would like to express my gratitude to the maintainers and contributors of abcjs for creating and maintaining such a powerful and versatile library. Your work has made it easier for developers like me to integrate musical notation and playback into web applications.
While exploring the synthesized sound functionality, I encountered a minor issue with the handling of the options parameter in MidiBuffer.init(). This PR is a small attempt to contribute back and improve the library further. Thank you for your continued efforts in maintaining this project and for considering this contribution.
Problem
In the current implementation of
MidiBuffer.init()
, theoptions.options
parameter is set toundefined
by default(reference). However, the code does not properly initialize this parameter, resulting in an error whenMidiBuffer.prime()
attempts to accessself.options.swing
. Specifically, at this line, the code tries to referenceself.options.swing
without handling the case whereself.options
isundefined
.This leads to the following error during playback:
Proposed Solution
Modify the condition at create-synth.js#L318 to safely access
self.options.swing
by using optional chaining (self.options?.swing
). This ensures thatprime()
does not throw an error ifself.options
is undefined.This fix aligns with the existing behavior of
MidiBuffer.init()
and ensures that the function works seamlessly without requiringoptions
to be explicitly defined.Additional Context
If the current behavior is intentional and
options
is expected to be initialized as an empty object when not provided, an alternative approach would be to ensureoptions
is always initialized withinMidiBuffer.init()
.Alternative Fix
This would ensure that
self.options
is always defined, avoiding the need for optional chaining in subsequent references.