Last active
January 24, 2023 20:50
-
-
Save GasparVardanyan/1a01f71e2eecb9bd191e4cd7a7b16496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
OPFILE=$2 | |
OPMIC_NAME=opusmic | |
OPMIC_FILE=/tmp/opusmic | |
OPMIC_RAW=/tmp/opusmic.raw | |
mkdir -p ~/.config/pulse | |
OPMIC_CONF=~/.config/pulse/client.conf | |
OPFILE_FORMAT=s16le | |
# OPFILE_RATE=48000 | |
# OPFILE_CHANNELS=2 | |
if [ "$1" == 'off' ] | |
then | |
pactl unload-module module-pipe-source | |
rm -f ~/.config/pulse/client.conf | |
rm -f "$OPMIC_FILE" | |
rm -f "$OPMIC_RAW" | |
exit 0 | |
fi | |
OPFILE_CHANNELS=$(opusinfo "$OPFILE" | grep Channels | cut -d ' ' -f 2) | |
OPFILE_RATE="$(opusinfo "$OPFILE" | grep 'Original sample rate:' | sed 's/[^:]*: //' | cut -d ' ' -f 1)" | |
echo CHANNELS $OPFILE_CHANNELS | |
echo RATE $OPFILE_RATE | |
echo "" | |
echo "Creating virtual microphone." | |
echo pactl load-module module-pipe-source source_name="$OPMIC_NAME" file="$OPMIC_FILE" format="$OPFILE_FORMAT" rate="$OPFILE_RATE" channels="$OPFILE_CHANNELS" | |
pactl load-module module-pipe-source source_name="$OPMIC_NAME" file="$OPMIC_FILE" format="$OPFILE_FORMAT" rate="$OPFILE_RATE" channels="$OPFILE_CHANNELS" | |
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 = $OPMIC_NAME" > "$OPMIC_CONF" | |
# Opus to raw" | |
echo "Opus to raw..." | |
ffmpeg -i "$OPFILE" -f "$OPFILE_FORMAT" -acodec pcm_"$OPFILE_FORMAT" "$OPMIC_RAW" | |
# 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 "$OPMIC_RAW" > "$OPMIC_FILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment