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 install_virtmic.sh
to create a "virtual microphone" for PulseAudio to use as well as PulseAudio clients:
#!/bin/bash
# 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
done
A quick script uninstall_virtmic.sh
for undoing everything done by the install script:
#!/bin/bash
# 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!
Play audio as microphone input
Just as there are printer drivers that do not connect to a printer at all but rather write to a PDF file, analogously there are virtual audio drivers available that do not connect to a physical microphone at all but can pipe input from other sources such as files or other programs.
I hope I'm not breaking any rules by recommending free/donation software, but VB-Audio Virtual Cable should let you create a pair of virtual input and output audio devices. Then you could play an MP3 into the virtual output device and then set the virtual input device as your "microphone". In theory I think that should work.
If all else fails, you could always roll your own virtual audio driver. Microsoft provides some sample code but unfortunately it is not applicable to the older Windows XP audio model. There is probably sample code available for XP too.
Related Topics
Multiplication with Expr in Shell Script
How to Open Remotely Installed Sonar on a Browser
Setting Up a Cronjob on Google Compute Engine
Which Real-Time Priority Is the Highest Priority in Linux
How to Change Rvm Install Location
Granting Access Permission to a File to a Specific User
Warning Building a Kernel Module That Uses Exported Symbols
Configure Error: Could Not Link Against Boost_System
On Linux: How to Programmatically Determine If a Nic Interface Is Enabled and Plugged In
How to Run Script Commands from Variables
How to Confirm Sftp File Delivery
How Do 2 or More Fork System Calls Work
Start Jboss 7 as a Service on Linux
How to Properly Set Java_Home in /Etc/Environment
Save in a Variable The Number of Seconds a Process Took to Run