-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualmic
executable file
·90 lines (79 loc) · 3.1 KB
/
virtualmic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
# Help message
usage() {
echo "Usage: $0 [-p pipe_filename] [-n source_name] [-f format]"
echo " [-r rate] [-c channels] [-v] [-d] [-h] [input_filename]"
echo
echo " input_filename This file is piped to the virtual mic (can be an url) (default: stdin)"
echo " -p, --pipe Set audio pipe filename (use mktemp otherwise)"
echo " -n, --name Set PulseAudio source_name (default: virtualmic)"
echo " -f, --format Set audio format (default: s16le)"
echo " -r, --rate Set audio sampling frequency (default: 44100)"
echo " -c, --channels Set number of audio channels (default: 1)"
echo " -v, --verbose Make output verbose"
echo " -d, --default Set virtual microphone as PulseAudio default source"
echo " -h, --help Show this help"
}
# Default variables
input_filename='-' # Positional argument
pipe_filename= # -p / --pipe
source_name='virtualmic' # -n / --name
format='s16le' # -f / --format
rate='44100' # -r / --rate
channels='1' # -c / --channels
verbose= # -v / --verbose
default= # -d / --default
# Read arguments
while [ "$1" != '' ]
do
case "$1" in
-p | --pipe ) shift ; pipe_filename="$1" ;;
-n | --name ) shift ; source_name="$1" ;;
-f | --format ) shift ; format="$1" ;;
-r | --rate ) shift ; rate="$1" ;;
-c | --channels) shift ; channels="$1" ;;
-v | --verbose ) verbose='1' ;;
-d | --default ) default='1' ;;
-h | --help ) usage ; exit 0 ;;
-* ) echo "$0: invalid option '$1'"
usage ; exit 1 ;;
*) input_filename="$1"
esac
shift
done
# If no --pipe, use mktemp
[ "$pipe_filename" ] || pipe_filename="$(mktemp -u)"
# Print variables
[ "$verbose" ] && {
echo "input_filename: $input_filename"
echo "pipe_filename: $pipe_filename"
echo "source_name: $source_name"
echo "format: $format"
echo "rate: $rate"
echo "channels: $channels"
echo "default: $default"
}
# Create source
pulseaudio_module_index="$(pactl load-module module-pipe-source \
source_name=$source_name \
file=$pipe_filename \
format=$format \
rate=$rate \
channels=$channels)" && {
# Cleanup
trap "pactl unload-module $pulseaudio_module_index" EXIT INT
[ "$verbose" ] && {
echo "pulseaudio_module_index: $pulseaudio_module_index"
ffmpeg_loglevel='verbose'
} || ffmpeg_loglevel='panic'
# Set source as default
[ "$default" ] && pactl set-default-source "$source_name"
# Redirect input to source
ffmpeg \
-loglevel "$ffmpeg_loglevel" \
-re -i "$input_filename" \
-f "$format" \
-ar "$rate" \
-ac "$channels" \
'-' > "$pipe_filename"
}