90 likes | 292 Views
Sounds in Jython. 16 bit samples : -32,768 and 32,767 Sound object holds all samples file = pickAFile() sound = makeSound( file ) SoundSample objects store a single sample Two ways of playing sounds play (sound ) #spawns process to play sound blockingPlay( sound ) #no spawning.
E N D
Sounds in Jython • 16 bit samples : -32,768 and 32,767 • Sound object holds all samplesfile = pickAFile()sound = makeSound( file ) • SoundSample objects store a single sample • Two ways of playing sounds • play (sound ) #spawns process to play sound • blockingPlay( sound ) #no spawning Slides available at:courses.cs.vt.edu/~cs1004/cs4hs/
Sound Samples • Samples retrieved either as: • collection/array of SoundSample objects#collection____________________________________for sample ingetSamples( sound):setSampleValue( sample, getSampleValue(sample)*2) #array_______________________________________samples = getSamples( sound)for s in range(0, getNumSamples(sound)): half = getSampleValue( samples[s] ) / 2setSampleValue( samples[s], half ) • single SoundSample objectssample = getSampleObjectAt( sound, 100 )
Normalize a Sound • Normalizing sounds involves making them as loud as possibledef driver():file = pickAFile() sound = makeSound( file )blockingPlay( sound ) normalize( sound )blockingPlay( sound )def normalize( source ): • large = 0 • for snd ingetSamples(source): • large = max(large, abs(getSample(snd)) ) • scaler = 32767.0 / large • for snd ingetSamples(source): • loud = scaler*getSample(snd) • setSample(snd, loud) Code available at:courses.cs.vt.edu/~cs1004/cs4hs/ 32767 -32767
Reverse a sound • In late 1969 a rumor started circulating that Paul McCartney of the Beatles had died in 1966 and had been replaced by a double. To support this urban legend many reported that if one played the song "Revolution 9" from the white album backwards one could hear "Turn me on, dead man". • Reverse the last 15 seconds of the song "Revolution 9" from the white album: rev9.wav • Modify the normalize code by adding a reverse() function that swaps all the samples in a sound, (first & last, second & next-to-last, etc.). • Extra-credit: do not modify the original sound.
Fade Outs • Create a new sound from copies of a sound, where the copies fade out with a delay between the copies: def fadeOut( snd1, delay, num ) : s1rate = getSamplingRate(snd1) ends1 = getLength( snd1 ) snd2 = makeEmptySound( ends1 * (num + 1) + num * int(delay * s1rate) ) ends2 = getLength( snd2 ) fadeAmplitude = 1.0 posn2 = 0 for echoCount in range( 0, num + 1 ) : for posn1 in range( 0, ends1-1 ) : values1 = getSampleValueAt( snd1, posn1) * fadeAmplitude setSampleValueAt( snd2, posn2, values1 ) posn2 = posn2 + 1 if (echoCount < num): for pause in range(0, int(s1rate * delay)): setSampleValueAt( snd2, posn2, 0 ) posn2 = posn2 + 1 fadeAmplitude = fadeAmplitude * 0.6 # each fade is 60% of previous return snd2 Code available at:courses.cs.vt.edu/~cs1004/cs4hs/
Blending Sounds • Creates a new sound from existing sounds. • Corresponding samples from the sounds are blended together. • Blending involves adding percentages (50%) of each sample together. • Lab/class activity: download a reading of the Gettysburg address and an instrumental recording of God Bless America and create a blending of the two, (i.e. sound mix).
ALVIN! Lurch? • Sounds can be speeded up or slowed down by under-sampling or over-sampling. • Here is a famous line from the classic Paul Newman movie "Cool Hand Luke" as spoken by the inimitable character actor Strother Martin: FailureToCommunicate.wav • Code a function to speed up the sound by creating a new sound half as long as the original using every other sample from the original. • Code a function to slow down the sound by creating a new sound twice as long as the original using every sample from the original twice.
Interpolation • Under-sampling or over-sampling are just specific instances of linear interpolation (the same as nearest neighbor in image scaling). • Given a length (sampling) factor, e.g., 0.80, 1.15, return a new sound of the new length and sampling using linear interpolation. • Linear Interpolation: x For any new sound sample s the corresponding sample x from the original sound = s / (length of new sound) * length of original sound
Sound Projects • Simple Telephony • Given sound clips for each spoken digit, combine them to speak any given number. • Sound Steganography • Hide a message inside a sound file • http://www.cs.uic.edu/~i101/projects/proj4.html • Mozart’s Dice Game • Create a waltz by pasting together pre-composed minuet & trio clips. • http://www.cs.cornell.edu/courses/cs1110/2008fa/assignments/a6/a6.html