Class: Collavoce::Voice
- Inherits:
-
Object
- Object
- Collavoce::Voice
- Defined in:
- lib/collavoce/voice.rb
Constant Summary collapse
- MidiSystem =
Java::javax.sound.midi.MidiSystem
Instance Attribute Summary collapse
-
#notes ⇒ Object
Returns the value of attribute notes.
Class Method Summary collapse
Instance Method Summary collapse
- #aug_notes(*indices) ⇒ Object
- #device ⇒ Object
- #dim_notes(*indices) ⇒ Object
-
#initialize(options = {}) ⇒ Voice
constructor
A new instance of Voice.
- #mod_notes(*indices) ⇒ Object
- #output_devices ⇒ Object
- #play(this_many = 1) ⇒ Object
- #receiver ⇒ Object
- #run ⇒ Object
- #send_note(note, channel) ⇒ Object
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( = {}) @channel = (.delete(:channel) || 1) - 1 @notes = .delete(:notes).map { |n| Note.new(n) } @bpm = .delete(:bpm) || 120 @bar_duration = (60.to_f / @bpm) * 4 end |
Instance Attribute Details
#notes ⇒ Object
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( = {}) super({:notes => @notes, :channel => @channel}.merge()) 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 |
#device ⇒ Object
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_devices ⇒ Object
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 |
#receiver ⇒ Object
52 53 54 55 |
# File 'lib/collavoce/voice.rb', line 52 def receiver return @receiver if @receiver @receiver = device.get_receiver end |
#run ⇒ Object
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 |