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