cscore icon indicating copy to clipboard operation
cscore copied to clipboard

Issue Recording Playback from Device

Open tjscience opened this issue 7 years ago • 7 comments

I am having an issue recording playback from a device (loopback capture).

This works fine when I use loopback capture to record audio being played out of my speakers from an external source (outside of cscore). However, if I try to play an audio file with cscore and then try to record that playback using loopback capture I receive an error. Is this possible to do and I am simply doing it wrong?

Here is the stacktrace for the error:

at CSCore.SoundIn.WasapiCapture.InitializeInternal() at CSCore.SoundIn.WasapiCapture.Initialize() at Audion.InputSource.Record(String filename) in ...\Repos\audion\Audion\InputSource.cs:line 298 at Audion.Sample.StartPage.Record(Object sender, MouseButtonEventArgs e) in ...\Repos\audion\Audion.Sample\StartPage.xaml.cs:line 198 at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnPreviewMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at Audion.Sample.App.Main() at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

tjscience avatar Apr 16 '17 00:04 tjscience

And the error message?

filoe avatar Apr 16 '17 09:04 filoe

Oops, thought I posted that:

IAudioClient::Initialize caused an error: 0x8889000a, "Unknown HRESULT".

tjscience avatar Apr 16 '17 16:04 tjscience

Well the error is "AUDCLNT_E_DEVICE_IN_USE". Is there anything using the capture device? For example any applications using it in exclusive mode?

filoe avatar Apr 21 '17 21:04 filoe

The only application that is using the loopback capture device (or any othe input/output sound device) is cscore. I am playing an audio file with cscore and trying to record that with loopback capture.

tjscience avatar Apr 23 '17 00:04 tjscience

Well, then it's hard to figure out. Are the samples working?

filoe avatar Apr 23 '17 09:04 filoe

I figured this out. It was a setting that I missed, CSCore.CoreAudioAPI.AudioClientShareMode.Exclusive. I needed to change this to CSCore.CoreAudioAPI.AudioClientShareMode.Shared to get it working. Are there any downsides to doing this?

tjscience avatar Apr 25 '17 20:04 tjscience

Well, the usual way is using shared mode. The exclusive mode reserves the device for only one single application (in fact one audioclient). It is used to achieve very low latency. But it will also degrade the user experience. System sounds and any other applications will be muted. Existing playbacks will be interrupted with an error. Some applications can't really deal with that (for example spotify). You can read more about it here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd370844(v=vs.85).aspx

filoe avatar May 01 '17 09:05 filoe