Building an Audio Visualizer in Python

In this Blender 2.5 video tutorial, we will be taking a look at how to use sound to drive an object’s scale.

We will be building a ‘field’ of cubes, each of which will pulse along to certain frequencies of a song.

What is covered in this tutorial:
– Baking sound to F-curves.
– Programatically creating an N-by-M matrix (field/ grid)
– Setting a keyframe and baking F-curves from Python
– Finding out which frequencies to use using Audacity

Note: In recent SVN versions the API has been updated. If you’re using one of these later versions please be sure to replace “bpy.ops.object.scale_apply()” with “bpy.ops.object.transform_apply(scale=True)

Leave Comment

Discussion

355 Responses to “Building an Audio Visualizer in Python”
  1. Posts: 1
    mimami says:

    Thanks for the great tutorial, I’ve always had an interest in audio visualization and I have lots of experience with python coding so this is great!
    I do have one question however. My visualizer works fine and I’ve been adding new features to it, but how do you get blender to render the whole song, as opposed to just the first 20 seconds? I know it will take a while, but I have a decent GPU and I’m willing to wait. I’ve found guides on how to do this manually but I’d love to implement it directly with python code.

  2. Posts: 2
    azagwen says:

    it’s not working and a lot of errors are detected

  3. Posts: 5
    awbarry00 says:

    Hello Patrick,
    First off, thanks for the great tutorial. This was very informative.

    Second, I found an add-on written by liero at blenderartists.org which allows re-sampling of an F-Curve which has been baked to a sound file. With this, after baking the curve, you can establish control points on the curve again and directly grab or scale F-Curve as you would prior to baking the sound to the curve. It works by creating a series of actions from the F-Curve to generate the control points. This gives you additional control and flexibility, you can find the add-on here: http://dl.dropbox.com/u/16486113/Blender/unbake.py

    However, I’m currently trying to create an Audio-Visualizer with an emissive material with the strength of the material baked to an F-Curve, and havent been able to break the range [0, 1]. I cannot apply the transform beforehand, as there is no transform, and this script unfortunately won’t let me do this. My most likely plan is to try and modify it to break down the F-Curve into key frames instead of actions so that it can work on an F-Curve for a material property. Do you have any other suggestions? Thank you!

    • Posts: 5
      awbarry00 says:

      Figured this out, albeit in a very convoluted manner. Used toned-down version of this script to create one cube with sound baked to f-curve on layer 3, then added another object on layer 2 with emissive material. Add driver to emissive material strength to take value from z-scaling of cube and applied desired transformation prior to baking.

      Bit of a round-a-bout way to go, but it got the job done. Had to do some compositing as well to get a nice glare effect.

    • Posts: 5
      awbarry00 says:

      Hey man, line 24, ‘true’, when used as a boolean value should be written ‘True’. Code (in almost any language) is case sensitive, so make sure to look for this. A few quick tips:
      1) As Patrick says, make sure to open blender via a console when developing in Python. You can’t debug code without error messages, and this one points to the problem pretty directly.
      2) This isn’t really the best place to post the question, in my opinion. Not that it’s bad, but I usually get very helpful and timely results from stackoverflow. There’s even a spin-off site specifically for blender: http://blender.stackexchange.com/. You got lucky I saw the post :)

      Hope this is helpful!

      • Posts: 5
        awbarry00 says:

        The code looks like this:
        bpy.context.active_object.animation_data.action.fcurves[0]. lock = True
        bpy.context.active_object.animation_data.action.fcurves[1]. lock = True

        You also need to include audio format in line 31. It needs to look something like:
        bpy.ops.graph.sound_bake(filepath=”/home/matthew/Music/Krewella – Come And Get It (Razihel Remix).mp3″, low=i*step, high=i*step + step)

Leave a Comment

You must be logged in to post a comment.