func uint32toBytes(value uint32) []byte {
	result := make([]byte, 4)
	binary.BigEndian.PutUint32(result, value)
	return result
}
uint32toBytes translate uint16 to []byte
package smfio
import (
	"bytes"
	"encoding/binary"
	"io"
	"github.com/algoGuy/EasyMIDI/smf"
)
func getMTrk() []byte {
	return []byte("MTrk")
}
const mtrkCode uint32 = 0x4D54726B
func parseAllTracks(midi *smf.MIDIFile, reader io.Reader, tracksNum uint16) (*smf.MIDIFile, error) {
	for i := uint16(0); i < tracksNum; i++ {
		var mtrk uint32
		if err := binary.Read(reader, binary.BigEndian, &mtrk); err != nil {
			return nil, &ReadError{"Track data corrupted"}
		}
		if mtrkCode != mtrk {
			return nil, &ReadError{"No MTrk on track start"}
		}
		var length uint32
		if err := binary.Read(reader, binary.BigEndian, &length); err != nil {
			return nil, &ReadError{"Track data corrupted"}
		}
		trackReader := &io.LimitedReader{R: reader, N: int64(length)}
		track, err := parseTrack(trackReader)
		if err != nil {
			return nil, err
		}
		err = midi.AddTrack(track)
		if err != nil {
			return nil, err
		}
	}
	return midi, nil
}
func parseTrack(reader io.Reader) (*smf.Track, error) {
	track := &smf.Track{}
	var event smf.Event
	var err error
	for !isEndOfTrack(event) {
		event, err = parseEvent(reader, event)
		if err != nil {
			return nil, &ReadError{err.Error()}
		}
		track.AddEvent(event)
	}
	return track, nil
}
func writeAllTracks(midi *smf.MIDIFile, writer io.Writer) error {
	tracksNum := midi.GetTracksNum()
	for i := uint16(0); i < tracksNum; i++ {
		err := writeTrack(midi.GetTrack(i), writer)
		if err != nil {
			return err
		}
	}
	return nil
}
func writeTrack(track *smf.Track, writer io.Writer) error {
	if track == nil {
		return &WriteError{"MIDI corrupted"}
	}
	writer.Write(getMTrk())
	buffer := &bytes.Buffer{}
	var prevEvent smf.Event
	iterator := track.GetIterator()
	for iterator.MoveNext() {
		event := iterator.GetValue()
		err := writeEvent(event, prevEvent, buffer)
		if err != nil {
			return err
		}
		prevEvent = event
	}
	writer.Write(uint32toBytes(uint32(buffer.Len())))
	writer.Write(buffer.Bytes())
	return nil
}
func isEndOfTrack(event smf.Event) bool {
	switch v := event.(type) {
	case *smf.MetaEvent:
		return v.GetMetaType() == smf.MetaEndOfTrack
	default:
		return false
	}
}