Tuesday, August 16, 2016

Minim 6. Draw Waveform And Level

The file "groove.mp3" is in the data folder for this sketch.


We draw each 1024 sample buffer as well as the RMS level.


# Ex6.pyde
# Based on Java File in Examples: DrawWaveformAndLevel.pde

'''
This sketch demonstrates how to use the AudioBuffer
objects of an AudioPlayer to draw the waveform and
level of the sound as it is playing. These same 
AudioBuffer objects are available on AudioInput,
AudioOuput, and AudioSample, so the same drawing
code will work in those cases.
'''

add_library('minim')

groove = None

def setup():
    size(1024, 200)
    global groove
    minim = Minim(this)
    groove = minim.loadFile("groove.mp3", 1024)
    groove.loop()
    
def draw():
    background(0)
    stroke(255)
    # draw the waveforms
    # the values returned by left.get() and right.get()
    # will be between -1 and 1, so we need to scale them
    # up to see the waveform. Note that if the file is
    # MONO, left.get() and right.get() will return the
    # same value
    for i in range(groove.bufferSize()-1):
        x1 = map(i, 0, groove.bufferSize(), 0, width)
        x2 = map(i+1, 0, groove.bufferSize(), 0, width)
        line( x1, 50 + groove.left.get(i)*50, 
             x2, 50 + groove.left.get(i+1)*50 )
        line( x1, 150 + groove.right.get(i)*50,
             x2, 150 + groove.right.get(i+1)*50)
    noStroke()
    fill(255,128)
    # the value returned by the level method is the RMS
    # (root-mean-square)
    # value of the current buffer of audio.
    # see: http://en.wikipedia.org/wiki/Root_mean_square
    rect( 0, 0, groove.left.level()*width, 100)
    rect( 0, 100, groove.right.level()*width,100)

This is the output:


No comments:

Post a Comment