/// <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) { IntPtr pointerToPtr = GetPointerToPointer(); // IntPtr.Zero; // Marshal.AllocHGlobal(Marshal.SizeOf<WaveFormatExtensible>()); closestMatchFormat = null; var pDesiredFormat = desiredFormat.AsInterop(); int hresult = audioClientInterface.IsFormatSupported(shareMode, ref pDesiredFormat, pointerToPtr); var closestMatchPtr = MarshalHelpers.PtrToStructure<IntPtr>(pointerToPtr); if (closestMatchPtr != IntPtr.Zero) { closestMatchFormat = MarshalHelpers.PtrToStructure<WaveFormatExtensibleInterop>(closestMatchPtr); Marshal.FreeCoTaskMem(closestMatchPtr); } Marshal.FreeHGlobal(pointerToPtr); // S_OK is 0, S_FALSE = 1 if (hresult == 0) { // directly supported return true; } if (hresult == 1) { return false; } if (hresult == (int)AudioClientErrors.UnsupportedFormat) { // Succeeded but the specified format is not supported in exclusive mode. return shareMode != AudioClientShareMode.Exclusive; } Marshal.ThrowExceptionForHR(hresult); // shouldn't get here throw new NotSupportedException("Unknown hresult " + hresult); }
/// <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; var pFormat = waveFormat.AsInterop(); int hresult = audioClientInterface.Initialize(shareMode, streamFlags, bufferDuration, periodicity, ref pFormat, ref audioSessionGuid); Marshal.ThrowExceptionForHR(hresult); // may have changed the mix format so reset it mixFormat = null; }