Class: StringyFi::Converter

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

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.



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

def initialize(filename)
  self.filename = filename
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



8
9
10
# File 'lib/stringyfi/converter.rb', line 8

def filename
  @filename
end

#xml_docObject

Returns the value of attribute xml_doc.



8
9
10
# File 'lib/stringyfi/converter.rb', line 8

def xml_doc
  @xml_doc
end

Instance Method Details

#convert!Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stringyfi/converter.rb', line 14

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



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

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

#encoding_softwareObject



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

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

#identificationObject



98
99
100
101
102
103
104
105
106
# File 'lib/stringyfi/converter.rb', line 98

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

#io_streamObject



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

def io_stream
  File.open(filename).read
end

#measure(measure_id) ⇒ Object



152
153
154
# File 'lib/stringyfi/converter.rb', line 152

def measure(measure_id)
  measures[measure_id]
end

#measuresObject

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



122
123
124
125
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
# File 'lib/stringyfi/converter.rb', line 122

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
        if actual_notes > 0 and normal_notes > 0
          duration = duration * 1.0 * normal_notes / actual_notes
        end
        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



107
108
109
110
111
112
113
# File 'lib/stringyfi/converter.rb', line 107

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



115
116
117
# File 'lib/stringyfi/converter.rb', line 115

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

#score_body(shortest_fractional_duration) ⇒ Object



47
48
49
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
# File 'lib/stringyfi/converter.rb', line 47

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 > 0 ? "+" : "-"
          (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



40
41
42
43
44
45
# File 'lib/stringyfi/converter.rb', line 40

def score_coda
  <<-EOS
\ttrest 8
\ttstop
EOS
end

#score_preambleObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stringyfi/converter.rb', line 27

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

\ttstart DemoTune
EOS
end

#tempoObject

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



86
87
88
# File 'lib/stringyfi/converter.rb', line 86

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

#titleObject



80
81
82
# File 'lib/stringyfi/converter.rb', line 80

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