Last active
August 29, 2015 14:16
-
-
Save julbouln/6a392e60c47ee2aec45d to your computer and use it in GitHub Desktop.
Simple ruby midi file player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require "unimidi" | |
require 'midilib/sequence' | |
# Prompts the user to select a midi output | |
# Sends some arpeggiated chords to the output | |
output = UniMIDI::Output.gets | |
begin | |
# Create a new, empty sequence. | |
seq = MIDI::Sequence.new() | |
# Read the contents of a MIDI file into the sequence. | |
File.open(ARGV[0], 'rb') { | file | | |
seq.read(file) | |
} | |
play_track=MIDI::Track.new(seq) | |
seq.each { | track | | |
puts track.name | |
play_track.merge(track.events) | |
} | |
play_track.recalc_times | |
#output.puts [0xF0,0x7F,0x7F,0x04,0x01,0x00,0x10,0xF7] | |
if true | |
play_track.each { | event | | |
time=seq.pulses_to_seconds(event.delta_time) | |
if MIDI::NoteEvent === event or | |
MIDI::Controller === event or | |
MIDI::ProgramChange === event or | |
MIDI::SystemCommon === event or | |
MIDI::ChannelPressure === event or | |
MIDI::PitchBend === event or | |
MIDI::SystemExclusive === event | |
puts "#{event.delta_time} (#{time}) #{event.class}: #{event}/#{event.data_as_bytes}" | |
sleep(time) | |
output.puts event.data_as_bytes | |
else | |
puts "IGNORED #{event.delta_time} (#{time}) #{event.class}: #{event}/#{event.data_as_bytes}" | |
sleep(time) | |
end | |
} | |
end | |
rescue SystemExit, Interrupt | |
puts "exit" | |
16.times do |i| | |
output.puts [0xB0+i,0x7B,0x00] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment