Class: Nilsteps::Sequencer

Inherits:
Object
  • Object
show all
Defined in:
lib/nilsteps.rb

Overview

16 steps / bar OSC sequencer for a track

Instance Method Summary collapse

Constructor Details

#initialize(bpm: 120, length: 16, osc_server: "localhost", osc_port: 2345) ⇒ Sequencer

Returns a new instance of Sequencer.



27
28
29
30
31
32
# File 'lib/nilsteps.rb', line 27

def initialize(bpm: 120, length: 16, osc_server: "localhost", osc_port: 2345)
  @steps = Array.new(length)
  @resolution = 16 # Cannot change resolution for the moment
  @osc_client = OSC::Client.new(osc_server, osc_port)
  @bpm_to_ms = BPMToMsec.new(bpm)
end

Instance Method Details

#note_lengthObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nilsteps.rb', line 47

def note_length
  case @resolution
  when 1
    @bpm_to_ms.whole_note
  when 2
    @bpm_to_ms.half_note
  when 4
    @bpm_to_ms.quater_note
  when 8
    @bpm_to_ms.eighth_note
  when 16
    @bpm_to_ms.sixteenth_note
  when 32
    @bpm_to_ms.thirtysecond_note
  else
    @bpm_to_ms.sixteenth_note
  end
end

#play(bars = 1) ⇒ Object

Play sequence

Parameters:

bars

Specify how many times it repeats a bar



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nilsteps.rb', line 73

def play(bars = 1)
  (1..bars).each do |_i|
    @steps.each do |step|
      if block_given?
        yield step if step
      else
        @osc_client.send(step) if step
      end
      sleep(note_length / 1000)
    end
  end
end

#setup_steps(notes) ⇒ Object

Setup notes in a secuence

Parameters:

notes

A hash for notes (e.g. { 0 => “C1”, 1 => “D2”, … })



41
42
43
44
45
# File 'lib/nilsteps.rb', line 41

def setup_steps(notes)
  notes.each do |key, value|
    @steps[key.to_i] = OSC::Message.new("/" + value.to_s, 1)
  end
end