Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring.

Similar presentations


Presentation on theme: "1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring."— Presentation transcript:

1 1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring

2 2 ANY QUESTIONS?

3 Let’s remember Sound We store sounds as array of integer values  Each value is stored as 16 bits  The range is from –2 (16-1) to +2 (16-1) – 1 i.e from –32768 to 32767 3 5928-1230-327682045632767 0123456 The length of this sound is 7

4 4 JES Functions about Sound pickAFile() Let the user pick a sound file (.wav file) makeSound(file) Takes a filename as input, reads the file, and creates a sound from it. Returns the sound. play(sound) Plays a sound provided as input. No return value. getLength(sound) Takes a sound as input and returns the number of samples in that sound. getSamplingRate(sound) Takes a sound as input and returns the number representing the number of samples in each second for the sound. getSamples(sound) Takes a sound as input and returns the Samples in that sound. writeSoundTo(sound, path) Takes a sound and a filename (a string) and writes the sound to that file as a WAV file. (Make sure that the filename ends in '.wav' if you want the operating system to treat it right.)

5 5 JES Functions about Sound getSampleValueAt(sound, index) Takes a sound and an index (an integer value), and returns the value of the sample (between -32768 and 32767) for that object. setSampleValueAt(sound, index, value) Takes a sound, an index, and a value (should be between -32768 and 32767), and sets the value of the sample at the given index in the given sound to the given value. getSampleObjectAt(sound, index) Takes a sound and an index (an integer value), and returns the Sample object at that index. getSampleValue(sample) getSample (sample) Takes a Sample object and returns its value (between -32768 and 32767). setSampleValue(sample, value) setSample (sample, value) Takes a Sample object and a value (should be between -32768 and 32767), and sets the sample to that value. getSound(sample) Takes a Sample object and returns the Sound that it belongs to. A sample object remembers its sound, so if you change the sample object, the sound gets changed.

6 Demonstrating Working with Sound in JES 6 >>> filename=pickAFile() >>> print filename /Users/guzdial/mediasources/preamble.wav >>> sound=makeSound(filename) >>> print sound Sound of length 421109 >>> samples=getSamples(sound) >>> print samples Samples, length 421109 >>> print getSampleValueAt(sound,1) 36 >>> print getSampleValueAt(sound,2) 29 >>> explore(sound)

7 Demonstrating working with samples 7 >>> print getLength(sound) 220568 >>> print getSamplingRate(sound) 22050.0 >>> print getSampleValueAt(sound,220567) 68 >>> print getSampleValueAt(sound,220568) I wasn't able to do what you wanted. The error java.lang.ArrayIndexOutOfBoundsException has occurred Please check line 0 of >>> print getSampleValueAt(sound,1) 36 >>> setSampleValueAt(sound,1,12) >>> print getSampleValueAt(sound,1) 12

8 Example: Changing Samples 8 >>> soundfile=pickAFile() >>> sound=makeSound(soundfile) >>> sample=getSampleObjectAt(sound,1) >>> print sample Sample at 1 value at 59 >>> print sound Sound of length 387573 >>> print getSound(sample) Sound of length 387573 >>> print getSample(sample) 59 >>> setSample(sample,29) >>> print getSample(sample) 29

9 Increasing and Decreasing the Volume 9 def increaseVolume(sound): for sample in getSamples(sound): value = getSampleValue(sample) setSampleValue(sample,value * 2) >>> f=pickAFile() >>> s=makeSound(f) >>> increaseVolume(s) def decreaseVolume(sound): for sample in getSamples(sound): value = getSampleValue(sample) setSampleValue(sample,value * 0.5) >>> f=pickAFile() >>> s=makeSound(f) >>> decreaseVolume(s)

10 Recognize some similarities? def decreaseVolume(sound): for sample in getSamples(sound): value = getSampleValue(sample) setSampleValue(sample, value*0.5) def increaseVolume(sound): for sample in getSamples(sound): value = getSampleValue(sample) setSampleValue(sample, value*2) def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) def increaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*1.2) 10

11 Maximizing (Normalizing) sound First, figure out the loudest sound (largest sample). Next, figure out how much we have to increase/decrease that sound to fill the available space  We want to find the amplification factor amp, where amp * loudest = 32767  In other words: amp = 32767/loudest Finally, amplify each sample by multiplying it by amp 11

12 Maxing (normalizing) the sound def normalize(sound): largest = 0 for s in getSamples(sound): largest = max(largest, getSampleValue(s)) amplification = 32767.0 / largest print "Largest sample value in original sound was", largest print "Amplification multiplier is", amplification for s in getSamples(sound): louder = amplification * getSampleValue(s) setSampleValue(s, louder) This loop finds the loudest sample This loop amplifies the sound Note that the largest sample will be updated with 32767 Why 32767.0 but not 32767? 12 We’re making an assumption here that the maximum positive value is also the maximum negative value.

13 Avoiding clipping Why are we being so careful to stay within range? What if we just multiplied all the samples by some big number and let some of them go over 32,767? The result then is clipping  Clipping: The awful, buzzing noise whenever the sound volume is beyond the maximum that your sound system can handle. 13

14 Processing only part of the sound What if we wanted to increase or decrease the volume of only part of the sound?  use a range() function with our for loop Let’s increase volume by sample index (using range) 14 def increaseVolumeByRange(sound): for sampleNumber in range(0, getLength(sound)): value = getSampleValueAt(sound, sampleNumber) setSampleValueAt(sound, sampleNumber, value * 2) def increaseVolume(sound): for sample in getSamples(sound): value = getSample(sample) setSample(sample,value * 2) SAME AS

15 Modify different sound sections Here we increase the volume in the first half, and decrease it in the second half. def increaseAndDecrease(sound): length = getLength(sound) for index in range(0, length/2): value = getSampleValueAt(sound, index) setSampleValueAt(sound, index, value*2) for sampleIndex in range(length/2, length): value = getSampleValueAt(sound, index) setSampleValueAt(sound, index, value*0.2) 15

16 Splicing Sounds Splicing gets its name from literally cutting and pasting pieces of magnetic tape together The easiest kind of splicing is when the component sounds are in separate files. All we need to do is copy each sound, in order, into a target sound. Here’s a recipe that creates the start of a sentence, “Guzdial is …” (You may complete the sentence.) 16

17 Splicing whole sound files def merge(): guzdial = makeSound(getMediaPath("guzdial.wav")) isSound = makeSound(getMediaPath("is.wav")) target = makeSound(getMediaPath("sec3silence.wav")) index = 0 for source in range(0, getLength(guzdial)): value = getSampleValueAt(guzdial, source) setSampleValueAt(target, index, value) index = index + 1 for source in range(0, int(0.1*getSamplingRate(target))): setSampleValueAt(target, index, 0) index = index + 1 for source in range(0, getLength(isSound)): value = getSampleValueAt(isSound, source) setSampleValueAt(target, index, value) index = index + 1 normalize(target) play(target) return target 17

18 How it works Creates sound objects for the words “Guzdial”, “is” and the target silence Set target’s index to 0, then let each loop increment index and end the loop by leaving index at the next empty sample ready for the next loop The 1 st loop copies “Guzdial” into the target The 2 nd loop creates 0.1 seconds of silence The 3 rd loop copies “is” into the target Then we normalize the sound to make it louder 18

19 Reversing Sounds We can also modify sounds by reversing them def reverse(source): target = makeEmptySound(getLength(source)) sourceIndex = getLength(source) - 1 # start at end for targetIndex in range(0, getLength(target)): value = getSampleValueAt(source, sourceIndex) setSampleValueAt(target, targetIndex, value) sourceIndex = sourceIndex - 1 # move backwards return target 19

20 Mirroring We can mirror sounds in exactly the same way we mirrored pictures def mirrorSound(sound): len = getLength(sound) mirrorpoint = len/2 for index in range(0, mirrorpoint): left = getSampleObjectAt(sound, index) right = getSampleObjectAt(sound, len-index-1) value = getSampleValue(left) setSampleValue(right, value) return (sound) 20

21 21 Final QUESTIONS???


Download ppt "1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring."

Similar presentations


Ads by Google