#
#	MTXHEADER.AWK
#
#  -----------------------------------------------------------------------
#  Copyright 2007  Alfred Steffens Jr.
#  -----------------------------------------------------------------------
#  -----------------------------------------------------------------------
#    Copying Permission:
#
#    mscore is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation.
#
#    mscore is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with mscore (see file called "COPYING"); if not, write to the Free
#    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#    02111-1307  USA
#  ------------------------------------------------------------------------
#
#   Mscore release 1.1
#
#
#	Usage:
#
#		echo "junk" | awk -f mtxheader.awk [options] >  header.mtx
#
#
#		options:
#
#		numtracks
#		bpm
#		ticks
#		name
#		tracks
#		programs
#		volumes
#
BEGIN {
	numtracks = 1		# default -- number of tracks
	bpm = 120			# default
	ticks = 220			# default
	name = ""
	tracks = ""			#	list of channel numbers to be assigned to tracks
	programs = ""		#	list of program numbers to be assigned to tracks
	volumes = ""		#	list of volumes to be assigned to tracks
}

{


}


END {

	print "#	-----------------------------------------------------------"
	print "#	MIDI SCORE FILE   (from mtxheader.awk)"
	print "#	-----------------------------------------------------------"
	print "#"
	print "#	" name "	--	MTX file"
	print "#"

	if (tracks != "") {
		numtracks = split(tracks, tary, ",")
	}
	if (programs != "") {
		perr = split(programs, pary, ",")
	}
	if (volumes != "") {
		verr = split(volumes, vary, ",")
	}

	if (numtracks > 1) {
		print "MIDI_FORMAT multi_track"
	}
	else {
		print "MIDI_FORMAT single_track"
	}

	print "TRACKS " numtracks

	print "TICS_PER_BEAT " ticks
	print "#	-----------------------------------------------------------"

	for (i=1; i<= numtracks; i++) {


		if (tary[i] < 1 || tary[i] > 16) {
			#
			#	invalid channel number
			#	we allow this in the case where we need a dummy track
			#
			print "	0		META	TK" i "	MIDIPORT	" 0
			print "	0		META	TK" i "	TEMPO	" bpm
			print "	0		META	TK" i "	TIME_SIGNATURE	04	02	24	08"
			continue
		} # if -- invalid channel number

		print "	0		META	TK" i "	MIDIPORT	" 0
		print "	0		META	TK" i "	TEMPO	" bpm
		print "	0		META	TK" i "	TIME_SIGNATURE	04	02	24	08"
		if (tracks != "") {
			if (volumes != ""  &&  i <= verr) {
				print "	0		MIDI	TK" i "	CH" tary[i] "	CT7	VL" vary[i]
			}
			else {
				print "	0		MIDI	TK" i "	CH" tary[i] "	CT7	VL90"
			}
			if (programs != ""  &&  i <= perr) {
				#
				#	don't set a program if it is set to -1
				#
				if (pary[i] >= 0) {
					print "	0		MIDI	TK" i "	CH" tary[i] "	PG" pary[i]
				}
			}
		}
	}


	print "#	-------------------------------------------------------------"

}

