A Balance UGen can change the magnitude of left or right channel in a stereo signal.
We use a LFO and Sine Oscillator as well.
# Ex14.pyde
# Based on Java File in Examples: balanceExample.pde
'''
balanceExample is an example of using the Balance UGen
inside an instrument. It is important to note that
Balance works specifically on stereo signals. It is
not the same as Pan, which takes a mono signal and
places it in a stereo field. Balance works by simply
attenuating either the left or right channel of a
stereo signal based on what the balance is set to.
Negative balance values will attenuate the left channel
and positive balance values attentuate the right channel.
For more information about Minim and additional features,
visit http://code.compartmental.net/minim/
author: Damien Di Fede
'''
add_library('minim')
out = None
# Every instrument must implement the Instrument interface
# so playNote() can call the instrument's methods.
class ToneInstrument(Instrument):
# declare our oscillators. sineOsc is used for the
# sounding tone and lFOOsc is used to control the
# value of Balance
def __init__(self, frequency, amplitude, lfoFrequency,
lfoAmplitude):
self.sineOsc = Oscil(frequency, amplitude,
Waves.SINE)
self.lFOOsc = Oscil(lfoFrequency, lfoAmplitude,
Waves.SINE)
# Balance takes the value of the Balance as an
# argument. 0 would result in no change in the
# signal fed into it
# negative values will attenuate the left channel
# and positive values will attenuate the right
# channel
self.balance = Balance(0.5)
# patch our LFO to the balance control of Balance
self.lFOOsc.patch(self.balance.balance)
# patch our oscillator to the balance and into the
# damp
self.sineOsc.patch(self.balance)
def noteOn(self,dur):
# every instrument must have a noteOn(float)
# method to start sounding we simply patch
# our balance to output. This is better than
# simply turning the volume up because
# it means we don't actually have to do any
# processing until we are meant to be heard.
self.balance.patch(out)
def noteOff(self):
# every instrument must have a noteOff() method
self.balance.unpatch(out)
def setup():
# initalize the drawing window
global out
size(512, 200)
# initalize the minim object and output
minim = Minim(this)
# note that we must ask for a stereo output
# because balance does not work with mono output.
out = minim.getLineOut(Minim.STEREO, 1024)
# pause time when adding a bunch of notes at once
out.pauseNotes()
# make an instance of my instrument and ask the
# output to play it
# arguments are: oscillator frequency, oscillator
# amplitude, lfo for the balance frequency, lfo
# for the balance amplitude
myNote = ToneInstrument(200.0, 0.3, 0.5, 1.0)
# play this instrument on the output.
# arguments are: how many seconds from now to
# play the note, and how long to play the note for
out.playNote(0.0, 8.0, myNote)
# make another instance of my instrument
myNote = ToneInstrument(415.3, 0.3, 1.0, 1.0)
out.playNote(2.0, 0.5, myNote)
myNote = ToneInstrument(415.3, 0.3, 2.0, 1.0)
out.playNote(3.5, 0.5, myNote)
myNote = ToneInstrument(415.3, 0.3, 3.0, 1.0)
out.playNote(5.0, 0.5, myNote)
myNote = ToneInstrument(830.6, 0.3, 5.0, 1.0)
out.playNote(6.5, 1.5, myNote)
# resume time after a bunch of notes are added
# at once
out.resumeNotes()
# draw is run many times
def draw():
# erase the window to black
background(0)
# draw using a white stroke
stroke(255)
# draw the waveforms
for i in range(out.bufferSize() - 1):
# find the x position of each buffer value
x1 = map(i, 0, out.bufferSize(), 0, width)
x2 = map(i+1, 0, out.bufferSize(), 0, width)
# draw a line from one buffer position to the next
# for both channels
line(x1, 50 + out.left.get(i)*50,
x2, 50 + out.left.get(i+1)*50)
line(x1, 150 + out.right.get(i)*50,
x2, 150 + out.right.get(i+1)*50)
This is the output: