Advertisement
Meneer_Jansen

midishcreaterc.sh

Jul 6th, 2025 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.70 KB | Music | 0 0
  1. #!/bin/bash
  2.  
  3. # Script to ad a Midi device to the RC file of midish. Download from:
  4. #    https://pastebin.com/edit/Q1xS8Ep8
  5. # Variables:
  6. #    $homedir        Home directory.
  7. #    $rcfile         RC file for midish.
  8. #    $port           Port Number of Midi device to be added.
  9. #    $clientname     Client Name of Midi device to be added.
  10. #    $maxdevice      Highest dnew number.
  11. #    $linenum        Line number with $maxdevice.
  12. #    $lineinput      Line after which to insert Midi input device.
  13. #    $lineoutput     Line after which to insert Midi output device.
  14. # Arrays:
  15. #    dnewarray[]     Array with dnew numbers.
  16. # Files:
  17. #    tempaseqdump    Tempfile with aseqdump output.
  18. # To do:
  19. #    - make the script 48 cols wide for use on the Pi screen
  20.  
  21.  
  22. homedir=/home/$USER
  23. rcfile=$homedir/.midishrc
  24. # Quit this script, remove it from backgroud and return to main menu
  25. trap "killall $(basename $0); midishscript.sh" EXIT
  26.  
  27.  
  28. # List currenty connected Midi devices and choose one.
  29. printf "Choose client number of Midi device to add to midish's RC file:\n\n"
  30. # Use sed to print only first 40 characters
  31. aseqdump -l | sed 's/.//40g'
  32. aseqdump -l > tempaseqdump
  33. echo
  34.  
  35. read -p "Choice: " port
  36.  
  37.  
  38. # Determine Client Name
  39. # ---------------------
  40. # Select line containing $port
  41. clientname=$(awk '/'$port'/' tempaseqdump)
  42. printf "$clientname" > tempaseqdump
  43. #printf "    Debug: client name is: $clientname.\n"
  44. # Use awk to select column 2 (= Client Name) and sed to remove trailing spaces.
  45. # Define field widths for awk as 9, 33 and 41:
  46. # see: https://stackoverflow.com/questions/26842477/awk-to-use-multiple-spaces-as-delimiter
  47. # Use sed to remove trailing spaces.
  48. # If the client name is in there twice (e.g. Korg Monologue) because the client has two ports - one for input and one for output - then remove 2nd line and the carriage return. Use: sed '2d'.
  49. clientname=$(awk -v FIELDWIDTHS="9 33 41" '{print $2}' tempaseqdump | sed 's/[ ]*$//' | sed '2d')
  50.  
  51.  
  52.  
  53. # Determine max device number
  54. # ---------------------------
  55. # Match line that (starts) with "dnew"; print column/field 2
  56. dnewarray=( $(awk '{if ($1 == "dnew") print $2}' $rcfile) )
  57. maxdevice=0
  58. for x in ${dnewarray[@]}; do
  59.    if (( $x > $maxdevice ))
  60.       then maxdevice=$x
  61.    fi
  62. done
  63.  
  64.  
  65.  
  66. # Insert the correct $clientname line into midishrc after $maxdevice number.
  67. # Use awk to determine number of line/record starting with "dnew $maxdevice".
  68. # Use sed to insert a new line after the one starting with "dnew $maxdevice".
  69. linenum=$(awk '/^dnew '$maxdevice'/ {print NR}' $rcfile)
  70. sed -i "$((linenum + 1))i dnew $((maxdevice + 1)) \"${clientname}\"  rw" $rcfile
  71.  
  72.  
  73. # Add $clientname as i or o to midishrc
  74. # -------------------------------------
  75. # Replace spaces by undersores in client name:
  76. clientname="${clientname// /_}"
  77. # Look for the remark line starting with "# Inputs", if not create it. And determine its line number. Finally insert stuff.
  78. lineinput=$(awk '/^# Inputs/ {print NR}' $rcfile)
  79. if [[ -z $lineinput ]]
  80.    then
  81.    sed -i "$((linenum + 3))i # Inputs" $rcfile
  82. fi
  83. sed -i "$((lineinput + 1))i inew $clientname {$((maxdevice + 1)) 0}" $rcfile
  84. # Do the same for output
  85. lineoutput=$(awk '/^# Outputs/ {print NR}' $rcfile)
  86. if [[ -z $lineoutput ]]
  87.    then
  88.    sed -i "$((linenum + 6))i # Outputs" $rcfile
  89. fi
  90. sed -i "$((lineoutput + 1))i onew $clientname {$((maxdevice + 1)) 0}" $rcfile
  91.  
  92.  
  93.  
  94. # Cleanup & exit
  95. rm tempaseqdump
  96. clear
  97. printf "Done. Your RC file is:
  98. \e[34m%s\e[0m
  99. I created for the Midi client named:
  100. \e[34m%s\e[0m
  101. a 'dnew' device number:
  102. \e[34m%d\e[0m
  103. which is now defined as Midi input and output on Midi channel 0. You can check this in midish with the command: \"ilist; olist\".\n" $rcfile $clientname $((maxdevice +1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement