Files
cuetools.net/CUETools.Codecs/UserDefinedReader.cs

113 lines
3.1 KiB
C#
Raw Normal View History

2011-10-24 00:13:35 +00:00
using System;
using System.Diagnostics;
using System.IO;
namespace CUETools.Codecs
{
public class CommandLineDecoder : IAudioSource
2011-10-24 00:13:35 +00:00
{
string _path;
2011-10-24 00:13:35 +00:00
Process _decoderProcess;
WAV.AudioDecoder rdr;
2011-10-24 00:13:35 +00:00
private CommandLineDecoderSettings m_settings;
public AudioDecoderSettings Settings => m_settings;
2011-10-24 00:13:35 +00:00
public long Position
{
get
{
Initialize();
return rdr.Position;
}
set
{
Initialize();
rdr.Position = value;
}
}
public long Length
{
get
{
Initialize();
return rdr.Length;
}
}
public long Remaining
{
get
{
Initialize();
return rdr.Remaining;
}
}
public AudioPCMConfig PCM
{
get
{
Initialize();
return rdr.PCM;
}
}
public string Path { get { return _path; } }
public CommandLineDecoder(CommandLineDecoderSettings settings, string path, Stream IO)
2011-10-24 00:13:35 +00:00
{
m_settings = settings;
2011-10-24 00:13:35 +00:00
_path = path;
_decoderProcess = null;
rdr = null;
}
void Initialize()
{
if (_decoderProcess != null)
return;
_decoderProcess = new Process();
_decoderProcess.StartInfo.FileName = m_settings.Path;
_decoderProcess.StartInfo.Arguments = m_settings.Parameters.Replace("%I", "\"" + _path + "\"");
2011-10-24 00:13:35 +00:00
_decoderProcess.StartInfo.CreateNoWindow = true;
_decoderProcess.StartInfo.RedirectStandardOutput = true;
_decoderProcess.StartInfo.UseShellExecute = false;
bool started = false;
Exception ex = null;
try
{
started = _decoderProcess.Start();
if (started)
_decoderProcess.PriorityClass = Process.GetCurrentProcess().PriorityClass;
}
catch (Exception _ex)
{
ex = _ex;
}
if (!started)
{
_decoderProcess = null;
throw new Exception(m_settings.Path + ": " + (ex == null ? "please check the path" : ex.Message));
}
rdr = new WAV.AudioDecoder(new WAV.DecoderSettings(), _path, _decoderProcess.StandardOutput.BaseStream);
2011-10-24 00:13:35 +00:00
}
public void Close()
{
if (rdr != null)
rdr.Close();
if (_decoderProcess != null && !_decoderProcess.HasExited)
try { _decoderProcess.Kill(); _decoderProcess.WaitForExit(); }
catch { }
}
public int Read(AudioBuffer buff, int maxLength)
{
Initialize();
return rdr.Read(buff, maxLength);
}
}
}