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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Georgia Institute of Technology Introduction to Processing Digital Sounds.
Sound, Part 3. Multiple Echoes Here is a recipe to create multiple echoes: def echoes(sndfile, delay, num): s1 = makeSound(sndfile) ends1 = getLength(s1)
From Sound to Music CSC 161: The Art of Programming Prof. Henry Kautz 11/16/2009.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Sound, Part 2 Using range to manipulate samples by index number.
CS 102 Computers In Context (Multimedia)‏ 04 / 01 / 2009 Instructor: Michael Eckmann.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
1 String processing Samuel Marteck © A character in a string can be accessed by indicating its subscript, also called index. For instance, how.
Games and Simulations O-O Programming in Java The Walker School
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 6: Modifying Sounds Using Loops.
First Courses Workshop Day 1 Mark Guzdial College of Computing Georgia Institute of Technology
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 7: Modifying Samples in a Range.
CompSci Today’s topics Sound Upcoming ä Intellectual property ä Network analysis ä Security Reading.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 8: Making Sounds by Combining Pieces.
Chapter 7: Modifying Samples in a Range. Chapter Objectives.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
UsingSoundRanges-part21 Processing Sound Ranges part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 7: Modifying Samples in a Range.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CS 102 Computers In Context (Multimedia)‏ 03 / 30 / 2009 Instructor: Michael Eckmann.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 6: Modifying Sounds Using Loops.
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
ACM SIGCSE 2003: Multimedia Construction Projects Mark Guzdial College of Computing Georgia Institute of Technology
CS1315: Introduction to Media Computation Sound Encoding and Manipulation.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
“But it looks right”: Bugs in non-majors media programs Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Changing colors in an area Part 3: Chromakey.
Chapter 8: Modifying Samples in a Range. Chapter Objectives.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
Sounds in Jython 16 bit samples : -32,768 and 32,767 Sound object holds all samples file = pickAFile() sound = makeSound( file ) SoundSample objects store.
CS1315: Introduction to Media Computation Introduction to Programming.
Session 18 The physics of sound and the manipulation of digital sounds.
CS1315: Introduction to Media Computation Transforming pictures by index number.
COSC 1P02 Introduction to Computer Science 5.1 Cosc 1P02 Week 5 Lecture slides Psychiatrist to patient "You have nothing to worry about - anyone who can.
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions.
OCR Nationals: Unit 22 – Creating Sound using ICT A02 – Design an audio clip Planning the clip.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 What Does This Program Do? ACSL.  Frequently one must use or modify sections of another programmer’s code.  It is essential to be able to read and.
Working with Sounds Barb Ericson College of Computing Georgia Institute of Technology
1 CS 177 Week 4 Recitation Slides for Loop if statement and range.
Chapter 8: Making Sounds by Combining Pieces
Georgia Institute of Technology
Processing Sound Ranges part 1
Processing Sound Ranges part 3
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 6: Modifying Sounds Using Loops
Chapter 7: Modifying Samples in a Range
Processing Sound Ranges part 1
Week 3.
How sound works: Acoustics, the physics of sound
Chapter 7: Modifying Samples in a Range
A Media Computation Cookbook
FUNCTION PRACTICE !!!.
Processing Sound Ranges part 2
Processing Sound Ranges
CS 177 Week 3 Recitation Slides
CS 177 Week 9 Recitation Slides
Processing Sound Ranges part 3
CS1315: Introduction to Media Computation
CS1315: Introduction to Media Computation
Sounds in Jython 16 bit samples : -32,768 and 32,767
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

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

2 ANY QUESTIONS?

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 The length of this sound is 7

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 JES Functions about Sound getSampleValueAt(sound, index) Takes a sound and an index (an integer value), and returns the value of the sample (between and 32767) for that object. setSampleValueAt(sound, index, value) Takes a sound, an index, and a value (should be between 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 and 32767). setSampleValue(sample, value) setSample (sample, value) Takes a Sample object and a value (should be between 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.

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

Demonstrating working with samples 7 >>> print getLength(sound) >>> print getSamplingRate(sound) >>> 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

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 >>> print getSound(sample) Sound of length >>> print getSample(sample) 59 >>> setSample(sample,29) >>> print getSample(sample) 29

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)

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

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 =  In other words: amp = 32767/loudest Finally, amplify each sample by multiplying it by amp 11

Maxing (normalizing) the sound def normalize(sound): largest = 0 for s in getSamples(sound): largest = max(largest, getSampleValue(s)) amplification = / 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 Why but not 32767? 12 We’re making an assumption here that the maximum positive value is also the maximum negative value.

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

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

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

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

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

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

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

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 Final QUESTIONS???