ffmpeg_microphone does not use current input device on Mac/Darwin
Author: ruisilvestre
Pull request: no
Labels: name: Audio

While going through the HF tutorials for STT [here](https://huggingface.co/learn/audio-course/chapter7/voice-assistant), I found some unexpected behaviour with the ffmpeg_microphone_live function on my Mac. I also just found someone that might be having the same issue [here](https://github.com/huggingface/transformers/issues/25183#issuecomment-1778473797) but it's an issue related to sound in Colab env so I'm creating this separately.

The input device index used is always 0, but that might not match the current system input device. Using the current system input device would be the expected behaviour (also according to the other platforms' code that all specify `default` for input device). E.g. I was working with my laptop closed (just connected to the monitor) and wanted to capture sound with my headphones but couldn't.

The solution seems to be fairly simple. Based on the [ffmpeg devices documentation](https://ffmpeg.org/ffmpeg-devices.html#avfoundation) the value `default` is also supported for audio in avfoundation, and it will match the current system input device.

I've changed this manually in audio_utils.py ffmpeg_microphone(...) and it seems to work as expected.
```
elif system == "Darwin":
        format_ = "avfoundation"
        input_ = ":default"
```
Here's the [link](https://github.com/huggingface/transformers/blob/fb78769b9c053876ed7ae152ee995b0439a4462a/src/transformers/pipelines/audio_utils.py#L68) to the same line in the HF repo.

I can make a PR for it if you want. This could also go with adding a param for the device index to those microphone functions similar to how other audio libraries do for easier customisation, which just falls back to use the `default` input device.

## Additional Info

`transformers-cli env` output
- `transformers` version: 4.35.2
- Platform: macOS-14.2-arm64-arm-64bit
- Python version: 3.10.13
- other info not relevant for this issue

Code to reproduce is the snippet in the voice-assistant tutorial. In case the 0th device is not the one you want to listen with, the code will just fail since it won't capture any audio.
```
import sys

def transcribe(chunk_length_s=5.0, stream_chunk_s=1.0):
    sampling_rate = transcriber.feature_extractor.sampling_rate

    mic = ffmpeg_microphone_live(
        sampling_rate=sampling_rate,
        chunk_length_s=chunk_length_s,
        stream_chunk_s=stream_chunk_s,
    )

    print("Start speaking...")
    for item in transcriber(mic, generate_kwargs={"max_new_tokens": 128}):
        sys.stdout.write("\033[K")
        print(item["text"], end="\r")
        if not item["partial"][0]:
            break

    return item["text"]
```

According to [ffmpeg devices documentation](https://ffmpeg.org/ffmpeg-devices.html#Examples) you can print out your system input devices using
`ffmpeg -f avfoundation -list_devices true -i ""`

For me this gives:
```
[...]
[AVFoundation indev @ 0x7fcc33004d00] AVFoundation video devices:
[AVFoundation indev @ 0x7fcc33004d00] [0] FaceTime HD Camera
[AVFoundation indev @ 0x7fcc33004d00] [1] Rui Silvestre’s iPhone Camera
[AVFoundation indev @ 0x7fcc33004d00] [2] Capture screen 0
[AVFoundation indev @ 0x7fcc33004d00] AVFoundation audio devices:
[AVFoundation indev @ 0x7fcc33004d00] [0] MacBook Pro Microphone
[AVFoundation indev @ 0x7fcc33004d00] [1] Rui Silvestre’s iPhone Microphone
[AVFoundation indev @ 0x7fcc33004d00] [2] AirPods Pro
[AVFoundation indev @ 0x7fcc33004d00] [3] Microsoft Teams Audio
```
The audio device at index 0 is my MacBook mic but I currently have my AirPods on and would want to use that as my input device. I've also noticed the indexes change fairly frequently depending on which devices are nearby.


Comments:
cc @sanchit-gandhi @ylacombe 

Hey @ruisilvestre, sorry for the long delay, I unfortunately don't have a Mac to try this on.
Could you open a PR and we'll discuss this with @sanchit-gandhi ?

Also cc @Narsil as you might have better experience with ffmpeg !

Encountered this issue today while [following the voice assistant course](https://github.com/rodrigobdz/hugging-face-voice-assistant?tab=readme-ov-file). In summary, @ruisilvestre proposed the following changes in [here](https://github.com/huggingface/transformers/blob/fb78769b9c053876ed7ae152ee995b0439a4462a/src/transformers/pipelines/audio_utils.py#L68):

```diff
 elif system == "Darwin":
    format_ = "avfoundation"
-    input_ = ":0"
+    input_ = ":default"
```

That solved the issue for me. 

> Encountered this issue today while [following the voice assistant course](https://github.com/rodrigobdz/hugging-face-voice-assistant?tab=readme-ov-file). In summary, @ruisilvestre proposed the following changes in [here](https://github.com/huggingface/transformers/blob/fb78769b9c053876ed7ae152ee995b0439a4462a/src/transformers/pipelines/audio_utils.py#L68):
> 
> ```diff
>  elif system == "Darwin":
>     format_ = "avfoundation"
> -    input_ = ":0"
> +    input_ = ":default"
> ```
> 
> That solved the issue for me.

I tried this on my mac and got it working as well. It would be nice if this was merged. 

The solution proposed seems really reasonable to me! Would one of you like to open a PR to fix this?

Also cc @Narsil for your opinion as you wrote that bit of code! 

@ylacombe @Narsil  I've opened a PR with the simplest fix for this, let me know if that is enough.

Hello any updates on this ?


This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

We should be able to close this as #31846 was merged

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.