ch08 - Chapter 8: Making Sounds by Combining Pieces 1 2 We...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8: Making Sounds by Combining Pieces 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2
Background image of page 2
We know that natural sounds are often the combination of multiple sounds. Adding waves in physics or math is hard. In computer science, it’s easy! Simply add the samples at the same index in the two waves: or srcSample in range(0, getLength (source)): destValue= getSampleValueAt (dest, srcSample) srcValue= getSampleValueAt (source, srcSample) setSampleValueAt (source, srcSample, srcValue+destValue) 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The first two are sine waves. The third is just the sum of the first two columns. a b a + b = c 4
Background image of page 4
Uses for adding sounds We can mix sounds We even know how to change the volumes of the two sounds, even over time (e.g., fading in or fading out) We can create echoes We can add sine (or other) waves together to create kinds of instruments/sounds that do not physically exist, but which sound interesting and complex 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
def addSoundInto(sound1, sound2): for sampleNmr in range(0, getLength (sound1)): sample1 = getSampleValueAt (sound1, sampleNmr) sample2 = getSampleValueAt (sound2, sampleNmr) setSampleValueAt (sound2, sampleNmr, sample1 + sample2) Notice that this adds sound1 and sound2 by adding sound1 into sound2 6
Background image of page 6
>>> c4=makeSound(getMediaPath("bassoon-c4.wav")) >>> e4=makeSound(getMediaPath("bassoon-e4.wav")) >>> g4=makeSound(getMediaPath("bassoon-g4.wav")) >>> addSoundInto(e4,c4) >>> play(c4) >>> addSoundInto(g4,c4) >>> play(c4) 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
def makeChord(sound1, sound2, sound3): for index in range(0, getLength (sound1)): s1Sample = getSampleValueAt (sound1, index) setSampleValueAt (sound1, index, s1Sample ) if index > 1000: s2Sample = getSampleValueAt (sound2, index - 1000) setSampleValueAt (sound1, index, s1Sample + s2Sample) if index > 2000: s3Sample = getSampleValueAt (sound3, index - 2000) setSampleValueAt (sound1, index, s1Sample + s2Sample + s3Sample) - Add in sound2 after 1000 samples - Add in sound3 after 2000 samples Note that in this version we’re adding into sound1! 8
Background image of page 8
def echo(sndFile, delay): s1 = makeSound(sndFile) s2 = makeSound(sndFile) for index in range(delay, getLength(s1)): echo = 0.6*getSampleValueAt(s2, index-delay) combo = getSampleValueAt(s1, index) + echo setSampleValueAt(s1, index, combo) play(s1) return s1 This creates a delayed echo sound, multiplies it by 0.6 to make it fainter and then adds it into the original sound. 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
def echoes(sndFile, delay, num): s1 = makeSound(sndFile) ends1 = getLength(s1) ends2 = ends1 + (delay * num) # convert samples secs = int(ends2/getSamplingRate(s1)) # to secs s2 = makeEmptySound(secs + 1) amp = 1.0 # make amplitude smaller for each echo for echoCount in range(0, num): amp = amp * 0.6 # amplitude 60% smaller each time for posns1 in range(0, ends1): posns2 = posns1 + (delay*echoCount) val1 = getSampleValueAt(s1, posns1)*amp val2 = getSampleValueAt(s2, posns2) setSampleValueAt(s2, posms2, val1 + val2) 10
Background image of page 10
work They have a huge memory with recordings of lots of
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2012 for the course CS 177 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 38

ch08 - Chapter 8: Making Sounds by Combining Pieces 1 2 We...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online