diff --git a/CUETools.Codecs.LAME/CUETools.Codecs.LAME.csproj b/CUETools.Codecs.LAME/CUETools.Codecs.LAME.csproj index 728f3f2..5af8888 100644 --- a/CUETools.Codecs.LAME/CUETools.Codecs.LAME.csproj +++ b/CUETools.Codecs.LAME/CUETools.Codecs.LAME.csproj @@ -75,9 +75,14 @@ + + + + + diff --git a/CUETools.Codecs.LAME/LameException.cs b/CUETools.Codecs.LAME/LameException.cs new file mode 100644 index 0000000..fafef2b --- /dev/null +++ b/CUETools.Codecs.LAME/LameException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CUETools.Codecs.LAME +{ + public class LameException : Exception + { + public LameException(string message) + : base(message) + { + } + } +} diff --git a/CUETools.Codecs.LAME/LameWriter.cs b/CUETools.Codecs.LAME/LameWriter.cs index 17e67a6..c4a998a 100644 --- a/CUETools.Codecs.LAME/LameWriter.cs +++ b/CUETools.Codecs.LAME/LameWriter.cs @@ -9,7 +9,7 @@ namespace CUETools.Codecs.LAME { #region Unmanaged Functions - private const string LameDll = "lame_enc"; + private const string LameDll = "libmp3lame"; private const CallingConvention LameCallingConvention = CallingConvention.Cdecl; [DllImport(LameDll, CallingConvention = LameCallingConvention)] @@ -50,7 +50,6 @@ namespace CUETools.Codecs.LAME private string outputPath; private Stream outputStream; - private LameWriterSettings settings; private bool closed = false, initialized = false; private IntPtr handle; private AudioPCMConfig pcm; @@ -99,15 +98,19 @@ namespace CUETools.Codecs.LAME { get { - return this.settings; + return null; } set { - if (!(value is LameWriterSettings)) - { - throw new ArgumentException("Settings must be of type LameWriterSettings"); - } - this.settings = (LameWriterSettings)value; + throw new MethodAccessException(); + } + } + + protected virtual LameWriterConfig Config + { + get + { + return LameWriterConfig.CreateCbr(320); } } @@ -232,6 +235,8 @@ namespace CUETools.Codecs.LAME { if (!this.initialized) { + var config = this.Config; + handle = lame_init(); lame_set_bWriteVbrTag(handle, 1); @@ -240,25 +245,25 @@ namespace CUETools.Codecs.LAME lame_set_num_channels(handle, this.pcm.ChannelCount); lame_set_in_samplerate(handle, this.pcm.SampleRate); - lame_set_quality(handle, (int)this.settings.Quality); + lame_set_quality(handle, (int)config.Quality); if (this.finalSampleCount != 0) { lame_set_num_samples(handle, this.finalSampleCount); } - lame_set_VBR(this.handle, (int)this.settings.VbrMode); + lame_set_VBR(this.handle, (int)config.VbrMode); - switch (this.settings.VbrMode) + switch (config.VbrMode) { case LameVbrMode.Abr: - lame_set_VBR_mean_bitrate_kbps(handle, this.settings.Bitrate); + lame_set_VBR_mean_bitrate_kbps(handle, config.Bitrate); break; case LameVbrMode.Default: - lame_set_VBR_quality(handle, this.settings.VbrQuality); + lame_set_VBR_quality(handle, config.VbrQuality); break; case LameVbrMode.Off: - lame_set_brate(handle, this.settings.Bitrate); + lame_set_brate(handle, config.Bitrate); break; default: throw new ArgumentException("Only ABR, Default and Off VBR modes are supported."); diff --git a/CUETools.Codecs.LAME/LameWriterCBR.cs b/CUETools.Codecs.LAME/LameWriterCBR.cs new file mode 100644 index 0000000..850f1f3 --- /dev/null +++ b/CUETools.Codecs.LAME/LameWriterCBR.cs @@ -0,0 +1,67 @@ +using System; +using System.IO; + +namespace CUETools.Codecs.LAME +{ + [AudioEncoderClass("lame2 CBR", "mp3", false, "96 128 192 256 320", "256", 2, typeof(LameWriterCBRSettings))] + public class LameWriterCBR : LameWriter + { + private static readonly int[] bps_table = new int[] { 96, 128, 192, 256, 320 }; + private int bps; + + public LameWriterCBR(string path, Stream IO, AudioPCMConfig pcm) + : base(IO, pcm) + { + } + + public LameWriterCBR(string path, AudioPCMConfig pcm) + : base(path, pcm) + { + } + + public override int CompressionLevel + { + get + { + for (int i = 0; i < bps_table.Length; i++) + { + if (bps == bps_table[i]) + { + return i; + } + } + return -1; + } + set + { + if (value < 0 || value > bps_table.Length) + throw new Exception("unsupported compression level"); + bps = bps_table[value]; + } + } + + LameWriterCBRSettings _settings = new LameWriterCBRSettings(); + + public override object Settings + { + get + { + return _settings; + } + set + { + if (value as LameWriterCBRSettings == null) + throw new Exception("Unsupported options " + value); + _settings = value as LameWriterCBRSettings; + } + } + + protected override LameWriterConfig Config + { + get + { + return LameWriterConfig.CreateCbr(this.bps, this._settings.Quality); + } + } + } +} diff --git a/CUETools.Codecs.LAME/LameWriterCBRSettings.cs b/CUETools.Codecs.LAME/LameWriterCBRSettings.cs new file mode 100644 index 0000000..c8a4789 --- /dev/null +++ b/CUETools.Codecs.LAME/LameWriterCBRSettings.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; + +namespace CUETools.Codecs.LAME +{ + public class LameWriterCBRSettings + { + [DefaultValue(LameQuality.High)] + public LameQuality Quality { get; set; } + + public LameWriterCBRSettings() + { + // Iterate through each property and call ResetValue() + foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this)) + { + property.ResetValue(this); + } + this.Quality = LameQuality.High; + } + } +} diff --git a/CUETools.Codecs.LAME/LameWriterConstants.cs b/CUETools.Codecs.LAME/LameWriterConstants.cs new file mode 100644 index 0000000..634faea --- /dev/null +++ b/CUETools.Codecs.LAME/LameWriterConstants.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CUETools.Codecs.LAME +{ + public enum LameQuality + { + High = 2, + Normal = 5, + Fast = 7, + } + + public enum LameVbrMode + { + Off = 0, + Mt = 1, + Rh = 2, + Abr = 3, + Default = 4, + } +} diff --git a/CUETools.Codecs.LAME/LameWriterSettings.cs b/CUETools.Codecs.LAME/LameWriterSettings.cs index c74a4ce..2131099 100644 --- a/CUETools.Codecs.LAME/LameWriterSettings.cs +++ b/CUETools.Codecs.LAME/LameWriterSettings.cs @@ -2,53 +2,33 @@ namespace CUETools.Codecs.LAME { - public class LameException: Exception - { - public LameException(string message) - : base(message) - { - } - } - - public enum LameQuality - { - High, - } - - public enum LameVbrMode - { - Off, - Default, - Abr, - } - - public class LameWriterSettings + public class LameWriterConfig { public LameQuality Quality { get; set; } public LameVbrMode VbrMode { get; set; } public int VbrQuality { get; set; } public int Bitrate { get; set; } - public LameWriterSettings() + public LameWriterConfig() { Quality = LameQuality.High; VbrMode = LameVbrMode.Default; VbrQuality = 5; } - public static LameWriterSettings CreateCbr(int bitrate, LameQuality encodeQuality = LameQuality.High) + public static LameWriterConfig CreateCbr(int bitrate, LameQuality encodeQuality = LameQuality.High) { - return new LameWriterSettings() { VbrMode = LameVbrMode.Off, Bitrate = bitrate, Quality = encodeQuality }; + return new LameWriterConfig() { VbrMode = LameVbrMode.Off, Bitrate = bitrate, Quality = encodeQuality }; } - public static LameWriterSettings CreateAbr(int bitrate, LameQuality encodeQuality = LameQuality.High) + public static LameWriterConfig CreateAbr(int bitrate, LameQuality encodeQuality = LameQuality.High) { - return new LameWriterSettings() { VbrMode = LameVbrMode.Abr, Bitrate = bitrate, Quality = encodeQuality }; + return new LameWriterConfig() { VbrMode = LameVbrMode.Abr, Bitrate = bitrate, Quality = encodeQuality }; } - public static LameWriterSettings CreateVbr(int vbrQuality, LameQuality encodeQuality = LameQuality.High) + public static LameWriterConfig CreateVbr(int vbrQuality, LameQuality encodeQuality = LameQuality.High) { - return new LameWriterSettings() { VbrMode = LameVbrMode.Default, VbrQuality = vbrQuality, Quality = encodeQuality }; + return new LameWriterConfig() { VbrMode = LameVbrMode.Default, VbrQuality = vbrQuality, Quality = encodeQuality }; } } } diff --git a/CUETools.Codecs.LAME/LameWriterVBR.cs b/CUETools.Codecs.LAME/LameWriterVBR.cs index 302d701..2a68378 100644 --- a/CUETools.Codecs.LAME/LameWriterVBR.cs +++ b/CUETools.Codecs.LAME/LameWriterVBR.cs @@ -3,7 +3,7 @@ using System.IO; namespace CUETools.Codecs.LAME { - [AudioEncoderClass("lame2 VBR", "mp3", false, "V9 V8 V7 V6 V5 V4 V3 V2 V1 V0", "V2", 2, typeof(LAMEEncoderVBRSettings))] + [AudioEncoderClass("lame2 VBR", "mp3", false, "V9 V8 V7 V6 V5 V4 V3 V2 V1 V0", "V2", 2, typeof(LameWriterVBRSettings))] public class LameWriterVBR : LameWriter { private int quality = 0; @@ -32,7 +32,7 @@ namespace CUETools.Codecs.LAME } } - LAMEEncoderVBRSettings _settings = new LAMEEncoderVBRSettings(); + LameWriterVBRSettings _settings = new LameWriterVBRSettings(); public override object Settings { @@ -42,9 +42,17 @@ namespace CUETools.Codecs.LAME } set { - if (value as LAMEEncoderVBRSettings == null) + if (value as LameWriterVBRSettings == null) throw new Exception("Unsupported options " + value); - _settings = value as LAMEEncoderVBRSettings; + _settings = value as LameWriterVBRSettings; + } + } + + protected override LameWriterConfig Config + { + get + { + return LameWriterConfig.CreateVbr(this.quality, this._settings.Quality); } } } diff --git a/CUETools.Codecs.LAME/LameWriterVBRSettings.cs b/CUETools.Codecs.LAME/LameWriterVBRSettings.cs new file mode 100644 index 0000000..37e3d0c --- /dev/null +++ b/CUETools.Codecs.LAME/LameWriterVBRSettings.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; + +namespace CUETools.Codecs.LAME +{ + public class LameWriterVBRSettings + { + [DefaultValue(LameQuality.High)] + public LameQuality Quality { get; set; } + + public LameWriterVBRSettings() + { + // Iterate through each property and call ResetValue() + foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this)) { + property.ResetValue(this); + } + this.Quality = LameQuality.High; + } + } +}