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.

Examples:

Converting hex string to bytes

MIDIEvents::TypeConversion.hex_string_to_numeric_byte_array("904040")
# => [0x90, 0x40, 0x40]

Converting bytes to hex string

MIDIEvents::TypeConversion.numeric_byte_array_to_hex_string([0x90, 0x40, 0x40])
# => "904040"

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

hex_chars_to_numeric_byte_array(["9", "0", "4", "0"])
# => [0x90, 0x40]

Parameters:

  • nibbles (Array<String>)

    Array of hex character strings (e.g., ["9", "0", "4", "0"])

Returns:

  • (Array<Integer>)

    Array of numeric bytes (e.g., [0x90, 0x40])



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

Examples:

hex_str_to_hex_chars("904040")
# => ["9", "0", "4", "0", "4", "0"]

Parameters:

  • string (String)

    A string of hex digits (e.g., "904040")

Returns:

  • (Array<String>)

    An array of individual hex 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

Examples:

hex_string_to_numeric_byte_array("904040")
# => [0x90, 0x40, 0x40]

Parameters:

  • string (String)

    A string of hex digits (e.g., "904040")

Returns:

  • (Array<Integer>)

    An array of numeric bytes (e.g., [0x90, 0x40, 0x40])



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

Examples:

numeric_byte_array_to_hex_string([0x90, 0x40, 0x40])
# => "904040"

Parameters:

  • bytes (Array<Integer>)

    An array of numeric bytes (e.g., [0x90, 0x40, 0x40])

Returns:

  • (String)

    An uppercase hex string (e.g., "904040")



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

Examples:

numeric_byte_to_hex_chars(0x90)
# => ["9", "0"]

Parameters:

  • num (Integer)

    A numeric byte (e.g., 0x90)

Returns:

  • (Array<String>)

    An array of two hex character nibbles (e.g., ["9", "0"])



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

Examples:

numeric_byte_to_nibbles(0x90)
# => [0x9, 0x0]

Parameters:

  • num (Integer)

    A numeric byte (e.g., 0x90)

Returns:

  • (Array<Integer>)

    An array of two nibbles high, low



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.

Examples:

hex_chars_to_numeric_byte_array(["9", "0", "4", "0"])
# => [0x90, 0x40]

Parameters:

  • nibbles (Array<String>)

    Array of hex character strings (e.g., ["9", "0", "4", "0"])

Returns:

  • (Array<Integer>)

    Array of numeric bytes (e.g., [0x90, 0x40])



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

Examples:

hex_str_to_hex_chars("904040")
# => ["9", "0", "4", "0", "4", "0"]

Parameters:

  • string (String)

    A string of hex digits (e.g., "904040")

Returns:

  • (Array<String>)

    An array of individual hex 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

Examples:

hex_string_to_numeric_byte_array("904040")
# => [0x90, 0x40, 0x40]

Parameters:

  • string (String)

    A string of hex digits (e.g., "904040")

Returns:

  • (Array<Integer>)

    An array of numeric bytes (e.g., [0x90, 0x40, 0x40])



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

Examples:

numeric_byte_array_to_hex_string([0x90, 0x40, 0x40])
# => "904040"

Parameters:

  • bytes (Array<Integer>)

    An array of numeric bytes (e.g., [0x90, 0x40, 0x40])

Returns:

  • (String)

    An uppercase hex string (e.g., "904040")



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

Examples:

numeric_byte_to_hex_chars(0x90)
# => ["9", "0"]

Parameters:

  • num (Integer)

    A numeric byte (e.g., 0x90)

Returns:

  • (Array<String>)

    An array of two hex character nibbles (e.g., ["9", "0"])



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

Examples:

numeric_byte_to_nibbles(0x90)
# => [0x9, 0x0]

Parameters:

  • num (Integer)

    A numeric byte (e.g., 0x90)

Returns:

  • (Array<Integer>)

    An array of two nibbles high, low



112
113
114
# File 'lib/midi-events/type_conversion.rb', line 112

def numeric_byte_to_nibbles(num)
  [((num & 0xF0) >> 4), (num & 0x0F)]
end