Class: Musicality::Meter
- Inherits:
-
Object
- Object
- Musicality::Meter
- Includes:
- Parseable, Packable, Validatable
- Defined in:
- lib/musicality/notation/model/meter.rb,
lib/musicality/printing/lilypond/meter_engraving.rb,
lib/musicality/notation/parsing/convenience_methods.rb
Constant Summary collapse
- PARSER =
Parsing::MeterParser.new
- CONVERSION_METHOD =
:to_meter
Constants included from Parseable
Parseable::DEFAULT_SPLIT_PATTERN
Constants included from Packable
Instance Attribute Summary collapse
-
#beat_duration ⇒ Object
readonly
Returns the value of attribute beat_duration.
-
#beats_per_measure ⇒ Object
readonly
Returns the value of attribute beats_per_measure.
-
#measure_duration ⇒ Object
readonly
Returns the value of attribute measure_duration.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #check_beat_duration ⇒ Object
- #check_beats_per_measure ⇒ Object
- #check_methods ⇒ Object
-
#initialize(beats_per_measure, beat_duration) ⇒ Meter
constructor
A new instance of Meter.
- #to_lilypond ⇒ Object
- #to_s ⇒ Object
Methods included from Parseable
Methods included from Validatable
#errors, #invalid?, #valid?, #validatables, #validate
Methods included from Packable
#class_str, included, #init_params, #pack, pack_val, recover_class, unpack_val
Constructor Details
#initialize(beats_per_measure, beat_duration) ⇒ Meter
Returns a new instance of Meter.
12 13 14 15 16 |
# File 'lib/musicality/notation/model/meter.rb', line 12 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_duration ⇒ Object (readonly)
Returns the value of attribute beat_duration.
10 11 12 |
# File 'lib/musicality/notation/model/meter.rb', line 10 def beat_duration @beat_duration end |
#beats_per_measure ⇒ Object (readonly)
Returns the value of attribute beats_per_measure.
10 11 12 |
# File 'lib/musicality/notation/model/meter.rb', line 10 def beats_per_measure @beats_per_measure end |
#measure_duration ⇒ Object (readonly)
Returns the value of attribute measure_duration.
10 11 12 |
# File 'lib/musicality/notation/model/meter.rb', line 10 def measure_duration @measure_duration end |
Instance Method Details
#==(other) ⇒ Object
42 43 44 45 |
# File 'lib/musicality/notation/model/meter.rb', line 42 def ==(other) return (@beats_per_measure == other.beats_per_measure && @beat_duration == other.beat_duration) end |
#check_beat_duration ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/musicality/notation/model/meter.rb', line 32 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_measure ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/musicality/notation/model/meter.rb', line 22 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_methods ⇒ Object
18 19 20 |
# File 'lib/musicality/notation/model/meter.rb', line 18 def check_methods [ :check_beats_per_measure, :check_beat_duration ] end |
#to_lilypond ⇒ Object
4 5 6 7 8 |
# File 'lib/musicality/printing/lilypond/meter_engraving.rb', line 4 def to_lilypond num = beats_per_measure * beat_duration.numerator den = beat_duration.denominator "\\time #{num}/#{den}" end |
#to_s ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/musicality/notation/model/meter.rb', line 47 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 |