Download presentation
Presentation is loading. Please wait.
Published byEunice Blair Modified over 9 years ago
1
Week 7 - Friday
2
What did we talk about last time? Array examples Sound
5
We can represent a deck of cards as an array of 52 items One easy way is to make each item a String giving the name of the card We can extend last time's lab and store each of these names in an array
6
Swapping the values of two variables is a fundamental operation in programming It is going to become more important in arrays because now the order of variables has become important The simplest way to swap two variables involves using a third variable as a temporary location
7
Here is an example of swapping two String s indexed i and j in an array of String s called array int i = in.nextInt(); int j = in.nextInt(); String temp = array[i]; array[i] = array[j]; array[j] = temp; int i = in.nextInt(); int j = in.nextInt(); String temp = array[i]; array[i] = array[j]; array[j] = temp;
8
Using the swap code, we can do a random shuffling of a deck To do so, we go through each element of the array, and randomly swap it with any of the later elements for( int i = 0; i < n; i++ ) { exchange = i + (int)(Math.random() * (n - i)); temp = deck[i]; deck[i] = deck[exchange]; deck[exchange] = temp; } for( int i = 0; i < n; i++ ) { exchange = i + (int)(Math.random() * (n - i)); temp = deck[i]; deck[i] = deck[exchange]; deck[exchange] = temp; }
10
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
11
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 MethodUse double[] read(String file) Read a WAV file into an array of double s void save(String file, double[] input) Save an array of double s (samples) into a WAV file void play(String file) Play a WAV file void play(double[] input) Play an array of double s (samples)
12
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); String file = "song.wav"; double[] samples = StdAudio.read(file); -.9-.7-.6-.4-.2-.1.1.2.3.4.5.6.5.4.3.20-.2-.4
13
With the audio samples loaded into the array named samples, we can play them as follows: StdAudio.play(samples);
14
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); 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);
15
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); 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);
18
StdDraw
19
Keep reading Chapter 6 of the textbook Read Project 3 carefully and get started It's a harder project than the previous two! Office hours from 3:30-5pm canceled this today due to travel
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.