Class: Metronome::OddBar

Inherits:
Bar
  • Object
show all
Defined in:
lib/metronome-odd.rb

Instance Attribute Summary collapse

Attributes inherited from Bar

#d, #downbeat_sound_file, #n, #upbeat_sound_file

Instance Method Summary collapse

Methods inherited from Bar

#play, #set_tempo

Constructor Details

#initialize(tempo, in_beat) ⇒ OddBar

Returns a new instance of OddBar.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/metronome-odd.rb', line 123

def initialize(tempo, in_beat)
  data_dir = Gem.datadir("metronome-odd")
  data_dir = data_dir ? data_dir : ""
  @upbeat_sound_file = data_dir + "/beat_upbeat.aiff"
  @downbeat_sound_file = data_dir + "/beat_downbeat.aiff"

  @beat_array = Array.new
  @in_beat = in_beat
  @tempo = tempo

  upbeat_b = true
  time = 1
  prev_beat = 1
  in_beat.each do |t|
    if upbeat_b
      beat = Beat.new(tempo/t, @upbeat_sound_file)
      upbeat_b = false
    else
      beat = Beat.new(tempo/t, @downbeat_sound_file)
    end
    @beat_array.push(beat)
  end

end

Instance Attribute Details

#beat_arrayObject

Returns the value of attribute beat_array.



121
122
123
# File 'lib/metronome-odd.rb', line 121

def beat_array
  @beat_array
end

#tempoObject

Returns the value of attribute tempo.



120
121
122
# File 'lib/metronome-odd.rb', line 120

def tempo
  @tempo
end

Instance Method Details



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/metronome-odd.rb', line 148

def print_sign
  # In order to print an odd bar, firstly we find the beat sound times
  #   in in_sum, starting from 1.
  # Then we add the silences in the beats in in_clone
  # Finally we print the bar, with numbers in the beats and
  #   semicolons in the silences that occur in a beat
  # width may be used to change size
  width = 8
  max_beat = @in_beat.sum
  in_sum = [1.0]
  acum = 1.0
  @in_beat.each do |e| acum=acum+e;in_sum.push(acum) end
  in_clone = in_sum.clone
  (1...max_beat.floor).each do |d|
    if not(in_clone.include?(d))
      in_clone.push(Float(d))
    end
  end
  in_clone = in_clone.sort

  print "|"
  in_clone[0...-1].each_with_index do |d, i|
    if in_sum.include?(d)
      if d == Integer(d)
        print(':')
      end
      print(d.floor)
    else
      print(':')
    end
    if d<in_clone.last
      t = Integer((in_clone[i+1]-in_clone[i])*width-1)
      t.times do print " " end 
    end
  end
  print "|"
end