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;
|
|
|
|
|
|
|
|
|
|
namespace CUETools.Processor
|
|
|
|
|
{
|
|
|
|
|
public static class AudioReadWrite {
|
2009-02-19 04:09:59 +00:00
|
|
|
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;
|
2018-03-23 20:26:28 -04:00
|
|
|
return new Codecs.NULL.AudioDecoder(len);
|
2010-02-06 23:17:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (extension == ".bin")
|
2018-03-23 19:26:26 -04:00
|
|
|
return new Codecs.WAV.AudioDecoder(new Codecs.WAV.DecoderSettings(), 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);
|
2013-03-31 01:11:00 -04:00
|
|
|
if (fmt.decoder == null)
|
2009-05-01 15:16:26 +00:00
|
|
|
throw new Exception("Unsupported audio type: " + path);
|
2018-03-24 12:15:49 -04:00
|
|
|
var settings = fmt.decoder.Settings.Clone();
|
2018-03-23 19:26:26 -04:00
|
|
|
try
|
2010-03-20 07:09:07 +00:00
|
|
|
{
|
2018-03-24 12:15:49 -04:00
|
|
|
object src = Activator.CreateInstance(fmt.decoder.Settings.DecoderType, settings, path, IO);
|
2010-03-20 07:09:07 +00:00
|
|
|
if (src == null || !(src is IAudioSource))
|
2018-03-24 12:15:49 -04:00
|
|
|
throw new Exception("Unsupported audio type: " + path + ": " + fmt.decoder.Settings.DecoderType.FullName);
|
2010-03-20 07:09:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2009-02-19 04:09:59 +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();
|
2013-04-12 22:40:56 -04:00
|
|
|
return GetAudioSource(path, IO, extension, config);
|
2008-12-07 23:38:00 +00:00
|
|
|
}
|
|
|
|
|
|
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;
|
2013-04-12 22:40:56 -04:00
|
|
|
if (audioEncoderType == AudioEncoderType.NoAudio || extension == ".dummy")
|
2018-03-25 17:24:27 -04:00
|
|
|
return new Codecs.NULL.AudioEncoder(path, new Codecs.WAV.EncoderSettings(pcm)) { FinalSampleCount = finalSampleCount };
|
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);
|
2018-03-23 19:26:26 -04:00
|
|
|
AudioEncoderSettingsViewModel 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);
|
2018-03-24 12:15:49 -04:00
|
|
|
var settings = encoder.Settings.Clone();
|
2013-04-07 20:41:58 -04:00
|
|
|
settings.PCM = pcm;
|
|
|
|
|
settings.Padding = padding;
|
2013-06-18 20:45:53 -04:00
|
|
|
object o;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-03-23 19:26:26 -04:00
|
|
|
o = Activator.CreateInstance(settings.EncoderType, settings, path, null);
|
2013-06-18 20:45:53 -04:00
|
|
|
}
|
|
|
|
|
catch (System.Reflection.TargetInvocationException ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex.InnerException;
|
|
|
|
|
}
|
2013-04-10 22:21:19 -04:00
|
|
|
if (o == null || !(o is IAudioDest))
|
2018-03-23 19:26:26 -04:00
|
|
|
throw new Exception("Unsupported audio type: " + path + ": " + settings.EncoderType.FullName);
|
2013-04-10 22:21:19 -04:00
|
|
|
dest = o as IAudioDest;
|
2013-04-04 22:07:15 -04:00
|
|
|
dest.FinalSampleCount = finalSampleCount;
|
2008-12-07 23:38:00 +00:00
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|