47

How can I redirect the microphone of one computer to listen to it on another computer via ssh? Which is the right device or which is the right command line?

Some years ago it was easy and fun to redirect sound from a remote microphone to a local computer or vice versa – it was an easy telephone. There are some instructions for it, but none of them seem to work on newer computers/linux distros. I don’t even have a /dev/audio on my computer (Fedora 17).

I think that it may have something to do with pulse audio. Or don’t I need pulse audio for this simple telephone? Which is the right device?

I can see all my sound devices when I start alsamixer and press the F6 key. But I don’t know which are the devices in my /dev tree.

53

OK, I've just found it, and it still works! Really funny. You don’t need any fancy applications, instant messengers or the like. With this command you send your audio to the remote host.

arecord -f cd -t raw | oggenc - -r | ssh <user>@<remotehost> mplayer -

Or if you like ffmpeg better

ffmpeg -f alsa -ac 1 -i hw:3 -f ogg - \
    | ssh <user>@<remotehost> mplayer - -idle -demuxer ogg

Source: http://shmerl.blogspot.de/2011/06/some-fun-with-audio-forwarding.html


If you want a real telephone:

The command above was only for one direction. For the other direction you have to start another ssh session. So, to receive what the other user says to you, use

ssh <user>@<remotehost> 'arecord -f cd -t raw | oggenc - -r' | mplayer -

Or if you like ffmpeg better

ssh <user>@<remotehost> ffmpeg -f alsa -ac 1 -i hw:3 -f ogg - \
    | mplayer - -idle -demuxer ogg

where hw:3 is the alsadevice you want to record (find it with arecord -l; you can also use a device name, find this with arecord -L; in many cases you can just use the device listed with the following command: arecord -L | grep sysdefault).

Update

In 2018 on my Fedora Linux systems ffmpeg does not have alsa support included (it seems to be the same on RaspberryPi systems with Raspbian). But there is a simple solution without recompiling. Just pipe the output of arecord (the alsarecorder) to ffmpeg:

ssh <user>@<remotehost> 'arecord -f cd -D plughw:2 | ffmpeg -ac 1 -i - -f ogg -' \
    | mplayer - -idle -demuxer ogg

You get the input device plughw:2 by finding your device in the output of the following command:

arecord -l

In my case I see card0 and card2 (my webcam which has a microphone). So I wrote plughw:2 for card2.

Update 2 (without mplayer)

If you don’t have or like mplayer but ffplay (which is part of ffmpeg) you can use:

  • uncompressed wave-audio (high bandwidth, very low cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2" | ffplay -nodisp -
    
  • compressed with flac (low bandwidth, low cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | flac - -o -" | ffplay -nodisp -
    
  • compressed with ogg (very low bandwidth, high cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | oggenc -" | ffplay -nodisp -
    
  • 1
    Is there some reason you are doing cat - | mplayer - instead of just mplayer -? – Chris Down Feb 28 '14 at 7:29
  • The reason is: I just copied the command from the cited source. Might be redundant. I have deleted it, seems to still work. Maybe there is an edge case where it was needed and the author knew about it without telling it in his post? – erik Feb 28 '14 at 19:46
3

Erik's answer works well, but using it how it is, using flac or oggenc, there's significant lag.
A solution is to add -cache 256 to the mplayer command, so it would look as follows:

ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | oggenc -" | mplayer -cache 256 -

Another thing to mention is that nowadays pretty much every modern distribution uses PulseAudio, which takes control over the hardware device, therefore using plughw:2 will fail if PulseAudio is running.
Generally, default should work fine and use PulseAudio, where you can use pavucontrol or similar to select your desired input source using PulseAudio.

That would make the command as follows:

ssh <user>@<remotehost> "arecord -f cd | oggenc -" | mplayer -cache 256 -

BUT: This introduces a lag. In my case, it is around eight seconds. That's terrible.


The solution is much simpler than any other command:

ssh <user>@<host> "arecord -f S16_LE -r 36000" | aplay

That's it. This is using barely any CPU (0.1% according to htop). It's using ~60KB/s of bandwidth for me.

If you want stereo sound: ssh <user>@<host> "arecord -f cd" | aplay
Only useful if you have a stereo microphone, increases bandwidth to ~150KB/s for me. (Since it also raises the sampling rate to 44100Hz).

More information on sampling rate (-r) and bit rate (affected by -f) can be found here.
Basically: The lower you go, the worse the quality but the lower the bandwidth needed.

This solution is fully compatible with PulseAudio, you can control output device, volume and more using pactl or the pavucontrol GUI application:

pavucontrol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.