public void GetDevicePeriod(out long DefaultDevicePeriod, out long MinimumDevicePeriod) { long r1; long r2; Marshal.ThrowExceptionForHR(_RealClient.GetDevicePeriod(out r1, out r2)); DefaultDevicePeriod = r1; MinimumDevicePeriod = r2; }
public DevicePeriod GetDevicePeriod() { long def; long min; int result = (_audioClient.GetDevicePeriod(out def, out min)); Console.WriteLine(result); return(new DevicePeriod(def, min)); }
public CoreAudio(bool microphone = false) { const uint REFTIMES_PER_SEC = 10000000; const uint CLSCTX_INPROC_SERVER = 1; Guid clsid = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E"); Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); oEnumerator = null; uint hResult = CoCreateInstance(ref clsid, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out oEnumerator); if (hResult != 0 || oEnumerator == null) { throw new Exception("CoCreateInstance() pInvoke failed"); } iMde = oEnumerator as IMMDeviceEnumerator; if (iMde == null) { throw new Exception("COM cast failed to IMMDeviceEnumerator"); } IntPtr pDevice = IntPtr.Zero; //iMde.EnumAudioEndpoints(EDataFlow.eCapture, DEVICE_STATE_ACTIVE,ref pDevice); int retVal; if (microphone) { retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole, ref pDevice); } else { retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, ref pDevice); } if (retVal != 0) { throw new Exception("IMMDeviceEnumerator.GetDefaultAudioEndpoint()"); } //int dwStateMask = DEVICE_STATE_ACTIVE | DEVICE_STATE_NOTPRESENT | DEVICE_STATE_UNPLUGGED; //IntPtr pCollection = IntPtr.Zero; //retVal = iMde.EnumAudioEndpoints(EDataFlow.eRender, dwStateMask, ref pCollection); //if (retVal != 0) //{ // throw new Exception("IMMDeviceEnumerator.EnumAudioEndpoints()"); //} imd = (IMMDevice)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pDevice); if (imd == null) { throw new Exception("COM cast failed to IMMDevice"); } Guid iid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A"); uint dwClsCtx = (uint)CLSCTX.CLSCTX_ALL; IntPtr pActivationParams = IntPtr.Zero; IntPtr pEndPoint = IntPtr.Zero; retVal = imd.Activate(iid, dwClsCtx, pActivationParams, out pEndPoint); if (retVal != 0) { throw new Exception("IMMDevice.Activate()"); } iAudioEndpoint = (IAudioEndpointVolume)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint); if (iAudioEndpoint == null) { throw new Exception("COM cast failed to IAudioEndpointVolume"); } iid = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2"); dwClsCtx = (uint)CLSCTX.CLSCTX_ALL; pActivationParams = IntPtr.Zero; pEndPoint = IntPtr.Zero; retVal = imd.Activate(iid, dwClsCtx, pActivationParams, out pEndPoint); if (retVal != 0) { throw new Exception("IAudioClient.Activate() " + Convert.ToString(retVal, 2)); } iAudioClient = (IAudioClient)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint); if (iAudioClient == null) { throw new Exception("COM cast failed to iAudioClient"); } ulong processInterval; ulong minimumInterval; retVal = iAudioClient.GetDevicePeriod(out processInterval, out minimumInterval); if (retVal != 0) { throw new Exception("iAudioClient.GetDevicePeriod()"); } waveformat = new WAVEFORMATEX(); waveformat.wFormatTag = (ushort)WaveFormatEncoding.Pcm; waveformat.nChannels = 2; waveformat.nBlockAlign = 4; waveformat.wBitsPerSample = 16; waveformat.nSamplesPerSec = 44100; waveformat.cbSize = 0; waveformat.nAvgBytesPerSec = 176400; IntPtr reqForm = Marshal.AllocHGlobal(Marshal.SizeOf(waveformat)); Marshal.StructureToPtr(waveformat, reqForm, false); IntPtr propForm = Marshal.AllocHGlobal(Marshal.SizeOf(waveformat)); retVal = iAudioClient.IsFormatSupported(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED, reqForm, out propForm); if (retVal != 0) { throw new Exception("IAudioClient.IsFormatSupported()"); } if (microphone) { retVal = iAudioClient.Initialize(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_EXCLUSIVE, 0, 2000000, 0, reqForm, Guid.Empty); } else { retVal = iAudioClient.Initialize(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 2000000, 0, reqForm, Guid.Empty); } if (retVal != 0) { throw new Exception("IAudioClient.Initialize() " + retVal); } uint buffersize = 0; retVal = iAudioClient.GetBufferSize(out buffersize); if (retVal != 0) { throw new Exception("IAudioClient.GetBufferSize()"); } iid = new Guid("C8ADBD64-E71E-48a0-A4DE-185C395CD317"); IntPtr capclient = IntPtr.Zero; retVal = iAudioClient.GetService(iid, out capclient); if (retVal != 0) { throw new Exception("IAudioClient.GetService()"); } iAudioCaptureClient = (IAudioCaptureClient)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(capclient); if (iAudioCaptureClient == null) { throw new Exception("COM cast failed to iAudioCaptureClient"); } hnsActualDuration = (double)(REFTIMES_PER_SEC * buffersize / waveformat.nSamplesPerSec); // 8391 smallest possible value recordingthread = new Thread(recordingloop); recordingthread.IsBackground = false; recordingthread.Start(); }