IoT Cookbook > Raspberry Pi > Sample Player

Pi Sample Player

Trigger samples stored on your Pi via WAMP messages, add samples via URL.

Pi with speaker

The example uses the pygame library mixer to play samples stored on the Pi in WAV or OGG format.

Try it out

The code for the example consists of an adapter written in Python and AutobahnPython using Twisted. The adapter runs on the Pi and connects to running on a network accessible from the Pi.

Included is a frontend running in the browser. The frontend is written in JavaScript using AutobahnJS and connects to the same router instance as the adapter connects to. Consequently, the frontend is able to invoke the procedures exposed on the Pi.

Preparations on the Pi

Enable audio output on the 3.5mm plug:

sudo amixer cset numid=3 1

The volume may be a bit low. To increase that to (near) the maximum, do

sudo amixer set PCM -- -100

to set this to near maximun (and try larger values instead of -100 to decrease the volume a bit again).

Install or update pygame:

pip install -U pygame

Install AutobahnPython

sudo pip install autobahn[twisted]

Install treq

pip install treq

Running the Sample Player

The code for this can be found in the crossbarexamples GitHub repository under iotcookbook. You need to clone this (or download it as a ZIP file).

You need a instance for the Sample Player adapter on the Pi and the browser frontend to connect to.

The simplest way is to navigate to iotcookbook/device/pi/sampleplayer in your local crossbarexamples repo, and do

crossbar start

This will also serve the browser frontend under http://localhost:8080.

Get the player and some sample files onto the Pi, e.g. via scp

scp pi@<IP of your Pi>:~/
scp -r samples pi@

and then start it


Then use the browser frontend to trigger one of the preset samples, or upload a sample. Samples can be in WAV or OGG format. (If you have another format, then you need to convert to one of these formats.)

When uploading, the sample is stored in the samples folder on the Pi. It can be triggered using the provided sample name. It is automatically added to the sample set on next startup.


The adapter exposes these procedures

Using it

Store your samples on the Pi. The sample name is the filename (minus the file type ending, if any).