Files
cuetools.net/CUETools.LossyWAV/Program.cs

144 lines
6.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using CUETools;
using CUETools.Codecs;
using CUETools.Codecs.LossyWAV;
namespace CUETools.LossyWAVSharp
{
class Program
{
static void Usage()
{
Console.WriteLine("Usage : CUETools.LossyWAV.exe <file.wav> <options>");
Console.WriteLine();
Console.WriteLine("Quality Options:");
Console.WriteLine();
Console.WriteLine("-I, --insane highest quality output, suitable for transcoding;");
Console.WriteLine("-E, --extreme high quality output, also suitable for transcoding;");
Console.WriteLine("-S, --standard default quality output, considered to be transparent;");
Console.WriteLine("-P, --portable good quality output for DAP use. Not considered to be fully");
Console.WriteLine(" transparent, but considered fit for its intended purpose.");
Console.WriteLine();
Console.WriteLine("Standard Options:");
Console.WriteLine();
Console.WriteLine("-C, --correction write correction file for processed WAV file; default=off.");
Console.WriteLine("-h, --help display help.");
Console.WriteLine();
Console.WriteLine("Advanced Options:");
Console.WriteLine();
Console.WriteLine("- if filename=\"-\" then WAV input is taken from STDIN.");
Console.WriteLine("-q, --quality <n> quality preset (10=highest quality, 0=lowest bitrate;");
Console.WriteLine(" default = --standard = 5; --insane = 10; --extreme = 7.5;");
Console.WriteLine(" --portable = 2.5)");
Console.WriteLine("-N --stdinname <t> pseudo filename to use when input from STDIN.");
Console.WriteLine(" --stdout write processed WAV output to STDOUT.");
}
static void Main(string[] args)
{
Console.SetOut(Console.Error);
Console.WriteLine("LossyWAV {0}, Copyright (C) 2007,2008 Nick Currie, Copyleft.", LossyWAVWriter.version_string);
Bump copyright year to 2020 The copyright year was last time updated in 2018. There is some cleanup involved in this commit and the next copyright year update is supposed to be simpler (i.e. substitute "-2020"). - Substitute occurrences of "-2018" with "-2020" using: git grep -I -l -e '-2018' -- ':(exclude)*.bak' | xargs \ sed -b -i -e 's/-2018/-2020/g' - Update special cases: CUEPlayer git grep -I -l -e 'Grigory Chudov 2010' -- | xargs \ sed -b -i -e 's/Grigory Chudov 2010/2010-2020 Grigory Chudov/g' CUERipper git grep -I -l -e '2008-2009' -- | xargs \ sed -b -i -e 's/2008-2009/2008-2020/g' CUETools, CUETools.FLACCL.cmd git grep -I -l -e '2008-2010' -- ':(exclude)*FlaCuda*' | xargs \ sed -b -i -e 's/2008-2010/2008-2020/g' git grep -I -l -e '2010-2013' -- | xargs \ sed -b -i -e 's/2010-2013/2010-2020/g' CUETools.ChaptersToCue git grep -I -l -e 'Grigory Chudov 2017' -- | xargs \ sed -b -i -e 's/Grigory Chudov 2017/2017-2020 Grigory Chudov/g' CUETools.CTDB.EACPlugin git grep -I -l -e 'Grigory Chudov 2012' -- | xargs \ sed -b -i -e 's/Grigory Chudov 2012/2012-2020 Grigory Chudov/g' git grep -I -l -e '2011-12' -- | xargs \ sed -b -i -e 's/2011-12/2011-2020/g' CUETools.Codecs.FLACCL git grep -I -l -e '2009-2010' -- ':(exclude)*FlaCuda*' | xargs \ sed -b -i -e 's/2009-2010/2009-2020/g' CUETools.eac3ui (BluTools) git grep -I -l -e '© 2018' -- | xargs \ sed -b -i -e 's/© 2018/© 2018-2020 Grigory Chudov/g' CUETools.Flake git grep -I -l -e ' 2009-2014 Gr' -- | xargs \ sed -b -i -e 's/ 2009-2014 Gr/ 2009-2020 Gr/g' CUETools.Processor git grep -I -l -e ' 2008-2013 Gr' -- | xargs \ sed -b -i -e 's/ 2008-2013 Gr/ 2008-2020 Gr/g' CUETools.Ripper.Console git grep -I -l -e ' 2008-10 Gr' -- | xargs \ sed -b -i -e 's/ 2008-10 Gr/ 2008-2020 Gr/g' CUETools.Ripper.Console, CUETools.Ripper.SCSI git grep -I -l -e ' 2008-13 Gr' -- | xargs \ sed -b -i -e 's/ 2008-13 Gr/ 2008-2020 Gr/g' Single year entries: 2008, 2009, 2010, 2011, 2017, 2018 git grep -I -l -e ' 2008 Gr' -- | xargs \ sed -b -i -e 's/ 2008 Gr/ 2008-2020 Gr/g' git grep -I -l -e ' 2009 Gr' -- ':(exclude)*FlaCuda*' | xargs \ sed -b -i -e 's/ 2009 Gr/ 2009-2020 Gr/g' git grep -I -l -e ' 2010 Gr' -- | xargs \ sed -b -i -e 's/ 2010 Gr/ 2010-2020 Gr/g' git grep -I -l -e ' 2011 Gr' -- | xargs \ sed -b -i -e 's/ 2011 Gr/ 2011-2020 Gr/g' git grep -I -l -e ' 2017 Gr' -- | xargs \ sed -b -i -e 's/ 2017 Gr/ 2017-2020 Gr/g' git grep -I -l -e ' 2018 Gr' -- | xargs \ sed -b -i -e 's/ 2018 Gr/ 2018-2020 Gr/g' Fix typo in copyright year of CUETools.Codecs.WMA/AudioDecoder.cs: Copyright (c) 20139 Grigory Chudov git grep -I -lw -e '20139' -- | xargs \ sed -b -i -e 's/20139/2013-2020/g'
2020-01-30 18:13:46 +01:00
Console.WriteLine("C# port Copyright (C) 2008-2020 Grigory Chudov.");
Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
if (args.Length < 1 || (args[0].StartsWith("-") && args[0] != "-"))
{
Usage();
return;
}
string sourceFile = args[0];
string stdinName = null;
double quality = 5.0;
bool createCorrection = false;
bool toStdout = false;
int outputBPS = 0;
for (int arg = 1; arg < args.Length; arg++)
{
bool ok = true;
if (args[arg] == "-I" || args[arg] == "--insane")
quality = 10;
else if (args[arg] == "-E" || args[arg] == "--extreme")
quality = 7.5;
else if (args[arg] == "-S" || args[arg] == "--standard")
quality = 5.0;
else if (args[arg] == "-P" || args[arg] == "--portable")
quality = 2.5;
else if (args[arg] == "-C" || args[arg] == "--correction")
createCorrection = true;
else if (args[arg] == "--16")
outputBPS = 16;
else if ((args[arg] == "-N" || args[arg] == "--stdinname") && ++arg < args.Length)
stdinName = args[arg];
else if ((args[arg] == "-q" || args[arg] == "--quality") && ++arg < args.Length)
ok = double.TryParse(args[arg], out quality);
else if (args[arg] == "--stdout")
toStdout = true;
else
ok = false;
if (!ok)
{
Usage();
return;
}
}
DateTime start = DateTime.Now;
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
#if !DEBUG
try
#endif
{
Codecs.WAV.AudioDecoder audioSource = new Codecs.WAV.AudioDecoder(new Codecs.WAV.DecoderSettings() { IgnoreChunkSizes = (sourceFile == "-") }, sourceFile, (sourceFile == "-" ? Console.OpenStandardInput() : null));
if (sourceFile == "-" && stdinName != null) sourceFile = stdinName;
AudioPCMConfig pcm = outputBPS == 0 ? audioSource.PCM : new AudioPCMConfig(outputBPS, audioSource.PCM.ChannelCount, audioSource.PCM.SampleRate, audioSource.PCM.ChannelMask);
Codecs.WAV.AudioEncoder audioDest = new Codecs.WAV.AudioEncoder(new Codecs.WAV.EncoderSettings(pcm), Path.ChangeExtension(sourceFile, ".lossy.wav"), toStdout ? Console.OpenStandardOutput() : null);
Codecs.WAV.AudioEncoder lwcdfDest = createCorrection ? new Codecs.WAV.AudioEncoder(new Codecs.WAV.EncoderSettings(audioSource.PCM), Path.ChangeExtension(sourceFile, ".lwcdf.wav")) : null;
LossyWAVWriter lossyWAV = new LossyWAVWriter(audioDest, lwcdfDest, quality, new Codecs.WAV.EncoderSettings(audioSource.PCM));
2010-02-06 23:17:07 +00:00
AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);
Console.WriteLine("Filename : {0}", sourceFile);
2010-02-06 23:17:07 +00:00
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));
lossyWAV.FinalSampleCount = audioSource.Length;
2010-02-06 23:17:07 +00:00
while (audioSource.Read(buff, -1) != 0)
{
2010-02-06 23:17:07 +00:00
lossyWAV.Write(buff);
TimeSpan elapsed = DateTime.Now - start;
if ((elapsed - lastPrint).TotalMilliseconds > 60)
{
Console.Error.Write("\rProgress : {0:00}%; {1:0.0000} bits; {2:0.00}x; {3}/{4}",
100.0 * audioSource.Position / audioSource.Length,
2010-02-06 23:17:07 +00:00
1.0 * lossyWAV.OverallBitsRemoved / audioSource.PCM.ChannelCount / lossyWAV.BlocksProcessed,
lossyWAV.SamplesProcessed / elapsed.TotalSeconds / audioSource.PCM.SampleRate,
elapsed,
TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / lossyWAV.SamplesProcessed * audioSource.Length)
);
lastPrint = elapsed;
}
2010-02-06 23:17:07 +00:00
}
TimeSpan totalElapsed = DateTime.Now - start;
Console.Error.Write("\r \r");
Console.WriteLine("Results : {0:0.0000} bits; {1:0.00}x; {2}",
2010-02-06 23:17:07 +00:00
(1.0 * lossyWAV.OverallBitsRemoved) / audioSource.PCM.ChannelCount / lossyWAV.BlocksProcessed,
lossyWAV.SamplesProcessed / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
totalElapsed
);
audioSource.Close();
lossyWAV.Close();
}
#if !DEBUG
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error: {0}", ex.Message);
//Console.WriteLine("{0}", ex.StackTrace);
}
#endif
}
}
}