Removed AudioEncoderSettings/AudioDecoderSettings classes, all of their functionality is now in IAudioEncoderSettings/IAudioDecoderSettings interfaces.

This commit is contained in:
Grigory Chudov
2018-03-25 17:24:27 -04:00
parent 50fcd93ab9
commit 320e75d709
98 changed files with 5274 additions and 4863 deletions

View File

@@ -4,6 +4,15 @@ using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
#if NET20
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
| AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute { }
}
#endif
namespace CUETools.Codecs
{
public interface IAudioEncoderSettings
@@ -17,67 +26,28 @@ namespace CUETools.Codecs
bool Lossless { get; }
int Priority { get; }
string SupportedModes { get; }
string DefaultMode { get; }
string EncoderMode { get; set; }
AudioPCMConfig PCM { get; set; }
int BlockSize { get; set; }
int Padding { get; set; }
IAudioEncoderSettings Clone();
}
[JsonObject(MemberSerialization.OptIn)]
public class AudioEncoderSettings : IAudioEncoderSettings
public static class IAudioEncoderSettingsExtensions
{
[Browsable(false)]
public virtual string Name => null;
[Browsable(false)]
public virtual string Extension => null;
[Browsable(false)]
public virtual Type EncoderType => null;
[Browsable(false)]
public virtual bool Lossless => false;
[Browsable(false)]
public virtual int Priority => 0;
public AudioEncoderSettings()
: this("", "")
{
}
public AudioEncoderSettings(AudioPCMConfig pcm)
: this("", "")
{
this.PCM = pcm;
}
public AudioEncoderSettings(string supported_modes, string default_mode)
{
// Iterate through each property and call ResetValue()
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this))
property.ResetValue(this);
this.m_supported_modes = supported_modes;
this.m_default_mode = default_mode;
if (default_mode == "")
GetSupportedModes(out default_mode);
this.EncoderMode = default_mode;
}
protected string m_supported_modes;
protected string m_default_mode;
public virtual string GetSupportedModes(out string defaultMode)
{
defaultMode = m_default_mode;
return this.m_supported_modes;
}
public AudioEncoderSettings Clone()
{
return this.MemberwiseClone() as AudioEncoderSettings;
}
public bool HasBrowsableAttributes()
public static bool HasBrowsableAttributes(this IAudioEncoderSettings settings)
{
bool hasBrowsable = false;
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this))
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(settings))
{
bool isBrowsable = true;
foreach (var attribute in property.Attributes)
@@ -90,101 +60,65 @@ namespace CUETools.Codecs
return hasBrowsable;
}
protected void SetDefaultValuesForMode()
public static int GetEncoderModeIndex(this IAudioEncoderSettings settings)
{
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this))
if (!property.CanResetValue(this))
return new List<string>(settings.SupportedModes.Split(' ')).FindIndex(m => m == settings.EncoderMode);
}
public static void SetEncoderModeIndex(this IAudioEncoderSettings settings, int value)
{
string[] modes = settings.SupportedModes.Split(' ');
if (modes.Length == 0 && value < 0)
return;
if (value < 0 || value >= modes.Length)
throw new IndexOutOfRangeException();
settings.EncoderMode = modes[value];
}
public static void SetDefaultValuesForMode(this IAudioEncoderSettings settings)
{
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(settings))
if (!property.CanResetValue(settings))
foreach (var attribute in property.Attributes)
if (attribute is DefaultValueForModeAttribute)
{
var defaultValueForMode = attribute as DefaultValueForModeAttribute;
property.SetValue(this, defaultValueForMode.m_values[EncoderModeIndex]);
property.SetValue(settings, defaultValueForMode.m_values[settings.GetEncoderModeIndex()]);
}
}
public bool HasDefaultValuesForMode(int index)
public static bool HasDefaultValuesForMode(this IAudioEncoderSettings settings, int index)
{
bool res = true;
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this))
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(settings))
foreach (var attribute in property.Attributes)
if (attribute is DefaultValueForModeAttribute)
{
var defaultValueForMode = attribute as DefaultValueForModeAttribute;
res &= property.GetValue(this).Equals(defaultValueForMode.m_values[index]);
res &= property.GetValue(settings).Equals(defaultValueForMode.m_values[index]);
}
return res;
}
public int GuessEncoderMode()
public static int GuessEncoderMode(this IAudioEncoderSettings settings)
{
string defaultMode;
string[] modes = this.GetSupportedModes(out defaultMode).Split(' ');
// return new List<string>(settings.SupportedModes.Split(' ')).FindIndex(m => settings.HasDefaultValuesForMode(m));
string[] modes = settings.SupportedModes.Split(' ');
if (modes == null || modes.Length < 1)
return -1;
for (int i = 0; i < modes.Length; i++)
if (HasDefaultValuesForMode(i))
if (settings.HasDefaultValuesForMode(i))
return i;
return -1;
}
[Browsable(false)]
public AudioPCMConfig PCM
public static void Init(this IAudioEncoderSettings settings, AudioPCMConfig pcm = null)
{
get;
set;
}
[Browsable(false)]
[DefaultValue(0)]
public int BlockSize
{
get;
set;
}
[Browsable(false)]
[DefaultValue(4096)]
public int Padding
{
get;
set;
}
[Browsable(false)]
[DefaultValue("")]
[JsonProperty]
public string EncoderMode
{
get;
set;
}
[Browsable(false)]
public int EncoderModeIndex
{
get
{
string defaultMode;
string[] modes = this.GetSupportedModes(out defaultMode).Split(' ');
if (modes == null || modes.Length < 1)
return -1;
for (int i = 0; i < modes.Length; i++)
if (modes[i] == this.EncoderMode)
return i;
return -1;
}
set
{
string defaultMode;
string[] modes = this.GetSupportedModes(out defaultMode).Split(' ');
if (modes.Length == 0 && value < 0)
return;
if (value < 0 || value >= modes.Length)
throw new IndexOutOfRangeException();
this.EncoderMode = modes[value];
}
// Iterate through each property and call ResetValue()
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(settings))
property.ResetValue(settings);
settings.EncoderMode = settings.DefaultMode;
settings.PCM = pcm;
}
}
}