Skip to content
Browse files

Now running with the midilib gem as a dependency.

  • Loading branch information...
1 parent 7691150 commit 5cf79915dda155d4e8348750d731739c85ac1e60 Jeremy Voorhis committed Nov 10, 2008
Showing with 17 additions and 68 deletions.
  1. +0 −18 lib/music/midi_time.rb
  2. +17 −20 lib/music/smf_writer.rb
  3. +0 −30 spec/midi_spec.rb
View
18 lib/music/midi_time.rb
@@ -1,18 +0,0 @@
-module Music
- class MidiTime
- attr :resolution
-
- def initialize(res)
- @resolution = res
- end
-
- def ppqn(val)
- case val
- when Numeric
- (val * resolution).round
- else
- raise ArgumentError, "Cannot convert #{val}:#{val.class} to midi time."
- end
- end
- end
-end
View
37 lib/music/smf_writer.rb
@@ -1,43 +1,44 @@
-require 'smf'
-require 'music/midi_time'
+require 'rubygems'
+require 'midilib'
module Music
class SMFWriter
- include SMF
+ include ::MIDI
def initialize(options = {})
- @time = MidiTime.new(options.fetch(:resolution, 480))
- @seq = Sequence.new(1, @time.resolution)
- @tempo = SetTempo.new(1, bpm_to_qn_per_usec(options[:tempo]))
+ @seq = Sequence.new
+ @tempo = Tempo.new(Tempo.bpm_to_mpq(options[:tempo]))
end
def track(arrangement_or_timeline, options = {})
timeline = arrangement_or_timeline.to_timeline
- track = Track.new
+ track = Track.new(@seq)
channel = options.fetch(:channel, 1)
name = options.fetch(:name, gen_seq_name)
- track << SequenceName.new(0, name)
- track << @tempo
+ track.events << MetaEvent.new(META_SEQ_NAME, name)
+ track.events << @tempo
timeline.each_with_time do |note, time|
- attack = @time.ppqn(time)
- release = attack + @time.ppqn(note.duration)
+ attack = @seq.length_to_delta(time)
+ release = @seq.length_to_delta(note.duration)
pitch = note.pitch.to_i
velocity = note.attributes.fetch(:velocity, 80)
- track << NoteOn.new(attack, channel, pitch, velocity)
- track << NoteOff.new(release, channel, pitch, velocity)
+ track.merge [
+ NoteOnEvent.new(channel, pitch, velocity, attack),
+ NoteOffEvent.new(channel, pitch, velocity, release)
+ ]
end
- @seq << track
+ @seq.tracks << track
self
end
def save(basename)
- filename = basename + '.mid'
- @seq.save(filename)
+ filename = 'midilib' + basename + '.mid'
+ open(filename, 'wb') { |f| @seq.write(f) }
end
protected
@@ -46,9 +47,5 @@ def gen_seq_name
@seqn += 1
"Untitled #@seqn"
end
-
- def bpm_to_qn_per_usec(bpm)
- (60_000_000.0 / bpm).to_i
- end
end
end
View
30 spec/midi_spec.rb
@@ -1,30 +0,0 @@
-require File.join( File.dirname(__FILE__), 'spec_helper')
-require 'rational'
-require 'music/midi_time'
-
-include Music
-
-describe Music, "midi interface" do
-
- describe MidiTime do
- it "should convert qn to ppqn" do
- t = MidiTime.new(480)
- { 1 => 480,
- 1.0 => 480,
- 2 => 960,
- 2.0 => 960,
- 2.quo(1) => 960,
- 0.5 => 240,
- 1.quo(2) => 240,
- 1.quo(3) => 160,
- 1.quo(7) => 69
- }.each do |dur,ppqn|
- x = t.ppqn(dur)
- x.should be_kind_of(Integer)
- x.should == ppqn
- end
-
- proc { t.ppqn(".5") }.should raise_error(ArgumentError)
- end
- end
-end

0 comments on commit 5cf7991

Please sign in to comment.
Something went wrong with that request. Please try again.