Class: Juicy::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/juicy/duration.rb

Constant Summary collapse

DURATIONS =
["whole", "half", "quarter", "eighth", "sixteenth", "thirty-second", "sixty-fourth"]
MULTIPLIER =
{:"" => 0, :triplet => -1, :dotted => 1}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration) ⇒ Duration

Returns a new instance of Duration.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/juicy/duration.rb', line 10

def initialize(duration)
  # @duration is a Rational number which represents the length of
  # a note relative to a quarter note
  # ex. Duration.new("dotted eighth")
  #  @duration = Rational(3,4)
  #
  if duration.kind_of? Rational
    @duration = duration
  else
    @duration = parse_duration(duration)
  end
  
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



8
9
10
# File 'lib/juicy/duration.rb', line 8

def duration
  @duration
end

Class Method Details

.duration_of_quarter_note_in_milliseconds(tempo) ⇒ Object



24
25
26
# File 'lib/juicy/duration.rb', line 24

def self.duration_of_quarter_note_in_milliseconds(tempo)
  60_000.0/tempo
end

Instance Method Details

#*(scalar) ⇒ Object



48
49
50
# File 'lib/juicy/duration.rb', line 48

def *(scalar)
  Duration.new(@duration*scalar)
end

#+(other_duration) ⇒ Object



44
45
46
# File 'lib/juicy/duration.rb', line 44

def +(other_duration)
  Duration.new(@duration + other_duration.duration)
end

#duration_in_milliseconds(tempo) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/juicy/duration.rb', line 28

def duration_in_milliseconds(tempo)
  # how long a note is depends on the tempo and the musical duration
  # e.g. at 120 bpm, and a duration of an eighth note, the duration
  # in milliseconds would be 60_000.0/120/2
  # milliseconds_per_second*seconds_per_minute/bpm/beats_of_given_type_per_quarter_note
  60_000.0/tempo*beats_of_given_type_per_quarter_note
end

#to_fObject



40
41
42
# File 'lib/juicy/duration.rb', line 40

def to_f
  @duration.to_f
end

#to_sObject



36
37
38
# File 'lib/juicy/duration.rb', line 36

def to_s
  @duration.to_s
end