]> git.draconx.ca Git - mpdhacks.git/commitdiff
mpdreload: Simplify script operation.
authorNick Bowler <nbowler@draconx.ca>
Sat, 23 May 2020 20:33:30 +0000 (16:33 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sat, 23 May 2020 20:33:30 +0000 (16:33 -0400)
This script doesn't need any metadata other than filenames and queue
IDs, so we don't need to store anything else in its data structures.

We can also ask MPD not to send us most unneeded information which
speeds things up a lot.

MPDHacks.pm
mpdreload.pl

index 0f5026eb237e7d9f60f3861cbbbc9d0cc6e268b9..7da3b06b7e5258a0413b0b7b17f518e20a1175f7 100644 (file)
@@ -115,4 +115,14 @@ sub exec {
        print $sock "$cmd\n";
 }
 
+# Submit a command to the MPD server and wait for it to complete.
+# Intended for simple cases where the command output is unneeded.
+sub run {
+       MPD::exec(@_);
+       while (<$sock>) {
+               last if (/^OK/);
+               die($_) if (/^ACK/);
+       }
+}
+
 1;
index c852a2bd4b85e693ab9eb396d0451816d7193a85..c82353eb3e91f435a7bc0103b00e2e5fa3de6691 100755 (executable)
@@ -26,8 +26,8 @@ use MPDHacks;
 
 my $sock;
 
-# Returns a hash reference containing all tracks in the current play queue.
-# The hash keys are filenames.
+# Returns a hash reference mapping filenames to an array reference listing
+# the queue IDs for that file in the current play queue.
 sub get_tracks_in_play_queue {
        my %matches;
        my $entry;
@@ -39,18 +39,9 @@ sub get_tracks_in_play_queue {
 
                if (/^(\w+): (.*)$/) {
                        if ($1 eq "file") {
-                               if (exists($matches{$2})) {
-                                       $entry = $matches{$2};
-                               } else {
-                                       $entry = {};
-                                       $matches{$2} = $entry;
-                               }
-                       }
-
-                       if (exists($entry->{$1})) {
-                               $entry->{$1}->{$2} = 1;
-                       } else {
-                               $entry->{$1} = { $2 => 1 }
+                               $entry = $matches{$2} //= [];
+                       } elsif ($1 eq "Id") {
+                               push @$entry, $2;
                        }
                }
        }
@@ -129,19 +120,14 @@ if (@ARGV != 1) {
 $sock = MPD::connect();
 
 # Retrieve the current play queue and target play queue.
+MPD::run("tagtypes", "clear");
 my $current = get_tracks_in_play_queue();
 my $target = get_playlist_files($ARGV[0]);
 
 MPD::exec("command_list_begin");
 for (my $i = 0; $i < @$target; $i++) {
        my $f = $target->[$i];
-       my $ids = $current->{$f}->{Id};
-
-       my $id = (keys %$ids)[0];
-       delete $ids->{$id};
-
-       # Remove tracks with no unused queue IDs
-       delete $current->{$f} unless (keys %$ids > 0);
+       my $id = shift @{ $current->{$f} };
 
        if (defined $id) {
                MPD::exec("moveid", $id, $i);
@@ -151,15 +137,5 @@ for (my $i = 0; $i < @$target; $i++) {
 }
 
 # Remove any tracks left from the old play queue.
-foreach (keys %$current) {
-       my $ids = $current->{$_}->{Id};
-       foreach (keys %$ids) {
-               MPD::exec("deleteid", $_);
-       }
-}
-
-MPD::exec("command_list_end");
-while (<$sock>) {
-       last if /^OK$/;
-       die($_) if /^ACK/;
-}
+MPD::exec("deleteid", $_) foreach (map { @$_ } values %$current);
+MPD::run("command_list_end");