Archive for the ‘ music ’ Category

## Raspberry FM Part 2, the sequal

Nearly a year ago I wrapped up a project I called “Raspberry FM“:  A Raspberry Pi based internet radio streamer coupled with a MaKey MaKey as the interface.   Why the name Raspberry FM?  It’s a mashup of a Raspberry Pi, and my favorite internet radio station, Soma FM.   It worked, but had a few problems:

• There was no visual feedback, the MaKey MaKey was purely an input.
• I was having a hard time getting Python to interface with with MPlayer (probably due to my own ignorance), the audio player I had chosen, so everything was done via bash.

Fast forward nearly a year:  After learning about the Mpd audio player/server, and (one of) its clients Mpc, it re-piqued my interest in programming a Python app to play music on the Pi.  Second time around, it all turned out really well:  New software combined with new hardware and a 3d-printed case turned it into a good looking compact unit.

Overview of the whole process below (pic on top, video on bottom).  During the development I decided to write my own Python music player around mpc & Adafruit’s LCD library.  At this point there are several others online, but I enjoyed coding it from scratch.

Raspberry FM Features:

• Auto-on with the Pi
• If no internet is present or it drops out, app will go into holding pattern until it returns.
• Can change between any number of stations. (left\right buttons).
• Stations are easy to add via the commandline:  No need to update Python source: SSH into the Pi, and add\remove what you need.
• Increase\decrease volume (up\down buttons).
• Station and song info is displayed and auto-scrolls.
• Shutdown Pi  (select+left) or turn off program (for debugging, select+right)
• Lots of color changing when buttons are pressed!

Hardware needed:

• Raspberry Pi (I used a B model)
• Optional:  Custom 3D printed case I designed (well, I designed the top part), download on Thingiverse.  Print & install!  Took me about an 1:20 on my Makerbot Replicator (1).
• I stream the internet radio over cat5, but I’ve also had success with wifi.
• I use the headphone jack for audio out.

Software needed:

• This was all done on the Raspbian distro via NOOBS.
• My “Raspberry FM” Python program.  Find on Bitbucket.
• You’ll need Adafruits CharLCDPlate library.
• FYI, I coded this all via the Adafruit WebIDE, I’d recommend anyone else to use it as well to help manage the various Python modules on the Pi.
• MPD & MPC:  sudo apt-get install mpc mpd

Steps:

• I presume you already have your Pi setup.  If not, see my notes here on the general steps needed to get a Pi live and kicking.
• Setup Pi to auto-login.  See notes here.
• Download the Raspberry FM Python program to a folder of your choosing.  Since I coded this via the WebIDE, both the creation of my code and the integration of the Adafruit LCD modules was all handled via the WebIDE.  Make sure you download all the Adafruit CharLCDPlate modules as well and put them in the same directory.
• Install MPD & MPC.
• Add stations to MPC.  This is super easy on the commandline.  May I recommend anything from SomaFM?
mpc add http://ice.somafm.com/groovesalad
• Setup Pi to auto-run a program on start.  See notes here.  You will point that script to wherever you saved the Raspberry FM Python script.  For example, my startup.sh script looks like:
• #!/bin/bash
echo startup.sh : Launching raspberryFm.py
sudo python /usr/share/adafruit/webide/repositories/my-pi-projects/Adafruit_CharLCDPlate/raspberryFm01.py
• Restart the Pi and listen to the music!

The final result in action:

## Raspberry FM : Project finale

This weekend I finally knocked out the final hardware config for me to consider the “Raspberry FM” project complete.  What is this project exactly?

• It’s an experiment I gave myself to combine a Raspberry Pi, Makey Makey, MicroRAX, and MPlayer to create an auto-on internet radio player.
• I called it ‘Raspberry FM’ after a combination of the Raspberry Pi, and Soma FM, my favorite internet radio station.

Pic & vid of the final result (more pics at the bottom) :

The YouTube video shows off changing the volume & station.

### Final Step: Building the enclosure, putting it all together…

There are a lot of Raspberry Pi enclosures over on Thingiverse ready for printing on my Makerbot Replicator.  But I needed something to also support the Makey Makey.  I knew I could modify one of those files, but I was interested in trying a different media, my MicroRAX kit.  I’d done some mockups with it earlier (bookholder, quad-copter frame), but nothing permanent, and this seemed like the perfect use case.  And it turned out great… other than the fact I actually had to have the Raspberry Pi in another enclosure, so that it wouldn’t short-out with all the exposed metal.  Luckily I had an acrylic laser-cut case that came with my Maker Shed Raspberry Pi Starter Kit.  Had I not had that case, I probably would have printed out a minimal one.

Other things I learned is that the Makey Makey can be driven directly off the RPi’s USB port:  No external powerd USB hub needed.

The final hardware setup was thus:

• Makey Makey connected to Raspberry Pi over USB.
• Raspberry Pi powered via wall-wart.
• Raspberry Pi connected to internet via Ethernet.
• Raspberry Pi connected to stereo system over headphone-jack.
• Whole thing wrapped up via MicroRAX.
The current on\off functionality is just pluggin\unplugging it : After it boots up, the first station in the play list fires off, and you’re good to go.

### Going further…

If I was to extend this project, these are some things I’d think about:

• Switch out the tethered Ethernet for WiFi.
• Hook up a battery-pack and speakers:  Make it a portable boom-box.
• Splice in a toggle-switch for on\off.
• Have some sort of better integration with MPlayer & Python, or use some other music player entirely:  Have LED’s that represent each station in the playlist that light up showing which one your on, or even a simple LCD screen broadcasting the station name and volume level.
• Hook up awesome fruit to the MakeyMakey to make it a fruit activated radio turner.  That one should be pretty easy 😉
Overall it was a great learning experience for me to grasp the basics of linux, the Raspberry Pi hardware, and streaming internet radio.

## Raspberry Salad Part 3: Auto-run program on startup

Continuing from my previous post of turning my Raspberry Pi and Makey Makey into a ‘internet radio tuner’, the next step is to get the RPi to auto-login and start playing music when booted:  I want this to be a standalone piece of hardware that won’t require a monitor and keyboard every time I want to use it.

Coming from the embedded microcontroller world of Arduino, I’m used to powering the hardware on, and it running “the code” I’ve programmed for it.  The Raspberry Pi is its own little Linux computer:  When it turns on, by default it expects a user to log-in, via password.  Then, it expects the user to “do stuff” on the computer.  But since the RPi is so small, it’s perfect for embedded systems like the Arduino.  But once embedded, how do you have it auto-login and execute code of your choosing?  Like many others on the web I encountered this issue, and like many others, I’m blogging about how I figured it out.  This is all completely new territory for me, everything was done via searching the web and trying it out.

### Auto login to the Pi:

The first thing to solve is how to get the RPi to auto-login, thus removing the necessity of a connecetd keyboard & monitor (or ssh) to get things running.  This doc completely covers it: http://elinux.org/RPi_Debian_Auto_Login.  But to paraphrase, this is what you need to do:

Edit /etc/inittab , the  file that controls the startup \ initialization process of the Pi.  Do a “What is /etc/inittab” in Google for a far more robust answer on the specifics of what it does.

• To execute the script, you need to run it via bash:  $bash startup.sh • Use ‘echo’ to print strings in the script. ### How to auto-start a script on (auto) login: I found a pretty good overview from this post: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=29&t=7192 I’ve sort or reorganized the posts to describe the different ways to o it: /etc/rc.local : Add something to this file if you want it to execute every time the RPi is booted or rebooted. • Putting something into /etc/rc.local adds it to the boot sequence. The word “sequence” is important, if your code gets stuck then the boot sequence cannot proceed. • It is good for starting extra services, doing it this way will give you an output to the display [and is what I need] and also the ability to stop the script drop to a terminal and restart it with ease . • It is also true that when you put it into the /etc/rc* sequence, if it fails, it gums up your booting process (and this can be fatal). You have to be careful what you put there. Human frailty and all that… • Put stuff in /etc/rc.local if you want it to run on boot – it will run with root privileges (I think you could use sudo to stop that), and will run before login. • You can see it running right there in the virtual console – and this is critical if it is interactive. ~/.bashrc : Add something to this file if you want it to execute every time a specific user logs in. • The same with rc.local, if you put a faulty command in here you may not be able to login. /boot/boot.rc • Note that the RPi may have a file on the FAT partition named /boot/boot.rc which performs the same function as /etc/rc.local. This is safer because you can edit this partition from any computer. • In my case, I didn’t have this. Based on that info, I decided to use the ~/.bashrc method (shown below): In case I do something terrible, I could always login as another user (pi) and try to rectify the situation. ### Author the startup script To make this terribly obvious, I created a startup.sh script in my home dir, running the code I wanted to execute on login. This is a modified version of the mplayer plalylist launcher code from my previous post, passing in the absolute path to the playlist to launch: $ nano ~/startup.sh
#!/bin/bash
echo startup.sh : Launching mplayer: somafm.pls
mplayer -playlist ~/mplayer/somafm.pls

Need to make it executable:

$chmod ugo+x startup.sh Now that the startup script is created, edit ~/.bashrc to execute it: $ nano ~/.bashrc

Go to the last line and add:

# Run the startup bash script:
echo .bashrc : Running startup.sh
bash startup.sh

Note I don’t give it a specific path to startup.sh:  Based on the fact startup.sh is in the home dir, it seems to execute fine from that location.  The echo statements in the above scripts are a bit extraneous considering for the most part a monitor is no longer needed, but I figured they’d be worth adding for troubleshooting purposes.

### Listen to the music

At this point, reboot the Pi, plugin some headphones, and wait for the music to start streaming  If you’ve followed the instructions in the previous posts, you can use the Makey Makey to ‘change stations’ based on the authored playlist.

What’s next?  Packaging:  Need to come up with a slick way to package this, and provide a good tactile interface via the Makey Makey.

Go to previous: Raspberry Salad Part 2: Raspberry FM

Go to next post: Raspberry FM : Project finale

## Raspberry Salad Part 2: Raspberry FM

Raspberry FM Tuner: Makey Makey hooked up to Raspberry Pi ready to change stations

Based off my previous post (which explains how to instal MPayer), I wanted to find a way to create a Raspberry Pi project combining my Makey Makey:  I figured the Makey Makey would work well as a ‘station changer’ for streaming internet radio.  I tried, for quite a while, very unsuccessfully, to get this working via a Python program wrapping PyGame’s keyboard detection:  It would lock up after the first station change.  This was all done via Adafruit’s ‘Raspberry Pi WebIDE‘, connecting in to the Pi over SSH.  I learned one very important thing from authoring code that way:

When logged into the Pi over SSH, either from the WebIDE or a PC, the Pi won’t detect the Makey Makey.

I thought I had a defective Makey Makey, until I realized it worked fine when plugged into my Mac.  Programming the Pi via the Mac over SSH had been working so well, I’d pretty much forgotten about connecting it to my TV over HDMI and working with it directly (via a USB keyboard).  Once I did that, the Makey Makey immediately started working, both in the shell, and the gui.

Once I knew the Makey Makey was functional, these were the steps I went through to turn the whole thing into an “internet radio tuner”.

### Makey Makey Configuration:

• Black alligator clip on ground, yellow on “Left Arrow”, red on “Right Arrow”.  White alligator clip on the “Up Arrow”, gray alligator clip on the “Down Arrow” (not shown in the above image).
• Connected to the Raspberry Pi, over a powered USB hub.

### Raspberry Pi Configuration:

• Audio out connected to a stereo system (headphone jack, or in my case to tv over HDMI)
• Connected to internet (CAT5 \ WiFi)
• USB Keyboard hooked up (over powered USB hub) to issue commands below.  Remember, the Makey Makey won’t function when connected to over SSH.
• In my case, video connected over HDMI so I could hook it up to my TV, to issue commands.

### MPlayer Playlist Creation:

You can create playlist files for MPlayer, which can be made up of internet radio station streams.  This allows you to use the keyboard (or in our case, the Makey Makey) to advance and go back though them.  It’s a simple text file, with each line being a different item to play.  I decided to make a directory under home called /mplayer to store the playlist:

cd ~
mkdir mplayer
cd mplayer
nano somafm.pls

And inside that file (not sure why it’s showing up as double-spaced, it shouldn’t be), I added some of my favorite stations from SomaFM:

http://ice.somafm.com/groovesalad
http://ice.somafm.com/lush
http://ice.somafm.com/beatblender
http://ice.somafm.com/dronezone

You can of course enter any type of streaming data MPlayer supports.  But those are the stations I recommend 😉

### Re-Configure MPlayer’s Key Bindings

By default MPlayer uses the ‘less than’ & ‘greater than’ keys < > to switch to the previous\next items in a playlist, and ‘9’ and ‘0’ to decrease/increase volume.  Those aren’t exposed on the Makey Makey by default, so we want to override that with the Left / Right arrow keys instead to control the playlist selection and Up/Down arrow keys to control the volume.  To do so, we need to create a ‘configuration file’ in the ~/.mplayer dir.  By default, I didn’t have one, so I created a new one:

cd ~/.mplayer
nano input.conf

And inside that file:

RIGHT pt_step 1
LEFT pt_step -1
UP volume 5
DOWN volume -5

Everything should be ready to go: Get back to the mplayer dir with the playlist, and launch MPlayer:

cd ~/mplayer
mplayer -playlist somafm.pls

At this point, music should start streaming in.  If you press the “Right Arrow” on the Makey Makey, it should advance to the next item in the playlist, until you hit the end.  The “Left Arrow” will go back through the previous playlists.  Pressing Up/Down Arrows should increase/decrease the volume in steps of 5.