RustAudio / cpal

Cross-platform audio I/O library in pure Rust
Apache License 2.0
2.72k stars 363 forks source link

[macOS] All input samples are zero #329

Open mlsteele opened 5 years ago

mlsteele commented 5 years ago

All input samples are 0.0 on this mbp (15-inch, Mid 2015) running macOS 10.14.16

Using this code https://github.com/mlsteele/cpal-input-demo, only zero samples come out of the input stream. Other audio stuff works like recording with quicktime.

The same cpal-input-demo outputs non-zero ✅ samples on two other machines. One newer mbp (13-inch, 2016) also running macOS 10.14.6, and one laptop running Ubuntu 16.04.

On the problem machine:

$ cd cpal-input-demo
$ cargo run --release                                                                                                                                                                                                                    [130]
    Finished release [optimized] target(s) in 0.03s
     Running `target/release/cpal-input-demo`
Default input device: Built-in Microphone
Default input format: Format { channels: 2, sample_rate: SampleRate(44100), data_type: F32 }
buffer f32 len=1024
buffer f32 len=1024
buffer f32 len=1024
buffer f32 len=1024
buffer f32 len=1024
buffer f32 len=1024
... etc

With no non-zero sample in sight.

$ cargo run --release --example enumerate                                                                                                                                                                                                [101]
   Compiling cpal v0.10.0 (/Users/miles/code/vendor/cpal)
    Finished release [optimized] target(s) in 0.56s
     Running `target/release/examples/enumerate`
Supported hosts:
  [CoreAudio]
Available hosts:
  [CoreAudio]
CoreAudio
  Default Input Device:
    Some("Built-in Microphone")
  Default Output Device:
    Some("Built-in Output")
  Devices:
  1. "Built-in Microphone"
    Default input stream format:
      Format { channels: 2, sample_rate: SampleRate(44100), data_type: F32 }
    All supported input stream formats:
      1.1. SupportedFormat { channels: 2, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), data_type: F32 }
      1.2. SupportedFormat { channels: 2, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), data_type: F32 }
      1.3. SupportedFormat { channels: 2, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), data_type: F32 }
      1.4. SupportedFormat { channels: 2, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), data_type: F32 }
  2. "Built-in Output"
    Default output stream format:
      Format { channels: 2, sample_rate: SampleRate(44100), data_type: F32 }
    All supported output stream formats:
      2.1. SupportedFormat { channels: 2, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), data_type: F32 }
      2.2. SupportedFormat { channels: 2, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), data_type: F32 }
      2.3. SupportedFormat { channels: 2, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), data_type: F32 }
      2.4. SupportedFormat { channels: 2, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), data_type: F32 }
image

What can I do to get non-zero samples from the mic?

tallen11 commented 5 years ago

Also experiencing this when trying to get input from guitar interface. All input samples are 0. On macOS Catalina.

sletz commented 5 years ago

Authorization issues ?

tallen11 commented 5 years ago

Authorization issues ?

@sletz there’s never a prompt for permissions. Maybe we just need to manually grant mic access to our programs?

Sent with GitHawk

sletz commented 5 years ago

I think so, see: https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos?language=objc

mlsteele commented 5 years ago

Confirmed authorization issue for me. Thanks @sletz and @tallen11!

The fix, for future readers, is to go to [System Preferences -> Security & Privacy -> Privacy -> Microphone] and add your terminal program, or whatever process is using cpal.

@tallen11 does that work for you?

Evidence:

Running cpal-input-demo on that same machine on the same 10.14.16 version, but months later, resulted in non-zero samples, working as it should. In [System Preferences -> Security & Privacy -> Privacy -> Microphone] there was a checked row for Alacritty (the terminal program I use) allowing mic access. Presumably that either didn't prompt or I denied it prior to September. Then at some point since the row got added.

To make sure I tried with the stock Terminal app. Running cargo run --release resulted in a prompt asking for Terminal to access mic. After granting, non-zero samples worked. Toggling the permission toggled whether non-zero samples were produced.

tallen11 commented 5 years ago

Confirmed authorization issue for me. Thanks @sletz and @tallen11!

The fix, for future readers, is to go to [System Preferences -> Security & Privacy -> Privacy -> Microphone] and add your terminal program, or whatever process is using cpal.

@tallen11 does that work for you?

Evidence:

Running cpal-input-demo on that same machine on the same 10.14.16 version, but months later, resulted in non-zero samples, working as it should. In [System Preferences -> Security & Privacy -> Privacy -> Microphone] there was a checked row for Alacritty (the terminal program I use) allowing mic access. Presumably that either didn't prompt or I denied it prior to September. Then at some point since the row got added.

To make sure I tried with the stock Terminal app. Running cargo run --release resulted in a prompt asking for Terminal to access mic. After granting, non-zero samples worked. Toggling the permission toggled whether non-zero samples were produced.

@mlsteele Yep seems to work! Thanks everyone.

EDIT: Is there a way to add arbitrary apps to the microphone permissions? I'm developing with CLion and running my project from there has the all zeros issue, while running from Terminal (which has mic access enabled) works fine. But I don't know how to allow either Clion or my program access to the Mic. Thanks.

justacec commented 3 years ago

I have been trying to use this to steal my output audio by using a Multi-Output device with soundflower and am having the zero issue described above. My Multi-Output device is set to send data to both the MacBook Speakers and also the Soundflower (2ch) output. When I open the soundflower (2ch) device for input, I am expecting to see what ever is coming out of my speakers, but all I get is zeros.

Does the authorization issue extend to devices other than the Microphone?