add two sounds
# Add one sound to another by averaging values
def addInto(sound1, sound2):
for sampleNmr in range(1, getLength(sound1)+1):
value1 = getSampleValueAt(sound1, sampleNmr)
value2 = getSampleValueAt(sound2, sampleNmr)
newValue = (value1 + value2) / 2
setSampleValueAt(sound2, sampleNmr, newValue)
#----------------------------------------------------------------------------------
# Return a new sound that is the average of two sounds passed in
# If the input sounds are musical notes, the result is a chord
def blendCopy(sound1, sound2):
secs = getLength(sound1)/getSamplingRate(sound1)
chord = makeEmptySound(int(secs+1))
for position in range(1, getLength(sound2)+1):
s1 = getSampleObjectAt(sound1, position)
s2 = getSampleObjectAt(sound2, position)
s3 = getSampleObjectAt(chord, position)
value1 = getSample(s1)
value2 = getSample(s2)
newValue = (value1 + value2) / 2
setSample(s3, newValue)
return chord
#----------------------------------------------------------------------------------
# Return an input sound that has an echo added to it after delay samples
def echo(sound1, delay):
# Extra is the extra length of our returned sound because of the echo.
# If we wanted num multiple, fading echoes, extra would be delay*num
# instead of just delay
extra = delay
# The copied/echoed sound will be longer by the extra amount
secs = (getLength(sound1)+extra)/getSamplingRate(sound1)
sound2 = makeEmptySound(int(secs+1))
# Go through sound2 from the first sample to the one that corresponds to the
# last copied sample from sound1 + the extra delay(s)
for position in range(1, getLength(sound1)+extra+1):
# In general, sound2 will be longer than sound 1, so pad it with zeros
if position < getLength(sound1):
origValue = getSampleValueAt(sound1, position)
else:
origValue = 0
# Add in the echo (which is quieter).
# This sample echoes the one occurring delay samples earlier
# (except during the first delay samples of the sound, for which there is no echo)
# If we were going to have multiple echoes, we would do this in a loop
echoPosition = position - delay
if echoPosition > 0:
# Echo should be quieter. For multiple echoes, the denominator should increase
echoValue = getSampleValueAt(sound1, echoPosition)/2
else:
# No echo for the first delay samples of the sound
echoValue = 0
# Having computed the extra echo value, add it to the current value
setSampleValueAt(sound2, position, origValue + echoValue)
return sound2
Link to this Page