#!/usr/bin/env python
#
# Copyright (c) 2011, 2012
# Contributors to the Freedoom project.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of the freedoom project nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ----------------------------------------------------------------------
#
# Dump the contents of a GENMIDI lump into separate .sbi instrument files,
# and output a config to stdout.
#

import os
import sys

import genmidi
import sbi_file
import midi
import sbi_file

def is_null_voice(voice_data):
	for f in sbi_file.FIELDS:
		if voice_data[f] != 0:
			return False

	return True

def instr_to_str_def(filename, filename2, instr):
	if is_null_voice(instr.voice1):
		return "NullInstrument"

	args = [ "\"%s\"" % filename ]

	if instr.voice2 is not None:
		args.append("\"%s\"" % filename2)

	if instr.fixed_note is not None:
		args.append("note=%s" % midi.def_for_note(instr.fixed_note))
	if instr.offset1 != 0:
		args.append("off1=%i" % instr.offset1)
	if instr.offset2 != 0:
		args.append("off2=%i" % instr.offset2)

	return "Instrument(%s)" % (", ".join(args))

def print_instr_def(filename, filename2, instr):
	print("\t%s," % instr_to_str_def(filename, filename2, instr))

def dump_instrument(filename, filename2, instr):

	if is_null_voice(instr.voice1):
		return

	sbi_file.write(os.path.join("instruments", filename), instr.voice1)

	if instr.voice2 is not None:
		sbi_file.write(os.path.join("instruments", filename2),
		               instr.voice2)

if len(sys.argv) != 2:
	sys.stderr.write("Usage: %s <filename>\n" % sys.argv[0])
	sys.exit(-1)

instruments = genmidi.read(sys.argv[1])

main_instrs = instruments[0:128]
percussion = instruments[128:]

print("INSTRUMENTS = [")

for i in range(len(main_instrs)):
	instr = main_instrs[i]
	filename = "instr%03i.sbi" % (i+1)
	filename2 = "instr%03i-2.sbi" % (i+1)
	dump_instrument(filename, filename2, instr)
	print_instr_def(filename, filename2, instr)

print("]")
print("")
print("PERCUSSION = [")

for i in range(len(percussion)):
	instr = percussion[i]
	filename = "perc%02i.sbi" % (i+35)
	filename2 = "perc%02i-2.sbi" % (i+35)
	dump_instrument(filename, filename2, instr)
	print_instr_def(filename, filename2, instr)

print("]")

