Class: Music::Transcription::Meter

Inherits:
Object
  • Object
show all
Includes:
Parseable, Validatable
Defined in:
lib/music-transcription/model/meter.rb,
lib/music-transcription/parsing/convenience_methods.rb

Constant Summary collapse

PARSER =
Parsing::MeterParser.new
CONVERSION_METHOD =
:to_meter

Constants included from Parseable

Parseable::DEFAULT_SPLIT_PATTERN

Instance Attribute Summary collapse

Attributes included from Validatable

#errors

Instance Method Summary collapse

Methods included from Parseable

included

Methods included from Validatable

#invalid?, #valid?, #validatables, #validate

Constructor Details

#initialize(beats_per_measure, beat_duration) ⇒ Meter

Returns a new instance of Meter.



9
10
11
12
13
# File 'lib/music-transcription/model/meter.rb', line 9

def initialize beats_per_measure, beat_duration
  @beats_per_measure = beats_per_measure
  @beat_duration = beat_duration
  @measure_duration = beats_per_measure * beat_duration    
end

Instance Attribute Details

#beat_durationObject (readonly)

Returns the value of attribute beat_duration.



7
8
9
# File 'lib/music-transcription/model/meter.rb', line 7

def beat_duration
  @beat_duration
end

#beats_per_measureObject (readonly)

Returns the value of attribute beats_per_measure.



7
8
9
# File 'lib/music-transcription/model/meter.rb', line 7

def beats_per_measure
  @beats_per_measure
end

#measure_durationObject (readonly)

Returns the value of attribute measure_duration.



7
8
9
# File 'lib/music-transcription/model/meter.rb', line 7

def measure_duration
  @measure_duration
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
# File 'lib/music-transcription/model/meter.rb', line 39

def ==(other)
  return (@beats_per_measure == other.beats_per_measure &&
    @beat_duration == other.beat_duration)
end

#check_beat_durationObject



29
30
31
32
33
34
35
36
37
# File 'lib/music-transcription/model/meter.rb', line 29

def check_beat_duration
  unless @beat_duration > 0
    raise NonPositiveError, "beat duration #{@beat_duration} is not positive"
  end
  
  unless @beat_duration > 0
    raise NonRationalError, "beat duration #{@beat_duration} is a rational"
  end
end

#check_beats_per_measureObject



19
20
21
22
23
24
25
26
27
# File 'lib/music-transcription/model/meter.rb', line 19

def check_beats_per_measure
  unless @beats_per_measure > 0
    raise NonPositiveError, "beats per measure #{@beats_per_measure} is not positive"
  end
  
  unless @beats_per_measure.is_a?(Integer)
    raise NonIntegerError, "beats per measure #{@beats_per_measure} is not an integer"
  end
end

#check_methodsObject



15
16
17
# File 'lib/music-transcription/model/meter.rb', line 15

def check_methods
  [ :check_beats_per_measure, :check_beat_duration ]
end

#to_sObject



44
45
46
47
48
49
50
51
52
# File 'lib/music-transcription/model/meter.rb', line 44

def to_s
  if beat_duration.numerator == 1
    num = beats_per_measure * beat_duration.numerator
    den = beat_duration.denominator
    "#{num}/#{den}"
  else
    "#{beats_per_measure}*#{beat_duration}"
  end
end