Tuesday, August 16, 2016

Minim 8. Trigger A Sample

We have kick and snare files in our data folder. They are loaded into two AudioSample objects. The objects have to be global so they will remain in memory for the draw function.


The kick or snare sounds are triggered by the trigger() method, and here they are triggered when a key is pressed: 's' (for snare) or 'k' (for kick).


# Ex8.pyde
# Based on Java File in Examples: TriggerASample.pde

'''
This sketch demonstrates how to use the loadSample
method of Minim. The loadSample method allows you
to specify the sample you want to load with a
String and optionally specify what you want the
buffer size of the returned AudioSample to be.
Minim is able to load wav files, au files, aif
files, snd files, and mp3 files. When you call
loadSample, if you just specify the filename it
will try to load the sample from the data folder
of your sketch. However, you can also specify an
absolute path (such as "C:\foo\bar\thing.wav")
and the file will be loaded from that location
(keep in mind that won't work from an applet). 
You can also specify a URL, such as "http://
www.mysite.com/mp3/song.mp3", but keep in mind
that if you run the sketch as an applet you may
run in to security restrictions if the applet is
not on the same domain as the file you want to
load. You can get around the restriction by
signing all of the jars in the applet.
An AudioSample is a special kind of file playback
that allows you to repeatedly trigger an audio
file. It does this by keeping the entire file in
an internal buffer and then keeping a list of
trigger points. AudioSample supports up to 20
overlapping triggers, which should be plenty for
short sounds. It is not advised that you use this
class for long sounds, like entire songs, for
example, because the entire file is kept in memory.
Use 'k' and 's' to trigger a kick drum sample and
a snare sample, respectively. You will see their
waveforms drawn when they are played back. For more
information about Minim and additional features, 
visit http://code.compartmental.net/minim/
'''

add_library('minim')

kick = None
snare = None

def setup():
    size(512, 200)
    global kick, snare
    minim = Minim(this)
    kick = minim.loadSample("BD.mp3", 512)
    # An AudioSample will spawn its own audio processing
    # Thread, and since audio processing works by
    # generating one buffer of samples at a time, we can
    # specify how big we want that buffer to be in the
    # call to loadSample. Above, we requested a buffer
    # size of 512 because this will make the triggering
    # of the samples sound more responsive. On some
    # systems, this might be too small and the audio
    # will sound corrupted, in that case, you can just
    # increase the buffer size.
    # if a file doesn't exist, loadSample will return null
    if kick == None:
        print "Didn't get kick!"
    # load SD.wav from the data folder
    snare = minim.loadSample("SD.wav", 512)
    if snare == None:
       print "Didn't get snare!"
    
def draw():
    background(0)
    stroke(255)
    # use the mix buffers to draw the waveforms
    for i in range(kick.bufferSize()-1):
        x1 = map(i, 0, kick.bufferSize(), 0, width)
        x2 = map(i+1, 0, kick.bufferSize(), 0, width)
        line( x1, 50 - kick.mix.get(i)*50, 
             x2, 50 - kick.mix.get(i+1)*50 )
        line( x1, 150 - snare.mix.get(i)*50,
             x2, 150 - snare.mix.get(i+1)*50)
        
def keyPressed():
    if key == 's': snare.trigger()
    if key == 'k': kick.trigger()

This is the output when the snare is triggered:


No comments:

Post a Comment