From 843da643c4068b7751d04ee9b768a325556335cb Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 16 Apr 2016 19:56:40 +1000 Subject: [PATCH] Write out .wav files instead of hardcoded binary format. Need to update the client to support this method. --- Launcher2/Patcher/ResourceFetcher.cs | 6 +-- Launcher2/Patcher/SoundPatcher.cs | 65 +++++++++++++++------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Launcher2/Patcher/ResourceFetcher.cs b/Launcher2/Patcher/ResourceFetcher.cs index 69eeea44f..4e77d9872 100644 --- a/Launcher2/Patcher/ResourceFetcher.cs +++ b/Launcher2/Patcher/ResourceFetcher.cs @@ -31,11 +31,9 @@ namespace Launcher { public void DownloadItems( AsyncDownloader downloader, Action setStatus ) { this.downloader = downloader; DownloadMusicFiles(); - digPatcher = new SoundPatcher( digSounds, "dig_", - "step_cloth1", digPath ); + digPatcher = new SoundPatcher( digSounds, "dig_", "step_cloth1" ); digPatcher.FetchFiles( digSoundsUri, altDigSoundsUri, this ); - stepPatcher = new SoundPatcher( stepSounds, "step_", - "classic jar", stepPath ); + stepPatcher = new SoundPatcher( stepSounds, "step_", "classic jar" ); stepPatcher.FetchFiles( stepSoundsUri, altStepSoundsUri, this ); if( !defaultZipExists ) { downloader.DownloadData( jarClassicUri, false, "classic_jar" ); diff --git a/Launcher2/Patcher/SoundPatcher.cs b/Launcher2/Patcher/SoundPatcher.cs index c28efc4ca..eccb48402 100644 --- a/Launcher2/Patcher/SoundPatcher.cs +++ b/Launcher2/Patcher/SoundPatcher.cs @@ -11,21 +11,16 @@ namespace Launcher { string[] files, identifiers; string prefix, nextAction; - FileStream outData; - StreamWriter outText; - RawOut outDecoder; public bool Done; - public SoundPatcher( string[] files, string prefix, - string nextAction, string outputPath ) { + public SoundPatcher( string[] files, string prefix, string nextAction ) { this.files = files; this.prefix = prefix; this.nextAction = nextAction; - InitOutput( outputPath ); } const StringComparison comp = StringComparison.OrdinalIgnoreCase; - public void FetchFiles( string baseUrl, string altBaseUrl, ResourceFetcher fetcher ) { + public void FetchFiles( string baseUrl, string altBaseUrl, ResourceFetcher fetcher ) { identifiers = new string[files.Length]; for( int i = 0; i < files.Length; i++ ) identifiers[i] = prefix + files[i].Substring( 1 ); @@ -51,7 +46,6 @@ namespace Launcher { // TODO: setStatus( next ); if( i == identifiers.Length - 1 ) { - Dispose(); Done = true; setStatus( fetcher.MakeNext( nextAction ) ); } else { @@ -63,35 +57,44 @@ namespace Launcher { } void DecodeSound( string name, byte[] rawData ) { - long start = outData.Position; - using( MemoryStream ms = new MemoryStream( rawData ) ) { - OggContainer container = new OggContainer( ms ); - outDecoder.PlayStreaming( container ); + string path = Path.Combine( Program.AppDirectory, "audio" ); + path = Path.Combine( path, prefix + name + ".wav" ); + + using( FileStream dst = File.Create( path ) ) + using ( MemoryStream src = new MemoryStream( rawData ) ) + { + dst.SetLength( 44 ); + RawOut output = new RawOut( dst, true ); + OggContainer container = new OggContainer( src ); + output.PlayStreaming( container ); + + dst.Position = 0; + BinaryWriter w = new BinaryWriter( dst ); + WriteWaveHeader( w, dst, output ); } - - long len = outData.Position - start; - outText.WriteLine( format, name, outDecoder.Frequency, - outDecoder.BitsPerSample, outDecoder.Channels, - start, len ); } - const string format = "{0},{1},{2},{3},{4},{5}"; - void InitOutput( string outputPath ) { - outData = File.Create( outputPath + ".bin" ); - outText = new StreamWriter( outputPath + ".txt" ); - outDecoder = new RawOut( outData, true ); + void WriteWaveHeader( BinaryWriter w, Stream stream, RawOut data ) { + WriteFourCC( w, "RIFF" ); + w.Write( (int)(stream.Length - 8) ); + WriteFourCC( w, "WAVE" ); - outText.WriteLine( "# This file indicates where the various raw decompressed sound data " + - "are found in the corresponding raw .bin file." ); - outText.WriteLine( "# Each line is in the following format:" ); - outText.WriteLine( "# Identifier, Frequency/Sample rate, BitsPerSample, " + - "Channels, Offset from start, Length in bytes" ); + WriteFourCC( w, "fmt " ); + w.Write( 16 ); + w.Write( (ushort)1 ); // audio format, PCM + w.Write( (ushort)data.Channels ); + w.Write( data.Frequency ); + w.Write((data.Frequency * data.Channels * data.BitsPerSample) / 8 ); // byte rate + w.Write( (ushort)((data.Channels * data.BitsPerSample) / 8) ); // block align + w.Write( (ushort)data.BitsPerSample ); + + WriteFourCC( w, "data" ); + w.Write( (int)(stream.Length - 44) ); } - void Dispose() { - outDecoder.Dispose(); - outData.Close(); - outText.Close(); + void WriteFourCC( BinaryWriter w, string fourCC ) { + w.Write( (byte)fourCC[0] ); w.Write( (byte)fourCC[1] ); + w.Write( (byte)fourCC[2] ); w.Write( (byte)fourCC[3] ); } } }