class Juicy::Chord
Constants
- QUALITIES
Attributes
duration[R]
how_far_into_the_song_you_are[RW]
notes[R]
sum_of_queued_chord_durations[RW]
Public Class Methods
new(options = {root: Note.new(name: :C), quality: :major, inversion: 0, context: :none, duration: Duration.new("quarter")})
click to toggle source
# File lib/juicy/chord.rb, line 15 def initialize(options = {root: Note.new(name: :C), quality: :major, inversion: 0, context: :none, duration: Duration.new("quarter")}) #binding.pry @root = (options[:root].kind_of?(Note) ? options[:root] : Note.new(name: options[:root])) || Note.new(name: :C) @quality = options[:quality] || :major @inversion = options[:inversion] || 0 @context = options[:context] || :none @duration = options[:duration] || Duration.new("quarter") @type = :triad @notes = [@root + QUALITIES[@quality][0], @root + QUALITIES[@quality][1], @root + QUALITIES[@quality][2]] end
Public Instance Methods
+(interval)
click to toggle source
# File lib/juicy/chord.rb, line 71 def +(interval) #in the context of a scale, change the base pitch to the new scale # degree and the quality to match #in a context of :none, change the base pitch # up a half step times the interval if @context.eql? :none options = { root: (Pitch.new(@root) + interval).frequency, quality: @quality, inversion: @inversion, context: @context } Chord.new(options) end end
-(interval)
click to toggle source
# File lib/juicy/chord.rb, line 88 def -(interval) if @context.eql? :none options = { root: (Pitch.new(@root) - interval).frequency, quality: @quality, inversion: @inversion, context: @context } Chord.new(options) end end
cycle(direction = :up, amount = 1)
click to toggle source
# File lib/juicy/chord.rb, line 101 def cycle(direction = :up, amount = 1) #inverts the chord # ex. 1, 3, 5 -> cycle(:up, 2) -> 5, 1, 3 case direction when :up @inversion = @inversion + amount when :down @inversion = @inversion - amount else puts "Unknown Direction" end puts self.inspect end
Also aliased as: invert
duration_in_milliseconds(tempo)
click to toggle source
# File lib/juicy/chord.rb, line 148 def duration_in_milliseconds(tempo) @duration.duration_in_milliseconds(tempo) end
initial_play_time()
click to toggle source
# File lib/juicy/chord.rb, line 144 def initial_play_time @initial_play_time end
initial_play_time=(time)
click to toggle source
# File lib/juicy/chord.rb, line 140 def initial_play_time=(time) @initial_play_time = time end
inspect()
click to toggle source
# File lib/juicy/chord.rb, line 30 def inspect "#{@root.name} #{@quality} Chord, inversion: #{@inversion}" end
play(options = {duration: 200, style: :default})
click to toggle source
# File lib/juicy/chord.rb, line 34 def play(options = {duration: 200, style: :default}) #Quick and dirty play function. Not intended for genuine use duration = options[:duration] || 200 style = options[:style] || :default notes = [] pitches = QUALITIES[@quality] inversion = @inversion while inversion > 0 pitches = pitches[1..-1] + [(pitches[0]+12)] inversion -= 1 end pitches.each do |interval| notes << Note.new(name: PITCHES.key((PITCHES[@root.name]+interval) % 12)) end case style when :arpeggiate notes.each do |note| Thread.new{note.play(duration: duration)}.join end when :default threads = [] notes.each do |note| threads << Thread.new{note.play(duration: duration)} end threads.each {|t| t.join} else puts "Unknown style type" end self end
play_prepared()
click to toggle source
# File lib/juicy/chord.rb, line 127 def play_prepared th = [] #puts @prepared_notes.inspect @prepared_notes.each do |note| th << Thread.new { note.play_prepared.join } end th.each {|t| t.join} end
prepare(options = {duration: 200, octave: (@octave-Note.default_octave)})
click to toggle source
# File lib/juicy/chord.rb, line 117 def prepare(options = {duration: 200, octave: (@octave-Note.default_octave)}) @prepared_notes = [] @notes.each do |note| options[:duration] = options[:duration] || 200 options[:octave] = options[:octave] || (note.octave-Note.default_octave) @prepared_notes << note.prepare(options) end end
to_s()
click to toggle source
# File lib/juicy/chord.rb, line 26 def to_s "chord type: #{@type}, quality: #{@quality}, root: #{@root}, inversion: #{@inversion}" end