#! /usr/bin/env ruby

# split.rb: Written by Tadayoshi Funaba 1999-2001, 2003
# $Id: split.rb,v 1.7 2003-08-12 05:06:21+09 tadf Exp $

require 'smf'
require 'smf/divert'
include  SMF

def usage
  warn 'usage: split ifile [ofile]'
  exit 1
end

usage unless $*.size >= 1 and $*.size <= 2
ifile = $*.shift
ofile = $*.shift
ofile ||= File.basename(ifile, '.mid') + '-split.mid'

sq = Sequence.decodefile(ifile)
nsq = Sequence.new(1, sq.division)
sq.each do |tr|
  tr.divert(nsq) do |ev|
    case ev
    when EndOfTrack;     nil
    when ChannelMessage; ev.ch + 1
    else;                0
    end
  end
end
nsq.encodefile(ofile)
