class Juicy::Note

Attributes

default_octave[R]
distance_from_beat_in_milliseconds[RW]
duration[R]
how_far_into_the_song_you_are[RW]
name[R]
occupying_beat[R]
octave[R]
pitch[R]
sum_of_queued_note_durations[RW]

Public Class Methods

new(options = {name: "A", duration: :quarter, octave_change: 0}) click to toggle source
# 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

Public Instance Methods

+(interval) click to toggle source
# File lib/juicy/note.rb, line 66
def +(interval)
  step(interval)
  Note.new(name: new_name, octave_change: octave_change)
end
-(interval) click to toggle source
# File lib/juicy/note.rb, line 71
def -(interval)
  step(-1*interval)
  Note.new(name: new_name, octave_change: octave_change)
end
<=>(other_note) click to toggle source
# 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=(duration) click to toggle source
# File lib/juicy/note.rb, line 120
def duration=(duration)
  @duration = Duration.new(duration)
end
duration_in_milliseconds(tempo) click to toggle source
# File lib/juicy/note.rb, line 108
def duration_in_milliseconds(tempo)
  @duration.duration_in_milliseconds(tempo)
end
initial_play_time() click to toggle source
# File lib/juicy/note.rb, line 104
def initial_play_time
  @initial_play_time
end
initial_play_time=(time) click to toggle source
# File lib/juicy/note.rb, line 100
def initial_play_time=(time)
  @initial_play_time = time
end
inspect() click to toggle source
# File lib/juicy/note.rb, line 32
def inspect
  "#{@name}#{@octave}"
end
length() click to toggle source
# File lib/juicy/note.rb, line 92
def length
  duration
end
play(options = {duration: 200, octave: (@octave-Note.default_octave)}) click to toggle source
# 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_prepared() click to toggle source
# 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) click to toggle source
# File lib/juicy/note.rb, line 112
def plays_during(beat)
  @occupying_beat = beat
end
plays_during?(beat) click to toggle source
# File lib/juicy/note.rb, line 116
def plays_during?(beat)
  beat == @occupying_beat
end
prepare(options = {duration: 200, octave: (@octave-Note.default_octave)}) click to toggle source
# 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
prev() click to toggle source
# File lib/juicy/note.rb, line 88
def prev
  return (self-1)
end
size() click to toggle source
# File lib/juicy/note.rb, line 96
def size
  duration
end
succ() click to toggle source
# File lib/juicy/note.rb, line 84
def succ
  return (self+1)
end
to_s() click to toggle source
# 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

Private Instance Methods

new_name() click to toggle source
# File lib/juicy/note.rb, line 134
def new_name
  PITCHES.key((@step) % 12)
end
octave_change() click to toggle source
# File lib/juicy/note.rb, line 126
def octave_change
  @octave - Note.default_octave + @step/12
end
parse_note_name(name) click to toggle source
# File lib/juicy/note.rb, line 138
def parse_note_name(name)
  # parses note name input
  # user should be able to say "A#" or "a#" or "a sharp" or "A_sharp" or "a_s"
  groups = name.to_s.match(/(?<name>[a-gA-G])(?<space> |_)?(?<accidental>.*)/)
  if name.to_s.match "rest" || name.to_s.match(/^_$/)
    note_name = "_"
  else
    note_name = groups[:name].upcase
    unless groups[:accidental].nil? || groups[:accidental].empty?
      note_name += case groups[:accidental]
      when /^(s|#)/
        "_sharp"
      when /^(f|b)/
        "_flat"
      else
        puts "Unknown note modifier: '#{groups[:accidental]}'"
        ""
      end
    end
  end
  note_name.to_sym
end
same_octave(other_note) click to toggle source
# File lib/juicy/note.rb, line 161
def same_octave(other_note)
  (self.octave <=> other_note.octave) == 0
end
step(interval) click to toggle source
# File lib/juicy/note.rb, line 130
def step(interval)
  @step = PITCHES[@name]+interval
end