Class: PicoTune::Parser
- Inherits:
-
Object
- Object
- PicoTune::Parser
- Defined in:
- lib/picotune.rb
Instance Method Summary collapse
-
#initialize(file) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #pattern_steps(pattern) ⇒ Object
Constructor Details
#initialize(file) ⇒ Parser
Returns a new instance of Parser.
517 518 519 520 |
# File 'lib/picotune.rb', line 517 def initialize file @lines = File.open(file).readlines.map(&:strip) @keywords = ['tune ', 'sequence', 'instrument ', 'phrase ', 'pattern '] end |
Instance Method Details
#parse ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/picotune.rb', line 522 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) ⇒ Object
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/picotune.rb', line 566 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 |