0:48

And now we have to, read a sound file, okay?

Â To read a sound file,

Â we need to use one package, SciPy, that has this functionality.

Â So, what we going to do is from SciPy, and

Â within SciPy there is a module called IO,

Â and within that there is one specifically to handle WAV files.

Â So from that part of this package, we going to import,

Â the read function.

Â Okay? So now, the read function is part of

Â our system Python.

Â And in fact, if we type help and the read function,

Â it knows about it and tells me what is the input and the output.

Â So as input, we going to input the name, of the file I want to have and

Â then as return, what it returns is the sampling rate and

Â an array with the actual samples of the sound.

Â 1:54

Okay, so what we're going to do is have fs as a variable where to put the sampling

Â rate, x as a variable where to put, the samples of the sound and

Â then we're going to say read, and we're going to put filename here.

Â Okay, in this directory I am in, I have a file name called flutea4.wav.

Â 2:21

Okay?

Â so, I will read this sound file into into memory.

Â Okay?

Â So now, fs has the sampling rate

Â 44,100 and x, is an array of integers.

Â They are 16 bit integers.

Â Including all the samples.

Â Here, of course,

Â it's not showing everything, just showing the first three and the last three.

Â 2:50

Now we can start asking things about this sound.

Â For example, if we want to know the length of the, the array,

Â we can just type x.size, and this tells me that this

Â sound has 94,803 samples If we want to know, how many seconds that is,

Â we can divide the, the number of samples into the sampling rate.

Â However, these two numbers are integers.

Â So, the result is an integer.

Â If we want to know, the duration in an exact way.

Â What we going to do, is just convert one of the numbers,

Â to a floating point number, like the FS.

Â So that, now, it returns a floating point number.

Â So this is, the duration of the sound, in seconds.

Â So, it's 2.149 seconds.

Â 4:26

Okay, and this is the sound the visualization of the wave form,

Â in the horizontal axis it basically has the samples I read and

Â then on the vertical axis it has the values of these samples.

Â Being in integer values it it goes from -10,000 to 8,000.

Â Okay? But this is

Â not a nice way to visualize sound.

Â What we want to do, is to visualize them in seconds, so

Â the time actually should be in seconds.

Â How do we do that?

Â 5:01

Okay?

Â So, what we going to do, is we need to get some arrays.

Â And for doing that, we need to import.

Â Another package, which is the numpy so import numpy as np and

Â now we can give some useful functions for numpy, specifically,

Â we can create a time axis, so we define a time axis t.

Â That uses a function of numpy called range and

Â range, what it does, is basically creates an array

Â of the length that I put in as input.

Â Going from zero to that value.

Â And then if I divide by the sampling rate,

Â 6:58

And we can see the oscillation of the sound.

Â In fact if we do it a little more, we're going to see it and even more,

Â we will see the individual periods, the oscillation of of the sound.

Â Okay? So that's, quite good.

Â We didn't have to, actually call the function with these specific numbers.

Â So, within the figure we can already navigate through this sound, okay?

Â But also, we can select samples with

Â Python commands, by choosing just the samples from a given array.

Â So for example, if we want to have another array that is just one fragment

Â of our input array, so we can say okay, take just samples for example,

Â from 44,100 coming from second one to, let's say,

Â 45,100, so this is going to be 1000 samples,

Â that are going to be stored in y.

Â 8:12

And now, of course, we can, just if we plot this

Â y it will just show that this 1000 samples.

Â Okay, now we can start doing operations for example, on this new array.

Â And for example, we can just ask what is the, the maximum of these arrays,

Â or there is this function called max from numpy,

Â that if we give it array y, it gives us what is the maximum value.

Â However of course we know that sound sort of oscillates around zero.

Â So it has positive and negative values.

Â So in fact, instead of doing that,

Â what it makes more sense is to just take the absolute value of y.

Â So that it considers also the negative values as values to be considered.

Â So if we now type the maximum of the absolute value of y, and it gives us

Â a different number which basically that means that this was a negative value

Â that of course was higher, in terms of absolute value than the positive values.

Â 9:46

Okay, now that we know how to process sounds, now what typical thing we will

Â want to do, is to write the result or, or write this new sound into a file.

Â So we need to get the, a function to write sound files.

Â So for, again from SciPy so

Â from sciPy.io.wavfile, we are going

Â to import the write function, okay?

Â And the write function, basically accepts as

Â input values, a name of a file to write.

Â Then we need to give it a sampling rate with, with the same as the input.

Â So fs, and now we can just put an array of numbers.

Â So for example this new y array.

Â Okay, and that's it we have created a sound file.

Â Then we type ls, it's going to be an test.wav file.

Â And from within iPython, we can play these sound files.

Â By calling Unix or terminal level programs or functions.

Â So specifically, if we put the, the exclamation mark, and

Â we there is this function in Unix, this program called Play.

Â And so we, this calls the, the Unix program play, and

Â we can give it any file that is in the directory.

Â So, we put the flute-A4.wav, it's playing this

Â flute sound, and it's, at the same time it's giving

Â me some information about this sound file.

Â Okay, so that's all I wanted to say so basically

Â we have been talking about reading and writing sound files.

Â And there is this package wavfile within SciPy, that allows it to do that.

Â And there is packages that allow us to process and to manipulate these sounds.

Â So with matplotlib, we can plot sounds fragments of sounds and

Â with numby, there is a whole bunch of functions that allows us to,

Â process this sound, and that's all for this class.

Â So we have already introduced Python, and now we have introduced some

Â basic functions to, to read write sound files.

Â And hopefully with that, we can start doing some interesting things but

Â of course we have to learn my more, much more.

Â So, I will see you next class.

Â Thank you.

Â