Class: Collavoce::Voice

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

Constant Summary collapse

MidiSystem =
Java::javax.sound.midi.MidiSystem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Voice

Returns a new instance of Voice.



19
20
21
22
23
24
# File 'lib/collavoce/voice.rb', line 19

def initialize(options = {})
  @channel      = (options.delete(:channel) || 1) - 1
  @notes        = options.delete(:notes).map { |n| Note.new(n) }
  @bpm          = options.delete(:bpm) || 120
  @bar_duration = (60.to_f / @bpm) * 4
end

Instance Attribute Details

#notesObject

Returns the value of attribute notes.



5
6
7
# File 'lib/collavoce/voice.rb', line 5

def notes
  @notes
end

Class Method Details

.channel(channel) ⇒ Object



11
12
13
# File 'lib/collavoce/voice.rb', line 11

def self.channel(channel)
  @channel = channel
end

.new(options = {}) ⇒ Object



15
16
17
# File 'lib/collavoce/voice.rb', line 15

def self.new(options = {})
  super({:notes => @notes, :channel => @channel}.merge(options))
end

.notes(notes) ⇒ Object



7
8
9
# File 'lib/collavoce/voice.rb', line 7

def self.notes(notes)
  @notes = notes
end

Instance Method Details

#aug_notes(*indices) ⇒ Object



91
92
93
94
95
# File 'lib/collavoce/voice.rb', line 91

def aug_notes(*indices)
  mod_notes(*indices) do |n|
    n.aug!
  end
end

#deviceObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/collavoce/voice.rb', line 35

def device
  name = Collavoce.device_name
  selected_device = output_devices.detect do |device|
    device.get_device_info.get_name == name
  end

  if !selected_device
    raise "Couldn't find device called #{name}" if name
    raise "No output devices available" if output_devices.empty?
    selected_device = output_devices.first
    $stderr.puts "INFO: Sending notes to #{selected_device.get_device_info.get_name}"
  end

  selected_device.open
  selected_device
end

#dim_notes(*indices) ⇒ Object



85
86
87
88
89
# File 'lib/collavoce/voice.rb', line 85

def dim_notes(*indices)
  mod_notes(*indices) do |n|
    n.dim!
  end
end

#mod_notes(*indices) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/collavoce/voice.rb', line 74

def mod_notes(*indices)
  if indices.empty?
    notes_to_mod = notes
  else
    notes_to_mod = notes.values_at(*indices)
  end
  notes_to_mod.each do |n|
    yield n
  end
end

#output_devicesObject



26
27
28
29
30
31
32
33
# File 'lib/collavoce/voice.rb', line 26

def output_devices
  devices = MidiSystem.get_midi_device_info.to_a.map do |info|
    MidiSystem.get_midi_device(info)
  end
  devices.select do |device|
    device.get_max_receivers != 0
  end
end

#play(this_many = 1) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/collavoce/voice.rb', line 61

def play(this_many = 1)
 this_many.times do
   @notes.each do |note|
     break unless Collavoce.running
     send_note(note, @channel)
   end
 end
end

#receiverObject



52
53
54
55
# File 'lib/collavoce/voice.rb', line 52

def receiver
  return @receiver if @receiver
  @receiver = device.get_receiver
end

#runObject



70
71
72
# File 'lib/collavoce/voice.rb', line 70

def run
  play
end

#send_note(note, channel) ⇒ Object



57
58
59
# File 'lib/collavoce/voice.rb', line 57

def send_note(note, channel)
  note.play(receiver, channel, @bar_duration)
end