Advertisement
Meneer_Jansen

Menuaudio.sh

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