How do I monitor microphone input?

6,213

Solution 1

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

Solution 2

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

Share:
6,213

Related videos on Youtube

Dims
Author by

Dims

Software developer & Machine Learning engineer C/C++/Java/C#/Python/Mathematica/MATLAB/Kotlin/R/PHP/JavaScript/SQL/HTML/ LinkedIn: http://www.linkedin.com/in/dimskraft Telegram: https://t.me/dims12 I prefer fishing rod over fish.

Updated on September 18, 2022

Comments

  • Dims
    Dims over 1 year

    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.

    • NickD
      NickD over 3 years
      If you are using PulseAudio, there is the Pulse Audio Volume Meter.
  • Alexander Abakumov
    Alexander Abakumov about 3 years
    Although it works, the delay is about 1 second. Is there a way to improve performance?
  • dirkt
    dirkt about 3 years
    @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.
  • sebasth
    sebasth about 3 years
    @AlexanderAbakumov you could try adjusting the latency parameter in the loopback module.
  • Arun
    Arun almost 3 years
    If don't know how to unload this module please see this answer
  • Mikko Rantalainen
    Mikko Rantalainen over 2 years
    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.
  • Admin
    Admin almost 2 years
    For completeness: If you want to unload the loopback module do pactl unload-module module-loopback. Or if you want to reset pulseaudio whatever the settings you have in the config file default.pa, then do pulseaudio -k.