Mp3 to png source code

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!
Click here to lend your support to: Waveform and make a donation at www.pledgie.com !

Useful links:

11 Comments

  1. Gabole
    Posted August 5, 2010 at 5:26 am | Permalink

    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 !

  2. Posted August 5, 2010 at 9:46 am | Permalink

    @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 ruby

    and making it executable.

  3. Gabole
    Posted August 5, 2010 at 11:37 am | Permalink

    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 ?

  4. Gabole
    Posted August 5, 2010 at 11:44 am | Permalink

    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 ?

  5. Posted August 5, 2010 at 12:53 pm | Permalink

    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.png

  6. Posted August 22, 2010 at 9:49 am | Permalink

    Hey 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?

  7. Posted August 23, 2010 at 12:29 am | Permalink

    weird…
    can you post the script you’re running, logs and outputs?

  8. Posted August 23, 2010 at 1:43 pm | Permalink

    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?

  9. Posted August 23, 2010 at 2:41 pm | Permalink

    it would be great!

  10. gabriel
    Posted September 3, 2010 at 1:16 am | Permalink

    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 !

  11. Posted September 3, 2010 at 9:31 am | Permalink

    wow!
    it looks like you don’t have ruby gems installed!
    try to look here http://rubygems.org/pages/download.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*