In a previous article I talked about a little rails project to convert mp3 audio files to their graphical representation (waveform) in png format. After few comments asking me to release the source code here it is, but before this I must thank rjp’s cdcover project from where I took some of the code showed here.
If you plan to use this code as it is or in a modified version please let me know so maybe we can contribute to improve the code or just to keep in touch!
Notes
- the code is written in ruby programming language (great!), you can run it from command line with few modifications or use it as a library in your rails projects
- to run this code you must install some dependent gems: RMagick (which also depends on ImageMagick libraries) and NArray
- to enable the conversion from mp3 audio encoding to raw audio format you must install SoX a command line utility described as “the Swiss Army knife of sound processing programs”
- I’ve embedded some content from sox man pages near the command execution to better explain how sox works
You’ll find all useful links at the bottom of this page.
If you find this code useful please donate!

Useful links:
- SoX – http://sox.sourceforge.net/
- RMagick – http://rmagick.rubyforge.org/
- ImageMagick – http://www.imagemagick.org/
- NArray – http://narray.rubyforge.org/
11 Comments
hi there
thanks for this code but i canno’t get it work on ubuntu 9
i think i installed all necessary packages, but when trying to start the waveform.rb script i have the following errors:
./waveform.rb: line 1: require: command not found
./waveform.rb: line 2: require: command not found
./waveform.rb: line 3: require: command not found
./waveform.rb: line 5: class: command not found
./waveform.rb: line 7: DEFAULT_WIDTH: command not found
./waveform.rb: line 8: DEFAULT_HEIGHT: command not found
./waveform.rb: line 10: DEFAULT_COLOR: command not found
./waveform.rb: line 12: DEFAULT_BACKGROUND_COLOR: command not found
./waveform.rb: line 14: syntax error near unexpected token `(’
./waveform.rb: line 14: ` def initialize(input, output)’
any clue ?
Mille Grazie !
@Gabole this is a ruby script, you must run it by using the ruby interpreter:
ruby waveform.rb
or adding this line to the file:
#!/usr/bin/ruby#!/usr/bin/env rubyand making it executable.
hi and thanks for your quick reply. I did try ruby waveform.rb but i stil have the same error. i’ve installed all necessary packages i think throught apt-get (ruby, ruby1.9, sox, narray, rmagick and imagemagick).
any other clue ?
allright, i made a mistake due to mp3 filename
so now i dont have any error but the png is not created.
im using the folowing command: ruby waveform.rb test.mp3 test.png
is this correct ?
the script include only a class to render waveforms, you must add some code to let it work as you want.
you can simply add these lines at the end of the file:
wf = WaveformRenderer.new(ARGV[0], ARGV[1])wf.render_waveform
then try again to run:
ruby waveform.rb test.mp3 test.pngHey Giovanni, thanks for the code. I’ve had a bit of a play with it and while it seems to be doing _something_ I can’t get it to generate a waveform. All dependencies are installed, I’m not getting any errors. It just runs and I don’t get any output… weird.
Any ideas?
weird…
can you post the script you’re running, logs and outputs?
Hey Giovanni,
Sorry, I’d not installed imagemagick / rmagick correctly. It’s up an running fine now!
Looks great, I’m going to make a couple of tweaks so it generates waveforms in the style that Soundcloud does. E.g. transparent pngs with the waveform area transparent, this allows them to do the clever progress indicators with different colours.
Do you want me to contribute the tweaks back to you?
it would be great!
hi Giovanni
sorry i come back to you, but i tryed to install your script on my production server and i have the following error when starting the script:
waveform.rb:1:in `require’: no such file to load — rubygems (LoadError)
from waveform.rb:1
any clue ?
thanks !
wow!
it looks like you don’t have ruby gems installed!
try to look here http://rubygems.org/pages/download.