aboutsummaryrefslogtreecommitdiff
path: root/discord_input.sh
blob: bde9c56a170502bcca8717e1c6f41521ccc2574e (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
# discord_input.sh: easily merge multiple audio streams together for discord, etc
# Copyright (C) 2021  Vidhu Kant Sharma (vidhukant@protonmail.ch)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

switch_discord_input=ask
sink_name=merged_streams
sink_description=""

show_help() {
  echo -e "Usage:"
  echo -e "\tNo Flags: Interactive Mode"
  echo -e "-n\tSpecify Number Of Streams"
  echo -e "-s\tSwitch Discord's Input Or Not (yes/no/ask)"
  echo -e "-N\tSpecify a Name For The Merged Sink"
  echo -e "-D\tSpecify a Description For The Merged Sink"

  exit
}

while getopts 'n:s:N:D:h' flag; do
  case "${flag}" in
    n) no_of_streams="${OPTARG}" ;;
    s) switch_discord_input="${OPTARG}" ;;
    N) sink_name="${OPTARG}" ;;
    D) sink_description="${OPTARG}" ;;
    h) show_help ;;
    *) ;;
  esac
done

# handle invalid input for -s
if [ "$switch_discord_input" != yes ] && \
   [ "$switch_discord_input" != no  ] && \
   [ "$switch_discord_input" != ask ]
then
  echo "ERROR: Please enter a valid input for -s (yes/no/ask)" >&2;exit 1
  exit 1
fi

# get the amount of streams to merge if not specified
if [ "$no_of_streams" = "" ]; then
  printf "\033[1;36mEnter number of sinks/sources to merge: \033[0m"
  read -r no_of_streams
else
  # handle invalid input for -n
  re='^[0-9]+$'
  if ! [[ "$no_of_streams" =~ $re ]] ; then
    echo "ERROR: -n is not specified a number!" >&2; exit 1
  fi
fi

if [ "$sink_description" == "" ]; then
  sink_description="$sink_name"
fi

# create NULL sink to merge all the audio streams
pacmd load-module module-null-sink sink_name="$sink_name"

# change desc of NULL sink and it's monitor for visual purpose
pacmd update-sink-proplist "$sink_name" device.description="$sink_description"
pacmd update-source-proplist "$sink_name.monitor" device.description="\"Monitor of $sink_name\""

i=0; while [ $i -lt "$no_of_streams" ]; do
  # get sources and split
  SAVEIFS=$IFS
  IFS=$'\n'
  # somebody help
  sources=($(pacmd list-sources | awk '/name:/ {print $2}; /device.description/ {$1=$2=""; print $0}' | sed 's/^[ \t\"]*//; s/[ \t\"]$//' | awk 'NR%2==0 {printf "\033[34m"$0"\t \033[0;32m"f"\033[0m\n"}  {f=$0}'))
  IFS=$SAVEIFS

  # print all the sources with their indices
  for (( j=0; j<${#sources[@]}; j++ )); do
    unformatted_sink_name="$(echo "${sources[j]}" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g')"
    new_sink_name="Monitor of $sink_name	 <$sink_name.monitor>"

    # do not print the newly created NULL output's monitor
    if [ "$unformatted_sink_name" != "$new_sink_name" ]; then
      printf "\033[1;33m%s: %s\n" "$j" "${sources[$j]}"
    fi
  done

  # ask to enter source number
  printf "\033[1;36mEnter a value: \033[0m"
  read -r source_idx

  # extract source name and save it
  source_sel=$(echo "${sources[source_idx]}" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' | awk -F "\t" '{print $2}' | sed 's/^[ <]*//; s/>$//')

  # finally create loopback
  pacmd load-module module-loopback sink="$sink_name" source="$source_sel"

  # get new loopback's index and rename loopback
  loopback_idx=$(pacmd list-source-outputs | grep -E '(.*index: .*)|(^\s+media.name = .*)' | awk "/Loopback to $sink_name/ {printf f} {f=\$2}")
  pacmd update-source-output-proplist "$loopback_idx" media.name="\"Merged stream #$((i+1))\""

  i=$((i + 1))
done

# check if switching disabled through flags
if [ "$switch_discord_input" != no ]; then
  # get index of discord's source output
  discord_idx="$(pacmd list-source-outputs | grep -E '(.*index: .*)|(^\s+application.process.binary = .*)' | awk '/Discord/ {printf f} {f=$2}')"
  
  # ask the user and change discord's input to new input
  if [ "$discord_idx" == "" ]; then
    printf "\033[1;34mDiscord instance not found/recording. If it is running, please use Discord settings or pavucontrol to manually change its input.\033[0m\n"
  else
    if [ "$switch_discord_input" == ask ]; then
      printf "\033[1;36mDiscord is running, switch discord's input to new merged input?(y/n)\033[0m "

      # get response
      while :; do
        read -r response
  
        if [ "$response" == y ] || [ "$response" == Y ]; then
          pacmd move-source-output "$discord_idx" "$sink_name.monitor"
          break
        elif [ "$response" == n ] || [ "$response" == N ]; then
          break
        else 
          printf "\033[1;36mPlease enter a valid input (y/n):\033[0m "
        fi
      done
    else
      pacmd move-source-output "$discord_idx" "$sink_name.monitor"
    fi
  fi
fi