Posted on by & filed under Content - Highlights and Reviews, Web Development.

In the last installment we shrimped our Compaq Presario 700 laptop up another notch. We started sending simple serial data over the USB connection to the Arduino using Python, driving a simple “bar chart” on the budget HL1606 full color LED array stuck to the back of its screen (see the video). Now we need to have that data showing something dynamic and interesting to justify such a hardcore display.

Wouldn’t it be great to be able to use it as a glorious graphic equalizer to show off the tunes which our little laptop is pumping out to the best effect? Well it turns out that with Lubuntu’s support for Python and Gstreamer it’s only a few lines of code to achieve just that.

The basic strategy is to feed the audio from our soundcard through a media pipeline constructed from Gstreamer elements, one of which – called spectrum – runs a periodic Fast Fourier Transform to work out the intensity of each frequency band in an audio stream. With the intensity information we get back from spectrum, we can control the LED bar graph on the back of the laptop in real time, to reflect what we’re listening to.

The key line above shows the configuration of the GStreamer pipeline, just before it is passed on to gst.parse_launch() to read the specification and create the flows of audio between the various named elements. The element pulsesrc (representing the default source from the pulseaudio sound system used by Lubuntu) is piped (using the ! sign) through spectrum to a fakesink element. The parameters to spectrum indicate how often the frequency bars should be updated, and the bands parameter shows how many separate frequency ranges should be kept track of. The threshold is the lowest intensity in decibels (dB) which will ever be monitored and reported. Values lower than this are ignored, and the threshold value is returned instead. The fakesink element makes sure to consume the bytes at the end of the pipeline to keep it flowing. Without it, the pipeline would not be considered complete and hence no audio would run through it.

After the pipeline is set up and running, we can expect to get callbacks within the on_message() function, thanks to the line:

This subscribes to messages coming from the spectrum element whenever it has a new slice of frequency data for us.

Within the definition of the on_message() function we can see a specific form of message which is detected, and from which the magnitudes of each different frequency are read. Once we have the array of magnitudes, we can turn this into a bytearray, which looks to the Arduino exactly like the one produced by our earlier shape_sender.py example. This is passed along the serial connection where the display reads it and interprets it to control the height of the bars in the bar chart.

In the next installment, we’ll add some color.

Safari Books Online has the content you need

These books in Safari Books Online will help you enhance your Arduino project:

Getting Started with Arduino gives you lots of ideas for Arduino projects and helps you get started with them right away. From getting organized to putting the final touches on your prototype, all the information you need is right in the book.
Arduino Cookbook, 2nd Edition helps you create your own toys, remote controllers, alarms, detectors, robots, and many other projects with the Arduino device.
In Beginning Arduino teaches by using an amazing set of 50 cool projects. You’ll progress from a complete beginner regarding Arduino programming and electronics knowledge to intermediate skills and the confidence to create your own amazing Arduino projects.
Building Wireless Sensor Networks helps you build a series of useful projects, including a complete Arduino- and XBee-powered wireless network that delivers remotely-sensed data.
Arduino Robotics will show you how to use your Arduino to control a variety of different robots, while providing step-by-step instructions on the entire robot building process.
Arduino Projects to Save the World shows that it takes little more than a few tools, a few wires and sensors, an Arduino board, and a bit of gumption to build devices that lower energy bills, help you grow our own food, monitor pollution in the air and in the ground, even warn you about earth tremors.

About this author

Cefn Hoile sculpts open source hardware and software, and supports others doing the same. Drawing on ten years of experience in R&D for a multinational technology company, he works as a public domain inventor, and an innovation catalyst and architect of bespoke digital installations and prototypes. He is a founder-member of the CuriosityCollective.org digital arts group, and a regular contributor to open source projects and not-for-profits. Cefn is currently completing a PhD in Digital Innovation at Highwire, University of Lancaster, UK.

Tags: Arduino, Gstreamer, Lubuntu, Python, shrimping,

Trackbacks/Pingbacks

  1.  Laptop Shrimping Tip: Serial Control | Safari Books Online's Official Blog