As a really compelling example of the utility,

being able to define our own functions,

we're going to talk about digital audio.

We'll give a quick crash course in sound.

This is not a physics course,

and we'll do the minimal amount in terms of what sound actually is.

We'll say it's the perception of the vibration of molecules.

We're going to work with musical tones,

which are steady periodic sounds.

A pure tone is a sine wave, a sinusoidal waveform.

Now, in music, it's the frequency of the sine wave that makes the different note.

So, 440 Hertz is concert A,

and there's a 12 notes in our scale,

and they're spaced logarithmically.

What that means is,

that so concert A is 440 frequency,

440 peaks per second.

Then the 12 things or 12 notes are space logarithmically,

that means their frequency is 440 times two to the i over 12,

where i is the index of a note above concert A.

So, B is for 440 times two to the one sixth power and so forth.

By the time we get up to the 12th note,

which is an octave higher,

than that's for 440 times two to the 12th over 12, or it's double.

So, if we double the frequency of concert A,

we get 440 to 880,

and we get a note that's an octave higher.

Those are the waveforms of the peaks occurring more frequently.

So, that's the basic idea of how we get music,

we produce sinusoidal waveforms,

and that vibrates molecules that we hear.

All right, so what we're going to do with digital audio,

is represent a wave by sampling it at regular intervals,

and saving the values in an array.

Just the same way as we did when we plotted a function in the previous lecture.

So, this is concert A taking 5,512 samples per second,

and so one-fortieth of a second is just 137 samples.

In the same way,

as in the previous lecture when we plotted a function,

if we take more samples,

we get a more accurate depiction.

So this is 275 samples per second,

this is I'm sorry for a 40th of a second 275.

This one's 22,000 samples per second,

so this 500 in a 40th of a second.

This one's 44,100, and you can see

there's a lot of samples but it's a pretty good representation of the wave.

This is the standard that's used for digital music and CDs, for example.

So, that's the one that we're going to use.

Now there's a lot of points,

but we have a computer,

and so we can represent a lot of points,

we have arrays and we have functions,

and you'll see that even though there's a lot of data,

it's not difficult to write programs to manipulate it and therefore process sound.

So, sound is nothing more than arrays of double values,

these samples, and we're going to write programs to manipulate them.

So, let's take a look at how that works.

So, the main thing,

is our Standard Audio library,

which is like our Standard Draw library and others,

providing output of your program.

We developed it for this course,

but now it's available,

from the standard download of our core software.

So what we do is,

the main function in this library is to play a sound wave.

It's given an array of double values,

and it plays the sound wave on your computer's audio output.

There's a standard file format called the.wav file format,

that you can also store arrays of doubles,

the library converts those into the standard format.

So, there's a method play,

to just play a given file.

There's a method to just play the sound wave that's in the array.

Now, so for a second that size of that array is 44,000,

so for 10 seconds,

it's half million, double values.

So, if you have a lot of sound,

you're going to have a lot of data and these arrays are going to be big.

But that's fine, you have a fast computer.

You can play 1/44100th of a second.

You can save to a.wav file,

you can read from a.wav file,

but what our library does is allow you to

hear the results of your program that manipulates sound.

It's an output library for sound,

and just a few methods.

So, let's take a look at how to use it.

Again, this is the "Hello World" the simplest program can play some sound.

So, we're going to use a function,

that takes as argument,

the pitch or the Hertz.

So, it's 440 for concert A or 880 for an octave higher and a duration,

so how long do you want to play that and that's in seconds.

So for 1 second, it's 44100 samples.

For 10 seconds is 10 times that, and so forth.

So N, is the number of samples that we have to take.

It's also the size of our array.

So, we build an array that's of size N,

well N plus one to handle the endpoints.

For i from 0, through N,

what we do is, we sample the sine wave.

The sine wave we have to sample,

you can check this math is not a big deal,

we want to take whatever the Hertz is,

and then multiply that by two Pi divided by 44100,

and then for each integer i,

that's the point that we want to get.

So, i is along this scale,

and that's the function that we want to sample.

Return a, so that's all that does,

is it takes that particular sine wave,

the one that's governed by the pitch and samples at,

well multiplies the duration samples 44100 times.

Creates the array, that corresponds to tone,

at that pitch for that duration.

So that's our function that we're going to use to manipulate tones,

and this is just the "Hello World" ,

so what we're going to do is,

take the pitch from the command line,

and take the duration from the command line,

and then just create an array,

and fill it up with the tone of that pitch and that duration, and then play it.

So that's all there is to it.

So let's hear what it sounds like.

So, if I invoke this program with 440 for concert A in three seconds,

then here's what I get.