Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Georgia Institute of Technology Introduction to Processing Digital Sounds.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Written by: Dr. JJ Shepherd
Intro-Sound-part31 Introduction to Processing Digital Sounds part 3 Barb Ericson Georgia Institute of Technology Oct 2009.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro-Sound-part1 Introduction to Processing Digital Sounds part 1 Barb Ericson Georgia Institute of Technology Oct 2009.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Barbara Ericson Georgia Tech Sept 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Manipulating Pictures, Arrays, and Loops part 3
Georgia Institute of Technology
Processing Sound Ranges part 1
Processing Sound Ranges part 3
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Arrays versus ArrayList
Processing Sound Ranges part 1
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology June 2006
Introduction to Processing Digital Sounds part 3
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Introduction to Object-Oriented Programming in Alice
Introduction to Processing Digital Sounds
Processing Sound Ranges part 2
Processing Sound Ranges
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Creating and Modifying Text part 3
Barb Ericson Georgia Institute of Technology Oct 2005
CSC1401 Manipulating Pictures 2
Presentation transcript:

Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009

Intro-Sound-part22 Learning Goals Introduce Sound manipulation –Arrays –Declaring variables –Sending objects messages –Iteration (Loops) –Writing methods

Intro-Sound-part23 Changing the Value of a Sound Sample You can set the value of a SoundSample –sample.setValue(value); –This will change the value in the Sound object as well So how would you change the value to the original value * 2? SoundSample sample = sampleArray[0]; sample.setValue(sample.getValue() * 2);

Intro-Sound-part24 Doubling all the Sound Values You could change each SoundSample by hand –There are 8808 SoundSamples in croak.wav –Do you really want to do that? How long would it take you? Let’s let the computer do it in a loop –For-each –While –For

Intro-Sound-part25 For-Each Loop (Java 5.0) For each of the elements in a collection of objects do the body of the loop –Each time through the loop the variableName will refer to a different object in the collection for (type variableName : collection) { // statement to repeat }

Comparison to Alice In Alice you had each rockette kick up her leg one at a time by having a list of rockettes and telling each one in the list to do the action –This is similar to a Java for-each loop Intro-Sound-part26

7 For-Each Loop to Process Sound Samples SoundSample[] sampleArray = this.getSamples(); int value = 0; for (SoundSample sample : sampleArray) { value = sample.getValue(); // get the value sample.setValue(value * 2); // set the value }

Intro-Sound-part28 Increase Volume with For-Each Loop public void increaseVolume() { SoundSample[] sampleArray = this.getSamples(); int value = 0; // value at sample // loop through SoundSample objects for (SoundSample sample : sampleArray) { value = sample.getValue(); // get the value sample.setValue(value * 2); // set the value }

Intro-Sound-part29 Testing increaseVolume String file = FileChooser.getMediaPath("gettysburg10.wav"); Sound soundObj = new Sound(file); soundObj.play(); soundObj.explore(); soundObj.increaseVolume(); soundObj.play(); soundObj.explore();

Intro-Sound-part210 Decrease Volume Exercise Write a method to decrease the volume of the sound –decreaseVolume() –Divide each value by 2 What parts need to change from the last method? –Only the calculation of the new value Try it: Sound s = new Sound( FileChooser.getMediaPath("gettysburg10.wav")); s.explore(); s.decreaseVolulme(); s.explore();

Intro-Sound-part211 What Does For-Each Do? It uses each element in the array one and only one time –So it needs to keep track of the current index –It needs to check that the current index is less than the length of the array And stop when they are equal –It needs to pull out the item at the index –It needs to increment the index after the loop body has executed This is explicit in a while loop

Intro-Sound-part212 While Loop Loops while a boolean (true or false) test is true –When the test is false execution continues with the first statement after the loop while(test) { // statements to be done while the test is true } –To use a while loop you may need to declare variables before the loop and change them in the loop

Alice While Loop In Alice we used a while loop to have a shark chase a goldfish Intro-Sound-part213

Intro-Sound-part214 While Loop to Process Sound Samples int index = 0; // starting index SoundSample sample = null; // current sample obj int value = 0; // value at sample while (index < sampleArray.length) { sample = sampleArray[index]; // get current obj value = sample.getValue(); // get the value sample.setValue(value * 2); // set the value index++; // increment index }

Intro-Sound-part215 Shortcuts for Common Operations In programming you often need to add one to a value index = index + 1; You may use the shortcut index++; If you wanted to subtract 1 instead index = index – 1; index--;

Intro-Sound-part216 Increase Volume with While Loop public void increaseVolumeWhile() { SoundSample[] sampleArray = this.getSamples(); // get array int index = 0; // starting index SoundSample sample = null; // current sample obj int value = 0; // value at sample // loop through SoundSample objects while (index < sampleArray.length) { sample = sampleArray[index]; // get current obj value = sample.getValue(); // get the value sample.setValue(value * 2); // set the value index++; // increment index }

Intro-Sound-part217 Testing increaseVolume String file = FileChooser.getMediaPath("gettysburg10.wav"); Sound soundObj = new Sound(file); soundObj.play(); soundObj.explore(); soundObj.increaseVolume(); soundObj.play(); soundObj.explore();

Intro-Sound-part218 Tracing Execution The index is set to 0 The value is set to the value in the array at that index (59) The sample value at the current index is set to 2 * value The index changes to the next index (1) We check if the index is less than the length of the array and –If so do the loop again –Else jump to the first statement after the loop

Intro-Sound-part219 Memory versus Disk When we read from a file we read from disk into memory –Computers only do calculations on memory We change the values in memory The file on the disk hasn’t changed To save our new sound we need to write a file to the disk –soundObj.write(fileName);

Intro-Sound-part220 Decrease Volume Exercise Copy decreaseVolume and call the new method decreaseVolumeWhile –Modify it to use a while loop Try it: Sound s = new Sound( FileChooser.getMediaPath("gettysburg10.wav")); s.explore(); s.decreaseVolulmeWhile(); s.explore();

Intro-Sound-part221 While Loop versus For Loop It is easy to make mistakes when you use a while loop for looping a set number of times –Forget to declare variables before the loop –Forget to increment the variables in the loop before the next test Programmers use a For loop when the number of times to loop is known –And a while loop when you don’t know

Intro-Sound-part222 For Loop A for loop allows you to declare and initialize variables, specify the test, and specify the way the variables change –All in one place –But, they still happen in the usual place for(int index = 0; index < sampleArray.length; index++) { }

Intro-Sound-part223 Comparison of While and For Loops int index = 0; while (index < pixelArray.length) { statements. index++; } for (int i=0; i < pixelArray.length; i++) { statements. }

Alice For Loop In Alice we used a general for loop to cause a bunny to hop a set number of times Intro-Sound-part224

Intro-Sound-part225 Increase Volume with a For Loop public void increaseVolumeFor() { SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; int value = 0; // loop through all the samples in the array for (int index = 0; index < sampleArray.length; index++) { sample = sampleArray[index]; value = sample.getValue(); sample.setValue(value * 2); }

Intro-Sound-part226 Modify decreaseVolume Exercise Copy decreaseVolume and create a decreaseVolumeFor –Modify it to use a for loop Comment out declaration of the index Comment out the increment of the index at the end of the loop Comment out the while and put in a for loop Test it to make sure it still works –String file = FileChooser.getMediaPath(“gettysburg10.wav“); –Sound soundObj = new Sound(file); –soundObj.explore(); –soundObj.decreaseVolumeFor(); –soundObj.explore();

Intro-Sound-part227 General Change Volume Method The methods increaseVolume and decreaseVolume are very similar –They multiply the current sound values by a given amount To change this you would need to modify the method and compile –The methods would be more reusable if we pass in the amount to multiply the current sound values by As a parameter to the method

Intro-Sound-part228 General changeVolume method public void changeVolume(double factor) { SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; int value = 0; // loop through all the samples in the array for (int i = 0; i < sampleArray.length; i++) { sample = sampleArray[i]; value = sample.getValue(); sample.setValue((int) (value * factor)); }

Intro-Sound-part229 Methods calling Methods One method can call another –Change decreaseVolume and increaseVolume to call changeVolume You can use –this.changeVolume To invoke the method on the current object Or you can use –changeVolume The this is implicit and will be added by the compiler

Intro-Sound-part230 Summary Use a for-each loop to loop through all the elements of an array Use a while loop to execute a statement or block of statements while a boolean test is true –Declare and initialize variables before the loop –And modify variables in the loop Use a for loop to execute a statement or block of statements while a boolean test is true –And specify how to initialize loop variables and change them after each loop in one place Use parameters to make methods more reusable