Files
cuetools.net/CUETools.Processor/AudioReadWrite.cs

149 lines
6.4 KiB
C#
Raw Normal View History

2008-12-07 23:38:00 +00:00
using System;
using System.IO;
2010-02-06 23:17:07 +00:00
using CUETools.CDImage;
2008-12-07 23:38:00 +00:00
using CUETools.Codecs;
using CUETools.Codecs.LossyWAV;
namespace CUETools.Processor
{
public static class AudioReadWrite {
public static IAudioSource GetAudioSource(string path, Stream IO, string extension, CUEConfig config)
2008-12-07 23:38:00 +00:00
{
2010-02-06 23:17:07 +00:00
if (extension == ".dummy")
{
using (StreamReader sr = (IO == null ? new StreamReader(path) : new StreamReader(IO))) {
string slen = sr.ReadLine();
long len;
if (!long.TryParse(slen, out len))
len = CDImageLayout.TimeFromString(slen) * 588;
return new SilenceGenerator(len);
}
}
if (extension == ".bin")
return new WAVReader(path, IO, AudioPCMConfig.RedBook);
2009-05-01 15:16:26 +00:00
CUEToolsFormat fmt;
if (!extension.StartsWith(".") || !config.formats.TryGetValue(extension.Substring(1), out fmt))
throw new Exception("Unsupported audio type: " + path);
CUEToolsUDC decoder;
if (fmt.decoder == null || !config.decoders.TryGetValue(fmt.decoder, out decoder))
throw new Exception("Unsupported audio type: " + path);
2010-02-06 23:17:07 +00:00
if (decoder.path != null)
return new UserDefinedReader(path, IO, decoder.path, decoder.parameters);
if (decoder.type == null)
throw new Exception("Unsupported audio type: " + path);
try
{
object src = Activator.CreateInstance(decoder.type, path, IO);
if (src == null || !(src is IAudioSource))
throw new Exception("Unsupported audio type: " + path + ": " + decoder.type.FullName);
return src as IAudioSource;
}
catch (System.Reflection.TargetInvocationException ex)
{
if (ex.InnerException == null)
throw ex;
throw ex.InnerException;
}
2008-12-07 23:38:00 +00:00
}
public static IAudioSource GetAudioSource(string path, Stream IO, CUEConfig config)
2008-12-07 23:38:00 +00:00
{
string extension = Path.GetExtension(path).ToLower();
string filename = Path.GetFileNameWithoutExtension(path);
string secondExtension = Path.GetExtension(filename).ToLower();
if (secondExtension != ".lossy" && secondExtension != ".lwcdf")
return GetAudioSource(path, IO, extension, config);
2008-12-07 23:38:00 +00:00
string lossyPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(filename) + ".lossy" + extension);
string lwcdfPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(filename) + ".lwcdf" + extension);
IAudioSource lossySource = GetAudioSource(lossyPath, null, extension, config);
IAudioSource lwcdfSource = null;
try
{
lwcdfSource = GetAudioSource(lwcdfPath, null, extension, config);
}
catch
{
return lossySource;
}
2008-12-07 23:38:00 +00:00
return new LossyWAVReader(lossySource, lwcdfSource);
}
2010-02-23 15:15:08 +00:00
public static IAudioDest GetAudioDest(AudioEncoderType audioEncoderType, string path, AudioPCMConfig pcm, long finalSampleCount, int padding, string extension, CUEConfig config)
2009-05-01 15:16:26 +00:00
{
2008-12-07 23:38:00 +00:00
IAudioDest dest;
2009-05-01 15:16:26 +00:00
if (audioEncoderType == AudioEncoderType.NoAudio || extension == ".dummy")
{
2010-02-06 23:17:07 +00:00
dest = new DummyWriter(path, pcm);
2009-05-01 15:16:26 +00:00
dest.FinalSampleCount = finalSampleCount;
return dest;
}
CUEToolsFormat fmt;
if (!extension.StartsWith(".") || !config.formats.TryGetValue(extension.Substring(1), out fmt))
throw new Exception("Unsupported audio type: " + path);
2010-02-23 15:15:08 +00:00
CUEToolsUDC encoder = audioEncoderType == AudioEncoderType.Lossless ? fmt.encoderLossless :
2009-05-01 15:16:26 +00:00
audioEncoderType == AudioEncoderType.Lossy ? fmt.encoderLossy :
null;
2009-08-06 13:03:02 +00:00
if (encoder == null)
2009-05-01 15:16:26 +00:00
throw new Exception("Unsupported audio type: " + path);
2010-02-06 23:17:07 +00:00
if (encoder.path != null)
dest = new UserDefinedWriter(path, null, pcm, encoder.path, encoder.parameters, encoder.default_mode, padding);
else if (encoder.type != null)
2009-05-01 15:16:26 +00:00
{
2010-02-06 23:17:07 +00:00
object o = Activator.CreateInstance(encoder.type, path, pcm);
if (o == null || !(o is IAudioDest))
throw new Exception("Unsupported audio type: " + path + ": " + encoder.type.FullName);
dest = o as IAudioDest;
2010-02-23 15:15:08 +00:00
}
else
2010-02-06 23:17:07 +00:00
throw new Exception("Unsupported audio type: " + path);
dest.CompressionLevel = encoder.DefaultModeIndex;
dest.FinalSampleCount = finalSampleCount;
2010-02-23 15:15:08 +00:00
if (encoder.type != null)
switch (encoder.type.FullName)
{
case "CUETools.Codecs.ALAC.ALACWriter":
dest.Options = string.Format("--padding-length {0}", padding);
break;
case "CUETools.Codecs.FLAKE.FlakeWriter":
dest.Options = string.Format("--padding-length {0}", padding);
break;
case "CUETools.Codecs.FlaCuda.FlaCudaWriter":
dest.Options = string.Format("{0}{1}--padding-length {2} --cpu-threads {3}",
config.FlaCudaVerify ? "--verify " : "",
config.FlaCudaGPUOnly ? "--gpu-only " : "",
2010-02-23 15:15:08 +00:00
padding,
config.FlaCudaThreads ? 1 : 0);
break;
case "CUETools.Codecs.FLAC.FLACWriter":
dest.Options = string.Format("{0}{1}--padding-length {2}",
config.disableAsm ? "--disable-asm " : "",
config.flacVerify ? "--verify " : "",
padding);
break;
case "CUETools.Codecs.WavPack.WavPackWriter":
dest.Options = string.Format("{0}--extra-mode {1}",
config.wvStoreMD5 ? "--md5 " : "",
config.wvExtraMode);
break;
}
2008-12-07 23:38:00 +00:00
return dest;
}
2009-08-08 16:14:06 +00:00
public static IAudioDest GetAudioDest(AudioEncoderType audioEncoderType, string path, long finalSampleCount, int bitsPerSample, int sampleRate, int padding, CUEConfig config)
2008-12-07 23:38:00 +00:00
{
string extension = Path.GetExtension(path).ToLower();
string filename = Path.GetFileNameWithoutExtension(path);
2010-02-06 23:17:07 +00:00
AudioPCMConfig pcm = new AudioPCMConfig(bitsPerSample, 2, sampleRate);
2009-05-01 15:16:26 +00:00
if (audioEncoderType == AudioEncoderType.NoAudio || audioEncoderType == AudioEncoderType.Lossless || Path.GetExtension(filename).ToLower() != ".lossy")
2010-02-06 23:17:07 +00:00
return GetAudioDest(audioEncoderType, path, pcm, finalSampleCount, padding, extension, config);
2008-12-07 23:38:00 +00:00
string lwcdfPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(filename) + ".lwcdf" + extension);
2010-02-06 23:17:07 +00:00
AudioPCMConfig lossypcm = new AudioPCMConfig((config.detectHDCD && config.decodeHDCD && !config.decodeHDCDtoLW16) ? 24 : 16, 2, sampleRate);
IAudioDest lossyDest = GetAudioDest(AudioEncoderType.Lossless, path, lossypcm, finalSampleCount, padding, extension, config);
IAudioDest lwcdfDest = audioEncoderType == AudioEncoderType.Hybrid ? GetAudioDest(AudioEncoderType.Lossless, lwcdfPath, lossypcm, finalSampleCount, padding, extension, config) : null;
return new LossyWAVWriter(lossyDest, lwcdfDest, config.lossyWAVQuality, pcm);
2008-12-07 23:38:00 +00:00
}
}
}