Ruby script to extract note values

I tend to use a MIDI keyboard to record musical chords, typically with a piano instrument.

Lately I’ve been interested in learning what chords I’ve played so I can try to recreate them on guitar.

I wrote a short Ruby program that reads an XML file and prints the note data.

You need to have the Nokogiri gem installed.

To use it, select a pattern, copy it (e.g. ctrl-c on Windows), then create a text file named pattern.xml and paste into it (ctrl+v).

Run the script in the same folder as this pattern XML file.

require 'nokogiri'   

@doc = Nokogiri::XML(open "pattern.xml")

@lines_data = {}

@doc.xpath("//Line[@index]").each do |line|
  note = line.xpath("NoteColumns/NoteColumn/Note").text
  @lines_data[ line[:index].to_i ] ||= [] 
  @lines_data[line[:index].to_i] <<  note
end

@lines_data.keys.sort.each do |line|
  puts "Line #{line}: #{@lines_data[line] }"
end

The usual caveats: Works for James, “tested” via practical usage, useful hack to solve a simple problem.

3 Likes