module MelodyGenerater::Rand

Public Instance Methods

generate(length, octave) click to toggle source
# File lib/kuromusic/Generator/Random.rb, line 36
def generate(length, octave)
  Track.new(@key, common(length, octave, @note_times[2..3], @scale))
end

Private Instance Methods

common(length, octave, _note_times, notes) click to toggle source
# File lib/kuromusic/Generator/Random.rb, line 15
def common(length, octave, _note_times, notes)
  f = fiber(octave, notes, 2)
  (1..length).map {|i|
    chords = []
    note_times = _note_times[0..(_note_times.length - 1)]
    dur = note_times.sample
    chords.push Chord.new(f.resume(dur, 100))
    _sub = @measure - dur
    note_times.reverse.each_with_index {|t, i|
      times = note_times[0..(note_times.length - 1 - i)]
      while _sub >= t do
        dur = times.sample
        chords.push Chord.new(f.resume(dur, 100))
        _sub = _sub - dur
      end
    }
    Measure.new(chords)
  }
end
fiber(octave, notes, num) click to toggle source
# File lib/kuromusic/Generator/Random.rb, line 4
def fiber(octave, notes, num)
  Fiber.new do |dur, velocity|
    while true do
      dur, velocity = Fiber.yield (0..(num-1)).to_a.map{|item|
        note = notes.sample
        Note.new(note, octave, velocity, dur)
      }
    end
  end
end