[Ruby] The road to Midi Mono (and maybe more)
Posted: Fri Oct 18, 2019 2:47 am
I thought, maybe this could be a good idea to have some kind of developer blog. Loose ideas and concepts, babbling while programming, that sort of stuff. However, my health isn't the best, and I can't promise, this series will last until the end, nor how often I'll post. But maybe there's something helpful along the way already, for you guys!
1. Preperation
Before we start with the real thing, we should save us some time with convenience functionality. I already know that I will go crazy when always having to type conditions that will never change. So let's pack them into class methods. The first candidates are "Note On" and "Note Off" messages. Let us define what counts as either of those.
Note On
Let's make use of Ruby's ability to extend a classes content at any time.
This code is written as the style guide recommends. That's why we see no "if...else" here. && means logical and, || means logical or.
We shouldn't think that one method excludes the other! We need both, since a Note On, as defined above will return "false" for anything that is not a note on (sysex, cc, pitch bend, etc.). The same is true for Note Off.
1. Preperation
Before we start with the real thing, we should save us some time with convenience functionality. I already know that I will go crazy when always having to type conditions that will never change. So let's pack them into class methods. The first candidates are "Note On" and "Note Off" messages. Let us define what counts as either of those.
Note On
- - status byte: the status byte must be of value 144
- velocity: this value has to be greater than 0, because many controllers send a 144/0 as a note off equivalent
- - the status byte must be 128, if note off velocity is to be used (does anybody know of a synth that makes use of this information? please tell me!)
- the status byte must be 144 and the velocity 0 to identify as a note off without note off velocity (note that this is not complying to the midi standard that only knows status 128 for note off)
Let's make use of Ruby's ability to extend a classes content at any time.
- Code: Select all
class Midi
## class definition, will write to an existing class of the same name, if exists
def note_on?
## style guide recommends to end a method's name with
## a question mark, if it returns only true or false
return status == 144 && data2 > 0 ? true : false
## just the code representation of what I wrote in the blog.
## status, channel, data1 and data 2 are already existing methods of the Midi class
end
def note_off?
return status == 144 && data2 == 0 || status == 128 ? true: false
end
end
This code is written as the style guide recommends. That's why we see no "if...else" here. && means logical and, || means logical or.
We shouldn't think that one method excludes the other! We need both, since a Note On, as defined above will return "false" for anything that is not a note on (sysex, cc, pitch bend, etc.). The same is true for Note Off.