Sunday, August 14, 2016

Minim 1. Synthesize Sound

We are using Processing in the Python Mode. Minim is one audio library that may be installed for Processing.


We are using an Oscil UGEN. This repeats at frequency (first argument in constructor). It is set at 440 initially. When the program is running we can change it by moving mouse in x direction.


# Ex1.pyde
# SynthesizeSound.pde in Examples

'''
This sketch demonstrates how to create synthesized
sound with Minim using an AudioOutput and an Oscil.
An Oscil is a UGen object, one of many different
types included with Minim. By using the numbers 1
thru 5, you can change the waveform being used by
the Oscil to make sound. These basic waveforms are
the basis of much audio synthesis. For many more
examples of UGens included with Minim, have a look
in the Synthesis folder of the Minim examples. For
more information about Minim and additional features,
visit http://code.compartmental.net/minim/
'''

add_library('minim')

out = None
wave = None

def setup():
    size(512, 200)
    global out, wave
    minim = Minim(this)
    # use the getLineOut method of the Minim object to
    # get an AudioOutput object
    out = minim.getLineOut()
    print type(out)
    # create a sine wave Oscil, set to 440 Hz, at 0.5
    # amplitude
    wave = Oscil(440,0.5, Waves.SINE)
    # patch the Oscil to the output
    wave.patch(out)
    print type(wave)
    
def draw():
    background(0)
    stroke(255)
    strokeWeight(1)
    # draw the waveform of the output
    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)
    # draw the waveform we are using in the oscillator
    stroke(128,0,0)
    strokeWeight(4)
    for i in range(width):
        point(i,height/2-(height*0.49) *
              wave.getWaveform().value(float(i)/ width))

def mouseMoved():
    # usually when setting the amplitude and frequency of
    # an Oscil you will want to patch something to the
    # amplitude and frequency inputs but this is a quick
    # and easy way to turn the screen into an x-y control
    # for them.
    amp = map(mouseY, 0, height, 1, 0)
    wave.setAmplitude(amp)
    freq = map( mouseX, 0, width, 110, 880 )
    wave.setFrequency(freq)
    
def keyPressed():
    if key == '1': wave.setWaveform(Waves.SINE)
    elif key == '2': wave.setWaveform(Waves.TRIANGLE)
    elif key == '3': wave.setWaveform(Waves.SAW)
    elif key == '4': wave.setWaveform(Waves.SQUARE)
    elif key == '5': wave.setWaveform(Waves.QUARTERPULSE)
    
# printouts
# <type 'ddf.minim.AudioOutput'>
# <type 'ddf.minim.ugens.Oscil'>

This is the output after pressing 3 to change Waveform to sawtooth:


No comments:

Post a Comment