Pi Sample Player
Trigger samples stored on your Pi via WAMP messages, add samples via URL.
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 Crossbar.io running on a network accessible from 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
pip install -U pygame
sudo pip install autobahn[twisted]
pip install treq
Running the Sample Player¶
You need a Crossbar.io 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
This will also serve the browser frontend under
Get the player and some sample files onto the Pi, e.g. via scp
scp sampleplayer_pi.py pi@<IP of your Pi>:~/ scp -r samples email@example.com:~/
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
io.crossbar.examples.iot.devices.pi.<serial_of_your_pi>.audioout.trigger_sample- which takes the name of the sample to trigger as an argument
io.crossbar.examples.iot.devices.pi.<serial_of_your_pi>.audioout.stop_sample- which takes the name of the sample whose playback should be stopped as an argument
io.crossbar.examples.iot.devices.pi.<serial_of_your_pi>.audioout.add_sample- which takes the URL from which the sample is to be downloaded and the name which shall be assigned to the sample as arguments
Store your samples on the Pi. The sample name is the filename (minus the file type ending, if any).