#! /usr/bin/env ruby

# dump.rb: Written by Tadayoshi Funaba 2003
# $Id: dump.rb,v 1.2 2003-08-12 05:06:21+09 tadf Exp $

require 'smf'
include  SMF

def usage
  warn 'usage: dump file'
  exit 1
end

usage unless $*.size == 1
file = $*.shift

sq = Sequence.decodefile(file)

printf('Sequence %d %d %d', sq.format, sq.ntrks, sq.division)
printf(' %d', sq.smpte) if sq.smpte
puts

sq.each do |tr|

  puts('Track')

  tr.each do |ev|

    printf('%d %s', ev.offset, ev.class.to_s.split('::')[-1])

    case ev
    when NoteOff, NoteOn
      printf(' %d %d %d', ev.ch, ev.note, ev.vel)
    when PolyphonicKeyPressure
      printf(' %d %d %d', ev.ch, ev.note, ev.val)
    when ControlChange
      printf(' %d %d %d', ev.ch, ev.num, ev.val)
    when ProgramChange
      printf(' %d %d', ev.ch, ev.num)
    when ChannelPressure
      printf(' %d %d', ev.ch, ev.val)
    when PitchBendChange
      printf(' %d %d', ev.ch, ev.val)
    when ChannelModeMessage
      printf(' %d %d %d', ev.ch, ev.num, ev.val)
    when Exclusive
      ev.data.each_byte do |c|
	printf(' %d', c)
      end
    when SequenceNumber
      printf(' %d', ev.num)
    when Text
      printf(' %s', ev.text.inspect)
    when ChannelPrefix
      printf(' %d', ev.ch)
    when MIDIPort
      printf(' %d', ev.num)
    when EndOfTrack
    when SetTempo
      printf(' %d', ev.tempo)
    when SMPTEOffset
      printf(' %d %d %d %d %d', ev.hr, ev.mn, ev.se, ev.fr, ev.ff)
    when TimeSignature
      printf(' %d %d %d %d', ev.nn, ev.dd, ev.cc, ev.bb)
    when KeySignature
      printf(' %d %d', ev.sf, ev.mi)
    when SequencerSpecific
      ev.data.each_byte do |c|
	printf(' %d', c)
      end
    end

    puts

  end
end
