using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using NAudio.Utils; namespace NAudio.Wave.Compression { /// /// Represents an installed ACM Driver /// public class AcmDriver : IDisposable { private static List drivers; private AcmDriverDetails details; private IntPtr driverId; private IntPtr driverHandle; private List formatTags; private List tempFormatsList; // used by enumerator private IntPtr localDllHandle; /// /// Helper function to determine whether a particular codec is installed /// /// The short name of the function /// Whether the codec is installed public static bool IsCodecInstalled(string shortName) { foreach (AcmDriver driver in EnumerateAcmDrivers()) { if (driver.ShortName == shortName) { return true; } } return false; } /// /// Attempts to add a new ACM driver from a file /// /// Full path of the .acm or dll file containing the driver /// Handle to the driver public static AcmDriver AddLocalDriver(string driverFile) { IntPtr handle = NativeMethods.LoadLibrary(driverFile); if (handle == IntPtr.Zero) { throw new ArgumentException("Failed to load driver file"); } var driverProc = NativeMethods.GetProcAddress(handle, "DriverProc"); if (driverProc == IntPtr.Zero) { NativeMethods.FreeLibrary(handle); throw new ArgumentException("Failed to discover DriverProc"); } IntPtr driverHandle; var result = AcmInterop.acmDriverAdd(out driverHandle, handle, driverProc, 0, AcmDriverAddFlags.Function); if (result != MmResult.NoError) { NativeMethods.FreeLibrary(handle); throw new MmException(result, "acmDriverAdd"); } var driver = new AcmDriver(driverHandle); // long name seems to be missing when we use acmDriverAdd if (string.IsNullOrEmpty(driver.details.longName)) { driver.details.longName = "Local driver: " + Path.GetFileName(driverFile); driver.localDllHandle = handle; } return driver; } /// /// Removes a driver previously added using AddLocalDriver /// /// Local driver to remove public static void RemoveLocalDriver(AcmDriver localDriver) { if (localDriver.localDllHandle == IntPtr.Zero) { throw new ArgumentException("Please pass in the AcmDriver returned by the AddLocalDriver method"); } var removeResult = AcmInterop.acmDriverRemove(localDriver.driverId, 0); // gets stored as a driver Id NativeMethods.FreeLibrary(localDriver.localDllHandle); MmException.Try(removeResult, "acmDriverRemove"); } /// /// Show Format Choose Dialog /// /// Owner window handle, can be null /// Window title /// Enumeration flags. None to get everything /// Enumeration format. Only needed with certain enumeration flags /// The selected format /// Textual description of the selected format /// Textual description of the selected format tag /// True if a format was selected public static bool ShowFormatChooseDialog( IntPtr ownerWindowHandle, string windowTitle, AcmFormatEnumFlags enumFlags, WaveFormat enumFormat, out WaveFormat selectedFormat, out string selectedFormatDescription, out string selectedFormatTagDescription) { AcmFormatChoose formatChoose = new AcmFormatChoose(); formatChoose.structureSize = Marshal.SizeOf(formatChoose); formatChoose.styleFlags = AcmFormatChooseStyleFlags.None; formatChoose.ownerWindowHandle = ownerWindowHandle; int maxFormatSize = 200; // guess formatChoose.selectedWaveFormatPointer = Marshal.AllocHGlobal(maxFormatSize); formatChoose.selectedWaveFormatByteSize = maxFormatSize; formatChoose.title = windowTitle; formatChoose.name = null; formatChoose.formatEnumFlags = enumFlags;//AcmFormatEnumFlags.None; formatChoose.waveFormatEnumPointer = IntPtr.Zero; if (enumFormat != null) { IntPtr enumPointer = Marshal.AllocHGlobal(Marshal.SizeOf(enumFormat)); Marshal.StructureToPtr(enumFormat,enumPointer,false); formatChoose.waveFormatEnumPointer = enumPointer; } formatChoose.instanceHandle = IntPtr.Zero; formatChoose.templateName = null; MmResult result = AcmInterop.acmFormatChoose(ref formatChoose); selectedFormat = null; selectedFormatDescription = null; selectedFormatTagDescription = null; if (result == MmResult.NoError) { selectedFormat = WaveFormat.MarshalFromPtr(formatChoose.selectedWaveFormatPointer); selectedFormatDescription = formatChoose.formatDescription; selectedFormatTagDescription = formatChoose.formatTagDescription; } Marshal.FreeHGlobal(formatChoose.waveFormatEnumPointer); Marshal.FreeHGlobal(formatChoose.selectedWaveFormatPointer); if(result != MmResult.AcmCancelled && result != MmResult.NoError) { throw new MmException(result, "acmFormatChoose"); } return result == MmResult.NoError; } /// /// Gets the maximum size needed to store a WaveFormat for ACM interop functions /// public int MaxFormatSize { get { int maxFormatSize; MmException.Try(AcmInterop.acmMetrics(driverHandle, AcmMetrics.MaxSizeFormat, out maxFormatSize), "acmMetrics"); return maxFormatSize; } } /// /// Finds a Driver by its short name /// /// Short Name /// The driver, or null if not found public static AcmDriver FindByShortName(string shortName) { foreach (AcmDriver driver in AcmDriver.EnumerateAcmDrivers()) { if (driver.ShortName == shortName) { return driver; } } return null; } /// /// Gets a list of the ACM Drivers installed /// public static IEnumerable EnumerateAcmDrivers() { drivers = new List(); MmException.Try(AcmInterop.acmDriverEnum(new AcmInterop.AcmDriverEnumCallback(DriverEnumCallback), IntPtr.Zero, 0), "acmDriverEnum"); return drivers; } /// /// The callback for acmDriverEnum /// private static bool DriverEnumCallback(IntPtr hAcmDriver, IntPtr dwInstance, AcmDriverDetailsSupportFlags flags) { drivers.Add(new AcmDriver(hAcmDriver)); return true; } /// /// Creates a new ACM Driver object /// /// Driver handle private AcmDriver(IntPtr hAcmDriver) { driverId = hAcmDriver; details = new AcmDriverDetails(); details.structureSize = Marshal.SizeOf(details); MmException.Try(AcmInterop.acmDriverDetails(hAcmDriver, ref details, 0), "acmDriverDetails"); } /// /// The short name of this driver /// public string ShortName { get { return details.shortName; } } /// /// The full name of this driver /// public string LongName { get { return details.longName; } } /// /// The driver ID /// public IntPtr DriverId { get { return driverId; } } /// /// ToString /// public override string ToString() { return LongName; } /// /// The list of FormatTags for this ACM Driver /// public IEnumerable FormatTags { get { if (formatTags == null) { if (driverHandle == IntPtr.Zero) { throw new InvalidOperationException("Driver must be opened first"); } formatTags = new List(); AcmFormatTagDetails formatTagDetails = new AcmFormatTagDetails(); formatTagDetails.structureSize = Marshal.SizeOf(formatTagDetails); MmException.Try(AcmInterop.acmFormatTagEnum(this.driverHandle, ref formatTagDetails, AcmFormatTagEnumCallback, IntPtr.Zero, 0), "acmFormatTagEnum"); } return formatTags; } } /// /// Gets all the supported formats for a given format tag /// /// Format tag /// Supported formats public IEnumerable GetFormats(AcmFormatTag formatTag) { if (driverHandle == IntPtr.Zero) { throw new InvalidOperationException("Driver must be opened first"); } tempFormatsList = new List(); var formatDetails = new AcmFormatDetails(); formatDetails.structSize = Marshal.SizeOf(formatDetails); // need to make sure we have enough space for a waveFormat. formatTag.FormatSize isn't reliable, // and some codecs MaxFormatSize isn't either formatDetails.waveFormatByteSize = 1024; formatDetails.waveFormatPointer = Marshal.AllocHGlobal(formatDetails.waveFormatByteSize); formatDetails.formatTag = (int)formatTag.FormatTag; // (int)WaveFormatEncoding.Unknown var result = AcmInterop.acmFormatEnum(driverHandle, ref formatDetails, AcmFormatEnumCallback, IntPtr.Zero, AcmFormatEnumFlags.None); Marshal.FreeHGlobal(formatDetails.waveFormatPointer); MmException.Try(result,"acmFormatEnum"); return tempFormatsList; } /// /// Opens this driver /// public void Open() { if (driverHandle == IntPtr.Zero) { MmException.Try(AcmInterop.acmDriverOpen(out driverHandle, DriverId, 0), "acmDriverOpen"); } } /// /// Closes this driver /// public void Close() { if(driverHandle != IntPtr.Zero) { MmException.Try(AcmInterop.acmDriverClose(driverHandle, 0),"acmDriverClose"); driverHandle = IntPtr.Zero; } } private bool AcmFormatTagEnumCallback(IntPtr hAcmDriverId, ref AcmFormatTagDetails formatTagDetails, IntPtr dwInstance, AcmDriverDetailsSupportFlags flags) { formatTags.Add(new AcmFormatTag(formatTagDetails)); return true; } private bool AcmFormatEnumCallback(IntPtr hAcmDriverId, ref AcmFormatDetails formatDetails, IntPtr dwInstance, AcmDriverDetailsSupportFlags flags) { tempFormatsList.Add(new AcmFormat(formatDetails)); return true; } #region IDisposable Members /// /// Dispose /// public void Dispose() { if (driverHandle != IntPtr.Zero) { Close(); GC.SuppressFinalize(this); } } #endregion } }