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

82 lines
3.3 KiB
C#
Raw Permalink 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;
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 Codecs.NULL.AudioDecoder(len);
2010-02-06 23:17:07 +00:00
}
}
if (extension == ".bin")
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);
if (fmt.decoder == null)
2009-05-01 15:16:26 +00:00
throw new Exception("Unsupported audio type: " + path);
var settings = fmt.decoder.Settings.Clone();
try
{
object src = Activator.CreateInstance(fmt.decoder.Settings.DecoderType, settings, path, IO);
if (src == null || !(src is IAudioSource))
throw new Exception("Unsupported audio type: " + path + ": " + fmt.decoder.Settings.DecoderType.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();
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;
if (audioEncoderType == AudioEncoderType.NoAudio || extension == ".dummy")
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);
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);
var settings = encoder.Settings.Clone();
settings.PCM = pcm;
settings.Padding = padding;
object o;
try
{
o = Activator.CreateInstance(settings.EncoderType, settings, path, null);
}
catch (System.Reflection.TargetInvocationException ex)
{
throw ex.InnerException;
}
2013-04-10 22:21:19 -04:00
if (o == null || !(o is IAudioDest))
throw new Exception("Unsupported audio type: " + path + ": " + settings.EncoderType.FullName);
2013-04-10 22:21:19 -04:00
dest = o as IAudioDest;
dest.FinalSampleCount = finalSampleCount;
2008-12-07 23:38:00 +00:00
return dest;
}
}
}