Class: Juicy::Note

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/juicy/note.rb

Constant Summary collapse

@@default_octave =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "A", duration = "quarter", octave_change = 0) ⇒ Note

Returns a new instance of Note.



12
13
14
15
16
17
# File 'lib/juicy/note.rb', line 12

def initialize(name = "A", duration = "quarter", octave_change = 0)
  @name = parse_note_name(name)
  @pitch = Pitch.new(@name)
  @duration = Duration.new(duration)
 @octave = @@default_octave + octave_change
end

Instance Attribute Details

#distance_from_beat_in_millisecondsObject

Returns the value of attribute distance_from_beat_in_milliseconds.



10
11
12
# File 'lib/juicy/note.rb', line 10

def distance_from_beat_in_milliseconds
  @distance_from_beat_in_milliseconds
end

#durationObject (readonly)

Returns the value of attribute duration.



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

def duration
  @duration
end

#how_far_into_the_song_you_areObject

Returns the value of attribute how_far_into_the_song_you_are.



9
10
11
# File 'lib/juicy/note.rb', line 9

def how_far_into_the_song_you_are
  @how_far_into_the_song_you_are
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#occupying_beatObject (readonly)

Returns the value of attribute occupying_beat.



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

def occupying_beat
  @occupying_beat
end

#octaveObject (readonly)

Returns the value of attribute octave.



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

def octave
  @octave
end

#pitchObject (readonly)

Returns the value of attribute pitch.



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

def pitch
  @pitch
end

#sum_of_queued_note_durationsObject

Returns the value of attribute sum_of_queued_note_durations.



9
10
11
# File 'lib/juicy/note.rb', line 9

def sum_of_queued_note_durations
  @sum_of_queued_note_durations
end

Class Method Details

.default_octaveObject



108
109
110
# File 'lib/juicy/note.rb', line 108

def self.default_octave
  @@default_octave
end

Instance Method Details

#+(interval) ⇒ Object



68
69
70
71
72
73
# File 'lib/juicy/note.rb', line 68

def +(interval)
  step = PITCHES[@name]+interval
  octave_change = step/12 #mathy stuff to figure out how many octaves were traversed (cant assume just one was
  name = PITCHES.key((PITCHES[@name]+interval) % 12)
  Note.new(name, @octave-@@default_octave + octave_change)
end

#-(interval) ⇒ Object



75
76
77
# File 'lib/juicy/note.rb', line 75

def -(interval)
  Note.new(PITCHES.key((PITCHES[@name]-interval) % 12))
end

#<=>(other_note) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/juicy/note.rb', line 79

def <=>(other_note)
  if same_octave
    self.pitch <=> other_note.pitch
  else
    self.octave <=> other_note.octave
  end
end

#duration_in_milliseconds(tempo) ⇒ Object



103
104
105
106
# File 'lib/juicy/note.rb', line 103

def duration_in_milliseconds(tempo)
  #puts tempo
  @duration.duration_in_milliseconds(tempo)
end

#initial_play_timeObject



99
100
101
# File 'lib/juicy/note.rb', line 99

def initial_play_time
  @initial_play_time
end

#initial_play_time=(time) ⇒ Object



95
96
97
# File 'lib/juicy/note.rb', line 95

def initial_play_time=(time)
  @initial_play_time = time
end

#inspectObject



26
27
28
# File 'lib/juicy/note.rb', line 26

def inspect
  "#{@name}"
end

#lengthObject



87
88
89
# File 'lib/juicy/note.rb', line 87

def length
  duration
end

#play(options = {duration: 200, octave: (@octave-@@default_octave)}) ⇒ Object



30
31
32
33
34
35
# File 'lib/juicy/note.rb', line 30

def play(options = {duration: 200, octave: (@octave-@@default_octave)})
  if @name == :_
    options[:volume] = 0
  end
  @pitch.play(options)
end

#play_preparedObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/juicy/note.rb', line 55

def play_prepared
  #puts @prepared_note.status
  #puts "playing"
  until @prepared_note.status.eql? "sleep"
    sleep 0.001
    #puts @prepared_note.status
    #Thread.pass
  end
  #Thread.pass
  #puts "waking up"
  @prepared_note.wakeup
end

#plays_during(beat) ⇒ Object



112
113
114
# File 'lib/juicy/note.rb', line 112

def plays_during(beat)
  @occupying_beat = beat
end

#plays_during?(beat) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/juicy/note.rb', line 116

def plays_during?(beat)
  beat == @occupying_beat
end

#prepare(options = {duration: 200, octave: (@octave-@@default_octave)}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/juicy/note.rb', line 37

def prepare(options = {duration: 200, octave: (@octave-@@default_octave)})
  options[:duration] = options[:duration] || 200
  options[:octave] = options[:octave] || (@octave-@@default_octave)
  if @name == :_
    options[:volume] = 0
  end
    Thread.pass
  @prepared_note = @pitch.prepare(options)
  @prepared_note[:sleep_time] = @distance_from_beat_in_milliseconds/1000.0
  #puts @prepared_note.status
  until @prepared_note.status.eql? "sleep"
    sleep 0.001
    #puts @prepared_note.status
  end
  @prepared_note
  self
end

#sizeObject



91
92
93
# File 'lib/juicy/note.rb', line 91

def size
  duration
end

#to_sObject



19
20
21
22
23
24
# File 'lib/juicy/note.rb', line 19

def to_s
  name = @name[0]
  name += "#" if @name=~/sharp/
  name += "b" if @name=~/flat/
  "#{name}#{@octave}"
end