Wednesday, August 17, 2016

Minim 13. set Loop Points

For the AudioPlayer object we may set the beginning and ending points.


A left-click sets the beginning and right-click sets the ending point. We also have to give initial values for loopBegin and loopEnd since they are needed in draw().


# Ex13.pyde
# Based on Java File in Examples: setLoopPoints.pde

'''
This sketch demonstrates how to use setLoopPoints
using an AudioPlayer. Left-click with the mouse
to set the start point of the loop and right-click
to set the end point of the loop. You will likely
find there to be a break during loops while the code
seeks from the beginning of the file to the start
of the loop. This seek time can become quite
noticable if you are using an mp3 file because it
will need to decode as it seeks.
'''

add_library('minim')

snip = None
loopBegin = 0
loopEnd = 1000

def setup():
    size(512, 200)
    global snip
    minim = Minim(this)
    snip = minim.loadFile("groove.mp3", 2048)
    
def draw():
    background(0)
    fill(255)
    text("Loop Count: %d" % snip.loopCount(), 5, 20)
    text("Looping: %s" % snip.isLooping(), 5, 40)
    text("Playing: %s" % snip.isPlaying(), 5, 60)
    p = snip.position()
    l = snip.length()
    text("Position: %d" % p, 5, 80)
    text("Length: %d" % l, 5, 100)
    x = map(p, 0, l, 0, width)
    stroke(255)
    line(x, height/2 - 50, x, height/2 + 50)
    lbx = map(loopBegin, 0, snip.length(), 0, width)
    lex = map(loopEnd, 0, snip.length(), 0, width)
    stroke(0, 255, 0)
    line(lbx, 0, lbx, height)
    stroke(255, 0, 0)
    line(lex, 0, lex, height)
    
        
def mousePressed():
    global loopEnd, loopBegin
    ms = int(map(mouseX, 0, width, 0, snip.length()))
    if mouseButton == RIGHT:
        snip.setLoopPoints(loopBegin, ms)
        loopEnd = ms
    else:
        snip.setLoopPoints(ms, loopEnd)
        loopBegin = ms

def keyPressed():
    snip.loop(2) 

This is the output:


No comments:

Post a Comment