class PicoTune::Parser

Public Class Methods

new(file) click to toggle source
# File lib/picotune.rb, line 523
def initialize file
  @lines = File.open(file).readlines.map(&:strip)
  @keywords = ['tune ', 'sequence', 'instrument ', 'phrase ', 'pattern ']
end

Public Instance Methods

parse() click to toggle source
# File lib/picotune.rb, line 528
def parse
  i = 0
  bag = []
  collecting_melodies = false

  while i < @lines.length
    line = @lines[i]

    if line.start_with? *@keywords
      collecting_melodies = false
      parts = line.split ' '
      item = {}

      item['type'] = parts[0]

      if parts[0] == 'sequence'
        item['list'] = parts[1..-1]
      elsif parts[0] == 'pattern'
        item['name'] = parts[1]
        item['list'] = pattern_steps parts[2..-1].join
      else
        item['name'] = parts[1..-1].join
      end

      bag << item
    elsif line.length > 0
      parts = line.split ' '

      if bag.last['type'] == 'phrase' && parts[0] == 'melodies'
        collecting_melodies = true
        bag.last['melodies'] = []
      elsif collecting_melodies
        bag.last['melodies'].push parts
      else
        bag.last[parts[0]] = parts[1..-1].join
      end
    end
    
    i += 1
  end

  bag
end
pattern_steps(pattern) click to toggle source
# File lib/picotune.rb, line 572
def pattern_steps pattern
  p = pattern.split(/([a-zA-Z][#b]?\d)/, -1)
             .map { |b| b.split(/(\.|-)/, -1) }
             .flatten
             .delete_if { |b| b.length.zero? }

  i = 0
  while i < p.length
    if p[i] == '-'
      p[i] = p[i - 1]
    end

    i += 1
  end

  p
end