using System; using System.Runtime.InteropServices; namespace NAudio.Wave.Compression { /// /// AcmStream encapsulates an Audio Compression Manager Stream /// used to convert audio from one format to another /// public class AcmStream : IDisposable { private IntPtr streamHandle; private IntPtr driverHandle; private AcmStreamHeader streamHeader; private readonly WaveFormat sourceFormat; /// /// Creates a new ACM stream to convert one format to another. Note that /// not all conversions can be done in one step /// /// The source audio format /// The destination audio format public AcmStream(WaveFormat sourceFormat, WaveFormat destFormat) { try { streamHandle = IntPtr.Zero; this.sourceFormat = sourceFormat; int sourceBufferSize = Math.Max(65536, sourceFormat.AverageBytesPerSecond); sourceBufferSize -= (sourceBufferSize % sourceFormat.BlockAlign); MmException.Try(AcmInterop.acmStreamOpen(out streamHandle, IntPtr.Zero, sourceFormat, destFormat, null, IntPtr.Zero, IntPtr.Zero, AcmStreamOpenFlags.NonRealTime), "acmStreamOpen"); int destBufferSize = SourceToDest(sourceBufferSize); streamHeader = new AcmStreamHeader(streamHandle, sourceBufferSize, destBufferSize); driverHandle = IntPtr.Zero; } catch { // suppress the finalise and clean up resources Dispose(); throw; } } /// /// Creates a new ACM stream to convert one format to another, using a /// specified driver identified and wave filter /// /// the driver identifier /// the source format /// the wave filter public AcmStream(IntPtr driverId, WaveFormat sourceFormat, WaveFilter waveFilter) { int sourceBufferSize = Math.Max(16384, sourceFormat.AverageBytesPerSecond); this.sourceFormat = sourceFormat; sourceBufferSize -= (sourceBufferSize % sourceFormat.BlockAlign); MmException.Try(AcmInterop.acmDriverOpen(out driverHandle, driverId, 0), "acmDriverOpen"); MmException.Try(AcmInterop.acmStreamOpen(out streamHandle, driverHandle, sourceFormat, sourceFormat, waveFilter, IntPtr.Zero, IntPtr.Zero, AcmStreamOpenFlags.NonRealTime), "acmStreamOpen"); streamHeader = new AcmStreamHeader(streamHandle, sourceBufferSize, SourceToDest(sourceBufferSize)); } /// /// Returns the number of output bytes for a given number of input bytes /// /// Number of input bytes /// Number of output bytes public int SourceToDest(int source) { if (source == 0) // zero is an invalid parameter to acmStreamSize return 0; int convertedBytes; var mmResult = AcmInterop.acmStreamSize(streamHandle, source, out convertedBytes, AcmStreamSizeFlags.Source); MmException.Try(mmResult, "acmStreamSize"); return convertedBytes; } /// /// Returns the number of source bytes for a given number of destination bytes /// /// Number of destination bytes /// Number of source bytes public int DestToSource(int dest) { if (dest == 0) // zero is an invalid parameter to acmStreamSize return 0; int convertedBytes; MmException.Try(AcmInterop.acmStreamSize(streamHandle, dest, out convertedBytes, AcmStreamSizeFlags.Destination), "acmStreamSize"); return convertedBytes; } /// /// Suggests an appropriate PCM format that the compressed format can be converted /// to in one step /// /// The compressed format /// The PCM format public static WaveFormat SuggestPcmFormat(WaveFormat compressedFormat) { // create a PCM format WaveFormat suggestedFormat = new WaveFormat(compressedFormat.SampleRate, 16, compressedFormat.Channels); MmException.Try(AcmInterop.acmFormatSuggest(IntPtr.Zero, compressedFormat, suggestedFormat, Marshal.SizeOf(suggestedFormat), AcmFormatSuggestFlags.FormatTag), "acmFormatSuggest"); /*IntPtr suggestedFormatPointer = WaveFormat.MarshalToPtr(suggestedFormat); IntPtr compressedFormatPointer = WaveFormat.MarshalToPtr(compressedFormat); MmResult result = AcmInterop.acmFormatSuggest2(IntPtr.Zero, compressedFormatPointer, suggestedFormatPointer, Marshal.SizeOf(suggestedFormat), AcmFormatSuggestFlags.FormatTag); suggestedFormat = WaveFormat.MarshalFromPtr(suggestedFormatPointer); Marshal.FreeHGlobal(suggestedFormatPointer); Marshal.FreeHGlobal(compressedFormatPointer); MmException.Try(result, "acmFormatSuggest");*/ return suggestedFormat; } /// /// Returns the Source Buffer. Fill this with data prior to calling convert /// public byte[] SourceBuffer => streamHeader.SourceBuffer; /// /// Returns the Destination buffer. This will contain the converted data /// after a successful call to Convert /// public byte[] DestBuffer => streamHeader.DestBuffer; /// /// Report that we have repositioned in the source stream /// public void Reposition() { streamHeader.Reposition(); } /// /// Converts the contents of the SourceBuffer into the DestinationBuffer /// /// The number of bytes in the SourceBuffer /// that need to be converted /// The number of source bytes actually converted /// The number of converted bytes in the DestinationBuffer public int Convert(int bytesToConvert, out int sourceBytesConverted) { if (bytesToConvert % sourceFormat.BlockAlign != 0) { System.Diagnostics.Debug.WriteLine(String.Format("Not a whole number of blocks: {0} ({1})", bytesToConvert, sourceFormat.BlockAlign)); bytesToConvert -= (bytesToConvert % sourceFormat.BlockAlign); } return streamHeader.Convert(bytesToConvert, out sourceBytesConverted); } /// /// Converts the contents of the SourceBuffer into the DestinationBuffer /// /// The number of bytes in the SourceBuffer /// that need to be converted /// The number of converted bytes in the DestinationBuffer [Obsolete("Call the version returning sourceBytesConverted instead")] public int Convert(int bytesToConvert) { int sourceBytesConverted; int destBytes = Convert(bytesToConvert, out sourceBytesConverted); if (sourceBytesConverted != bytesToConvert) { throw new MmException(MmResult.NotSupported, "AcmStreamHeader.Convert didn't convert everything"); } return destBytes; } /* Relevant only for async conversion streams public void Reset() { MmException.Try(AcmInterop.acmStreamReset(streamHandle,0),"acmStreamReset"); } */ #region IDisposable Members /// /// Frees resources associated with this ACM Stream /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Frees resources associated with this ACM Stream /// protected virtual void Dispose(bool disposing) { if (disposing) { // Free other state (managed objects). if (streamHeader != null) { streamHeader.Dispose(); streamHeader = null; } } // Free your own state (unmanaged objects). if (streamHandle != IntPtr.Zero) { MmResult result = AcmInterop.acmStreamClose(streamHandle, 0); streamHandle = IntPtr.Zero; if (result != MmResult.NoError) { throw new MmException(result, "acmStreamClose"); } } // Set large fields to null. if (driverHandle != IntPtr.Zero) { AcmInterop.acmDriverClose(driverHandle, 0); driverHandle = IntPtr.Zero; } } /// /// Frees resources associated with this ACM Stream /// ~AcmStream() { // Simply call Dispose(false). System.Diagnostics.Debug.Assert(false, "AcmStream Dispose was not called"); Dispose(false); } #endregion } }