Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux pipe audio to virtual microphone using pactl

I found an example that shows how to pass a wave file as microphone input by utilizing "pactl load-module module-pipe-source". The issue with this example is that it relies on an infinite while loop and does not stop when the audio file is success put through the microphone a single time. If someone can a fix to this example that would be great. I heard of: sudo modprobe snd-dummy But do not know how to use it. documentation is quite lacking.

The example is as follows:

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

# Set the virtmic as the default source device.
pactl set-default-source virtmic

# 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 good_morning_vietnam.wav > /home/cammy/audioFiles/virtmic
done

The result of this as you can imagine is that the audio clip is continuously looped repeatedly. I only want this played once, not many times. If i try line:

cat good_morning_vietnam.wav > /home/cammy/audioFiles/virtmic

outside of the while loop. It appears that only a tiny sample of the audio file reaches the microphone, not the entire clip. I have no idea why this is the case. Not sure if like the mic file is regularly purged or something.

like image 243
Critical Labs Avatar asked Sep 03 '25 15:09

Critical Labs


1 Answers

I found the answer as a comment on another similar question (Linux pipe audio file to microphone input):

ffmpeg -re -i $AUDIO_FILE -f s16le -ar 16000 -ac 1 - > $VIRTUAL_MIC
like image 150
UncleZeiv Avatar answered Sep 05 '25 15:09

UncleZeiv