1 / 25

CSE 8A Lecture 14

CSE 8A Lecture 14. Reading for next class: None (INTERM EXAM #3) Today’s topics: More Sounds ! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19) BRING HEADPHONES TO LAB THIS WEEK. Solo: (60 sec) Discuss: (2 min ) Group: (20 sec). How would we fill in this SampleSound [].

kenton
Download Presentation

CSE 8A Lecture 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 8A Lecture 14 • Reading for next class: None (INTERM EXAM #3) • Today’s topics: • More Sounds! • PSA 6 due tonight • PSA 7 (sounds) due next Monday (11/19) • BRING HEADPHONES TO LAB THIS WEEK

  2. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) How would we fill in this SampleSound[]

  3. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) How would we fill in this SampleSound[] 6

  4. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) According to Nyquist’s Theoremwhat is the minimum sampling rate? 1.5Hz 3Hz 6Hz 10,000Hz 20,000Hz

  5. The Sample Rate that the Sound class ASSUMES is 22 KHz:How long is a SoundSample[] in a Sound object of 1 second? • 22 elements • 11,000 elements • 22,000 elements • 44,000 elements • We can’t tell from the data provided

  6. Solo: 45 sec) • Discuss: (2 min) • Group: (20 sec) Write code thatreduces the value of the second half of the SoundsSample array by half noiseArray 3700 String fileName = FileChooser.pickAFile(); Sound noise = new Sound(fileName); SoundSample[] noiseArray = noise.getSamples(); <<< PICK SOME CODE >>> inti = 0; while (i < noiseArray.length) { SoundSample sample = noiseArray[i]; intval = sample.getValue(); sample.setValue(val/2); i++; } for (SoundSample sample: noiseArray) { intval= sample.getValue(); sample.setValue(val/2); } 3400 3600 3600 4000 4400 4400 6000 6800 8000 for (inti = noiseArray.length/2; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; intval= sample.getValue(); sample.setValue(val/2); }

  7. What does that code do • Makes a lower pitched sound during first half of play • Makes a quieter sound during first half of play • Makes a lower pitched sound during second half of play • Makes a quieter sound during second half of play • For each SoundSample element in the second half of the array it gets the Value and stores that in an int and then sets the Value with something that is half that

  8. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) What’s printed by this code?(assume calling object as shown) • 0,9 • 60,0 • 90,5 • 100,4 • None of the above public void guess() { SoundSample[] noiseArray = this.getSamples(); int a = 0, b = 0; for (int i=0;i<noiseArray.length;i++) { SoundSample sample = noiseArray[i]; intfoo = sample.getValue(); if (foo > a) { a = foo; b = i; } } System.out.println(a + "," + b); }

  9. 2 min group What does this code do? … int a=0, b=0; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; intfoo = sample.getValue(); if (foo > a) { a = foo; b = i; } }

  10. Why would we do that?: Normalizing… public void normalize() { SoundSample[] noiseArray = this.getSamples(); intmaxVal, maxIndex= 0; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; intval= sample.getValue(); if (val> maxVal) { maxVal= val; maxIndex= i; } } double factor = 32767.0 / maxVal; for (inti = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue((int) (sample.getValue() * factor)); } }

  11. CS Concept: Refactoring public void normalize() { SoundSample[] noiseArray = this.getSamples(); intmaxVal, maxIndex= 0; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; intval= sample.getValue(); if (val> maxVal) { maxVal= val; maxIndex= i; } } double factor = 32767.0 / maxVal; for (inti = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue((int) (sample.getValue() * factor)); } } Find the maximum value normalize

  12. CS Concept: Refactoring public intfindMax( SoundSample[] noiseArray ){ intmaxVal; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; intval= sample.getValue(); if (val> maxVal) { maxVal= val; } } return maxVal; } public void normalize() { SoundSample[] noiseArray = this.getSamples(); intmaxVal = findMax( noiseArray ); double factor = 32767.0 / maxVal; for (inti = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue((int) (sample.getValue() * factor)); } }

  13. Changing Pitch of a Sound • Play a recording of someone reading a sentence • Now play it so that it sounds “high pitched” • How long does it take to play the sound high pitched, compared to how long the original takes? • Now play it so that it sounds “low pitched” • How long does it take to play the sound low pitched, compared to how long the original takes?

  14. Raise the pitch of a Sound • Take only every nth sample from the original sound • The length of the sound sample is 1/n of the original, and all frequencies in the sound have been increased by a factor of n • Example, with n==2:

  15. Options to raisePitch • Create new Sound • V1) Of exact length needed for higher pitched sound • V2) Of same length as original with “silence” at end

  16. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) V1: Raise pitch by 2 and returna new sound half as long Write a method as part of the Sound class that returns a new Sound object whose pitch is double the calling object and whose length is half as long.Create the new sound by taking every other sample from the calling object. What is the method header for this method? public void raisePitch( Sound s ) public void raisePitch() public Sound raisePitch() public Sound raisePitch( Sound s )

  17. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) A start on raisePitch public Sound raisePitch() { SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); intnewPlace = 0; for (intorigI = 0; origI < original.length; origI+=2) { What object will this method return? this highP original higher void

  18. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) A start on raisePitch public Sound raisePitch() { SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); intnewPlace = 0; for (intorigI = 0; origI < original.length; origI+=2) { What do you think newPlace should be used for ? As an index into the original sample array As an index into the higher sample array To store the value to be copied from original To store the length of the higher sample array

  19. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) Complete the raisePitch method public Sound raisePitch() { SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); intnewPlace = 0; for (intorigI = 0; origI < original.length; origI+=2) {

  20. Complete V2: Create new sound of same length with 0 at end public Sound raiseP() { Sound highP = new Sound(this); SoundSample[] original = this.getSamples(); SoundSample[] higher = highP.getSamples(); intnewPlace = 0; for (intorigI = 0; origI < original.length; origI+=2) {

  21. Concept Summary • When you want to create a “new” object… • Call a “constructor” with new. • Look in the file of that class to find out what constructors are available • What parameters you can send • Don’t forget to return the object you created with a return statement! • When working with 2 (multiple) arrays • Sometimes you will want 2 index variables (to index into them) moving independently • If you are indexing “in synchrony” then use one index variable– it’s easier to understand!

  22. Exam 3 practice problem • Write a method in the Picture class that draws a 10x10 red square in the center of the calling object.

  23. Exam 3 practice problem • Write a method in the Picture class that takes a threshold value for green (an int). It then creates a new Picture and copies into this new Picture only those pixels whose green value is above the threshold. It leaves the rest of the Pixels in the new Pictures blank. It returns the new Picture.

  24. Exam 3 practice problem • Write a method in the Picture class that takes a target Picture object and copies the calling object’s picture upside down onto the target picture. Can you handle the case where the target is smaller than then calling object? How about where the target is larger?

  25. Load Phet simulator TODO • Reading for next class: None • Study for exam 3 • Start on PSA7 (Bring headphones to lab!)

More Related