Audio spectrum with 128×32 display

Audio-Spektrum mit 128x32 Display

If a 8×8 LED matrix audio level meter is ​​insufficient for you, I’ll show you in this article how to use a small 128×32 display to view an audio spectrum.

How the visual representation of the spectrum should look like, you can set yourself. My version is a bar graph.

What you need

The hardware you need is the display itself. But in the I2C version with the driver IC SSD1306.

128x64 display for the audio spectrum
Example of a 128×64 display with SSD1306 driver IC

Like the 8×8 LED matrix there is needed a component of TMS software to control the 128×32 display. How to install it in Lazarus, I described in the article 8×8 LED matrix with Raspberry and Lazarus.

The components of TMS have been developed for the hardware of Adafruit. But it also works with other manufacturers.

I’m using a display with 128×64 pixels. Which unfortunately can be addressed only for 32 lines. But the result looks pretty useful too.

The great thing is that the program works just as you learned at the audio level meter article.

The connection to the display is done via the component TTMSLCLAdaDispl128x32. In the app sample I use the instance display, so that the amount of writing is a little smaller.

component TTMSCLCAdaDispl128x32 for visualizing the audio spectrum
Component TTMSLCLAdaDispl128x32

Initializing works very similar to the 8×8 LED matrix. The address for the I2C is hexadecimal 3C (or as decimal number: 60).

display.I2CAddress:=Hex2Dec('$3c'); 
display.Open; 
display.Clear;

You can use the command i2cdetect -y 1 in a shell to get the address of your device.

MP3

The basic functions of the MP3 processing are the same as in the previous article. For the MP3 output the BASS library of un4seen.com is used again.

Audio spectrum

Example App created with Lazarus

The visualization of the audio spectrum is triggered by a timer event. As timer interval I use 100 ms.

In order to get the appropriate data in parallel to the sound output, the BASS library provides the function ChannelGetData.

 BASS_ChannelGetData(Channel, @FFTData, 1024);

With a few instructions, an audio spectrum can be generated from the data.

//Clear bitmap for screen output 
Bitmap.Canvas.Brush.Color:=clBlack;
Bitmap.Canvas.Pen.Color:=clBlack;
Bitmap.Canvas.Rectangle(0,0,Bitmap.Width,Bitmap.Height);

Bitmap.Canvas.Pen.Color:=clWhite;
if CheckboxFilled.Checked=true then Bitmap.Canvas.Brush.Color:=clWhite;

j:=Round(128/SpinEditColWidth.Value);
if j*SpinEditColWidth.Value>127 then j:=j-1;
for i := 0 to j do begin
YVal:=Abs(FFTData[(i*1)+5]);
YPosI:=Trunc((YVal)*500);

if YPosI>MaxBar then MaxBar:=YPosI;
YPos:=conv(YPosI/MaxBar*SpecHeight);
if YPos>SpecHeight then YPos:=SpecHeight;
if YPos>=FFTPeacks[i] then FFTPeacks[i]:=YPos
else FFTPeacks[i]:=FFTPeacks[i]-PeakFall;
if YPos>=FFTFallOff[i] then FFTFallOff[i]:=YPos
else FFTFallOff[i]:=FFTFallOff[i]-LineFall;

if (SpecHeight-FFTPeacks[i])>SpecHeight then FFTPeacks[i]:=0;
if (SpecHeight-FFTFallOff[i])>SpecHeight then FFTFallOff[i]:=0;

//128x32 display output
if CheckboxFilled.Checked=true then
display.FillRect(X+(i*SpinEditColWidth.Value),Y+SpecHeight-FFTFallOff[i],
X+((i+1)*SpinEditColWidth.Value)-1-SpinEditGap.Value,Y+SpecHeight)
else
display.DrawRect(X+(i*SpinEditColWidth.Value),Y+SpecHeight-FFTFallOff[i],
X+((i+1)*SpinEditColWidth.Value)-1,Y+SpecHeight);

//screen output
Bitmap.Canvas.Rectangle(X+(i*SpinEditColWidth.Value),Y+SpecHeight-FFTFallOff[i],
X+((i+1)*SpinEditColWidth.Value),Y+SpecHeight);
end;

//show screen
Panel1.Canvas.CopyRect(Rect(0,0,Bitmap.Width,Bitmap.Height),Bitmap.Canvas,
Rect(0,0,Bitmap.Width,Bitmap.Height));

//show display
display.Display;

For your first tests it’s best to download the sample app and have a look at my spectrum display.

Have fun experimenting!

Download

Source code of the sample app Miniplayer2
(with a directly executable application miniplayer2)

Links

BASS library for Lazarus: http://www.un4seen.com/

The basic routine for my app is based of the example Spectrum Visualyzation of Alessandro Cappellozza. Which is part of the BASS library.

Website of TMS Software: https://www.tmssoftware.com/site/tmslclhwpack.asp

TMS package on github: https://github.com/tmssoftware/TMS-LCL-HW-Pack-for-Raspberry-Pi

Article 8×8 LED-Matrix with Raspberry and Lazarus

The MP3 sample files come from FRAMETRAXX : GEMA-free music


Lesen Sie diesen Artikel in Deutsch