Skip to content

Commit

Permalink
removed restart, added some more start options and changed spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosty-be committed Feb 5, 2021
1 parent cb9acfa commit 08c1bb2
Showing 1 changed file with 60 additions and 30 deletions.
90 changes: 60 additions & 30 deletions virtmic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,72 @@ AUDIOFILE=input.mp3
# trap ctrl-c and call stop()
trap stop SIGINT

function createvirtualmic()
{
# Load the "module-pipe-source" module to read audio data from a FIFO special file.
echo "Creating virtual microphone."
pacmd load-module module-pipe-source source_name=virtmic file=${VIRTMICPATH}/virtmic format=s16le rate=16000 channels=1

# Set the virtmic as the default source device.
echo "Set the virtual microphone as the default device."
pacmd 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" > ~/.config/pulse/client.conf
}

function start()
{
# Load the "module-pipe-source" module to read audio data from a FIFO special file.
echo "Creating virtual microphone."
pacmd load-module module-pipe-source source_name=virtmic file=${VIRTMICPATH}/virtmic format=s16le rate=16000 channels=1

# Set the virtmic as the default source device.
echo "Set the virtual microphone as the default device."
pacmd 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" > ~/.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 (in a loop)."
while :
do
ffmpeg -loglevel quiet -re -i ${AUDIOFILE} -f s16le -ar 16000 -ac 1 - > ${VIRTMICPATH}/virtmic
done
createvirtualmic
echo "start your own stream:"
echo "ffmpeg -loglevel quiet -re -i ${AUDIOFILE} -f s16le -ar 16000 -ac 1 - > ${VIRTMICPATH}/virtmic"
echo "and don't forget to run '$0 stop' when you are finished"
}

function startlooped()
{
createvirtualmic
loopmp3
}

function startplayonce()
{
createvirtualmic
playmp3
stop
}

function loopmp3()
{
# 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 (in a loop)."
echo "press ctrl-c to stop"
while :
do
playmp3
done
}

function playmp3()
{
ffmpeg -loglevel quiet -re -i ${AUDIOFILE} -f s16le -ar 16000 -ac 1 - > ${VIRTMICPATH}/virtmic
}

# Uninstall the virtual microphone.
function stop()
{
echo ""
echo "virtmic interrupted... cleaning up..."
echo "removing virtual microphone"
pacmd unload-module module-pipe-source
echo "removing the virtual microphone as the default device."
rm ~/.config/pulse/client.conf
echo ""
echo "virtmic stopped... cleaning up..."
echo "removing virtual microphone"
pacmd unload-module module-pipe-source
echo "removing the virtual microphone as the default device."
rm ~/.config/pulse/client.conf
}

function usage()
{
echo "Usage: $0 {start|stop|restart}"
exit 1
echo "Usage: $0 {start|startlooped|startplayonce|stop}"
exit 1
}

case "$1" in
Expand All @@ -59,15 +89,15 @@ case "$1" in
stop)
stop
;;
restart)
stop
sleep 1
start
startlooped)
startlooped
;;
startplayonce)
startplayonce
;;
*)
usage
;;
esac

exit 0

0 comments on commit 08c1bb2

Please sign in to comment.