forked from jimm/midilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.rb
executable file
·48 lines (41 loc) · 1.36 KB
/
split.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/env ruby
#
# usage: split.rb [-x] [midi_file]
#
# This script splits a MIDI file into muliple files, one for each track. The
# output files are named with the track's names. Each file contains a copy of
# the 0th track, which contains tempo information.
#
# If -x is specified, the 0th temp track is not included in each file.
# Instead, it is output in a separate file named 'tempo_track.mid'.
require_relative '../lib/midilib/sequence'
DEFAULT_MIDI_TEST_FILE = 'NoFences.mid'
# Command line argument processing
filename = ARGV[0]
include_tempo_track = true
if filename == '-x'
include_tempo_track = false
filename = ARGV[1]
end
# Read from MIDI file
seq = MIDI::Sequence.new
File.open(filename || DEFAULT_MIDI_TEST_FILE, 'rb') do |file|
# The block we pass in to Sequence.read is called at the end of every
# track read. It is optional, but is useful for progress reports.
seq.read(file) do |track, num_tracks, i|
puts "read track #{track ? track.name : ''} (#{i} of #{num_tracks})"
end
end
t0 = seq.tracks[0]
unless include_tempo_track
s = MIDI::Sequence.new
s.tracks << t0
File.open('tempo_track.mid', 'wb') { |file| s.write(file) }
end
seq.each_with_index do |track, i|
next unless i > 0
s = MIDI::Sequence.new
s.tracks << t0 if include_tempo_track
s.tracks << track
File.open("#{track.name}.mid", 'wb') { |file| s.write(file) }
end