Wednesday, August 17, 2016

Minim 12. rewind

We can use the rewind() method of an AudioPlayer to point the audio to beginning.


Pressing any key will rewind the AudioPlayer object.


# Ex12.pyde
# Based on Java File in Examples: rewind.pde

'''
This sketch demonstrates how to use the rewind method
of a Playable class. The class used here is AudioPlayer,
but you can also rewind an AudioSnippet. Rewinding a
Playable sets the position to zero, the beginning.
Rewinding doesn't change the play state of a Playable,
so if it is playing or looping when you rewind, it will
continue to play or loop after you rewind it. Press 'r'
to rewind the player.
'''

add_library('minim')

groove = None

def setup():
    size(512, 200)
    global groove
    minim = Minim(this)
    groove = minim.loadFile("groove.mp3", 2048)
    groove.loop()
    
def draw():
    background(0)
    stroke(255)
    for i in range(groove.bufferSize()-1):
        line(i, 50 + groove.left.get(i)*50, 
             i+1, 50 + groove.left.get(i+1)*50 )
        line(i, 150 + groove.right.get(i)*50,
             i+1, 150 + groove.right.get(i+1)*50)
    posx = map(groove.position(), 0, groove.length(),
               0, width)
    stroke(0,200,0)
    line(posx, 0, posx, height)
    stroke(255)
    text("Press any key to rewind.", 10, 20)
    
def keyPressed():
    groove.rewind()     

This is the output:


No comments:

Post a Comment