Wednesday 1 January 2014

Radio Caroline on the RaspberryPi

This is a simple project to create an internet radio and music box which can be attached to your home stereo amplifier.


It is also an excuse to celebrate the 50th anniversary of Radio Caroline, a station that fueled the British popular music industry in the 1960's.


I'll cover the RaspberryPi details later, but let's start with a history lesson.

The Swinging 60's


This is an appropriate label for the 1960's for 2 reasons: (1) it was a decade of great change, especially regarding popular music & fashion, (2) You have to now be in your 60's to have experienced it as a 1960's teenager.

The 1960's got off to a slow start. Great Britain was a popular music desert during the 1950's and early 1960's. By this, I don't mean that we didn't have great singers and musicians. The Beatles released their first real single ("Love Me Do") towards the end of 1962. This struggled to get into the top 20, while Rolf Harris (who was a regular on British TV at the time) easily passed them and entered the top 5.

No, not a CD. It's a 45rpm vinyl record.


The Beatles first single recording should have sold many more copies, but it was difficult for new "acts" to get exposure. Up to the beginning of 1964 there were only 2 TV stations: BBC and ITA. There were probably only 2 or 3 TV programs each week devoted to popular music, and TV regulars (like Rolf Harris, Ken Dodd & Max Bygraves) were always guaranteed an appearance to plug their latest record.

Even the dreadful "Top Of The Pops" (which continued this tradition of favouring light entertainment celebs) only started in January 1964. However, the ITA (ITV) did run the truly excellent "Ready Steady Go!" show on Friday evenings from mid-1963.

As for radio, the situation was even worse.

There were only 3 British radio stations, all operated by the BBC: the Light Programme, the Home Service and the Third Programme.

Our family radio: nothing here for a 1960's teenager

The BBC was still run by middle aged, middle class, white men who thought they had a duty to provide us plebs with what they thought we needed, rather than what we actually wanted. The only BBC radio program that was of interest to a 1960's teenager was "Pick of the Pops" the top 20 chart show on Sunday afternoon (4-5pm on the Light Programme).

There was also a European station called Radio Luxembourg which transmitted popular music programs in English after dark. The "after dark" bit is important because the signal only reached Britain at night by "bouncing" off the ionosphere when conditions were right.

Even then, when listening to music on Luxembourg, you might hear the first 20 seconds of a track, then lose the signal as it faded out for 30 seconds, then back again for maybe 30 seconds and so on. Not great, and it use to drive my dad nuts!

On Radio Luxembourg, all programmes or segments where sponsored by major record labels, and they determined who was on the "play list". So again, very difficult for new bands to get exposure.

There were a few young people with a bit of money to spend (mostly late-teens, early-twenties) who read the music press and hung-out in record shops on Saturday afternoons. Their magazines and 45's got passed around, which helped to spread the word.

"In 1963 I was 11 years old. I could divide my friends into two groups based upon their knowledge of popular music. Those (like me) that had some exposure to the "new sounds" because they had elder brothers or sisters. And those that didn't have elder siblings, who were like miniature versions of their parents".

Add One Bloody-minded Irishman


Against this rather sterile controlled backdrop, a young wealthy Irishman called Ronan O'Rahilly entered the scene. He managed a few "groups" (a 60's term for what we now just call a band) including "Georgie Fame & The Blue Flames", but he could not get any air-time on the BBC or Luxembourg to "plug" their records.

So Ronan bought a boat, fitted it out with the necessary equipment, and one dark night, sailed out of the Irish port of Greenore and around the southern coast of England. The boat (now called the MV Caroline) anchored off the east coast of England, just outside the 3 mile territorial limit.

On Easter Sunday 1964, Britain's first offshore radio station opened with the words;
 "This is Radio Caroline on 199, your all day music station"

Wow! we could now listen, not only to Pop music but also Blues, Soul, R & B, Motown, Country and Folk, and were suddenly exposed to singers and groups we had never heard before.

Radio Caroline South on the Mi Amigo

Caroline was quickly joined by other offshore stations operating from boats and old war-time forts. The MV Caroline moved back to the Irish sea and became Radio Caroline North, while Radio Atlanta, transmitting from the Mi Amigo, was renamed Radio Caroline South.

The radio revolution had begun. British popular music really took off, with new bands appearing from Liverpool, Manchester, Glasgow, London and just about everywhere else in Britain and Ireland.

Without offshore radio stations like Caroline and Big-L(Radio London) the history of 1960's music and the development of radio in the UK would have been very, very different.

It's unlikely that there would have been any change at the BBC for another 20 years (the BBC was forced to launch Radio 1 in 1967), and the introduction of commercial radio (Oct 1973) would have remained a very low priority.

"50 years on, and we now have approximately 600 licensed radio stations in the UK"

The Technical Bit


A few months ago I created a simple music box using a RaspberryPi. For this new version, I was going to stick with the original application "music123" but found it very difficult to kill the process. Each time I killed it, it would restart. The other problem is that it does not play internet audio streams.

So I've completely replaced it with mplayer, as this seems to work well for both capturing a stream and playing a stored bunch of audio files.

As this new music box will run our music and stream Radio Caroline, we need some kind of selector switch, and as I already have a few minature push button switches, I'm going to make use of one of these.

My switch is push to make, release to break, and here is my circuit.


With the switch in its normally-open position, the 2 resistors provide a path to 0 Volts, thereby producing a logic "0" at the Pin 7 input.

When the switch is pressed, these resistors are connected to 3.3Volts, and a current flows through the 1k resistor to the Pin 7 input, producing a logical "1".

This 1k resistor would not be necessary if we could be sure that Pin 7 is always an input. But if it were ever (maybe accidentally) programmed as an output, closing the switch may destroy the Raspberry Pi GPIO circuitry.

I fitted the switch by drilling a small 4mm hole in the lid of the RaspberryPi box.


The switch is held in place by 3 blobs of hot-melt glue on the inside. I also had to use my Dremel to grind away (or at least thin-down) a small area of the inside lid to accommodate the GPIO push on connectors.

The Programming bit


First we need to install mplayer, so in a terminal window type:-

sudo apt-get install mplayer

We need a python support library to deal with processes:-


sudo apt-get install python-psutil

And of course your RaspberryPi will need internet access.

You also need to copy a selection of your music files to your RaspberryPi folder:-
 /home/pi/Music

MPlayer should be fine with files located within sub-folders (e.g. /home/pi/Music/PopClassics, /home/pi/Music/EricClapton & so on), providing they are .ogg, .mp3 & .wma files.

Here is my simple Python program which will read the switch condition and select our music source.

#music-box.py
#Randon music player (Juke Box) and internet radio player
#SteveDee
#11/12/13
#==========================================================

#Import the operating system library
import os

#Import the Process & cpu utilisation library
import psutil
#Import the time library
import time
#Import the Raspberry Pi library which controls the GPIO
import RPi.GPIO as GPIO
#Clear the current GPIO settings
GPIO.cleanup()
#Set mode to use Raspberry Pi pins numbers
GPIO.setmode(GPIO.BOARD)
#Set connector pin 7 to be an input
GPIO.setup(7,GPIO.IN)

PLAYER = "mplayer"
MUSIC = "/home/pi/Music"
RADIO_STATION = "http://sc3.radiocaroline.net:8030"   #96k aac stream

#create/update music playlist
os.system('find '+ MUSIC + ' -type f -iname \*.ogg -o -iname \*.wma -o -iname \*.mp3 > ' + MUSIC + '/playlist')

#Start with JukeBox
JukeBox = True
os.system('mplayer -shuffle -playlist ' + MUSIC + '/playlist &')

#Create a LOOP that runs & runs (...until you force it to stop)
while True:
    switch = GPIO.input(7)
    if switch == True:
        JukeBox = not JukeBox
        #kill player
        for proc in psutil.process_iter():
            if proc.name() == PLAYER:
                proc.kill()           
        if JukeBox == True:
            #start Juke-box
            os.system('mplayer -shuffle -playlist ' + MUSIC + '/playlist &')
        else:
            #start radio
            os.system('mplayer ' + RADIO_STATION + ' &')
    #Reduce cpu load
        time.sleep(0.2)

    while switch == True:
        #switch debounce       
        time.sleep(1)
        switch = GPIO.input(7)
       

Save this python file as music-box.py in /home/pi

Now connect your RaspberryPi to an audio amplifier and test your system.

Once you are happy that it works, there are just 2 more steps to complete.

Auto Run Config


The system can run headless (i.e. no display, keyboard or mouse) so run rpi-config and disable the boot to desktop option.

We also want to auto-run our music player when the system boots, so we need to edit the /etc/rc.local file with root privileges. Most people recommend you do this by running some hideous command line editor. Its up to you. If you are a coward like me, try this.

If you have already shutdown the desktop (after disabling boot to desktop) at the prompt type:-

startx

From the desktop select file manager, navigate to /etc then select from the file manager menu bar Tools > Open current folder as root

Wait for the new file window to appear, then right click the rc.local file and open with LeafPad.

Your rc.local file should look like this, except you need to add the python line near the end of the file:-

# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

python /home/pi/music-box.py

exit 0



Now reboot your CarolinePi and you will (hopefully) find it starts playing music after about 30 seconds. Press and hold the button until the music stops, then release. The system should switch between your music and Radio Caroline.

Don't Byte Off More Than You Can Chew


If you don't have a limitless download deal with your ISP, be aware that internet streaming will gobble your bytes.

By my calculations, if you use a 96kbits per second stream, you will consume 43.2MBytes per hour, or 432MB for a 10 hour day.

Radio Caroline provides other streaming options. Using a 48k stream is going to help a bit (or is it a byte?): http://sc5.radiocaroline.net:8010

Hitting the Buffers


I haven't experienced any problems so far, but if you get stream drop-outs you could try adding a buffer for mplayer. This is done by using the keyword cache (just Google: mplayer cache).

Using Python 3


Again, I haven't tried this, but if you would rather use Python 3 (instead of Python 2.x) just install python3-pcutil and change "python" to "python3" in the rc.local file.

Re-Gaining Control Of Your RaspberryPi


Since you've configured your CarolinePi to auto-start your prgram, that's exactly what it will do each time you power it up.

To re-gain control, just put your SD card into your Linux computer (you are a Linux user, aren't you?) and comment out the line in rc.local (with a #) like this:-

#python /home/pi/music-box.py

If you just want to add more audio files or modify your Python program, you can do this with the SD card plugged into you Linux computer.

Expanding The Options


So now you have a RaspberryPi with a switch, you can easily expand the options.

In this example I've added a second internet radio station: PlanetRock

#music-box.py
#Randon music player (Juke Box) and internet radio player
#SteveDee
#18/12/13
#==========================================================

import os
import psutil
#Import the time library
import time
#Import the Raspberry Pi library which controls the GPIO
import RPi.GPIO as GPIO
#Clear the current GPIO settings
GPIO.cleanup()
#Set mode to use Raspberry Pi pins numbers
GPIO.setmode(GPIO.BOARD)
#Set connector pin 7 to be an input
GPIO.setup(7,GPIO.IN)

PLAYER = "mplayer"
MUSIC = "/home/pi/Music"
RADIO_CAROLINE = "http://sc5.radiocaroline.net:8010"        #48k aacplus
PLANET_ROCK = "http://sharpflow.sharp-stream.com:8000/planetrock.mp3"        #128k mp3

#update music playlist
os.system('find '+ MUSIC + ' -type f -iname \*.ogg -o -iname \*.wma -o -iname \*.mp3 > ' + MUSIC + '/playlist')

JukeBox = 0
RadioCaroline = 1
PlanetRock = 2

#Start with Juke-Box
os.system('mplayer -shuffle -playlist ' + MUSIC + '/playlist &')

MusicSource = 0
#Create a LOOP that runs & runs (...until you shutdown)
while True:
    switch = GPIO.input(7)
    if switch == True:
        MusicSource += 1
        if MusicSource > 2:
            MusicSource = 0
        #kill player
        for proc in psutil.process_iter():
            if proc.name == PLAYER:
                proc.kill()           
        if MusicSource == RadioCaroline:
            #start radio
            os.system('mplayer ' + RADIO_CAROLINE + ' &')
        elif MusicSource == PlanetRock:
            #start radio
            os.system('mplayer ' + PLANET_ROCK + ' &')               
        else:
            #start Juke-Box
            os.system('mplayer -shuffle -playlist ' + MUSIC + '/playlist &')
        #Reduce cpu load
        time.sleep(0.2)

    while switch == True:
        #switch debounce       
        time.sleep(1)
        switch = GPIO.input(7)


Horrible Histories


I really enjoyed watching the film "The Boat That Rocked". It was fun and it was fiction.

But the real story of Radio Caroline and the other "pirate" radio stations would also make a great film. It would include the elements of drama, action, humour, political deception and murder.

You can read a much more detailed history on the Radio Caroline website.

The last Radio Caroline ship: The Ross Revenge


No comments:

Post a Comment