More refactoring: UserDefinedWriter is now a more typical IAudioDest, that has it's own UserDefinedEncoderSettings

This commit is contained in:
Grigory Chudov
2013-04-09 20:51:13 -04:00
parent e3d0c595c0
commit 218dad5c45
9 changed files with 127 additions and 131 deletions

View File

@@ -1012,11 +1012,11 @@ namespace CUERipper
{ {
string defaultMode; string defaultMode;
encoder.settings.GetSupportedModes(out defaultMode); encoder.settings.GetSupportedModes(out defaultMode);
encoder.EncoderMode = defaultMode; encoder.settings.EncoderMode = defaultMode;
} }
trackBarEncoderMode.Maximum = modes.Length - 1; trackBarEncoderMode.Maximum = modes.Length - 1;
trackBarEncoderMode.Value = encoder.EncoderModeIndex == -1 ? modes.Length - 1 : encoder.EncoderModeIndex; trackBarEncoderMode.Value = encoder.EncoderModeIndex == -1 ? modes.Length - 1 : encoder.EncoderModeIndex;
labelEncoderMode.Text = encoder.EncoderMode; labelEncoderMode.Text = encoder.settings.EncoderMode;
labelEncoderMinMode.Text = modes[0]; labelEncoderMinMode.Text = modes[0];
labelEncoderMaxMode.Text = modes[modes.Length - 1]; labelEncoderMaxMode.Text = modes[modes.Length - 1];
trackBarEncoderMode.Visible = true; trackBarEncoderMode.Visible = true;
@@ -1044,8 +1044,8 @@ namespace CUERipper
{ {
CUEToolsUDC encoder = bnComboBoxEncoder.SelectedItem as CUEToolsUDC; CUEToolsUDC encoder = bnComboBoxEncoder.SelectedItem as CUEToolsUDC;
string[] modes = encoder.SupportedModes; string[] modes = encoder.SupportedModes;
encoder.EncoderMode = modes[trackBarEncoderMode.Value]; encoder.settings.EncoderMode = modes[trackBarEncoderMode.Value];
labelEncoderMode.Text = encoder.EncoderMode; labelEncoderMode.Text = encoder.settings.EncoderMode;
} }
private void trackBarSecureMode_Scroll(object sender, EventArgs e) private void trackBarSecureMode_Scroll(object sender, EventArgs e)

View File

@@ -12,6 +12,20 @@ namespace CUETools.Codecs
{ {
} }
[DefaultValue(null)]
public string Path
{
get;
set;
}
[DefaultValue(null)]
public string Parameters
{
get;
set;
}
public string SupportedModes public string SupportedModes
{ {
get get

View File

@@ -6,7 +6,7 @@ namespace CUETools.Codecs
{ {
public class UserDefinedWriter : IAudioDest public class UserDefinedWriter : IAudioDest
{ {
string _path, _encoder, _encoderParams, _encoderMode; string _path;
Process _encoderProcess; Process _encoderProcess;
WAVWriter wrt; WAVWriter wrt;
CyclicBuffer outputBuffer = null; CyclicBuffer outputBuffer = null;
@@ -14,7 +14,7 @@ namespace CUETools.Codecs
string tempFile = null; string tempFile = null;
long _finalSampleCount = -1; long _finalSampleCount = -1;
bool closed = false; bool closed = false;
private AudioEncoderSettings m_settings; private UserDefinedEncoderSettings m_settings;
public long Position public long Position
{ {
@@ -40,24 +40,26 @@ namespace CUETools.Codecs
public string Path { get { return _path; } } public string Path { get { return _path; } }
public UserDefinedWriter(string path, Stream IO, AudioEncoderSettings settings, string encoder, string encoderParams, string encoderMode, int padding) public UserDefinedWriter(string path, UserDefinedEncoderSettings settings)
: this(path, null, settings)
{ {
m_settings = settings as AudioEncoderSettings; }
public UserDefinedWriter(string path, Stream IO, UserDefinedEncoderSettings settings)
{
m_settings = settings;
_path = path; _path = path;
_encoder = encoder; useTempFile = m_settings.Parameters.Contains("%I");
_encoderParams = encoderParams;
_encoderMode = encoderMode;
useTempFile = _encoderParams.Contains("%I");
tempFile = path + ".tmp.wav"; tempFile = path + ".tmp.wav";
_encoderProcess = new Process(); _encoderProcess = new Process();
_encoderProcess.StartInfo.FileName = _encoder; _encoderProcess.StartInfo.FileName = m_settings.Path;
_encoderProcess.StartInfo.Arguments = _encoderParams.Replace("%O", "\"" + path + "\"").Replace("%M", encoderMode).Replace("%P", padding.ToString()).Replace("%I", "\"" + tempFile + "\""); _encoderProcess.StartInfo.Arguments = m_settings.Parameters.Replace("%O", "\"" + path + "\"").Replace("%M", m_settings.EncoderMode).Replace("%P", m_settings.Padding.ToString()).Replace("%I", "\"" + tempFile + "\"");
_encoderProcess.StartInfo.CreateNoWindow = true; _encoderProcess.StartInfo.CreateNoWindow = true;
if (!useTempFile) if (!useTempFile)
_encoderProcess.StartInfo.RedirectStandardInput = true; _encoderProcess.StartInfo.RedirectStandardInput = true;
_encoderProcess.StartInfo.UseShellExecute = false; _encoderProcess.StartInfo.UseShellExecute = false;
if (!_encoderParams.Contains("%O")) if (!m_settings.Parameters.Contains("%O"))
_encoderProcess.StartInfo.RedirectStandardOutput = true; _encoderProcess.StartInfo.RedirectStandardOutput = true;
if (useTempFile) if (useTempFile)
{ {
@@ -77,7 +79,7 @@ namespace CUETools.Codecs
ex = _ex; ex = _ex;
} }
if (!started) if (!started)
throw new Exception(_encoder + ": " + (ex == null ? "please check the path" : ex.Message)); throw new Exception(m_settings.Path + ": " + (ex == null ? "please check the path" : ex.Message));
if (_encoderProcess.StartInfo.RedirectStandardOutput) if (_encoderProcess.StartInfo.RedirectStandardOutput)
{ {
Stream outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read); Stream outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
@@ -108,7 +110,7 @@ namespace CUETools.Codecs
ex = _ex; ex = _ex;
} }
if (!started) if (!started)
throw new Exception(_encoder + ": " + (ex == null ? "please check the path" : ex.Message)); throw new Exception(m_settings.Path + ": " + (ex == null ? "please check the path" : ex.Message));
} }
wrt = null; wrt = null;
if (!_encoderProcess.HasExited) if (!_encoderProcess.HasExited)
@@ -118,7 +120,7 @@ namespace CUETools.Codecs
if (outputBuffer != null) if (outputBuffer != null)
outputBuffer.Close(); outputBuffer.Close();
if (_encoderProcess.ExitCode != 0) if (_encoderProcess.ExitCode != 0)
throw new Exception(String.Format("{0} returned error code {1}", _encoder, _encoderProcess.ExitCode)); throw new Exception(String.Format("{0} returned error code {1}", m_settings.Path, _encoderProcess.ExitCode));
} }
public void Delete() public void Delete()
@@ -136,7 +138,7 @@ namespace CUETools.Codecs
catch (IOException ex) catch (IOException ex)
{ {
if (_encoderProcess.HasExited) if (_encoderProcess.HasExited)
throw new IOException(string.Format("{0} has exited prematurely with code {1}", _encoder, _encoderProcess.ExitCode), ex); throw new IOException(string.Format("{0} has exited prematurely with code {1}", m_settings.Path, _encoderProcess.ExitCode), ex);
else else
throw ex; throw ex;
} }

View File

@@ -90,9 +90,7 @@ namespace CUETools.Processor
settings.PCM = pcm; settings.PCM = pcm;
settings.Padding = padding; settings.Padding = padding;
settings.Validate(); settings.Validate();
if (encoder.path != null) if (encoder.type != null)
dest = new UserDefinedWriter(path, null, settings, encoder.path, encoder.parameters, encoder.EncoderMode, padding);
else if (encoder.type != null)
{ {
object o = Activator.CreateInstance(encoder.type, path, settings); object o = Activator.CreateInstance(encoder.type, path, settings);
if (o == null || !(o is IAudioDest)) if (o == null || !(o is IAudioDest))

View File

@@ -151,7 +151,7 @@ namespace CUETools.Processor
language = Thread.CurrentThread.CurrentUICulture.Name; language = Thread.CurrentThread.CurrentUICulture.Name;
encoders = new CUEToolsUDCList(); encoders = new CUEToolsUDCList(true);
foreach (Type type in CUEProcessorPlugins.encs) foreach (Type type in CUEProcessorPlugins.encs)
foreach (AudioEncoderClassAttribute enc in Attribute.GetCustomAttributes(type, typeof(AudioEncoderClassAttribute))) foreach (AudioEncoderClassAttribute enc in Attribute.GetCustomAttributes(type, typeof(AudioEncoderClassAttribute)))
{ {
@@ -163,7 +163,7 @@ namespace CUETools.Processor
{ {
} }
} }
decoders = new CUEToolsUDCList(); decoders = new CUEToolsUDCList(false);
foreach (Type type in CUEProcessorPlugins.decs) foreach (Type type in CUEProcessorPlugins.decs)
foreach (AudioDecoderClass dec in Attribute.GetCustomAttributes(type, typeof(AudioDecoderClass))) foreach (AudioDecoderClass dec in Attribute.GetCustomAttributes(type, typeof(AudioDecoderClass)))
decoders.Add(new CUEToolsUDC(dec, type)); decoders.Add(new CUEToolsUDC(dec, type));
@@ -333,24 +333,11 @@ return processor.Go();
sw.Save(string.Format("ExternalEncoder{0}Name", nEncoders), encoder.name); sw.Save(string.Format("ExternalEncoder{0}Name", nEncoders), encoder.name);
sw.Save(string.Format("ExternalEncoder{0}Extension", nEncoders), encoder.extension); sw.Save(string.Format("ExternalEncoder{0}Extension", nEncoders), encoder.extension);
sw.Save(string.Format("ExternalEncoder{0}Lossless", nEncoders), encoder.lossless); sw.Save(string.Format("ExternalEncoder{0}Lossless", nEncoders), encoder.lossless);
if (encoder.path != null) using (TextWriter tw = new StringWriter())
using (XmlWriter xw = XmlTextWriter.Create(tw, xmlEmptySettings))
{ {
sw.Save(string.Format("ExternalEncoder{0}Modes", nEncoders), encoder.SupportedModesStr); encoder.settingsSerializer.Serialize(xw, encoder.settings, xmlEmptyNamespaces);
sw.Save(string.Format("ExternalEncoder{0}Mode", nEncoders), encoder.EncoderMode); sw.SaveText(string.Format("ExternalEncoder{0}Settings", nEncoders), tw.ToString());
sw.Save(string.Format("ExternalEncoder{0}Path", nEncoders), encoder.path);
sw.Save(string.Format("ExternalEncoder{0}Parameters", nEncoders), encoder.parameters);
}
else
{
if (encoder.settingsSerializer != null)
{
using (TextWriter tw = new StringWriter())
using (XmlWriter xw = XmlTextWriter.Create(tw, xmlEmptySettings))
{
encoder.settingsSerializer.Serialize(xw, encoder.settings, xmlEmptyNamespaces);
sw.SaveText(string.Format("ExternalEncoder{0}Parameters", nEncoders), tw.ToString());
}
}
} }
nEncoders++; nEncoders++;
} }
@@ -478,42 +465,22 @@ return processor.Go();
{ {
string name = sr.Load(string.Format("ExternalEncoder{0}Name", nEncoders)); string name = sr.Load(string.Format("ExternalEncoder{0}Name", nEncoders));
string extension = sr.Load(string.Format("ExternalEncoder{0}Extension", nEncoders)); string extension = sr.Load(string.Format("ExternalEncoder{0}Extension", nEncoders));
string path = sr.Load(string.Format("ExternalEncoder{0}Path", nEncoders)); string settings = sr.Load(string.Format("ExternalEncoder{0}Settings", nEncoders));
string parameters = sr.Load(string.Format("ExternalEncoder{0}Parameters", nEncoders));
bool lossless = sr.LoadBoolean(string.Format("ExternalEncoder{0}Lossless", nEncoders)) ?? true; bool lossless = sr.LoadBoolean(string.Format("ExternalEncoder{0}Lossless", nEncoders)) ?? true;
string supported_modes = sr.Load(string.Format("ExternalEncoder{0}Modes", nEncoders)) ?? "";
string default_mode = sr.Load(string.Format("ExternalEncoder{0}Mode", nEncoders)) ?? "";
CUEToolsUDC encoder; CUEToolsUDC encoder;
if (name == null) continue; if (name == null || extension == null) continue;
if (!encoders.TryGetValue(extension, lossless, name, out encoder)) if (!encoders.TryGetValue(extension, lossless, name, out encoder))
{ {
if (path == null || parameters == null || extension == null) continue; encoder = new CUEToolsUDC(name, extension, lossless, "", "", "", "");
encoders.Add(new CUEToolsUDC(name, extension, lossless, supported_modes, default_mode, path, parameters)); encoders.Add(encoder);
} }
else if (version == 203) try
{
using (TextReader reader = new StringReader(settings))
encoder.settings = encoder.settingsSerializer.Deserialize(reader) as AudioEncoderSettings;
}
catch
{ {
if (encoder.path != null)
{
if (path == null || parameters == null || extension == null) continue;
encoder.extension = extension;
encoder.path = path;
encoder.lossless = lossless;
encoder.parameters = parameters;
encoder.SupportedModesStr = supported_modes;
encoder.EncoderMode = default_mode;
}
else
{
if (encoder.settingsSerializer != null && parameters != "")
try
{
using (TextReader reader = new StringReader(parameters))
encoder.settings = encoder.settingsSerializer.Deserialize(reader) as AudioEncoderSettings;
}
catch
{
}
}
} }
} }

View File

@@ -17,9 +17,6 @@ namespace CUETools.Processor
public bool lossless = false; public bool lossless = false;
public int priority = 0; public int priority = 0;
private string supported_modes = "";
private string default_mode = "";
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
public CUEToolsUDC( public CUEToolsUDC(
@@ -35,12 +32,12 @@ namespace CUETools.Processor
name = _name; name = _name;
extension = _extension; extension = _extension;
lossless = _lossless; lossless = _lossless;
supported_modes = _supported_modes;
default_mode = _default_mode;
priority = 0; priority = 0;
path = _path; path = null;
parameters = _parameters; parameters = null;
type = null; type = typeof(UserDefinedWriter);
settingsSerializer = new XmlSerializer(typeof(UserDefinedEncoderSettings));
settings = new UserDefinedEncoderSettings() { SupportedModes = _supported_modes, DefaultMode = _default_mode, Path = _path, Parameters = _parameters };
} }
public CUEToolsUDC(AudioEncoderClassAttribute enc, Type enctype) public CUEToolsUDC(AudioEncoderClassAttribute enc, Type enctype)
@@ -50,15 +47,28 @@ namespace CUETools.Processor
lossless = enc.Lossless; lossless = enc.Lossless;
priority = enc.Priority; priority = enc.Priority;
path = null; path = null;
parameters = ""; parameters = null;
type = enctype; type = enctype;
settingsSerializer = null; settingsSerializer = new XmlSerializer(enc.Settings);
settings = null; settings = Activator.CreateInstance(enc.Settings) as AudioEncoderSettings;
if (enc.Settings != null && typeof(AudioEncoderSettings).IsAssignableFrom(enc.Settings)) if (settings == null)
{ throw new InvalidOperationException("invalid codec");
settingsSerializer = new XmlSerializer(enc.Settings); }
settings = Activator.CreateInstance(enc.Settings) as AudioEncoderSettings;
} public CUEToolsUDC(
string _name,
string _extension,
string _path,
string _parameters
)
{
name = _name;
extension = _extension;
lossless = true;
priority = 0;
path = _path;
parameters = _parameters;
type = null;
} }
public CUEToolsUDC(AudioDecoderClass dec, Type dectype) public CUEToolsUDC(AudioDecoderClass dec, Type dectype)
@@ -89,13 +99,33 @@ namespace CUETools.Processor
} }
public string Path public string Path
{ {
get { return path; } get
set { path = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Path")); } {
var settings = this.settings as UserDefinedEncoderSettings;
return settings == null ? path : settings.Path;
}
set
{
var settings = this.settings as UserDefinedEncoderSettings;
if (settings == null) path = value;
else settings.Path = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Path"));
}
} }
public string Parameters public string Parameters
{ {
get { return parameters; } get
set { parameters = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Parameters")); } {
var settings = this.settings as UserDefinedEncoderSettings;
return settings == null ? parameters : settings.Parameters;
}
set
{
var settings = this.settings as UserDefinedEncoderSettings;
if (settings == null) parameters = value;
else settings.Parameters = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Parameters"));
}
} }
public bool Lossless public bool Lossless
{ {
@@ -119,26 +149,14 @@ namespace CUETools.Processor
get get
{ {
string defaultMode; string defaultMode;
return this.settings == null ? this.supported_modes : this.settings.GetSupportedModes(out defaultMode); return this.settings.GetSupportedModes(out defaultMode);
} }
set set
{ {
if (this.settings != null) throw new NotSupportedException(); var settings = this.settings as UserDefinedEncoderSettings;
supported_modes = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SupportedModesStr")); if (settings == null) throw new InvalidOperationException();
} settings.SupportedModes = value;
} if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SupportedModesStr"));
public string EncoderMode
{
get
{
if (this.settings != null) return this.settings.EncoderMode;
else return this.default_mode;
}
set
{
if (this.settings != null) this.settings.EncoderMode = value;
else this.default_mode = value;
} }
} }
@@ -158,7 +176,7 @@ namespace CUETools.Processor
if (modes == null || modes.Length < 2) if (modes == null || modes.Length < 2)
return -1; return -1;
for (int i = 0; i < modes.Length; i++) for (int i = 0; i < modes.Length; i++)
if (modes[i] == this.EncoderMode) if (modes[i] == this.settings.EncoderMode)
return i; return i;
return -1; return -1;
} }
@@ -168,7 +186,7 @@ namespace CUETools.Processor
{ {
get get
{ {
return path != null; return type == null || type == typeof(UserDefinedWriter);
} }
} }
} }

View File

@@ -5,15 +5,20 @@ namespace CUETools.Processor
{ {
public class CUEToolsUDCList : BindingList<CUEToolsUDC> public class CUEToolsUDCList : BindingList<CUEToolsUDC>
{ {
public CUEToolsUDCList() bool m_encoder;
public CUEToolsUDCList(bool encoder)
: base() : base()
{ {
AddingNew += OnAddingNew; AddingNew += OnAddingNew;
m_encoder = encoder;
} }
private void OnAddingNew(object sender, AddingNewEventArgs e) private void OnAddingNew(object sender, AddingNewEventArgs e)
{ {
e.NewObject = new CUEToolsUDC("new", "wav", true, "", "", "", ""); e.NewObject = m_encoder ?
new CUEToolsUDC("new", "wav", true, "", "", "", "") :
new CUEToolsUDC("new", "wav", "", "");
} }
public bool TryGetValue(string extension, bool lossless, string name, out CUEToolsUDC result) public bool TryGetValue(string extension, bool lossless, string name, out CUEToolsUDC result)

View File

@@ -2469,11 +2469,11 @@ namespace JDP
{ {
string defaultMode; string defaultMode;
encoder.settings.GetSupportedModes(out defaultMode); encoder.settings.GetSupportedModes(out defaultMode);
encoder.EncoderMode = defaultMode; encoder.settings.EncoderMode = defaultMode;
} }
trackBarEncoderMode.Maximum = modes.Length - 1; trackBarEncoderMode.Maximum = modes.Length - 1;
trackBarEncoderMode.Value = encoder.EncoderModeIndex == -1 ? modes.Length - 1 : encoder.EncoderModeIndex; trackBarEncoderMode.Value = encoder.EncoderModeIndex == -1 ? modes.Length - 1 : encoder.EncoderModeIndex;
labelEncoderMode.Text = encoder.EncoderMode; labelEncoderMode.Text = encoder.settings.EncoderMode;
labelEncoderMinMode.Text = modes[0]; labelEncoderMinMode.Text = modes[0];
labelEncoderMaxMode.Text = modes[modes.Length - 1]; labelEncoderMaxMode.Text = modes[modes.Length - 1];
trackBarEncoderMode.Visible = true; trackBarEncoderMode.Visible = true;
@@ -2487,8 +2487,8 @@ namespace JDP
{ {
CUEToolsUDC encoder = comboBoxEncoder.SelectedItem as CUEToolsUDC; CUEToolsUDC encoder = comboBoxEncoder.SelectedItem as CUEToolsUDC;
string[] modes = encoder.SupportedModes; string[] modes = encoder.SupportedModes;
encoder.EncoderMode = modes[trackBarEncoderMode.Value]; encoder.settings.EncoderMode = modes[trackBarEncoderMode.Value];
labelEncoderMode.Text = encoder.EncoderMode; labelEncoderMode.Text = encoder.settings.EncoderMode;
} }
//private void toolStripButton1_Click(object sender, EventArgs e) //private void toolStripButton1_Click(object sender, EventArgs e)

View File

@@ -471,22 +471,14 @@ namespace JDP
CUEToolsFormat format = _config.formats[encoder.extension]; // _config.formats.TryGetValue(encoder.extension, out format) CUEToolsFormat format = _config.formats[encoder.extension]; // _config.formats.TryGetValue(encoder.extension, out format)
labelEncoderExtension.Visible = true; labelEncoderExtension.Visible = true;
comboBoxEncoderExtension.Visible = true; comboBoxEncoderExtension.Visible = true;
comboBoxEncoderExtension.Enabled = encoder.path != null; comboBoxEncoderExtension.Enabled = encoder.CanBeDeleted;
groupBoxExternalEncoder.Visible = encoder.path != null; groupBoxExternalEncoder.Visible = encoder.CanBeDeleted;
checkBoxEncoderLossless.Enabled = format != null && format.allowLossless && format.allowLossy; checkBoxEncoderLossless.Enabled = format != null && format.allowLossless && format.allowLossy;
if (!checkBoxEncoderLossless.Enabled && format != null && encoder.Lossless != format.allowLossless) propertyGridEncoderSettings.Visible = !encoder.CanBeDeleted;
encoder.Lossless = format.allowLossless; propertyGridEncoderSettings.SelectedObject = encoder.CanBeDeleted ? null : encoder.settings;
if (encoder.settingsSerializer != null) if (!checkBoxEncoderLossless.Enabled && format != null && encoder.Lossless != format.allowLossless)
{ encoder.Lossless = format.allowLossless;
propertyGridEncoderSettings.Visible = encoder != null && encoder.settingsSerializer != null; foreach (KeyValuePair<string, CUEToolsFormat> fmtEntry in _config.formats)
propertyGridEncoderSettings.SelectedObject = encoder.settings;
}
else
{
propertyGridEncoderSettings.Visible = false;
propertyGridEncoderSettings.SelectedObject = null;
}
foreach (KeyValuePair<string, CUEToolsFormat> fmtEntry in _config.formats)
{ {
CUEToolsFormat fmt = fmtEntry.Value; CUEToolsFormat fmt = fmtEntry.Value;
if (fmt.encoderLossless == encoder && (fmt.extension != encoder.extension || !encoder.Lossless)) if (fmt.encoderLossless == encoder && (fmt.extension != encoder.extension || !encoder.Lossless))