Advertisement
copper

adevices

Oct 26th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ "$1" = '-h' -o "$1" = '--help' ]; then
  4.     me="${0##*/}"
  5.     echo "$me: list ALSA devices and relevant information."
  6.     exit 0
  7. fi
  8.  
  9. for (( i=0; i<10; i++ )); do
  10.     if [ -d "/proc/asound/card${i}" ]; then
  11.         cname="$( cat "/proc/asound/card${i}/id" )"
  12.         echo "Card ${i} (${cname}):"
  13.  
  14.         for t in 'p' 'c'; do
  15.             case "$t" in
  16.                 p) typeString='Playback' ;;
  17.                 c) typeString='Recording' ;;
  18.             esac
  19.  
  20.             for (( j=0; j<10; j++ )); do
  21.                 if [ -d "/proc/asound/card${i}/pcm${j}${t}" ]; then
  22.                     dname=''
  23.                     if [ -e "/proc/asound/card${i}/pcm${j}${t}/info" ]; then
  24.                         dname="$( grep -E '^name: ' "/proc/asound/card${i}/pcm${j}${t}/info" 2>/dev/null | cut -d ' ' -f 2- )"
  25.                     fi
  26.                     echo
  27.                     if [ -n "$dname" ]; then
  28.                         echo "  * ${typeString} Device ${j} (${dname}):"
  29.                     else
  30.                         echo "  * ${typeString} Device ${j}:"
  31.                     fi
  32.                     ownerPID="$( grep -E '^owner_pid' "/proc/asound/card${i}/pcm${j}${t}/sub0/status" 2>/dev/null | tr -cd '0-9' )"
  33.                     if [ -n "$ownerPID" ]; then
  34.                         ownerName=''
  35.                         if [ -e "/proc/${ownerPID}/comm" ]; then
  36.                             ownerName="$( cat "/proc/${ownerPID}/comm" 2>/dev/null )"
  37.                         fi
  38.                         if [ -n "$ownerName" ]; then
  39.                             echo "    used by: ${ownerName} (PID ${ownerPID})"
  40.                         else
  41.                             echo "    used by: some program (PID ${ownerPID})"
  42.                         fi
  43.                     fi
  44.                     while read line; do
  45.                         echo "    $line"
  46.                     done < "/proc/asound/card${i}/pcm${j}${t}/sub0/hw_params"
  47.                 fi
  48.             done
  49.         done
  50.         echo
  51.     fi
  52. done
  53.  
  54. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement