#!/usr/bin/perl
#
# dx7sysex2lv2presets - SysEx patchset to LV2 converter for
# Dexed.lv2 - LV2 plugin
#
# This generator converts DX7 SYSEX to LV2 suitable sounds for Dexed
#
# (c) by H. Wirtz <dcoredump@googlemail.com>
#
# This program 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; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
#
# Hints:
# https://github.com/rogerallen/dxsyx/blob/master/dx7-sysex-format.txt
#
# $ hexdump -v -s6 -e '/1  "%_ad#    "' -e '/1    "%02X hex"' -e '/1 " | %03i dec"' -e '/1 " | %03o oct"' -e '/1 " | _%c\_\n"' rom1A.syx |less
#

$DXSYX="/usr/local/bin/dxsyx";

$LV2_BUNDLE_NAME="dexed.lv2";
$LV2_URI="https://github.com/dcoredump/".$LV2_BUNDLE_NAME;
$LV2_INSTALL_PATH="/zynthian/zynthian-my-plugins/lv2";

@CMD_OPTION=@ARGV;
while($option=shift(@CMD_OPTION))
{
	if($option=~/^-(.+)\s*/)
	{
		if($1=~/b/i)
		{
			$option{'bank'}=1;
		}
		elsif($1=~/n/i)
		{
			$option{'voice_no'}=1;
		}
	}
}

while($SYSEX=shift(@ARGV))
{
	next if($SYSEX=~/^-/);

	$BANK=$SYSEX;
	$BANK=~s{.*/}{};      # removes path  
	$BANK=~s{\.[^.]+$}{}; # removes extension

	$_LV2_BUNDLE_NAME="dexed_lv2";

	$voice_no=0;

	open(SYSEX,"$DXSYX -y $SYSEX |") || die ("Cannot open \'$DXSYX -y $SYSEX\': $!");
	while($s=<SYSEX>)
	{
		chop($s);
		next if($s=~/^\s*$/);
		next if($s=~/^\s*---\s*$/);
		next if($s=~/^\s*filename/);
		if($s=~/^\s+voice_name:\s+(.+)\s*/)
		{
			my($voice)=$1;
			$voice_no++;
			$voice=~s/\s+$//; # right trim
			my($voice_name)=$voice;
       		 	$voice=~tr/[ %§&-*+\"\'`\!\$\%\?\/\<\>\[\]\^\{\}]\|/_/;
       		 	$voice=~s/%3c/_/i;
       		 	$voice=~s/%3e/_/i;
       		 	$voice=~s/%5b/_/i;
       		 	$voice=~s/%5d/_/i;
       		 	$voice=~s/%5e/_/i;
       		 	$voice=~s/%7b/_/i;
       		 	$voice=~s/%7d/_/i;
       		 	$voice=~s/%7c/_/i;

			if(defined($option{'voice_no'}) && defined($option{'bank'}))
			{
				if($voice_no<10)
				{
					$voice=$BANK."__0".$voice_no."__".$voice;
				}
				else
				{
					$voice=$BANK."__".$voice_no."__".$voice;
				}
			}
			elsif(defined($option{'bank'}))
			{
				$voice=$BANK."__".$voice;
			}
			elsif(defined($option{'voice_no'}))
			{
				$voice=$voice_no."__".$voice;
			}

			if($deeaxe_voice)
			{
				print $deeaxe_voice "        ] .\n";
				close($deeaxe_voice);
			}
	
			my($path)=$LV2_INSTALL_PATH."/".$_LV2_BUNDLE_NAME."-".$voice.".lv2";

			if(!-d $LV2_INSTALL_PATH)
			{
				mkdir($LV2_INSTALL_PATH) || die("Cannot create dir \'$LV2_INSTALL_PATH\':$!\n");
			}
			if(!-d $path)
			{
				mkdir($path) || die("Cannot create dir \'$path\':$!\n");
			}

			print "Writing ".$path."/".$voice.".ttl\n";

			write_manifest($path,$voice);

			open($deeaxe_voice,">".$path."/".$voice.".ttl") || die ("Cannot open \'".$path."/".$voice.".ttl\': $!");
			write_preset_header($voice,$voice_name);
			next;
		}
		else
		{
			print $deeaxe_voice "        ] , [\n";
		}
		if($s=~/^\s+(.+):\s*(\d+)/)
		{
			print_lv2_port($1,$2);
		}
	}

	if($deeaxe_voice)
	{
		print $deeaxe_voice "        ] .\n";
		close($deeaxe_voice);
	}
	close(SYSEX);
}

sub write_preset_header
{
	($voice,$voice_name)=@_;
        print $deeaxe_voice "\@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
        print $deeaxe_voice "\@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
        print $deeaxe_voice "\@prefix pset: <http://lv2plug.in/ns/ext/presets#> .\n";
        print $deeaxe_voice "\@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
        print $deeaxe_voice "\@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
        print $deeaxe_voice "\@prefix state: <http://lv2plug.in/ns/ext/state#> .\n";
        print $deeaxe_voice "\@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n";
        print $deeaxe_voice "\n";
        #print $deeaxe_voice "<".$LV2_URI."#".$voice.">\n";
        print $deeaxe_voice "<>\n";
        print $deeaxe_voice "        a pset:Preset ;\n";
        print $deeaxe_voice "        lv2:appliesTo <".$LV2_URI."> ;\n";
        #print $deeaxe_voice "        rdfs:label \"".$voice_name."\" ;\n";
        print $deeaxe_voice "        lv2:port [\n";
        print_lv2_port("cutoff","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("resonance","0.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("output","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("engine","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("number_of_voices","16");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("polymono","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("pitch_bend_range","1");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("pitch_bend_step","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("mod_wheel_range","99");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("mod_wheel_assign","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("foot_ctrl_range","99");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("foot_ctrl_assign","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("breath_ctrl_range","99");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("breath_ctrl_assign","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("aftertouch_range","99");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("aftertouch_assign","0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("master_tune","0.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op1_enable","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op2_enable","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op3_enable","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op4_enable","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op5_enable","1.0");
	print $deeaxe_voice "        ] , [\n";
        print_lv2_port("op6_enable","1.0");
}

sub write_manifest
{
	($path,$voice)=@_;

        open(MANIFEST,">$path/manifest.ttl")||die("Cannot open \'$path/manifest.ttl\': $!\n");
        print MANIFEST "\@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
        print MANIFEST "\@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
        print MANIFEST "\@prefix pset: <http://lv2plug.in/ns/ext/presets#> .\n";
        print MANIFEST "\@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
        print MANIFEST "\@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
        print MANIFEST "\@prefix state: <http://lv2plug.in/ns/ext/state#> .\n";
        print MANIFEST "\@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n";

        print MANIFEST "\n";
        #print MANIFEST "<".$LV2_URI."#".$voice.">\n";
        print MANIFEST "<".$voice.".ttl>\n";
        print MANIFEST "        lv2:appliesTo <".$LV2_URI."> ;\n";
        #print MANIFEST "        a pset:Bank $BANK ;\n";
        print MANIFEST "        a pset:Preset ;\n";
        print MANIFEST "        rdfs:label \"".$voice."\" ;\n";
        print MANIFEST "        rdfs:seeAlso <".$voice.".ttl> .\n";
        close(MANIFEST);
}

sub print_lv2_port
{
        ($port_name,$port_val)=@_;

	# Specials
	if($port_name eq "algorithm_num")
	{
		$port_val+=1;
	}
	if($port_name=~/op\d_osc_detune/)
	{
		$port_val-=7;
	}

        print $deeaxe_voice "                lv2:symbol \"".$port_name."\" ;\n";
	$port_val.=".0" if($port_val!~/\./);
        print $deeaxe_voice "                pset:value ".$port_val."\n";
}

