Wednesday, August 17, 2016

Minim 10. loop Num

We can enter a number on the keyboard for the number of loops.


We create a tuple t with three values returned by 3 methods of AudioPlayer object, for use in the text() expression.


# Ex10.pyde
# Based on Java File in Examples: loopNum.pde

'''
This sketch demonstrates how to use the loop method of
a Playable class. The class used here is AudioPlayer,
but you can also loop an AudioSnippet. When you call
loop(int) it will make the Playable loop for the number
of times you specify. So, loop(3) will loop the recording
three times, which will result in the recording being
played 4 times. This may seem odd, but it is consistent
with the behavior of a JavaSound Clip. If you want to
make it stop looping you can call play() and it will
finish the current loop and then stop. Press any of the
number keys to make the player loop that many times.
Text will be displayed on the screen indicating your
most recent choice.
'''

add_library('minim')

groove = None

def setup():
    size(600, 200)
    global groove
    minim = Minim(this)
    groove = minim.loadFile("groove.mp3", 2048)
    textFont(createFont("Verdana", 18))
    
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)
    t = (groove.loopCount(), groove.isPlaying(),
         groove.isLooping())
    s1 = 'The player has %d loops left  '
    s2 = 'Is playing: %s  '
    s3 = 'Is looping: %s'
    s = s1 + s2 + s3
    text(s % t, 5, 25)
        
def keyPressed():
    loopcount = int(key)
    if loopcount>0 and loopcount<10:
        groove.loop(loopcount)

This is the output:


No comments:

Post a Comment