filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.19k stars 454 forks source link

Issue Recording Playback from Device #231

Open tjscience opened 7 years ago

tjscience commented 7 years ago

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()

filoe commented 7 years ago

And the error message?

tjscience commented 7 years ago

Oops, thought I posted that:

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

filoe commented 7 years ago

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?

tjscience commented 7 years ago

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.

filoe commented 7 years ago

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

tjscience commented 7 years ago

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?

filoe commented 7 years ago

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