3

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.

Critical Labs
  • 31
  • 1
  • 2
  • 1
    This will probably be the fact that `cat` will dump the raw contents of the file into the virtual microphone almost instantly. If you time the call to `cat /file > /virtmic` it will probably execute almost instantly. I would think that you need to feed in data at the rate at which the wav file is recorded, such that if your audio file is 3s, it will take 3s to write all the bytes. – Matt Clark Jul 02 '18 at 03:31
  • Make sure no clients are connected to the virtual mic source when you redirect the wav file. That includes, for example, the sound configuration GUI which will 'slurp' the sound if you select the input (as it reads the input to display the input meter). When no clients are connected the file redirect should 'block' until the first client connects. Another thing I found was that some clients don't read the whole audio (or aren't given the whole audio...not sure which). I found that reading with `sox` would only partially work, however reading with `parec` worked perfectly. – IanB Aug 14 '18 at 11:45

1 Answers1

3

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
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77