Class: Musikov::MidiParser

Inherits:
Object
  • Object
show all
Defined in:
lib/musikov/midi_parser.rb

Overview

This class is responsible for interacting with MidiLib in order to read the input midi files.

Instance Method Summary collapse

Constructor Details

#initialize(file_or_folder_paths = []) ⇒ MidiParser

Initializes the parser using the file (or folder) path parameter

  • Parameter can be a single file path or a folder



18
19
20
21
# File 'lib/musikov/midi_parser.rb', line 18

def initialize(file_or_folder_paths = [])
  @paths = []
  @paths += file_or_folder_paths
end

Instance Method Details

#parseObject

Obtains the list of midi files to parse and call the Midilib parse routine



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/musikov/midi_parser.rb', line 24

def parse
  result = []
  files = []
  
  @paths.each { |path|
    begin
      raise FileNotFoundError unless File.exists?(path)
      
      if File.directory?(path) then
        files += Dir.glob("#{path}/**/*.mid")
      else
        files << path if File.extname(path) == ".mid"
      end
    rescue
      puts "Not a valid file path : #{path} => #{$!}"
    end
  }
  
  
  if files.empty? then
    puts "No files were added."
  else
    files.each { |file_path|
      result << read_midi(file_path)
    }
  end
  
  return result
end