Touhou Wiki
Advertisement



*Return to BGM



<?php

$sample_size = 4; // 16-bit stereo

$files = scandir('.');

foreach( $files as $file )
{
	if( 0 == preg_match('/^(.*?)\.ogg$/i', $file, $match) )
		continue;
	$name = $match[1];

	if( name2title($name) == null )
		continue;

	// decode ogg file
	exec("oggdec.exe $name.ogg");

	// retrieve loop information
	$sfl = @fopen($name.'.sfl', 'r');
	if( $sfl )
	{
		fseek($sfl, 28, SEEK_SET);
		$start = 0x2C + $sample_size * b32_ul(fread($sfl, 4));

		fseek($sfl, 72, SEEK_SET);
		$length = $sample_size * b32_ul(fread($sfl, 4));

		// write titles file
		$fp = fopen("titles.txt", "w");
		fprintf($fp, "0000002C,%08X,%08X,%s\n", $start, $length, $name);
		fclose($fp);

		// process wav file
		exec("thbgmtowav.exe -l titles.txt $name.wav");

		// rename output
		rename("thbgm_01.wav", name2title($name).".wav");

		unlink("$name.wav");
	}
	else
	{// no sfl file, just copy
		rename("$name.wav", name2title($name).".wav");
	}
}

function b32_ul($tt)
{
   $nn = 0;
   for($i = 0; $i < 4; $i++){
      $nn += (int)(pow(256, $i)) * ord($tt[$i]);
   }
   return $nn;
}

function name2title($name)
{
	$titles = array (
	"op2"    => "th123 - 01 - Did You See that Shadow_",
	"select" => "th123 - 02 - Gathered Dreams",
	"ta00"   => "th123 - 03 - Usual Days",
	"ta01"   => "th123 - 04 - Argue for and Against",
	"ta03"   => "th123 - 05 - Beautiful Nature Sight",
	"ta05"   => "th123 - 06 - Swing a Fish to Drive Away Flies",
	"ta04"   => "th123 - 07 - Drunk as I Like",
	"ta06"   => "th123 - 08 - Darkening Dusk",
	"ta20"   => "th123 - 09 - The Legendary Titan",
	"ta21"   => "th123 - 10 - Our Hisou Tensoku",
	"ta22"   => "th123 - 11 - The Scenery of Living Dolls",
	"sr2"    => "th123 - 12 - The Eternal Steam Engine",
	"st30"   => "th123 - 13 - Faith Is for the Transient People",
	"st31"   => "th123 - 14 - Beloved Tomboyish Girl",
	"st32"   => "th123 - 15 - Shanghai Teahouse ~ Chinese Tea",
	"st33"   => "th123 - 16 - Solar Sect of Mystic Wisdom ~ Nuclear Fusion",
	"st34"   => "th123 - 17 - Tomorrow Will Be Special, Yesterday Was Not",
	"st35"   => "th123 - 18 - Unknown X ~ Unfound Adventure",
	"st36"   => "th123 - 19 - X, the Floating Objects in the Sky",
	"st40"   => "th123 - 20 - Dichromatic Lotus Butterfly ~ Ancients",
	"st41"   => "th123 - 21 - Love-Colored Magic",
	"st42"   => "th123 - 22 - The Grimoire of Alice",
	"st43"   => "th123 - 23 - Voile, the Magic Library",
	"st99"   => "th123 - 24 - Mystic Oriental Love Consultation (Fantasy Heaven remix)",
	);
	return @$titles[$name];
}

?>



Advertisement