# Sound Processing Examples # # Function changes in JES 3.1 # # --makeEmptySound() now takes a number of samples instead of a number of seconds. # To create a sound in terms of seconds, use the new method makeEmptySoundBySeconds(). # In addition, both methods optionally take a sampling rate as a parameter along with the duration. # # --getSample() and setSample have been replaced by getSampleValue() and setSampleValue(). # --getNumSamples() is a new method that returns the number of samples in a sound, same as getLength(). # --getDuration() is a new method that returns the length of the sound in seconds. # --stopPlaying() is a new method that forces a sound to cease playback. # --The new explore() method will take a Sound object and open up the Sound Tool. # --copySoundInto() and cropSound() are available when using Object-Oriented notation only. These methods # are not listed in the JES Functions menu. # # --The following six methods are no longer documented, but # their functionality still exists within JES. # playAtRate(), playAtRateDur(), playInRange(), blockingPlayInRange(), # playAtRateInRange(), blockingPlayAtRateInRange() def g(): path = "c:\\Documents and Settings\\My Downloads\\" # change to your directory f1 = path + "g4.wav" f2 = path + "c4.wav" f3 = path + "gettysburg.wav" f4 = path + "notify.wav" s1 = makeSound(f1) s2 = makeSound(f2) s3 = makeSound(f3) s4 = makeSound(f4) # displaySamples(s1,1000) # blockingPlay(s1) # blockingPlay(s2) # blockingPlay(s4) # increaseVolume(s1,2) # blockingPlay(s1) # decreaseVolume(s1) # blockingPlay(s1) # bw = backwards(s4) # blockingPlay(bw) # blockingPlay(s4) # normalize(s4) # blockingPlay(s4) # s5= merge(s1,s2) # blockingPlay(s5) # playNote(60,3000,127) # playNotes() # s5 = double(f3) # s5 = doubleFrequency(s3) # blockingPlay(s5) # s6 = half(f3) # blockingPlay(s6) # s6 = halfFrequency(s3) # blockingPlay(s6) # onlymaximize(s3) # blockingPlay(s3) # will be very loud # addSounds(s1,s2) # blockingPlay(s2) # s7 = echo(f3,10000) # blockingPlay(s7) # blockingPlay(s1) # s8 = shift(f3,1.25) def displaySamples(sound,n): for index in range(0,n): sample = getSampleValueAt(sound,index) print sample def playNotes(): for n in range(1,89): playNote(n,100,127) def increaseVolume(sound,n): for sample in getSamples(sound): value = getSample(sample) setSample(sample,value*n) # writeSoundTo(sound,"g4-louder.wav") def decreaseVolume(sound): for sample in getSamples(sound): value = getSample(sample) setSample(sample,value*0.5) # writeSoundTo(sound,"g4-softer.wav") def normalize(sound): largest = 0 for s in getSamples(sound): if abs(getSample(s)) > largest: largest = abs(getSample(s)) multiplier = 32767.0 / largest print "The largest value we saw was:",largest print "Our multiplier is",multiplier for s in getSamples(sound): louder = multiplier * getSample(s) setSample(s,louder) def onlymaximize(sound): for sample in getSamples(sound): value = getSample(sample) if value > 0: setSample(sample,10000) if value < 0: setSample(sample, -10000) # writeSoundTo(sound,"g4-max.wav") def backwards(sound): # different from textbook sourceIndex = getLength(sound)-1 target = makeEmptySound(sourceIndex) for targetIndex in range (0, getLength(target)): sourceValue = getSampleValueAt(sound,sourceIndex) setSampleValueAt(target,targetIndex,sourceValue) sourceIndex = sourceIndex - 1 return target def echo(filename, delay): # uses filename parameter s1 = makeSound(filename) s2 = makeSound(filename) for index in range (delay + 1, getLength(s1)): echoSample = 0.6 * getSampleValueAt(s2, index - delay) setSampleValueAt(s1,index, getSampleValueAt(s1,index) + echoSample) return s1 def changeVolume(sound,factor): for sample in getSamples(sound): value = getSample(sample) setSample(sample,value*factor) writeSoundTo(sound,"g4-louder.wav") def merge(sound1,sound2): # different from textbook - no pause inserted target = makeEmptySound(getLength(sound1) + getLength(sound2)) index = 0 # Copy in sound1 for source in range(0,getLength(sound1)): value = getSampleValueAt(sound1,source) setSampleValueAt(target,index,value) index = index + 1 # Copy in sound2 for source in range(0,getLength(sound2)): value = getSampleValueAt(sound2,source) setSampleValueAt(target,index,value) index = index + 1 return target def double(filename): source = makeSound(filename) target = makeSound(filename) targetIndex = 0 for sourceIndex in range(0, getLength(source), 2): setSampleValueAt(target,targetIndex, getSampleValueAt(source, sourceIndex)) targetIndex = targetIndex + 1 #Clear out the rest of the target sound -- it's only half full! for secondHalf in range(getLength(target)/2, getLength(target)): setSampleValueAt(target,targetIndex, 0) targetIndex = targetIndex + 1 blockingPlay(target) #instead of play return target def doubleFrequency(sound): # different from double in textbook length = getLength(sound) newlength = length/2 rate = getSamplingRate(sound) duration = (newlength/rate)+1 newsnd = makeEmptySoundBySeconds(int(duration)) for index in range(0, newlength-1): source = index * 2 value = getSampleValueAt(sound, source) sampobj = getSampleObjectAt(newsnd, index) setSample(sampobj, value) return newsnd def half(filename): source = makeSound(filename) target = makeSound(filename) sourceIndex = 0 for targetIndex in range(0, getLength(target)): value = getSampleValueAt(source,int(sourceIndex)) setSampleValueAt(target,targetIndex, value) sourceIndex = sourceIndex + 0.5 blockingPlay(target) #instead of play return target def halfFrequency(sound): # different from half in textbook length = getLength(sound) newlength = length rate = getSamplingRate(sound) duration = (newlength/rate)+1 newsnd = makeEmptySoundBySeconds(int(duration)) for dest in range(0, newlength): source = max(dest / 2, 1) value = getSampleValueAt(sound, source) sampobj = getSampleObjectAt(newsnd, dest) setSample(sampobj, value) return newsnd def shift(filename,factor): source = makeSound(filename) target = makeSound(filename) sourceIndex = 0 for targetIndex in range(0, getLength(target)): setSampleValueAt(target, targetIndex, getSampleValueAt(source,int(sourceIndex))) sourceIndex = sourceIndex + factor if sourceIndex > getLength(source): sourceIndex = 1 blockingPlay(target) # instead of play return target def sineWave( freq, amplitude ): # Make a blank sound buildSin = makeEmptySoundBySeconds(1) # Get sampling rate sr = getSamplingRate(buildSin) interval = 1.0/freq # Make sure floating point samplesPerCycle = interval * sr # Samples per cycle maxCycle = 2 * pi for pos in range(1,getLength(buildSin)+1): rawSample = sin((pos / samplesPerCycle) * maxCycle) sampleVal = int(amplitude*rawSample) setSampleValueAt(buildSin, pos, sampleVal) return buildSin def squareWave(freq, amplitude): # Make an empty sound of one second square = makeEmptySoundBySeconds(1) # Get sampling rate sr = getSamplingRate(square) seconds = 1 # One second sound duration # Build tools for this wave interval = 1.0 * seconds / freq # Make sure floating point # Creates floating point since interval is floating point samplesPerCycle = interval * sr # Samples per cycle # We need to switch every half-cycle samplesPerHalfCycle = int(samplesPerCycle / 2) sampleVal = amplitude i = 1 for pos in range(1,getLength(square)+1): # if end of a half-cycle if (i > samplesPerHalfCycle): # Reverse the amplitude sampleVal = sampleVal * -1 # And reinitialise the half-cycle counter i = 0 setSampleValueAt(square, pos, sampleVal) i = i + 1 return square def triangleWave(freq): # changed to return sound object # Make an empty sound of one second triangle = makeEmptySoundBySeconds(1) # Loudness at 6000 could be from 1 to 32767 amplitude = 6000 # sampling rate samplingRate = 22050 # play for i second seconds = 1 # seconds per cycle interval = 1.0 * seconds/freq # Make sure floating point samplesPerCycle = interval * samplingRate samplesPerHalfCycle = int(samplesPerCycle / 2) increment = int(amplitude / samplesPerHalfCycle) sampleVal = -amplitude i = 1 for s in range(1,samplingRate + 1): if (i > samplesPerHalfCycle): increment = increment * -1 i = 0 sampleVal = sampleVal + increment setSampleValueAt(triangle, s, sampleVal) i = i + 1 return triangle def sawtoothWave(freq, amplitude): # Make an empty sound of one second mySound = makeEmptySound(44100, 44100) # Get sampling rate sr = getSamplingRate(mySound) interval = 1.0/freq # Make sure floating point samplesPerCycle = interval * sr # Samples per cycle for pos in range(1,getLength(mySound)+1): t = pos / samplesPerCycle rawSample = 2 * ( t - floor( t + .5 ) ) sampleVal = int(amplitude*rawSample) setSampleValueAt(mySound, pos, sampleVal) return mySound def addSounds(sound1, sound2): for index in range(0, getLength(sound1)): s1Sample = getSampleValueAt(sound1, index) s2Sample = getSampleValueAt(sound2, index) setSampleValueAt(sound2, index, s1Sample + s2Sample) return sound2