Class: Juicy::Note

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {name: "A", duration: :quarter, octave_change: 0}) ⇒ Note

Returns a new instance of Note.



15
16
17
18
19
20
21
22
23
# File 'lib/juicy/note.rb', line 15

def initialize(options = {name: "A", duration: :quarter, octave_change: 0})
  options[:name] ||= "A"
  options[:duration] ||= :quarter
  options[:octave_change] ||= 0
  @name = parse_note_name(options[:name])
  @pitch = Pitch.new(@name)
  @duration = Duration.new(options[:duration])
  @octave = Note.default_octave + options[:octave_change]
end

Class Attribute Details

.default_octaveObject (readonly)

Returns the value of attribute default_octave.



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

def default_octave
  @default_octave
end

Instance Attribute Details

#distance_from_beat_in_millisecondsObject

Returns the value of attribute distance_from_beat_in_milliseconds.



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

def distance_from_beat_in_milliseconds
  @distance_from_beat_in_milliseconds
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
end

#how_far_into_the_song_you_areObject

Returns the value of attribute how_far_into_the_song_you_are.



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

def how_far_into_the_song_you_are
  @how_far_into_the_song_you_are
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#occupying_beatObject (readonly)

Returns the value of attribute occupying_beat.



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

def occupying_beat
  @occupying_beat
end

#octaveObject (readonly)

Returns the value of attribute octave.



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

def octave
  @octave
end

#pitchObject (readonly)

Returns the value of attribute pitch.



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

def pitch
  @pitch
end

#sum_of_queued_note_durationsObject

Returns the value of attribute sum_of_queued_note_durations.



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

def sum_of_queued_note_durations
  @sum_of_queued_note_durations
end

Instance Method Details

#+(interval) ⇒ Object



66
67
68
69
# File 'lib/juicy/note.rb', line 66

def +(interval)
  step(interval)
  Note.new(name: new_name, octave_change: octave_change)
end

#-(interval) ⇒ Object



71
72
73
74
# File 'lib/juicy/note.rb', line 71

def -(interval)
  step(-1*interval)
  Note.new(name: new_name, octave_change: octave_change)
end

#<=>(other_note) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/juicy/note.rb', line 76

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

#duration_in_milliseconds(tempo) ⇒ Object



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

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

#initial_play_timeObject



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

def initial_play_time
  @initial_play_time
end

#initial_play_time=(time) ⇒ Object



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

def initial_play_time=(time)
  @initial_play_time = time
end

#inspectObject



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

def inspect
  "#{@name}#{@octave}"
end

#lengthObject



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

def length
  duration
end

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



36
37
38
39
40
41
# File 'lib/juicy/note.rb', line 36

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

#play_preparedObject



59
60
61
62
63
64
# File 'lib/juicy/note.rb', line 59

def play_prepared
  until @prepared_note.status.eql? "sleep"
    sleep 0.001
  end
  @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-Note.default_octave)}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/juicy/note.rb', line 43

def prepare(options = {duration: 200, octave: (@octave-Note.default_octave)})
  options[:duration] = options[:duration] || 200
  options[:octave] = options[:octave] || (@octave-Note.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
  until @prepared_note.status.eql? "sleep"
    sleep 0.001
  end
  @prepared_note
  self
end

#prevObject



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

def prev
  return (self-1)
end

#sizeObject



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

def size
  duration
end

#succObject



84
85
86
# File 'lib/juicy/note.rb', line 84

def succ
  return (self+1)
end

#to_sObject



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

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