Advertisement
Meneer_Jansen

Menuaudio.sh

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