Class: Juicy::Track
- Inherits:
-
Object
- Object
- Juicy::Track
- Defined in:
- lib/juicy/track.rb
Instance Attribute Summary collapse
-
#notes ⇒ Object
Returns the value of attribute notes.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(init_time, notes, tempo) ⇒ Track
constructor
A new instance of Track.
- #play ⇒ Object
Constructor Details
#initialize(init_time, notes, tempo) ⇒ Track
Returns a new instance of Track.
8 9 10 11 12 13 14 |
# File 'lib/juicy/track.rb', line 8 def initialize(init_time, notes, tempo) @start_time = init_time @notes = notes @tempo = tempo end |
Instance Attribute Details
#notes ⇒ Object
Returns the value of attribute notes.
6 7 8 |
# File 'lib/juicy/track.rb', line 6 def notes @notes end |
Class Method Details
.play_concurrently(tracks, tempo) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/juicy/track.rb', line 118 def self.play_concurrently(tracks, tempo) @song_start_time = Time.now threads = [] # iterate over each track over and over again, preparing notes in the current beat # in each iteration, store the notes you've prepared into an array and store that # array into the prepared_notes array which the playing thread will play notes from # when it has enough to play. # A track is an array of playable musical objects. for now, these are individual # notes or chords. a chord is an array of individual notes # prepared_beats = [] out_of_notes_to_prepare = false threads << Thread.new do Thread.current[:name] = "prepare beats thread" current_beat = 1 last_beat = 1 tracks.each do |track| track.notes.each do |note| last_beat = note. if note. > last_beat end end until current_beat > last_beat if prepared_beats.size <= 20 this_beats_notes = [] tracks.each do |track| while !track.notes[0].nil? && track.notes[0].plays_during?(current_beat) playable_thing = track.notes.shift if playable_thing.kind_of?(Note) this_beats_notes << playable_thing.prepare(duration: playable_thing.duration_in_milliseconds(tempo)) elsif playable_thing.kind_of?(Chord) playable_thing.notes.each do |note| this_beats_notes << note.prepare(duration: note.duration_in_milliseconds(tempo)) end end end end prepared_beats << this_beats_notes current_beat += 1 end Thread.pass end out_of_notes_to_prepare = true end threads << Thread.new do Thread.current[:name] = "play prepared beats thread" # when prepared_beats has at least a few beats to play (a measure's worth?) # "start" the song by iterating through each element, waking up each note # and then waiting the remainder of a beat's worth of milliseconds until the # next beat until (prepared_beats.size >= 2) || out_of_notes_to_prepare sleep 0.01 end last_note = Thread.new {} time = Time.now until prepared_beats.empty? && out_of_notes_to_prepare time = Time.now beat = prepared_beats.shift beat.each do |note| last_note = note.play_prepared end sleep rand(100..300)/1000.0 sleep_amount = Duration.duration_of_quarter_note_in_milliseconds(tempo)/1000.0 - (Time.now - time) puts sleep_amount sleep sleep_amount unless sleep_amount < 0 end last_note.join end #threads.each {|t| puts t[:name] } threads.each {|t| t.join} end |
.tracks_is_empty(tracks) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/juicy/track.rb', line 106 def self.tracks_is_empty(tracks) empty = true tracks.each do |track| #puts track.object_id puts track.notes.inspect unless track.notes.empty? empty = false end end empty end |
Instance Method Details
#play ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/juicy/track.rb', line 16 def play # prepare notes ahead of time, and play them at a specified time. # each note must be prepared separately in it's own thread. # only 20 threads are allowed to be alive at a time in a current track, and each track is preparing its notes seperately # th = [] # while(notes_left_to_prepare) # if (th.count {|t| t.alive? }) <= 20 # note = notes_left_to_prepare.shift # th << Thread.new do # note.prepare # sleep_amount = when_the_note_should_be_played - how_far_into_the_song_you_are # unless sleep_amount < 0 # sleep sleep_amount # end # note.play_prepared # end # end # end # th.each {|t| t.join} notes = [] if @notes[0].kind_of? Chord #notes << Thread.new { @notes.each { |chord| 4.times {chord.play} } } chords_left_to_prepare = @notes.dup @sum_of_queued_chord_durations = 0 until chords_left_to_prepare.size == 1 if (notes.count {|t| t.alive?} <= 20) notes << Thread.new do chord = chords_left_to_prepare.shift puts "#{chord}: #{chord.duration}" chord.sum_of_queued_chord_durations = @sum_of_queued_chord_durations @sum_of_queued_chord_durations += chord.duration_in_milliseconds(@tempo) chord.initial_play_time = @start_time + chord.sum_of_queued_chord_durations chord.how_far_into_the_song_you_are = how_far_into_the_song_you_are chord.prepare(duration: chord.duration_in_milliseconds(@tempo)) sleep_amount = (chord.initial_play_time - chord.how_far_into_the_song_you_are)/1000.0 unless sleep_amount < 0 sleep sleep_amount end Thread.pass chord.play_prepared.join #puts "tehe" end end Thread.pass end elsif @notes[0].kind_of? Note # @notes.each do |note| # note.play # end notes_left_to_prepare = @notes.dup @sum_of_queued_note_durations = 0 until notes_left_to_prepare.size == 1 #puts notes_left_to_prepare.size if (notes.count {|t| t.alive? }) <= 20 #Thread.pass notes << Thread.new do note = notes_left_to_prepare.shift puts "#{note}: #{note.duration}" note.sum_of_queued_note_durations = @sum_of_queued_note_durations @sum_of_queued_note_durations += note.duration_in_milliseconds(@tempo) #puts @sum_of_queued_note_durations note.initial_play_time = @start_time + note.sum_of_queued_note_durations #puts "note.initial_play_time: #{note.initial_play_time}" note.how_far_into_the_song_you_are = how_far_into_the_song_you_are #puts "note.how_far_into_the_song_you_are: #{note.how_far_into_the_song_you_are}" note.prepare(duration: note.duration_in_milliseconds(@tempo)) # puts "note.initial_play_time: #{note.initial_play_time}" # puts "note.how_far_into_the_song_you_are: #{note.how_far_into_the_song_you_are}" sleep_amount = (note.initial_play_time - note.how_far_into_the_song_you_are)/1000.0 #puts sleep_amount unless sleep_amount < 0 sleep sleep_amount end Thread.pass note.play_prepared end end Thread.pass end end notes.each {|t| t.join} end |