aboutsummaryrefslogtreecommitdiff
path: root/discord_input.sh
blob: e895f6b62cfec18eb96a4d0d83c0c3e46fb8549c (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
#!/usr/bin/env bash

switch_discord_input=ask # ask|yes|no
sink_name=merged_streams
sink_description=""

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

if [ "$sink_description" == "" ]; then
  sink_description="$sink_name"
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
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 | egrep '(.*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 | egrep '(.*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;31mDiscord 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
          echo did it
          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
      echo did it
      pacmd move-source-output "$discord_idx" "$sink_name.monitor"
    fi
  fi
fi