Class: StringyFi::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/stringyfi/converter.rb

Overview

Main controller for the conversion process

Constant Summary collapse

INITIAL_OCTAVE =

setup by the stringy firmware

1
OCTAVE_OFFSET =

how many octaves to offset the source

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Converter

Returns a new instance of Converter.



13
14
15
# File 'lib/stringyfi/converter.rb', line 13

def initialize(filename)
  self.filename = filename
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/stringyfi/converter.rb', line 10

def filename
  @filename
end

#xml_docObject



159
160
161
# File 'lib/stringyfi/converter.rb', line 159

def xml_doc
  @xml_doc ||= Nokogiri::XML(io_stream)
end

Instance Method Details

#convert!Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stringyfi/converter.rb', line 17

def convert!
  $stderr.puts "converting #{filename}.."
  shortest_fractional_duration = measures.shortest_fractional_duration
  $stderr.puts "  shortest_fractional_duration: #{shortest_fractional_duration}"
  $stderr.puts "  octave_range: #{measures.octave_range.inspect}"

  puts score_preamble
  puts score_body(shortest_fractional_duration)
  puts score_coda

  $stderr.puts '.. done.'
end

#encoding_dateObject



93
94
95
# File 'lib/stringyfi/converter.rb', line 93

def encoding_date
  xml_doc.xpath('//identification/encoding/encoding-date').text
end

#encoding_softwareObject



97
98
99
# File 'lib/stringyfi/converter.rb', line 97

def encoding_software
  xml_doc.xpath('//identification/encoding/software').text
end

#identificationObject



101
102
103
104
105
106
107
108
109
# File 'lib/stringyfi/converter.rb', line 101

def identification
  {
    title: title,
    encoding: {
      date: encoding_date,
      software: encoding_software
    }
  }
end

#io_streamObject



163
164
165
# File 'lib/stringyfi/converter.rb', line 163

def io_stream
  File.open(filename).read
end

#measure(measure_id) ⇒ Object



155
156
157
# File 'lib/stringyfi/converter.rb', line 155

def measure(measure_id)
  measures[measure_id]
end

#measuresObject

Returns measures for the piece. only converts one part (for now) only includes staff 1



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/stringyfi/converter.rb', line 126

def measures
  @measures ||= begin
    measures = StringyFi::Measures.new
    part = parts.first
    part.xpath('measure').each_with_index do |part_measure, m|
      measures[m] ||= []
      part_measure.xpath('note').each_with_object(measures[m]) do |note, memo|
        next unless note.xpath('staff').text == '1'
        next unless note.xpath('voice').text == '1'

        pitch = note.xpath('pitch')
        duration = note.xpath('duration').text.to_i
        actual_notes = note.xpath('actual-notes').text.to_i
        normal_notes = note.xpath('normal-notes').text.to_i
        duration = duration * 1.0 * normal_notes / actual_notes if actual_notes.positive? && normal_notes.positive?
        duration_type = note.xpath('type').text
        memo << StringyFi::Note.new(
          pitch.xpath('step').text,
          pitch.xpath('octave').text,
          pitch.xpath('alter').text,
          duration,
          duration_type
        )
      end
    end
    measures
  end
end

#part_listObject



111
112
113
114
115
116
117
# File 'lib/stringyfi/converter.rb', line 111

def part_list
  xml_doc.xpath('//part-list/score-part').each_with_object([]) do |part, memo|
    h = {}
    h[:id] = part.attr('id')
    memo << h
  end
end

#partsObject



119
120
121
# File 'lib/stringyfi/converter.rb', line 119

def parts
  xml_doc.xpath('//part')
end

#score_body(shortest_fractional_duration) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stringyfi/converter.rb', line 50

def score_body(shortest_fractional_duration)
  lines = []
  current_octave = INITIAL_OCTAVE + OCTAVE_OFFSET
  measures.each_with_index do |measure, measure_index|
    lines << "\t; measure #{measure_index + 1}"
    measure.each do |note|
      unless note.rest?
        if note.octave != current_octave
          delta = note.octave - current_octave
          sign = delta.positive? ? '+' : '-'
          delta.abs.times do
            lines << "\ttoctave #{sign}1"
          end
          current_octave = note.octave
        end
      end
      short_repeats, medium_repeats, long_repeats, very_long_repeats = note.stringy_durations(shortest_fractional_duration)
      if note.rest?
        short_repeats.times     { lines << "\ttrest 1" }
        medium_repeats.times    { lines << "\ttrest 2" }
        long_repeats.times      { lines << "\ttrest 3" }
        very_long_repeats.times { lines << "\ttrest 4" }
      else
        short_repeats.times     { lines << "\ttnote #{note.to_stringy(current_octave)},1,0  ; #{note.to_note_id}" }
        medium_repeats.times    { lines << "\ttnote #{note.to_stringy(current_octave)},2,0  ; #{note.to_note_id}" }
        long_repeats.times      { lines << "\ttnote #{note.to_stringy(current_octave)},3,0  ; #{note.to_note_id}" }
        very_long_repeats.times { lines << "\ttnote #{note.to_stringy(current_octave)},4,0  ; #{note.to_note_id}" }
      end
    end
  end
  lines
end

#score_codaObject



43
44
45
46
47
48
# File 'lib/stringyfi/converter.rb', line 43

def score_coda
  <<~CODA
    \ttrest 8
    \ttstop
  CODA
end

#score_preambleObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stringyfi/converter.rb', line 30

def score_preamble
  <<~PREAMBLE
    ;**************************************************************************
    ;** Title: #{title}
    ;** Tempo: #{tempo}
    ;** Encoded: #{encoding_date} with #{encoding_software}
    ;** Stringyfied: #{Time.now}
    ;**************************************************************************

    \ttstart DemoTune
  PREAMBLE
end

#tempoObject

Simplified - only supports one tempo for the piece assumed for quarter note



89
90
91
# File 'lib/stringyfi/converter.rb', line 89

def tempo
  @tempo ||= xml_doc.xpath('//measure/direction/sound/@tempo').to_s.to_i
end

#titleObject



83
84
85
# File 'lib/stringyfi/converter.rb', line 83

def title
  xml_doc.xpath('//work/work-title').text
end