Topic: Shuffled music as alarm -- one solution

So, one of the main reasons I got a chumby is that I start to sleep through ANY sound that plays every morning.  I wanted an internet radio with good songs but an unpredictable pattern.  Pandora worked fine for this, but eventually will not be a solution.

Squeezebox server is a potential solution, but if my home network goes down for some reason ... like insomnified typo-ing leading to wiping out a good part of my operating system, oops ... it won't.

Having all of the music on the chumby, then, and playing it as the alarm, this seems like the safest method.  You can wake up to a "stream" which is a playlist of all your music.  However, it does not shuffle.  It'll just keep playing the same songs each time.

So here's how I dealt with that.  I'll try to make a second post with a script that should do ALL of this for you, but here's the breakdown.  Hey, I'm assuming that you have your music on a flash drive stuck into your chumby, in a directory called "Music".  And that you have your music either directly in directories under Music (like "Music/Artist/mp3s") or in album directories (like "Music/Artist/Album/mp3s").  If you've got something significantly different, and don't already know how to alter the following to make it work, feel free to ask me how to change it.

~ ~ ~

SSH into chumby,  You need your chumby's IP, which you can get from the "device information" screen.  You'll also need to touch the little pi in the top right corner and, on the hidden screen that opens, press the "SSHD" button.  From command line or in putty,

 ssh root@[your_chumbys_ip] 

Once in, make a list of all the music on the flash drive (some is directly in artist directories, some is in artist/album):

ls /mnt/usb/Music/*/*.mp3 /mnt/usb/Music/*/*/*.mp3 > /mnt/usb/Music/AllSongs.m3u

So that's a playlist of all the mp3's, complete with directory.  I went to the control panel, "Music," and "My Streams," and created a stream that read:

file:////mnt/usb/Music/AllSongs.m3u

and played it, to test that it works.

Okay, so now you have a stream that you can set as your alarm wakeup.

But it'll play the same songs over and over.  You'll need scripts to help you deal with this.  Create a directory to put them in:

mkdir /mnt/usb/bin

And tell the chumby to look there for scripts.  Edit your /psp/.profile file to add a line.  First, make a backup:

cp /psp/.profile /psp/.profile.bak

Then edit the file ".profile".  I'll tell you what to put in it in a minute.  First, here's how you can open it to edit it:
* you can edit it directly on the chumby if you have nano or are comfortable with vi, or

 nano /psp/.profile 

* you can copy it to the chumby's flash drive, edit it on your desktop and then put back into the chumby, or

cp /psp/.profile /mnt/usb/bin/
[remove the drive, take it to your computer, plug it in, edit, put it back in the chumby]
mv /mnt/usb/bin/.profile /psp/

* you can scp it from the chumby it on your desktop and scp it back once it's complete.

scp root@[your_chumbys_ip]:/psp/.profile .
[edit .profile]
scp .profile root@[your_chumbys_ip]:/psp/

Here's what you add to .profile at the end; this adds your new directory to the working path that already existed:

export PATH=$PATH:/mnt/usb/bin

Now, at the command line, type this to go ahead and make your chumby start looking for executable files in "/mnt/usb/bin/" right away:

cd /psp/
. .profile
cd

So I have a script that runs on a cron job that shuffles the file.  (Fair confession: got help from spouse the Perl God.)  You'll make a file called "random_reorder" with the following code.  Create the file in whatever way is easiest for you:
* you can use any of the methods above: edit on the chumby ("nano /mnt/usb/bin/random_reorder"), put it on the chumby's flash drive directly )from your desktop, edit the file and stick it in "bin" in the flash drive), or scp it over (scp random_reorder root@[your_chumbys_ip]:/mnt/usb/bin/").
* Or if you'd find it easiest, you can get a copy from my server.  I'll tell you how to do that at the end.

#!/usr/bin/perl -w

# @(#) randomize Effectively _unsort_ a text file into random order.
# 96.02.26 / drl.
# Based on Programming Perl, p 245, "Selecting random element ..."

# Set the random seed, PP, p 188
srand(time|$$);

if (! scalar(@ARGV) ) {
    die "\nUsage: random_reorder <filename> [filename] ... [filename]\n";
}

foreach my $file (@ARGV) {
    reorder_file($file);
}

sub reorder_file {
    my $filename = shift;
    
    if ( ! -f $filename ) { 
        warn "File $filename does not exist.\n";
        return;            
    }                      
                           
    open(INFILE,$filename);
    my @lines = <INFILE>;  
    close (INFILE);        
                           
    #backup input file     
    print `/bin/cp $filename $filename.bak`;
                           
                           
    open(OUTFILE,">$filename"); 
    while ( @lines ) {     
        my $choice = splice(@lines,rand @lines, 1);
        print OUTFILE $choice;
    }                      
    close OUTFILE;         
}   
 

If you don't feel up to creating this file yourself,

cd /mnt/usb/bin/
wget http://www.lady-of-lothlorien.com/chumby/bin/random_reorder
cd

So, once you have created random_reorder, you'll want to make it executable, then tell your chumby to use it regularly.  First, at the command line, type:

 chmod u+x /mnt/usb/bin/random_reorder 

Then you'll work on your cron jobs, which means you have to temporarily mount the filesystem writeable.  Normally, you can write to files in /psp/, but not to files in /etc/. 

mount -o remount,rw /

Now you'll want to edit the cron file, which is what tells the chumby to do things on a regular basis.  If you have nano or are comfortable with vi, you can do it directly on your machine.

 nano /etc/cron/crontabs/root 

In the cron file, add this line:

 15 * * * * random_reorder /mnt/usb/Music/AllSongs.m3u ; echo `date` > /mnt/usb/Music/shuffled.last 

That will shuffle the lines in the playlist 15 minutes after every hour, and it will note the time it last shuffled the files in a file called "shuffled_last" in your Music directory.

Now tidy up and finish this off:

sync
mount -o remount,ro /

Re: Shuffled music as alarm -- one solution

Okay.  If you don't feel comfortable with doing all this manually, this SHOULD work:

SSH into chumby,  You need your chumby's IP, which you can get from the "device information" screen.  You'll also need to touch the little pi in the top right corner and, on the hidden screen that opens, press the "SSHD" button.  From command line or in putty,

 ssh root@[your_chumbys_ip] 

Now you'll get the script and run it:

cd /mnt/usb/
wget http://www.lady-of-lothlorien.com/chumby/bin/alarm_song_shuffle
./alarm_song_shuffle

You'll still need to go into your control panel, and create this stream:

file:////mnt/usb/Music/AllSongs.m3u

And then go into alarms, and set it as your wakeup sound.

but all the rest should be done for you.

Re: Shuffled music as alarm -- one solution

Another little script people might like is here:
http://lady-of-lothlorien.com/chumby/bin/play_this

I wrote it this morning.  I wanted to be able to quickly select and play a song based on a keyword.  This script lists all the songs I have with that keyword in the directory or title, and then lets me select the one I want. 

All you need to be able to use it are the commands from the above posts: the one that that puts a list of all your mp3's in a file "/mnt/usb/Music/AllSongs.m3u", and the one that adds "/mnt/usb/bin/" to your path (these are already done if you used the script, or if you followed the instructions in the first post); then:

cd /mnt/usb/bin
wget http://lady-of-lothlorien.com/chumby/bin/play_this
chmod u+x play_this

An example I just used, looking for Garbage's "Cherry Lips":

Rohan 11:07am ~# play_this Cherr
1) /mnt/usb/Music/PunkyFunky80sey/Wild Cherry - Play That Funky Music.mp3 n
2) /mnt/usb/Music/Rock/Wild Cherry - Play That Funky Music.mp3 n
3) /mnt/usb/Music/Swing/Cherry Poppin' Daddies - Zoot Suit Riot.mp3 n
4) /mnt/usb/Music/Garbage/Absolute Garbage/Garbage - Cherry Lips (Go Baby Go).mp3 n
5) /mnt/usb/Music/Warrant/Cherry Pie/Warrant - Cherry Pie.mp3 n
Which Song?
4
btplay client v1.4.1.38.48
Connected to btplayd instance 1772
Got response: OK 100 reset
Matched request 100
Got response: OK 101 playnow 0
Matched request 101
playnow * /mnt/usb/Music/Garbage/Absolute Garbage/Garbage - Cherry Lips (Go Baby Go).mp3

aaaand you can see it played "Cherry Lips" for me.

This will mostly be useful when I have a specific song stuck in my head or want to play something randomly in reference to a conversation Spouse and I are having.  It will be a lot faster than digging around in directories trying to remmeber where I put it--even faster than scrolling/searching in Rhythmbox on my computer to find the specific song. 

Well, at least, it'll be faster if you're like me and have an ssh terminal always open to the chumby. wink But who wouldn't?

Re: Shuffled music as alarm -- one solution

Thanks. Finally got around to implementing this, although I had problems with the chron entry - eventually I just put a full path for <random_reorder> and it started working!