CUETools.Converter: support lossy formats, encoder modes, ctrl-c

This commit is contained in:
Grigory Chudov
2013-04-11 00:05:48 -04:00
parent 2aad9888ea
commit 19a0e99a00

View File

@@ -10,27 +10,62 @@ namespace CUETools.Converter
{ {
static void Usage() static void Usage()
{ {
Console.WriteLine("Usage : CUETools.Converter.exe <infile> <outfile>"); Console.Error.WriteLine("Usage : CUETools.Converter.exe [options] <infile> <outfile>");
Console.WriteLine(); Console.Error.WriteLine();
//Console.WriteLine("-N --stdinname <t> pseudo filename to use when input from STDIN."); Console.Error.WriteLine("Options:");
//Console.WriteLine(" --stdout write processed WAV output to STDOUT."); Console.Error.WriteLine();
Console.Error.WriteLine(" --lossy Use lossy encoder.");
Console.Error.WriteLine(" --lossless Use lossless encoder (default).");
Console.Error.WriteLine(" -p # Padding bytes.");
Console.Error.WriteLine(" -m <mode> Encoder mode (0..8 for flac, V0..V9 for mp3, etc)");
Console.Error.WriteLine();
} }
static void Main(string[] args) static int Main(string[] args)
{ {
TextWriter stdout = Console.Out; bool ok = true;
Console.SetOut(Console.Error); string sourceFile = null, destFile = null;
Console.WriteLine("CUETools.Converter, Copyright (C) 2009 Grigory Chudov."); int padding = 8192;
Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to"); string encoderMode = null;
Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details."); AudioEncoderType audioEncoderType = AudioEncoderType.NoAudio;
if (args.Length < 2) for (int arg = 0; arg < args.Length; arg++)
{
if (args[arg].Length == 0)
ok = false;
else if ((args[arg] == "-p" || args[arg] == "--padding") && ++arg < args.Length)
ok = int.TryParse(args[arg], out padding);
else if ((args[arg] == "-m" || args[arg] == "--mode") && ++arg < args.Length)
encoderMode = args[arg];
else if (args[arg] == "--lossy")
audioEncoderType = AudioEncoderType.Lossy;
else if (args[arg] == "--lossless")
audioEncoderType = AudioEncoderType.Lossless;
else if (args[arg][0] != '-' && sourceFile == null)
sourceFile = args[arg];
else if (args[arg][0] != '-' && sourceFile != null && destFile == null)
destFile = args[arg];
else
ok = false;
if (!ok)
break;
}
Console.Error.WriteLine("CUETools.Converter, Copyright (C) 2009-13 Grigory Chudov.");
Console.Error.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
Console.Error.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
if (!ok || sourceFile == null || destFile == null)
{ {
Usage(); Usage();
return; return 22;
} }
string sourceFile = args[0];
string destFile = args[1];
if (File.Exists(destFile))
{
Console.Error.WriteLine("Error: file already exists.");
return 17;
}
string extension = Path.GetExtension(destFile).ToLower();
DateTime start = DateTime.Now; DateTime start = DateTime.Now;
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0); TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
CUEConfig config = new CUEConfig(); CUEConfig config = new CUEConfig();
@@ -41,12 +76,51 @@ namespace CUETools.Converter
try try
#endif #endif
{ {
IAudioSource audioSource = AudioReadWrite.GetAudioSource(sourceFile, null, config); IAudioSource audioSource = null;
IAudioDest audioDest = AudioReadWrite.GetAudioDest(AudioEncoderType.Lossless, destFile, (long)audioSource.Length, 8192, audioSource.PCM, config); IAudioDest audioDest = null;
try
{
audioSource = AudioReadWrite.GetAudioSource(sourceFile, null, config);
AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);
Console.Error.WriteLine("Filename : {0}", sourceFile);
Console.Error.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate));
Console.WriteLine("Filename : {0}", sourceFile); CUEToolsFormat fmt;
Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate)); if (!extension.StartsWith(".") || !config.formats.TryGetValue(extension.Substring(1), out fmt))
throw new Exception("Unsupported audio type: " + destFile);
CUEToolsUDC encoder = audioEncoderType == AudioEncoderType.Lossless ? fmt.encoderLossless :
audioEncoderType == AudioEncoderType.Lossy ? fmt.encoderLossy :
fmt.encoderLossless != null ? fmt.encoderLossless : fmt.encoderLossy;
if (encoder == null)
throw new Exception("Encoder available for format " + extension + ": " + (fmt.encoderLossless != null ? fmt.encoderLossless.Name + " (lossless)" : fmt.encoderLossy != null ? fmt.encoderLossy.Name + " (lossy)" : "none"));
var settings = encoder.settings.Clone();
settings.PCM = audioSource.PCM;
settings.Padding = padding;
settings.EncoderMode = encoderMode ?? settings.EncoderMode;
settings.Validate();
object o = null;
try
{
o = Activator.CreateInstance(encoder.type, destFile, settings);
}
catch (System.Reflection.TargetInvocationException ex)
{
throw ex.InnerException;
}
if (o == null || !(o is IAudioDest))
throw new Exception("Unsupported audio type: " + destFile + ": " + encoder.type.FullName);
audioDest = o as IAudioDest;
audioDest.FinalSampleCount = audioSource.Length;
bool keepRunning = true;
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
{
keepRunning = false;
if (e.SpecialKey == ConsoleSpecialKey.ControlC)
e.Cancel = true;
else
audioDest.Delete();
};
while (audioSource.Read(buff, -1) != 0) while (audioSource.Read(buff, -1) != 0)
{ {
@@ -62,14 +136,23 @@ namespace CUETools.Converter
); );
lastPrint = elapsed; lastPrint = elapsed;
} }
if (!keepRunning)
throw new Exception("Aborted");
} }
TimeSpan totalElapsed = DateTime.Now - start; TimeSpan totalElapsed = DateTime.Now - start;
Console.Error.Write("\r \r"); Console.Error.Write("\r \r");
Console.WriteLine("Results : {0:0.00}x; {1}", Console.Error.WriteLine("Results : {0:0.00}x; {1}",
audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate, audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
totalElapsed totalElapsed
); );
}
catch (Exception ex)
{
if (audioSource != null) audioSource.Close();
if (audioDest != null) audioDest.Delete();
throw ex;
}
audioSource.Close(); audioSource.Close();
audioDest.Close(); audioDest.Close();
@@ -86,11 +169,13 @@ namespace CUETools.Converter
#if !DEBUG #if !DEBUG
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(); Console.Error.Write("\r \r");
Console.WriteLine("Error: {0}", ex.Message); Console.Error.WriteLine("Error : {0}", ex.Message);
return 1;
//Console.WriteLine("{0}", ex.StackTrace); //Console.WriteLine("{0}", ex.StackTrace);
} }
#endif #endif
return 0;
} }
} }
} }