CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Georgia Institute of Technology Introduction to Processing Digital Sounds.
Written by: Dr. JJ Shepherd
Intro-Sound-part31 Introduction to Processing Digital Sounds part 3 Barb Ericson Georgia Institute of Technology Oct 2009.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Computer Science 1620 Loops.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
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.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
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.
Introduction to Java Java Translation Program Structure
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 7 User-Defined Methods.
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
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
An Introduction to Java – Part I, language basics
Lecture Notes – Week 3 Lecture-2
Arrays versus ArrayList
<INSERT_WITTY_QUOTE_HERE>
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 Processing Digital Sounds
Processing Sound Ranges part 2
Week 4 Lecture-2 Chapter 6 (Methods).
Processing Sound Ranges
More on Creating Classes
Processing Sound Ranges part 3
Presentation transcript:

CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3

Learning Goals Review the while loop Increase and decrease the volume of a sound Trace a method Review the for loop Make a method more reusable by passing in a parameter

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

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 }

Increase Volume with While Loop public static void increaseVolume(Sound source) { SoundSample[] sampleArray = source.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 }

Testing increaseVolume String file = FileChooser.getMediaPath(“gettysburg10.wav“); Sound soundObj = new Sound(file); soundObj.play(); soundObj.explore(); soundObj.increaseVolume(); soundObj.play(); soundObj.explore();

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

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);

Decrease Volume Exercise Write a method to decrease the volume of the sound  decreaseVolume()  Multiply each value by 0.5 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();

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

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++) { }

Increase Volume with a For Loop public void increaseVolume(Sound source) { SoundSample[] sampleArray = source.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); }

Modify decreaseVolume Exercise Change decreaseVolume 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.decreaseVolume();  soundObj.explore();

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

General changeVolume method public void changeVolume(Sound source, double factor) { SoundSample[] sampleArray = source.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)); }

Methods calling Methods One method can call another  Change decreaseVolume and increaseVolume to call changeVolume You can use  source.changeVolume To invoke the method on the source object

Summary Use a while loop to execute a statement or block of statements while a boolean test is true  Often 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