collavoce 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = collavoce
2
+
3
+ Powering MIDI through ruby.
4
+
5
+
@@ -0,0 +1,83 @@
1
+ module Collavoce
2
+ class Note
3
+ class UnparsableError < RuntimeError; end
4
+
5
+ attr_accessor :value
6
+ attr_accessor :duration
7
+
8
+ NOTES = {
9
+ "C" => 60,
10
+ "D" => 62,
11
+ "E" => 64,
12
+ "F" => 65,
13
+ "G" => 67,
14
+ "A" => 69,
15
+ "B" => 71,
16
+ }
17
+
18
+ DIVISIONS = {
19
+ "w" => 1,
20
+ "h" => 1.to_f / 2,
21
+ "q" => 1.to_f / 4,
22
+ "e" => 1.to_f / 8,
23
+ "s" => 1.to_f / 16,
24
+ "t" => 1.to_f / 32
25
+ }
26
+
27
+ def initialize(note)
28
+ case note
29
+ when String
30
+ init_from_string(note)
31
+ when Note
32
+ init_from_note(note)
33
+ end
34
+ end
35
+
36
+ def init_from_string(note)
37
+ match = note.match(/^([ABCDEFGR])([#]*)?([b]*)?(\d)?([whqest]*)/)
38
+
39
+ raise UnparsableError.new("Couldn't parse note: #{note}") unless match
40
+
41
+ if match[5].empty?
42
+ @duration = DIVISIONS["q"]
43
+ else
44
+ @duration = 0
45
+ match[5].each_char do |c|
46
+ @duration += DIVISIONS[c]
47
+ end
48
+ end
49
+
50
+ if base_value = NOTES[match[1]]
51
+ offset = (match[2] || "").length
52
+ offset -= (match[3] || "").length
53
+ octave = (match[4] || "4").to_i
54
+ @value = base_value + ((octave - 4) * 12) + offset
55
+ end
56
+ end
57
+
58
+ def init_from_note(note)
59
+ @value = note.value
60
+ @duration = note.duration
61
+ end
62
+
63
+ def aug!
64
+ @value += 1
65
+ end
66
+
67
+ def dim!
68
+ @value -= 1
69
+ end
70
+
71
+ def aug
72
+ copy = Note.new(self)
73
+ copy.aug!
74
+ copy
75
+ end
76
+
77
+ def dim
78
+ copy = Note.new(self)
79
+ copy.dim!
80
+ copy
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,50 @@
1
+ module Collavoce
2
+ class Voice
3
+ include Java
4
+
5
+ MidiSystem = javax.sound.midi.MidiSystem
6
+ ShortMessage = javax.sound.midi.ShortMessage
7
+
8
+ BPM = 160
9
+ BarDuration = (60.to_f / BPM) * 4
10
+
11
+ attr_accessor :notes
12
+
13
+ def initialize(options = {})
14
+ @channel = (options.delete(:channel) || 1) - 1
15
+ @notes = options.delete(:notes).map { |n| Note.new(n) }
16
+ end
17
+
18
+ def device
19
+ all = MidiSystem.get_midi_device_info.to_a
20
+ possible = all.select { |i| i.get_name == "Bus 1" }
21
+ devices = possible.map { |i| MidiSystem.get_midi_device(i) }
22
+ device = devices.select { |d| d.get_max_receivers != 0 }.first
23
+ device.open
24
+ device
25
+ end
26
+
27
+ def receiver
28
+ return @receiver if @receiver
29
+ @receiver = device.get_receiver
30
+ end
31
+
32
+ def send_note(note, channel)
33
+ noteon = ShortMessage.new
34
+ noteon.set_message(ShortMessage::NOTE_ON, channel, note.value, 127);
35
+ noteoff = ShortMessage.new
36
+ noteoff.set_message(ShortMessage::NOTE_OFF, channel, note.value, 127);
37
+ receiver.send(noteon, 0)
38
+ sleep BarDuration * note.duration
39
+ receiver.send(noteoff, 0)
40
+ end
41
+
42
+ def play(this_many = 1)
43
+ this_many.times do
44
+ @notes.each do |note|
45
+ send_note(note, @channel)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/collavoce.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'collavoce/note'
2
+ require 'collavoce/voice'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collavoce
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Mat Schaffer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-24 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description:
33
+ email: mat@schaffer.me
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README.rdoc
40
+ files:
41
+ - README.rdoc
42
+ - lib/collavoce.rb
43
+ - lib/collavoce/note.rb
44
+ - lib/collavoce/voice.rb
45
+ has_rdoc: true
46
+ homepage: https://github.com/matschaffer/collavoce
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: nowarning
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Powering MIDI through ruby
75
+ test_files: []
76
+