Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.50 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. #Notification delay in ms
  5. my $delay = 3000;
  6.  
  7. #Hash of card names that should not be used for output (only the key matter, any value will do)
  8. my %vetoCardNames = ('HDA NVidia'=>1, 'AT2020USB+'=>1, 'Scarlett Solo USB'=>1, 'MG-XU'=>1);
  9.  
  10. my $notifyCmd = "notify-send -t $delay";  
  11.  
  12. my $cardNumber = -1;
  13. my $cardShortName = "";
  14. my $cardLongName = "";
  15. my $activeCard = -1;
  16.  
  17. my %cards = ();
  18.  
  19. sub logError{
  20.     my $title = $_[0];
  21.     my $error = $_[1];
  22.     `$notifyCmd "$title" "$error"`;
  23.     die("$title: $error\n");   
  24. }
  25.  
  26. sub showActiveCardPopup{
  27.     my $message = "";
  28.     foreach my $card (sort(keys(%cards))){
  29.     my $name =$cards{$card};
  30.    
  31.     if ($card == $activeCard){
  32.         $message .= "<b>$name</b>\n";
  33.         print "Switched to: $name\n";
  34.     }
  35.     else {
  36.         $message .= "$name\n";
  37.     }
  38.     }
  39.     if ($message eq ""){
  40.     $message = "No soundcards";
  41.     }
  42.     `$notifyCmd "Active Soundcard Changed" "$message"`;
  43. }
  44.  
  45. sub processData {
  46.     if (defined($vetoCardNames{$cardShortName})){ return; }
  47.     $cards{$cardNumber} = "$cardShortName";
  48. }
  49.  
  50. my @cardData = split(/\n/, `pactl list sinks`);
  51. foreach my $line (@cardData){
  52.     if ($line =~ /^Sink \#(\d+)/){
  53.     my $number = $1;
  54.     if ($cardNumber != -1){
  55.         processData();
  56.     }
  57.     $cardNumber = $number;
  58.     $cardShortName = "";
  59.     $cardLongName = "";
  60.     }
  61.     elsif ($line =~ /alsa.card_name\s*=\s*"(.*)"/){
  62.     $cardShortName = $1;
  63.     }
  64.     elsif ($line =~ /Description:\s*([^\n]+)/){
  65.     $cardLongName = $1;
  66.     }
  67.     elsif ($line =~ /State:\s+RUNNING/){
  68.     $activeCard = int($cardNumber);
  69.     }
  70. }
  71. if ($cardNumber != -1){
  72.     processData();
  73. }
  74.  
  75. if (scalar(keys(%cards))==0){
  76.     logError("No Soundcards Found", "Unable to find any soundcards!\n\nYou may have added them all to \%vetoCardNames");
  77. }
  78.  
  79.  
  80. if ($activeCard == -1){
  81.      logError("Active Soundcard Not Found", "Unable to change active card as it could not be found.\n\n<b>Perhaps no applications are currently playing?</b>");
  82. }
  83.  
  84. #Find new active card
  85. my @cardList = sort(keys(%cards));
  86. my $newCard = 0;
  87. for (my $i = 0; $i<scalar(@cardList)-1; $i++) {
  88.     if ($activeCard==$cardList[$i]){
  89.     $newCard=$i+1;
  90.     }
  91. }
  92. $newCard = int($cardList[$newCard]);
  93.  
  94. #Switch to new active card
  95. my @appsData = split(/\n/, `pactl list short sink-inputs`);
  96. foreach my $appLine (@appsData){
  97.     if ($appLine =~ /^(\d+)/){
  98.     my $id = int($1);
  99.     `pactl move-sink-input $id $newCard`;
  100.     }
  101. }
  102. `pactl set-default-sink $newCard`;
  103. $activeCard = $newCard;
  104.  
  105. showActiveCardPopup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement