/// <summary> /// Determines if the specified output format is supported in shared mode /// </summary> /// <param name="shareMode">Share Mode</param> /// <param name="desiredFormat">Desired Format</param> /// <param name="closestMatchFormat">Output The closest match format.</param> /// <returns>True if the format is supported</returns> public bool IsFormatSupported(AudioClientShareMode shareMode, WaveFormat desiredFormat, out WaveFormatExtensible closestMatchFormat) { int hresult = audioClientInterface.IsFormatSupported(shareMode, desiredFormat, out closestMatchFormat); // S_OK is 0, S_FALSE = 1 if (hresult == 0) { // directly supported return true; } if (hresult == 1) { return false; } if (hresult == (int)AudioClientErrors.UnsupportedFormat) { return false; } Marshal.ThrowExceptionForHR(hresult); // shouldn't get here throw new NotSupportedException("Unknown hresult " + hresult); }
/// <summary> /// Determines whether if the specified output format is supported /// </summary> /// <param name="shareMode">The share mode.</param> /// <param name="desiredFormat">The desired format.</param> /// <returns>True if the format is supported</returns> public bool IsFormatSupported(AudioClientShareMode shareMode, WaveFormat desiredFormat) { WaveFormatExtensible closestMatchFormat; return IsFormatSupported(shareMode, desiredFormat, out closestMatchFormat); }
/// <summary> /// Initializes the Audio Client /// </summary> /// <param name="shareMode">Share Mode</param> /// <param name="streamFlags">Stream Flags</param> /// <param name="bufferDuration">Buffer Duration</param> /// <param name="periodicity">Periodicity</param> /// <param name="waveFormat">Wave Format</param> /// <param name="audioSessionGuid">Audio Session GUID (can be null)</param> public void Initialize(AudioClientShareMode shareMode, AudioClientStreamFlags streamFlags, long bufferDuration, long periodicity, WaveFormat waveFormat, Guid audioSessionGuid) { this.shareMode = shareMode; int hresult = audioClientInterface.Initialize(shareMode, streamFlags, bufferDuration, periodicity, waveFormat, ref audioSessionGuid); Marshal.ThrowExceptionForHR(hresult); // may have changed the mix format so reset it mixFormat = null; }
/// <summary> /// Helper function to marshal WaveFormat to an IntPtr /// </summary> /// <param name="format">WaveFormat</param> /// <returns>IntPtr to WaveFormat structure (needs to be freed by callee)</returns> public static IntPtr MarshalToPtr(WaveFormat format) { int formatSize = Marshal.SizeOf(format); IntPtr formatPointer = Marshal.AllocHGlobal(formatSize); Marshal.StructureToPtr(format, formatPointer, false); return formatPointer; }
/// <summary> /// Creates a new 32 bit IEEE floating point wave format /// </summary> /// <param name="sampleRate">sample rate</param> /// <param name="channels">number of channels</param> public static WaveFormat CreateIeeeFloatWaveFormat(int sampleRate, int channels) { WaveFormat wf = new WaveFormat(); wf.waveFormatTag = WaveFormatEncoding.IeeeFloat; wf.channels = (short)channels; wf.bitsPerSample = 32; wf.sampleRate = sampleRate; wf.blockAlign = (short) (4*channels); wf.averageBytesPerSecond = sampleRate * wf.blockAlign; wf.extraSize = 0; return wf; }
/// <summary> /// Creates a WaveFormat with custom members /// </summary> /// <param name="tag">The encoding</param> /// <param name="sampleRate">Sample Rate</param> /// <param name="channels">Number of channels</param> /// <param name="averageBytesPerSecond">Average Bytes Per Second</param> /// <param name="blockAlign">Block Align</param> /// <param name="bitsPerSample">Bits Per Sample</param> /// <returns></returns> public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample) { WaveFormat waveFormat = new WaveFormat(); waveFormat.waveFormatTag = tag; waveFormat.channels = (short)channels; waveFormat.sampleRate = sampleRate; waveFormat.averageBytesPerSecond = averageBytesPerSecond; waveFormat.blockAlign = (short)blockAlign; waveFormat.bitsPerSample = (short)bitsPerSample; waveFormat.extraSize = 0; return waveFormat; }