Tuesday, August 16, 2016

Minim 5. Record Audio Output

It is often important to save the audio to disk in a wav file. When 'r' is pressed we save audio to memory, and when 's' is pressed we finally save it to disk.


A sine oscillator is controlled by a sawtooth modulator. The sawtooth is at 4 Hz, and we can see the audio output (shown at bottom), modulates over 0.25 seconds.


# Ex5.pyde
# Based on Java File in Examples: RecordAudioOutput.pde

'''
This sketch demonstrates how to use an AudioRecorder
to record audio to disk. Press 'r' to toggle recording
on and off and the press 's' to save to disk. The
recorded file will be placed in the sketch folder
of the sketch. For more information about Minim and
additional features, visit
http://code.compartmental.net/minim/
'''

add_library('minim')

out = None
recorder = None

def setup():
    size(512, 200)
    global out, recorder
    minim = Minim(this)
    # use the getLineOut method of the Minim object to
    # get an AudioOutput object
    out = minim.getLineOut()
    # create a recorder that will record from the output
    # to the filename specified. The file will be located
    # in the sketch's root folder.
    recorder = minim.createRecorder(out, "myrecording.wav")
    # patch some sound into the output so we have something
    # to record
    wave = Oscil(440, 1.0)
    mod  = Oscil(4.0,  0.25, Waves.SAW)
    mod.offset.setLastValue(0.5)
    mod.patch(wave.amplitude)
    wave.patch(out)
    textFont(createFont("Arial", 12));
    
    
def draw():
    background(0,0,255)
    stroke(255,0,0)
    # draw the waveforms
    for i in xrange(out.bufferSize()-1):
        line(i, 50 - out.left.get(i)*50,
             i+1, 50 - out.left.get(i+1)*50)
        line(i, 150 - out.right.get(i)*50,
             i+1, 150 - out.right.get(i+1)*50)
    if recorder.isRecording():
        text("Currently recording ...", 5, 15)
    else:
        text("Not recording.", 5, 15)
        
def keyReleased():
    if key == 'r':
        # to indicate that you want to start or stop
        # capturing audio data, you must call
        # beginRecord() and endRecord() on the
        # AudioRecorder object. You can start and stop
        # as many times as you like, the audio data
        # will be appended to the end of the buffer,
        # in the case of buffered recording, or to
        # the end of the file, in the case of streamed
        # recording).
        if recorder.isRecording():
            recorder.endRecord()
        else:
            recorder.beginRecord()
    if key == 's':
        # we've filled the file out buffer,
        # now write it to the file we specified
        # in createRecorder in the case of buffered
        # recording, if the buffer is large, this
        # will appear to freeze the sketch for sometime
        # in the case of streamed recording, it will not
        # freeze as the data is already in the file
        # and all that is being done is closing the file.
        # The method returns the recorded audio as an
        # AudioRecording, see the example
        # AudioRecorder >> RecordAndPlayback for more
        # about that
        recorder.save()
        print "Done saving."

This is the output while recording:



The saved file shows a sine wave which changes amplitude:

No comments:

Post a Comment