Tuesday, May 13, 2014

18. Envelope Csound Opcodes


Four different envelope opcodes are covered. Envelopes are used to control an audio signal. A basic use is in controlling the amplitude of an audio event over it's duration.




These are the four opcodes that will be covered. csound has many more. But these are enough. If you need a more complex envelope, you should use a table, a topic explored in the next Tutorial.




The opcode adsr has four parameters. The attack time is how fast a signal changes from 0 to 1. The delay time refers to time to a sustain value. The third parameter gives the sustain level. The fourth parameter is the release time, time from sustain level to zero. Even though the sustain time is not explicit here, it is internally calculated to fit the duration of an audio event.





This shows an Instrument String using the adsr opcode and the wave file it results in. The attack time is 0.2 second and the decay time is 0.3 second. The sustain value is 0.7. Finally the release time is 0.9 second.


# main1.py
from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(ksmps=4410, nch=2)
ins1="""
kadsr adsr 0.2,0.3,0.7,0.9
aadsr adsr 0.2,0.3,0.7,0.9
asig upsamp kadsr
out asig,aadsr
"""
instrument(1,ins1)
add(stopIns,startSco)
score(1,0,2)
add(stopSco,stopSyn)
writeRun('tut18a')




The opcode linseg is used to create an arbitrary piecewise linear shape. The inputs are (x,y) values. The first x is assumed 0 and thus you only give a starting y-value. The last y-value lasts till the event ends.




This is an example of an Instrument String using the linseg opcode. Even though we have 5 inputs, this is really 3 (x,y) points since the initial value of x of 0 is assumed. Thus the signal starts at 0.2 and after 0.3 seconds is at 0.7. After 0.9 seconds, it is at 0.5. Actually, it is not (x,y), but (change in x, y).


# main2.py
from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(ksmps=4410, nch=2)
ins1="""
klinseg linseg 0.2,0.3,0.7,0.9,0.5
alinseg linseg 0.2,0.3,0.7,0.9,0.5
ahello upsamp klinseg
out ahello,alinseg
"""
instrument(1,ins1)
add(stopIns,startSco)
score(1,0,2)
add(stopSco,stopSyn)
writeRun('tut18b')




The expon opcode is similar to the line opcode. However, it changes exponentially from level a to level b.




This is an example of using the expon opcode. The resulting wave file is also shown. The signal goes from 0.01 to 0.9 over the entire duration of the audio event.


# main3.py
from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(ksmps=4410, nch=2)
ins1="""
kexpon expon 0.01,p3,0.9
aexpon expon 0.01,p3,0.9
ahello upsamp kexpon
out ahello,aexpon
"""
instrument(1,ins1)
add(stopIns,startSco)
score(1,0,2)
add(stopSco,stopSyn)
writeRun('tut18c')




The opcode expseg, is similar to linseg, except now the points are connected by exponential lines. Also now the last rate is maintained until the audio event ends.




This is example of an Instrument String using the expseg opcode. It goes exponentially from 0.1 to 0.9 over 0.5 seconds. Then it goes to 0.001 after another 0.5 seconds. Then after another second, it goes to level of 0.5.


# main4.py
from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(ksmps=4410, nch=2)
ins1="""
kexpseg expseg 0.01,0.5,0.9,0.5,.001,1,0.5
aexpseg expseg 0.01,0.5,0.9,0.5,.001,1,0.5
ahello upsamp kexpseg
out ahello,aexpseg
"""
instrument(1,ins1)
add(stopIns,startSco)
score(1,0,2)
add(stopSco,stopSyn)
writeRun('tut18d')




You will find additional information at pythonaudio.blogspot.com, including the source code.



This is the video of Tutorial 18:


1 comment:

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.