Linux pipe audio file to microphone input

After many painstaking hours, I finally got something acceptable working. I undid everything I did with ALSA (since the default is for ALSA to use PulseAudio instead, which I initially overrode). I created a simple bash script to create a "virtual microphone" for PulseAudio to use as well as PulseAudio clients:


# This script will create a virtual microphone for PulseAudio to use and set it as the default device.

# Load the "module-pipe-source" module to read audio data from a FIFO special file.
echo "Creating virtual microphone."
pactl load-module module-pipe-source source_name=virtmic file=/home/charles/audioFiles/virtmic format=s16le rate=16000 channels=1

# Set the virtmic as the default source device.
echo "Set the virtual microphone as the default device."
pactl set-default-source virtmic

# Create a file that will set the default source device to virtmic for all 
PulseAudio client applications.
echo "default-source = virtmic" > /home/charles/.config/pulse/client.conf

# Write the audio file to the named pipe virtmic. This will block until the named pipe is read.
echo "Writing audio file to virtual microphone."
while true; do
    cat audioFile0.raw > /home/charles/audioFiles/virtmic

A quick script for undoing everything done by the install script:


# Uninstall the virtual microphone.

pactl unload-module module-pipe-source
rm /home/charles/.config/pulse/client.conf

I then fired up Chromium and clicked the microphone to use its voice search feature and it worked! I also tried with arecord test.raw -t raw -f S16_LE -c 1 -r 16000 and it worked too! It isn't perfect, because I keep writing to the named pipe virtmic in an infinite loop in the script (which quickly made my test.raw file insanely large), but it will do for now.

Please feel free to let me know if anyone out there finds a better solution!

  1. Make sure you have PulseAudio Volume Control (pavucontrol) installed. if not, you can install it simply typing in your terminal: sudo apt install pavucontrol.

Recording Tab

  1. Open PulseAudio Volume Control (pavucontrol) and go to "Recording" tab. When you don't have any devices recording your microphone, this panel should look empty. As soon as any application start recording or start Skype call, this panel should have at least one entry.

Recording Tab - Active

  1. Selecting "Built-in Audio Analog Stereo" is the default behavior (redirect your voice to microphone output). You should also see "Monitor of Built-in Audio Analog Stereo". when you select this option, the output any application that plays audio (e.g VLC) should be redirected to the microphone instead of your voice.

  2. Make sure to change it back to "Built-in Audio Analog Stereo" after the audio is finished playing for everything to go back to normal.