Advertisement
Guest User

menuaudio.sh

a guest
Dec 11th, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Menuaudio.sh is a shell script to start/stop Jack and do some audio things. It uses/creates
  4. # a settings file thats callled "menuaudio.cfg".
  5. # Original unmodified example script written by oToGamez (www.pro-toolz.net).
  6.  
  7. # Define variables.
  8. # This is your config file:
  9. cfgfile="`dirname $0`/menuaudio.cfg"
  10. # Use that file as the source for variables:
  11. source $cfgfile
  12.  
  13.  
  14. # If received SIGINT (i.e. Ctrl + C) then reset screen and exit. See "trap -l"
  15. # (notice that R is a function)
  16. trap "R;exit" 2    
  17.  
  18.  
  19. # This is the menu:
  20. #<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>
  21.      M0 () { TPUT  7 2; echo -en       "Jack audio daemon: start               ";}
  22.      M1 () { TPUT  8 2; echo -en       "                 : kill                ";}
  23.      M2 () { TPUT  9 2; echo -en       "Check Midi       : port numbers        ";}
  24.      M3 () { TPUT 10 2; echo -en       "                 : output              ";}
  25.      M4 () { TPUT 11 2; echo -en       "Check Alsa       : device/card numbers ";}
  26.      M5 () { TPUT 12 2; echo -en       "Pulseaudio       : start               ";}
  27.      M6 () { TPUT 13 2; echo -en       "                 : stop                ";}
  28.      M7 () { TPUT 14 2; echo -en       "                 : PAVUControl         ";}
  29.      M8 () { TPUT 15 2; echo -en       "Config file      : create/edit         ";}
  30.      M9 () { TPUT 16 2; echo -en       "                 : delete              ";}
  31.      M10() { TPUT 17 2; echo -en       "                 : view                ";}
  32.      M11() { TPUT 18 2; echo -en "\e[32mHelp                                  \e[0m";}
  33.      M12() { TPUT 19 2; echo -en "\e[31mExit                                  \e[0m";}
  34. # Last Menu item is:
  35.      LM=12
  36. #<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. # These are some more functions that are needed by the script. You don't ever need to edit this.
  46. #<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>
  47. # Reset = clear screen, clean terminal and no inverse:
  48.       R(){ clear;stty sane;echo -en "\e[27m";}
  49.  
  50.    TPUT(){ echo -en "\e[${1};${2}H";}                # Print text starting on line $1 and
  51.                                                      # column $2.
  52.    MARK(){ echo -en "\e[7m";}                        # Reverse video characters.
  53.  UNMARK(){ echo -en "\e[27m";}                       # Reset reverse.
  54.    HEAD()
  55.          {
  56.            TPUT 1 1
  57.            echo -e "\e[34;1m\e(0lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk\e(B"
  58.            echo -e     "\e(0x\e(B              Audio menu             \e(0x"
  59.            echo -e         "\e(0mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj\e[0m\e(B"
  60.            echo -e "Use ↑ and ↓ keys to select and Enter"
  61.            echo -e "to execute."
  62.          }
  63.   ARROW()
  64.          {
  65.            read -s -n3 key 2>/dev/null >&2
  66.            if [[ $key = $(echo -en "\e")[A ]];then echo up;fi   # Up in a terminal is:
  67.                                                                 # "escape char."A.
  68.            if [[ $key = $(echo -en "\e")[B ]];then echo dn;fi;  # Dn in a terminal is:
  69.                                                                 # "escape char."B.
  70.          }
  71. # More functions (build menu and catch cursor movement):
  72.    i=0
  73.    MENU(){ for each in $(seq 0 $LM);do M${each};done;}
  74.     POS(){ if [[ $cur == up ]];then ((i--));fi
  75.            if [[ $cur == dn ]];then ((i++));fi
  76.            if [[ $i -lt 0   ]];then i=$LM;fi
  77.            if [[ $i -gt $LM ]];then i=0;fi;}
  78. REFRESH(){ after=$((i+1)); before=$((i-1))
  79.            if [[ $before -lt 0  ]];then before=$LM;fi
  80.            if [[ $after -gt $LM ]];then after=0;fi
  81.            if [[ $j -lt $i      ]];then UNMARK;M$before;else UNMARK;M$after;fi
  82.            if [[ $after -eq 0 ]] || [ $before -eq $LM ];then
  83.            UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;}
  84.    INIT(){ R;HEAD;MENU;}
  85. # SC determines if cursur is selected?
  86.      SC(){ REFRESH;MARK;$S;cur=`ARROW`;}                    
  87. # ES is the footer:
  88.      ES(){ echo -en "\n\e[31mPress Enter for main menu...\e[0m";read;INIT;};INIT  
  89. #<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. # And now, the actual commands to be executed:
  97. while [[ "$O" != " " ]]; do case $i in
  98. ########
  99. # JACK #
  100. ########
  101. #  Jack audio daemon start
  102. 0) S=M0;SC;if [[ $cur == "" ]]; then R
  103.    jackd -d alsa --device hw:$sndcrd &
  104.    sleep 1
  105.    # See https://kx.studio/News 2019-03-22: we'd better kill jackdbus.
  106.    # pkill -9 jackdbus
  107.    # Route Jack via PullseAudio on my 2nd so I can listen to Youtube
  108.    # as well as Jack (restart Chrome!).
  109.    # Handy commands:
  110.    # pactl info
  111.    # pacmd list-sinks
  112.    # Dont use 'pacmd set-default-sink 1' (makes the Trust C-Media PCI default)
  113.    if [[ $jack_sink == "y" ]]
  114.       then
  115.       pactl load-module module-jack-sink &
  116.       pactl set-default-sink jack_out &
  117.       sleep 1
  118.       echo -e "\n\e[34mNow open PAVUcontrol and set 'jack_out' as Chrome's playback device.\e[0m"
  119.    fi
  120.    # Make Alsa's Midi also visible as Jack Midi.
  121.    if [[ $a2j == "y" ]]
  122.       then
  123.       a2j &
  124.       sleep 0.5
  125.    fi
  126. ES;fi;;
  127.  
  128. #  Jack audio daemon stop
  129. 1) S=M1;SC;if [[ $cur == "" ]]; then R
  130.    pkill -9 jackdbus
  131.    killall jackd
  132.    sleep 1
  133. ES;fi;;
  134.  
  135.  
  136. ########
  137. # MIDI #
  138. ########
  139. #  Check Midi port numbers
  140. 2) S=M2;SC;if [[ $cur == "" ]]; then R
  141.    aconnect -io
  142. ES;fi;;
  143.  
  144. # Check Midi output:
  145. 3) S=M3;SC;if [[ $cur == "" ]]; then R
  146.    echo "Type in Midi port number:"
  147.    read portnumber
  148.    aseqdump -p $portnumber
  149. ES;fi;;
  150.  
  151.  
  152.  
  153.  
  154. ########
  155. # Alsa #
  156. ########
  157. 4) S=M4;SC;if [[ $cur == "" ]]; then R
  158.    echo -e "Determine the number of your soundcards/audio devices. Watch the output in blue below. It's the first number in the line. Numbering starts with 0. So if you have two sound cards then the number of your second card is 1. \n"
  159.    echo -en "\e[34m"         # Make the following output blue
  160.    cat /proc/asound/cards
  161.    echo -en "\e[0m"
  162. ES;fi;;
  163.  
  164.  
  165. #########
  166. # PULSE #
  167. #########
  168. #  Pulse start:
  169. 5) S=M5;SC;if [[ $cur == "" ]]; then R
  170.    pulseaudio --start -v
  171. ES;fi;;
  172.  
  173. # Pulse kill:
  174. 6) S=M6;SC;if [[ $cur == "" ]]; then R
  175.    pulseaudio --kill
  176. ES;fi;;
  177.  
  178. #  PA VU Control:
  179. 7) S=M7;SC;if [[ $cur == "" ]]; then R
  180.    echo -e "\e[34;1mPAVUControl is running. \e[0m"
  181.    pavucontrol
  182. ES;fi;;
  183.  
  184.  
  185. ###############
  186. # Config file #
  187. ###############
  188. # Create/edit config file:
  189. 8) S=M8;SC;if [[ $cur == "" ]]; then R
  190.    echo -e "Determine the number of your soundcards/audio devices. Watch the output in blue below. It's the first number in the line. Numbering starts with 0. So if you have two sound cards then the number of your second card is 1. \n"
  191.    echo -en "\e[34m" # Make the following output blue
  192.    cat /proc/asound/cards
  193.    echo -en "\n\e[0m" # End of blue text
  194.    # If not exist then create config file and fill it with the variables:
  195.    if [ ! -e $cfgfile ]
  196.       then
  197.          echo -e "=> No config file found that is named $cfgfile. Going to create one...\n"
  198.          touch $cfgfile
  199.          echo -e "sndcrd=0"    >> $cfgfile
  200.          echo -e "a2j=n"       >> $cfgfile
  201.          echo -e "jack_sink=n" >> $cfgfile
  202.    fi
  203.    echo -e "=> Config file is: $cfgfile"  
  204.       function set_config ()
  205.         {
  206.            sed -i "s/^\($1\s*=\s*\).*\$/\1$2/" $cfgfile
  207.            source $cfgfile
  208.         }
  209.    set_config
  210.    echo -e "\nType in the number of your sound card/audio device:"
  211.       read new_sndcrd
  212.       set_config sndcrd $new_sndcrd
  213.    echo -e "\nWant to make Alsa's Midi also visible as Jack Midi? [y/n]"
  214.       read new_a2j
  215.       set_config a2j $new_a2j
  216.    echo -e "\nWant to sink Jack to PulseAudio? [y/n]"
  217.       read new_jack_sink
  218.       set_config jack_sink $new_jack_sink
  219.    echo -e "Your config file now looks like this: \n"
  220.    cat $cfgfile
  221. ES;fi;;
  222.  
  223. # Delete config file:
  224. 9) S=M9;SC;if [[ $cur == "" ]]; then R
  225.    echo -e "Delete $cfgfile? (y/n)"
  226.    read choice01
  227.    if [[ $choice01 == "y" ]]
  228.       then
  229.          rm $cfgfile
  230.       else
  231.          echo "You chose n or typed in the wrong letter. Not deleting $cfgfile."
  232.    fi
  233. ES;fi;;
  234.  
  235. # Show config file:
  236. 10) S=M10;SC;if [[ $cur == "" ]]; then R
  237.     echo -e "Your config file is: $cfgfile, and its contents is:\n"
  238.     cat $cfgfile
  239. ES;fi;;
  240.  
  241.  
  242.  
  243. ########
  244. # HELP #
  245. ########
  246. 11) S=M11;SC;if [[ $cur == "" ]]; then R
  247.     echo -e  "\e[32;1m\e(0lqqqqqqqqqqqqk "
  248.     echo -e          "x\e(B    Help    \e(0x"
  249.     echo -e  "\e[32;1m\e(0mqqqqqqqqqqqqj  \e[0m\e(B"
  250.     echo -e  "\
  251. 1. You can only get audio in Firefox AFTER starting Pulseaudio, not before!
  252.  
  253. 2. Store/restore alsamixer settings for card 2:
  254.  
  255.   sudo alsactl (re)store 2
  256.  
  257. Beware! Store alsa settings *before* switching from input to output (F3 and F4 etc.). Else: no store.
  258.  
  259. 3. Jack VU meter:
  260.  
  261.   (jmeters -t vu -f mbrl \"Left channel\" \"Right channel\" &); sleep 0.1;\
  262.   jack_connect system:capture_1 jmeters:in-1; jack_connect system:capture_2 jmeters:in-2
  263.  
  264. Note: the jmeters backround process is killed after closing its window.
  265.  
  266. 4. Stop PA from respwawning. Uncomment the following 2 lines from /etc/pulse/client.conf:
  267.  
  268.   autospawn = no
  269.   daemon-binary = /bin/true
  270.  
  271. For newer Linux systems (i.e. Debian 10):
  272.  
  273.   systemctl --user mask pulseaudio.socket
  274.   systemctl --user stop pulseaudio
  275.  
  276. 5. Some handy PulseAudio commands:
  277.  
  278.   pactl info
  279.   pacmd list-sinks
  280. "
  281. ES;fi;;
  282.  
  283. 12) S=M12;SC;if [[ $cur == "" ]]; then R
  284.     clear
  285.     exit 0
  286. fi;;
  287.  
  288.  
  289. esac; POS; done; R
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement