Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awwaiid/c56db01c9deb24d1a8ed to your computer and use it in GitHub Desktop.
Save awwaiid/c56db01c9deb24d1a8ed to your computer and use it in GitHub Desktop.
polly6 -- a cli music wrapper
#!/usr/bin/env perl6
use v6;
use File::Find;
# This is the mapping we use to play a file
my @play_commands =
/\.ogg$/ => 'ogg123',
/\.mp3$/ => 'mpg123',
/\.m4a$/ => 'mplayer',
/^http:\/\// => 'vlc',
/\.(mod|xm|s3m|it)$/ => 'vlc -I curses',
/.*/ => 'vlc', # fallback
;
# subset File of Str where *.IO.e; # .IO returns an IO::Path object, and .e is for "exists"
sub expand_songs($verbose, @songs) {
my @expanded_songs = [];
for @songs -> $song {
if $song.IO ~~ :d {
my @dirsongs;
say "Adding all files in $song" if $verbose;
push @expanded_songs, find(:dir<<$song>>, :type<file>).sort;
} else {
say "Adding $song" if $verbose;
push @expanded_songs, $song;
}
}
return @expanded_songs;
}
sub osd_song($song) {
my $saysong = "$song";
$saysong ~~ s/^.*\///; # Strip out directory
$saysong ~~ s/\..*?$//; # remove extension
$saysong ~~ s/ \s \- \s/,\n/;
say "notify-send '$saysong'";
# shell("notify-send '$saysong'");
}
sub announce_song($song) {
my $saysong = "$song";
$saysong ~~ s/.*\///; # Strip out directory
$saysong ~~ s/\s\-\s\d\d\d?\s\-\s/ - /; # Strip out track number
$saysong ~~ s/\....$//; # remove 3-letter extension
$saysong ~~ s:g/_+/ /;
# Turn separators into a pause (two newlines will work)
$saysong ~~ s:g/\s \- \s /\n\n/;
$saysong ~~ s:g/\&/ and /;
$saysong ~~ s:g/\'//;
say "say $saysong";
# shell("say '$saysong'");
}
sub playsong($song) {
my $cmd = @play_commands.first({$song.match(.key)}).value;
say "$cmd '$song'";
# shell("$cmd '$song'");
}
# sub USAGE {
# say q:to[END];
# USAGE: polly6 [parameters] file1 dir1 ...
# -a: announce the current song (festival)
# -h: show this
# -o: On Screen Display of song title (osdctl)
# -s: force sort (dirs are already sorted)
# -v: be verbose
# -z: shuffle the file list
# END
# }
sub MAIN(
*@songs,
Bool :a(:$announce) = False,
Bool :o(:$osd) = False,
Bool :s(:$sort) = False,
Bool :v(:$verbose) = False,
Bool :z(:$shuffle) = False,
) {
@songs ||= <.>;
@songs = expand_songs($verbose, @songs);
@songs.shift; # WHY do I have to do this?!
say "Playing {@songs.elems} songs";
# say "songs: " ~ @songs.join("\n");
@songs .= pick(*) if $shuffle;
@songs .= sort if $sort;
for @songs -> $song {
announce_song($song) if $announce;
osd_song($song) if $osd;
playsong($song);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment