Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7 - Friday CS 121.

Similar presentations


Presentation on theme: "Week 7 - Friday CS 121."— Presentation transcript:

1 Week 7 - Friday CS 121

2 Quiz

3 Last time What did we talk about last time? Array examples Sound

4 Questions?

5 Project 3

6 StdAudio Class

7 Purpose of the StdAudio class
Audio data on Windows machines is sometimes stored in a WAV file A WAV file is much simpler than an MP3 because it has no compression Even so, it contains two channels (for stereo) and can have many different sample rates and formats for recording sound The StdAudio class lets you read and write a WAV file easily and always deal with a single array of sound, sampled at 44,100 Hz

8 StdAudio methods Everything you’d want to do with sound:
To do interesting things, you have to manipulate the array of samples Make sure you added StdAudio.java to your project before trying to use it Method Use double[] read(String file) Read a WAV file into an array of doubles void save(String file, double[] input) Save an array of doubles (samples) into a WAV file void play(String file) Play a WAV file void play(double[] input) Play an array of doubles (samples)

9 StdAudio example Let’s load a file into an array:
If the song has these samples: Perhaps samples will contain: String file = "song.wav"; double[] samples = StdAudio.read(file); -.9 -.7 -.6 -.4 -.2 -.1 .1 .2 .3 .4 .5 .6

10 StdAudio example With the audio samples loaded into the array named samples, we can play them as follows: StdAudio.play(samples);

11 Generating sound with StdAudio
Or, we could generate sound from scratch with StdAudio This example from the book creates 1 second of the pitch A440: double[] sound = new double[StdAudio.SAMPLE_RATE + 1]; for( int i = 0; i < sound.length; i++ ) sound[i] = Math.sin(2 * Math.PI * i * 440 / StdAudio.SAMPLE_RATE); StdAudio.play(sound);

12 Breaking a sound into parts
What if we wanted to play the second half of a sound followed by the first half? I know, why would we want to do that? double[] samples = StdAudio.read(file); double[] switched = new double[samples.length]; for(int i = 0; i < samples.length/2; i++ ) switched[i + samples.length/2] = samples[i]; for(int i = samples.length/2; i < samples.length; i++ ) switched[i - samples.length/2] = samples[i]; StdAudio.play(switched);

13 Lab 7

14 Upcoming

15 Next time… StdDraw

16 Reminders Keep reading Chapter 6 of the textbook
Read Project 3 carefully and get started It's a harder project than the previous two!


Download ppt "Week 7 - Friday CS 121."

Similar presentations


Ads by Google