9

How do I monitor microphone input in Linux?

Note: monitoring is not the same as recording and playing what was recorded.


In Windows, I can listen to my own microphone in the following ways:

  1. With some program, which will capture my microphone and send it to my sound card for playing. For example, with ffplay command

    ffplay -f dshow -i audio="Front panel mic (Realtek High Definition Audio)"
    

    It will produce significant delay, up to several seconds.

  2. With "Listen" option of input device properties

    enter image description here

    It will produce a smaller delay, up to one second.

  3. With sound card widget

    enter image description here

    It will produce ZERO delay. I think, this will make input sound to go to output without leaving sound card, but not sure.

I need option #2 in Linux command line.

1
  • If you are using PulseAudio, there is the Pulse Audio Volume Meter.
    – NickD
    Sep 24, 2020 at 12:55

2 Answers 2

10
+50

You should be able to monitor your microphone with PulseAudio's loopback module. Module should automatically create loopback outputs for available input devices. To load the module manually

pactl load-module module-loopback

To make the change persistent, append /etc/pulse/default.pa with

load-module module-loopback
6
  • Although it works, the delay is about 1 second. Is there a way to improve performance? Mar 15, 2021 at 3:05
  • 1
    @AlexanderAbakumov you may want to ask a separate question for this (you can reference this answer), where you describe exactly what you've done, and your audio setup. When I use module-loopback, the delay is not noticable.
    – dirkt
    Mar 15, 2021 at 6:32
  • @AlexanderAbakumov you could try adjusting the latency parameter in the loopback module.
    – sebasth
    Mar 15, 2021 at 10:14
  • 2
    If don't know how to unload this module please see this answer
    – Arun
    May 9, 2021 at 4:05
  • If you have analog input and analog output, I'd recommend just using pavucontrol to route input audio to output at the mixer. However, if you have e.g. USB mic then the audio cannot be just mixed but some software must record the audio and then playback it to output stream. And depending on your hardware this record + playback can cause some (or even high) latency. Sep 29, 2021 at 9:32
1

If you have analog input and analog output, I'd recommend just using pavucontrol (PulseAudio Volume Control) to route input audio to output at the mixer. However, if you have e.g. USB mic then the audio cannot be just mixed but some software must record the audio and then playback it to output stream. And depending on your hardware this record + playback can cause some (or even high) latency.

You could try this:

First get names of inputs and outputs you want to use:

$ pactl list short | egrep "alsa_(input|output)" | fgrep -v ".monitor"

for me, the results look like this:

0   alsa_output.pci-0000_00_1b.0.analog-stereo  module-alsa-card.c  s16le 2ch 44100Hz   RUNNING
0   alsa_input.usb-Microsoft_Microsoft___LifeCam_HD-5000-02.analog-mono module-alsa-card.c  s16le 1ch 44100Hz   SUSPENDED

which means that my output device at is called alsa_output.pci-0000_00_1b.0.analog-stereo and my USB webcam/microphone is called alsa_input.usb-Microsoft_Microsoft___LifeCam_HD-5000-02.analog-mono.

I can now record the USB mic and output it to the audio output like this:

$ pacat -r --latency-msec=1 -d alsa_input.usb-Microsoft_Microsoft___LifeCam_HD-5000-02.analog-mono | pacat -p --latency-msec=2 -d alsa_output.pci-0000_00_1b.0.analog-stereo

That is, one pacat process reads from the mic and requests audio stack to try to get the latency to 1 ms or 0.001 seconds. And another pacat process writes the audio to my output device and tries to get the latency to 2 ms or 0.002 seconds. You can try reducing the output latency to 1 ms, too, but at least for my pretty old hardware the audio clips a bit too easily in that case.

However, if I keep this combination of recording and playback output running for long periods it seems that the latency slowly increases over time. I'd guess that my USB mic clock is slightly faster than my output audio clock which causes the buffer to slowly increase. I don't know a nice way to to allow those pactl processes to skip audio to keep output realtime. I guess I would need to write a custom app for that.

For the above pacat ... | pacat pipeline the best case latency for my hardware seems to be around 6 ms from soundwaves hitting the mic to the soundwaves emitting from the output speakers while using Linux kernel with PREEMPT enabled (e.g. Ubuntu linux-lowlatency kernel).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .