Module: MIDIEvents::TypeConversion
- Extended by:
- TypeConversion
- Included in:
- TypeConversion
- Defined in:
- lib/midi-events/type_conversion.rb
Overview
Utilities for converting between different MIDI data representations
Provides methods to convert between hex strings, nibbles, and numeric byte arrays. Useful for working with MIDI data in different formats.
Class Method Summary collapse
-
.hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Integer>
Convert an array of hex character nibbles to numeric bytes.
-
.hex_str_to_hex_chars(string) ⇒ Array<String>
Convert a hex string to an array of character nibbles.
-
.hex_string_to_numeric_byte_array(string) ⇒ Array<Integer>
Convert a hex string to an array of numeric bytes.
-
.numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to an uppercase hex string.
-
.numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex character nibbles.
-
.numeric_byte_to_nibbles(num) ⇒ Array<Integer>
Split a numeric byte into its high and low nibbles.
Instance Method Summary collapse
-
#hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Integer>
Convert an array of hex character nibbles to numeric bytes.
-
#hex_str_to_hex_chars(string) ⇒ Array<String>
Convert a hex string to an array of character nibbles.
-
#hex_string_to_numeric_byte_array(string) ⇒ Array<Integer>
Convert a hex string to an array of numeric bytes.
-
#numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to an uppercase hex string.
-
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex character nibbles.
-
#numeric_byte_to_nibbles(num) ⇒ Array<Integer>
Split a numeric byte into its high and low nibbles.
Class Method Details
.hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Integer>
Convert an array of hex character nibbles to numeric bytes
Pairs nibbles together to form bytes. If there's an odd number of nibbles, the second-to-last nibble is removed.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/midi-events/type_conversion.rb', line 32 def hex_chars_to_numeric_byte_array(nibbles) nibbles = nibbles.dup # Don't mess with the input # get rid of last nibble if there's an odd number # it will be processed later anyway nibbles.slice!(nibbles.length-2, 1) if nibbles.length.odd? bytes = [] while !(nibs = nibbles.slice!(0,2)).empty? byte = (nibs[0].hex << 4) + nibs[1].hex bytes << byte end bytes end |
.hex_str_to_hex_chars(string) ⇒ Array<String>
Convert a hex string to an array of character nibbles
70 71 72 |
# File 'lib/midi-events/type_conversion.rb', line 70 def hex_str_to_hex_chars(string) string.split(//) end |
.hex_string_to_numeric_byte_array(string) ⇒ Array<Integer>
Convert a hex string to an array of numeric bytes
53 54 55 56 57 58 59 60 |
# File 'lib/midi-events/type_conversion.rb', line 53 def hex_string_to_numeric_byte_array(string) string = string.dup bytes = [] until string.length == 0 bytes << string.slice!(0, 2).hex end bytes end |
.numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to an uppercase hex string
82 83 84 85 86 87 88 89 |
# File 'lib/midi-events/type_conversion.rb', line 82 def numeric_byte_array_to_hex_string(bytes) string_bytes = bytes.map do |byte| string = byte.to_s(16) string = "0#{string}" if string.length == 1 string end string_bytes.join.upcase end |
.numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex character nibbles
99 100 101 102 |
# File 'lib/midi-events/type_conversion.rb', line 99 def numeric_byte_to_hex_chars(num) nibbles = numeric_byte_to_nibbles(num) nibbles.map { |n| n.to_s(16) } end |
.numeric_byte_to_nibbles(num) ⇒ Array<Integer>
Split a numeric byte into its high and low nibbles
112 113 114 |
# File 'lib/midi-events/type_conversion.rb', line 112 def numeric_byte_to_nibbles(num) [((num & 0xF0) >> 4), (num & 0x0F)] end |
Instance Method Details
#hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Integer>
Convert an array of hex character nibbles to numeric bytes
Pairs nibbles together to form bytes. If there's an odd number of nibbles, the second-to-last nibble is removed.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/midi-events/type_conversion.rb', line 32 def hex_chars_to_numeric_byte_array(nibbles) nibbles = nibbles.dup # Don't mess with the input # get rid of last nibble if there's an odd number # it will be processed later anyway nibbles.slice!(nibbles.length-2, 1) if nibbles.length.odd? bytes = [] while !(nibs = nibbles.slice!(0,2)).empty? byte = (nibs[0].hex << 4) + nibs[1].hex bytes << byte end bytes end |
#hex_str_to_hex_chars(string) ⇒ Array<String>
Convert a hex string to an array of character nibbles
70 71 72 |
# File 'lib/midi-events/type_conversion.rb', line 70 def hex_str_to_hex_chars(string) string.split(//) end |
#hex_string_to_numeric_byte_array(string) ⇒ Array<Integer>
Convert a hex string to an array of numeric bytes
53 54 55 56 57 58 59 60 |
# File 'lib/midi-events/type_conversion.rb', line 53 def hex_string_to_numeric_byte_array(string) string = string.dup bytes = [] until string.length == 0 bytes << string.slice!(0, 2).hex end bytes end |
#numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to an uppercase hex string
82 83 84 85 86 87 88 89 |
# File 'lib/midi-events/type_conversion.rb', line 82 def numeric_byte_array_to_hex_string(bytes) string_bytes = bytes.map do |byte| string = byte.to_s(16) string = "0#{string}" if string.length == 1 string end string_bytes.join.upcase end |
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex character nibbles
99 100 101 102 |
# File 'lib/midi-events/type_conversion.rb', line 99 def numeric_byte_to_hex_chars(num) nibbles = numeric_byte_to_nibbles(num) nibbles.map { |n| n.to_s(16) } end |
#numeric_byte_to_nibbles(num) ⇒ Array<Integer>
Split a numeric byte into its high and low nibbles
112 113 114 |
# File 'lib/midi-events/type_conversion.rb', line 112 def numeric_byte_to_nibbles(num) [((num & 0xF0) >> 4), (num & 0x0F)] end |