View Source Midiex.Message (Midiex v0.1.1)
Conveniences for creating MIDI messages.
about-midi-messages
About MIDI messages
MIDI messages are in the format of:
status_byte + note_number (0-127) + velocity (0-127)
For example, taking the status byte for Note On which in HEX is 0x90
, and the note Middle C which is 60 and a maximum key velocity of 127, the MIDI message in binary format is:
<<0x90, 60, 127>>
midi-message-functions
MIDI message functions
So that you don't have to remember all the MIDI message codes, this library has the following functions to generate messages:
- note_on(note_number, velocity, opts)
- note_off(note_number, velocity, opts)
- polyphonic_aftertouch(note_number, pressure, opts)
- channel_aftertouch(note_number, pressure, opts)
- control_change(control_number, value, opts)
- program_change(program_number, opts)
- pitch_wheel(lsbyte, msbyte, opts)
- sysex - coming soon
Link to this section Summary
Functions
Creates a MIDI CC or 'control change' message.
Returns the MIDI numerical code for a note.
Creates a MIDI note-off message.
Creates a MIDI note-on message.
Change the panoramic (pan) of a channel. This shifts the sound from the left or right ear in when playing stereo. Values below 64 moves the sound to the left, and above to the right.
Creates a pitch bend message, representing a change in pitch.
Creates a polyphonic aftertouch message.
Creates a program change message, used select the instrument type to play sounds with.
Link to this section Functions
Creates a MIDI CC or 'control change' message.
The following options can be passed:
- value: depends on the control function, but usually is a a number between 0 and 127. See the MIDI 1.0 Control Change Messages Spec or consult the MIDI device manual for specific codes an values.
- channel: the MIDI channel to which the message will be sent (there are 16 channels per MIDI device, in the range 0 to 15). By default channel 0 is used.
example
Example
The MIDI CC message of 123
equates to "All Notes Off", thus stopping all notes being played.
# Create a 'all notes off' CC message.
Midiex.control_change(127)
reference
Reference
See the official MIDI 1.0 Control Change Messages Spec.
Returns the MIDI numerical code for a note.
Takes either a string or atom representation of a note as the first parameter.
example
Example
# Return the code for middle-C (also known as C4)
Message.note(:C4)
Message.note("C4")
Message.note(:MiddleC)
Message.note("MiddleC")
# These all return: 60
# Return the code for A-sharp 3
Message.note(:As3)
Message.note("A#3")
# These both return: 58
Creates a MIDI note-off message.
Takes a note as a string (e.g. "C4"), atom (e.g. :C4) or number (e.g. 60) as the first parameter.
The following options can be passed:
- velocity: a number between 0 and 127 representing how hard (or loud) a key was pressed. By defaut 127 is used.
- channel: the MIDI channel to which the message will be sent (there are 16 channels per MIDI device, in the range 0 to 15). By default channel 0 is used.
example
Example
# Note-off for middle-C
Message.note_off(:C4)
# Returns: <<128, 60, 127>>
Creates a MIDI note-on message.
Takes a note as a string (e.g. "C4"), atom (e.g. :C4) or number (e.g. 60) as the first parameter.
The following options can be passed:
- velocity: a number between 0 and 127 representing how hard (or loud) a key was pressed. By defaut 127 is used.
- channel: the MIDI channel to which the message will be sent (there are 16 channels per MIDI device, in the range 0 to 15). By default channel 0 is used.
Note that MIDI channels are in the range 0 - 15. But in MIDI software and hardware it may be offset by +1, so MIDI channel 0 might be called MIDI channel 1 and so on to channel 16.
examples
Examples
# Note-on message for middle-C
Midiex.Message.note_on(:C4)
# Returns: <<144, 60, 127>>
# Note-on message for middle-C on channel 2
Midiex.Message.note_on(:C4, channel: 2)
# Returns: <<146, 60, 127>>
# Note-on message for middle-C on channel 2 with a velocity of 40
Midiex.Message.note_on(:C4, channel: 2, velocity: 40)
# Returns: <<146, 60, 40>>
These can be sent to a connection using Midiex.send_msg/2
, for example:
alias Midiex.Message
Midiex.send_msg(out_conn, Message.note_on(:C4, channel: 2, velocity: 40))
Change the panoramic (pan) of a channel. This shifts the sound from the left or right ear in when playing stereo. Values below 64 moves the sound to the left, and above to the right.
Creates a pitch bend message, representing a change in pitch.
Pitch bend change messages a usually sent from a keyboard with a pitch bend wheel.
A pitch bend message includes two data bytes to specify the pitch bend value. Having two bites allows creates a higher resolution, making the pitch changes smoother.
Creates a polyphonic aftertouch message.
On a keyboard, a polyphonic aftertouch message is sent by pressing down further on a key after it has already reached the bottom. Not all keyboards have aftertouch.
Note: polyphonic_aftertouch
is specific to each key, where as channel_aftertouch
is average amount of pressure applied to whichever keys are held down.
Takes a note as the first parameter and the following options:
- pressure: a number between 0 and 127 representing the pressure on the key. By defaut 127 is used.
- channel: the MIDI channel to which the message will be sent (there are 16 channels per MIDI device, in the range 0 to 15). By default channel 0 is used.
example
Example
alias Midiex.Message
# Create a series of aftertouch messages from 0 to 127 for the note middle-C (C4)
0..127//1
|> Enum.map(fn pressure -> Message.polyphonic_aftertouch(:C4, pressure: pressure) end)
Creates a program change message, used select the instrument type to play sounds with.