robbert-vdh / nih-plug

Rust VST3 and CLAP plugin framework and plugins - because everything is better when you do it yourself
ISC License
1.42k stars 127 forks source link

[WASAPI] [Regression] Standalone target cpal_wasapi_out thread fails because of buffer size #147

Open YPares opened 1 week ago

YPares commented 1 week ago

After updating to a recent nih-plug commit (b3038b458c35a620fbc07b65a7261693f6e68e0e), I'm getting this right when running the standalone version of the basic template from https://github.com/robbert-vdh/nih-plug-template (with a main that just runs nih_plug::nix_export_standalone::<MyPlugin>):

[ERROR] nih_plug::wrapper::util: thread 'cpal_wasapi_out' panicked at 'Received 1056 samples, while the configured buffer size is 512': C:\Users\limes\.cargo\git\checkouts\nih-plug-a2d2dc277b128e13\b3038b4\src\wrapper\standalone\backend\cpal.rs:832
   0: backtrace::backtrace::dbghelp64::trace
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\backtrace-0.3.71\src\backtrace\dbghelp64.rs:99
      backtrace::backtrace::trace_unsynchronized<backtrace::capture::impl$1::create::closure_env$0>
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\backtrace-0.3.71\src\backtrace\mod.rs:66
   1: backtrace::backtrace::trace<backtrace::capture::impl$1::create::closure_env$0>
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\backtrace-0.3.71\src\backtrace\mod.rs:53
   2: backtrace::capture::Backtrace::create
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\backtrace-0.3.71\src\capture.rs:193
   3: backtrace::capture::Backtrace::new
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\backtrace-0.3.71\src\capture.rs:158
   4: nih_plug::wrapper::util::log_panics::closure$0::closure$0
             at C:\Users\limes\.cargo\git\checkouts\nih-plug-a2d2dc277b128e13\b3038b4\src\wrapper\util.rs:139
   5: assert_no_alloc::permit_alloc<tuple$<>,nih_plug::wrapper::util::log_panics::closure$0::closure_env$0>
             at C:\Users\limes\.cargo\git\checkouts\rust-assert-no-alloc-cb7191db54a1fe46\a6fb4f6\src\lib.rs:113
   6: nih_plug::util::permit_alloc<tuple$<>,nih_plug::wrapper::util::log_panics::closure$0::closure_env$0>
             at C:\Users\limes\.cargo\git\checkouts\nih-plug-a2d2dc277b128e13\b3038b4\src\util.rs:18
   7: nih_plug::wrapper::util::log_panics::closure$0
             at C:\Users\limes\.cargo\git\checkouts\nih-plug-a2d2dc277b128e13\b3038b4\src\wrapper\util.rs:136
   8: alloc::boxed::impl$49::call
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\alloc\src\boxed.rs:2029
      std::panicking::rust_panic_with_hook
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\panicking.rs:785
   9: std::panicking::begin_panic_handler::closure$0
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\panicking.rs:659
  10: std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\sys_common\backtrace.rs:171
  11: std::panicking::begin_panic_handler
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\panicking.rs:647
  12: core::panicking::panic_fmt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\core\src\panicking.rs:72
  13: nih_plug::wrapper::standalone::backend::cpal::impl$4::build_output_data_callback::closure$0<plop::Plop,f32,nih_plug::wrapper::standalone::wrapper::impl$2::run_audio_thread::closure_env$0<plop::Plop,nih_plug::wrapper::standalone::backend::cpal::CpalMidir>
             at C:\Users\limes\.cargo\git\checkouts\nih-plug-a2d2dc277b128e13\b3038b4\src\wrapper\standalone\backend\cpal.rs:832
  14: cpal::traits::DeviceTrait::build_output_stream::closure$0<cpal::platform::platform_impl::Device,f32,nih_plug::wrapper::standalone::backend::cpal::impl$4::build_output_data_callback::closure_env$0<plop::Plop,f32,nih_plug::wrapper::standalone::wrapper::impl
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cpal-0.15.3\src\traits.rs:169
  15: cpal::host::wasapi::stream::process_output
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cpal-0.15.3\src\host\wasapi\stream.rs:483
  16: cpal::host::wasapi::stream::run_output
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cpal-0.15.3\src\host\wasapi\stream.rs:313
  17: cpal::host::wasapi::stream::impl$1::new_output::closure$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platform::platform_impl::Device,f32,nih_plug::wrapper::standalone::backend::cpal::impl$4::build_output_data_callback::closure_env
             at C:\Users\limes\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cpal-0.15.3\src\host\wasapi\stream.rs:139
  18: core::hint::black_box
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\core\src\hint.rs:334
      std::sys_common::backtrace::__rust_begin_short_backtrace<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platform::platform_impl::Device,f32,nih_plug::wrapper::standalone::b
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\sys_common\backtrace.rs:155
  19: std::thread::impl$0::spawn_unchecked_::closure$1::closure$0<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platform::platform_impl::Device,f32,nih_plug::wrapper::standalone
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\thread\mod.rs:529
  20: core::panic::unwind_safe::impl$23::call_once<tuple$<>,std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platfor
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\core\src\panic\unwind_safe.rs:272
  21: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_e
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\panicking.rs:554
  22: std::panicking::try::do_catch<core::panic::unwind_safe::AssertUnwindSafe<std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<nih_plug::wrapper::standalone::backend::cpal::impl$3::run::closure$0::closure$2::closure_env$0<plop::Plop,nih_plug::wr
  23: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_e
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\panicking.rs:518
  24: std::panic::catch_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\panic.rs:142
      std::thread::impl$0::spawn_unchecked_::closure$1<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platform::platform_impl::Device,f32,nih_plug::wrapper::standalone::backend::
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\std\src\thread\mod.rs:528
  25: core::ops::function::FnOnce::call_once<std::thread::impl$0::spawn_unchecked_::closure_env$1<cpal::host::wasapi::stream::impl$1::new_output::closure_env$0<cpal::traits::DeviceTrait::build_output_stream::closure_env$0<cpal::platform::platform_impl::Device,f
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\core\src\ops\function.rs:250
  26: std::sys::pal::windows::thread::impl$0::new::thread_start
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\sys\pal\windows\thread.rs:58
  27: BaseThreadInitThunk
  28: RtlUserThreadStart

I noticed it with my plugin (https://github.com/YPares/nih-faust-jit) but tried with the template in order to have a more minimal repro.

This is a regression, as this error wasn't present back when I was using nih-plug at commit bb274976ca138289436b0bfa209b2c8c13244225. This happens when building in both release & debug.

robbert-vdh commented 1 week ago

Try increasing the period size from the default 512 samples to 2048 (or 1056 which is a weird number but that seems to be what you're getting from CPAL/wasapi). The configured buffer size seems to be fairly meaningless on Windows and macOS.

YPares commented 1 week ago

Okay, indeed with -p 1056 it fixes the issue. Would it be possible to add a reference to that flag in the error message, if that's something you have control over?