cse 8a lecture 14 reading for next class: none (interm exam #3) todays topics: more sounds! psa 6...

25
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

Upload: sabrina-arnold

Post on 19-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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

Page 2: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

How would we fill in this SampleSound[]

1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

Page 3: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

How would we fill in this SampleSound[]

1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

6

Page 4: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

According to Nyquist’s Theoremwhat is the minimum sampling rate?

1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

A.1.5HzB.3HzC.6HzD.10,000HzE.20,000Hz

Page 5: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

The Sample Rate that the Sound class ASSUMES is 22 KHz:

How long is a SoundSample[] in a Sound object of 1 second?

A. 22 elements

B. 11,000 elements

C. 22,000 elements

D. 44,000 elements

E. We can’t tell from the data provided

Page 6: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

Write code that reduces the value of the second half of the SoundsSample array by half

3400 40003600 3600 4400 4400 6000 6800 8000

String fileName = FileChooser.pickAFile();Sound noise = new Sound(fileName);SoundSample[] noiseArray = noise.getSamples();<<< PICK SOME CODE >>>

for (SoundSample sample: noiseArray){ int val = sample.getValue(); sample.setValue(val/2);}

for (int i = noiseArray.length/2; i < noiseArray.length; i++){ SoundSample sample = noiseArray[i]; int val = sample.getValue(); sample.setValue(val/2);}

int i = 0;while (i < noiseArray.length){ SoundSample sample = noiseArray[i]; int val = sample.getValue(); sample.setValue(val/2); i++;}

1) Solo: 45 sec)2) Discuss: (2 min)3) Group: (20 sec)

3700

noiseArray

Page 7: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

What does that code do

A. Makes a lower pitched sound during first half of play

B. Makes a quieter sound during first half of play

C. Makes a lower pitched sound during second half of play

D. Makes a quieter sound during second half of play

E. 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

Page 8: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

What’s printed by this code?(assume calling object as shown)

A. 0,9

B. 60,0

C. 90,5

D. 100,4

E. None of the above

1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

public void guess(){ SoundSample[] noiseArray = this.getSamples(); int a = 0, b = 0; for (int i=0;i<noiseArray.length;i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); if (foo > a) { a = foo; b = i; } } System.out.println(a + "," + b);}

Page 9: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

What does this code do?1) 2 min group

…int a=0, b=0;for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); if (foo > a) { a = foo; b = i; } }

Page 10: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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

Page 11: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

CS Concept: Refactoringpublic void normalize(){ SoundSample[] noiseArray = this.getSamples(); int maxVal, maxIndex = 0; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int val = sample.getValue(); if (val > maxVal) { maxVal = val; maxIndex = i; } } double factor = 32767.0 / maxVal; for (int i = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue((int) (sample.getValue() * factor)); }}

Find the maximum value

normalize

Page 12: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

CS Concept: Refactoringpublic int findMax( SoundSample[] noiseArray ) { int maxVal; for (int i=0; i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int val = sample.getValue(); if (val > maxVal) { maxVal = val; } } return maxVal;}

public void normalize() { SoundSample[] noiseArray = this.getSamples(); int maxVal = findMax( noiseArray ); double factor = 32767.0 / maxVal; for (int i = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue((int) (sample.getValue() * factor)); }}

Page 13: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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?

Page 14: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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:

Page 15: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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

Page 16: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

V1: Raise pitch by 2 and returna new sound half as long

1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

Write a method as part of the Sound class that returns a new Sound objectwhose 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?

A.public void raisePitch( Sound s )B.public void raisePitch()C.public Sound raisePitch()D.public Sound raisePitch( Sound s )

Page 17: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

A start on raisePitch1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

public Sound raisePitch(){ SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2) {

What object will this method return?A.thisB.highPC.originalD.higherE.void

Page 18: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

A start on raisePitch1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

public Sound raisePitch(){ SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2) {

What do you think newPlace should be used for ?A. As an index into the original sample arrayB. As an index into the higher sample arrayC.To store the value to be copied from originalD.To store the length of the higher sample array

Page 19: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

Complete the raisePitch method1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)

public Sound raisePitch(){ SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2) {

Page 20: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2) {

Page 21: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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!

Page 22: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

Exam 3 practice problem• Write a method in the Picture class that draws a

10x10 red square in the center of the calling object.

Page 23: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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.

Page 24: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

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?

Page 25: CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Todays topics: More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)

TODO

• Reading for next class: None• Study for exam 3• Start on PSA7 (Bring headphones to lab!)