From owner-freebsd-questions@FreeBSD.ORG Tue Jan 4 04:34:16 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4395A106564A for ; Tue, 4 Jan 2011 04:34:16 +0000 (UTC) (envelope-from joji@eskimo.com) Received: from ultra7.eskimo.com (ultra7.eskimo.com [204.122.16.70]) by mx1.freebsd.org (Postfix) with ESMTP id 21C308FC13 for ; Tue, 4 Jan 2011 04:34:15 +0000 (UTC) Received: from shell.eskimo.com (root@shell.eskimo.com [204.122.16.72]) by ultra7.eskimo.com (8.14.0/8.14.3) with ESMTP id p044Nci5029349; Mon, 3 Jan 2011 20:23:38 -0800 Received: from shell.eskimo.com (joji@localhost [127.0.0.1]) by shell.eskimo.com (8.14.3/8.14.3) with ESMTP id p044NeRv008439; Mon, 3 Jan 2011 20:23:40 -0800 Received: (from joji@localhost) by shell.eskimo.com (8.14.3/8.12.10/Submit) id p044NdL6008404; Mon, 3 Jan 2011 20:23:39 -0800 Date: Mon, 3 Jan 2011 20:23:38 -0800 From: Joseph Olatt To: Frank Shute Message-ID: <20110104042338.GA29930@shell.eskimo.com> References: <20101226170930.GA68817@orange.esperance-linux.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20101226170930.GA68817@orange.esperance-linux.co.uk> User-Agent: Mutt/1.4.2.2i Cc: FreeBSD Questions Subject: Re: randomising tracks: scripting question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Jan 2011 04:34:16 -0000 On Sun, Dec 26, 2010 at 05:09:30PM +0000, Frank Shute wrote: > > I generally play my tracks of an album like so: > > for track in $(cat trombone_shorty-backatown.m3u); do > mplayer $track > done > > They then play in the correct order. > > How would I go about randomising the order of play using > sh (preferably) or perl? > > Sorry for the OT posting but I thought a brainteaser might clear the > fog caused by excessive Xmas indulgence ;) > > > Regards, > > -- > > Frank > > Contact info: http://www.shute.org.uk/misc/contact.html > > A little while back I wrote a perl script to randomly pick mp3, ogg, flac files from any directory specified as arg 1 and play them using mplayer. I categorize my genres by directory and with this perl script I can randomly play songs from any directory. If the script is invoked without any arguments, then it will play songs from the default hard-coded directory defined by $SONG_DIR. Don't know if this would be useful to you (or someone else). #!/usr/bin/perl -w use strict; my $SONG_DIR = "/home/joji/songs/good"; if ($#ARGV == 0) { $SONG_DIR = $ARGV[0]; } my %played = (); my $rand; my $dh; my @song_list; opendir($dh, $SONG_DIR); @song_list = readdir($dh); closedir($dh); my $count = $#song_list; # Perl counts from zero. If there is one item, Perl will say 0. # So to get the real count, we have to increment by 1. $count++; chdir($SONG_DIR); while ((keys %played) < $count) { while (1) { $rand = int(rand($count)); if (! $played{$rand}) { $played{$rand} = 1; last; } if ((keys %played) >= $count) { last; } } if ($song_list[$rand] eq "." || $song_list[$rand] eq "..") { ; } else { print "Playing song # " . $rand . " [" . $song_list[$rand] . "]\n"; `mplayer \"$song_list[$rand]\"`; } } exit(0);