Let's take a look at our record audio function
which you saw is called twice in this application.
Once to record the audio of the person speaking English,
and once to record the audio
of the person speaking Spanish.
In both cases, we are going to give it a file name,
that's going to allow us to store that spoken audio
into a file on the local system
and that file will then be sent off
to the Watson Web Services
for transcription.
Now, recording audio of course is not central
to working with IMB Watson,
so what we did here is we used the recommended settings
from the pyaudio documentation.
So just briefly here, we do have
a few constants that we've defined
up at the top of the function.
These are used when configuring the audio recorder.
One is the frame rate,
and the number 44,100
represents 44.1 kilohertz,
and that is a common format
for CD quality audio
when you're recording audio on your system.
The audio is going to come into the program
in chunks of 1,024 frames
at a time.
And the information will be stored in a 16-bit format
which is two-byte integers.
There will be two channels of audio,
which means two samples per frame
that is actually recorded into our system.
And again, these are just the recommended settings
for a simple recorder object.
And the detailed discussions of those constants
and opening a recorder
can be found in the pyaudio online documentation.
Now we have a fifth constant here for our example,
which is the number of seconds of audio recording
that we're going to store.
And we'll use all of these constants down below.
We create the pyaudio object for the purpose of recording
and to actually get the input stream
from the microphone on our system,
we're going to say to that recorder object
to open the stream and we give it a number of arguments
that will configure that stream for input.
The format argument simply specifies
that the data we will receive will be in two-byte integers.
The channels arguments simply says that there will be
two channels or two samples per frame.
The frame rate is the third argument here,
input equals true is saying that we wanna
read from the microphone
and frames per buffer is going to be how much information
is delivered into the program at a time
as we receive the audio from the microphone.
Now, all of those chunks of information
are going to get stored into a list,
which we're calling audio frames,
and we'll write the contents of that list out
as an audio file, and store it on our local system.
Now once we start recording,
this loop is actually going to read in
chunks of information from the microphone
and to figure out how many times to iterate
we perform this calculation,
where we take the frame rate of 44.1 kilohertz,
multiply that by the number of seconds
that we want to record,
and remember, we're getting the input in
chunk frames per iteration.
So we divide by chunk and that tells us the total number
of iterations of the loop.
During each iteration, we read one chunk of 1,024
and we take those frames that come back,
and append them to our audio frames list.
Now once we're done reading,
we have to shut down the stream and release our resources.
So the audio stream object, you call stop stream
to stop the recording,
you call close to shut down the stream,
and you tell the recorder to terminate when you're ready
to release it's underlying system resources,
such as the microphone.
Now, once we get to that point,
we use a with statement
to output the remaining information.
And in this case, the with statement is going to
write into the specified final using binary format
once again.
And because we're writing audio,
there's some additional information that we need to specify
as we configure the file for output.
The setnchannels is going to specify the number
of samples per frame.
The sample width is going to specify the
16-byte samples,
and again,
we could use the constant that we defined up above.
But we actually, just to confirm it,
asked the recorder to give us its sample size.
We also specified what the frame rate was,
this is all information that needs to be
encoded into the audio file,
and once we've configured those things,
we can actually write out all of the frames
in the list called the audio frames.
And to do that,
we are using the string method join,
and we are creating what is known as
a byte string in python.
So this is actually a first introduction,
we haven't seen these previously.
We've seen things like raw strings that were preceded
with the letter R,
we've seen things like F strings, which are format strings,
a string preceded by B represents a byte string,
and in this case, we are starting with the empty byte string
and we're saying join together every single object
in the audio frames list
by separating them
with empty byte strings.
And the result of this expression
will be one big byte string
representing all of the audio that was recorded,
and in one operation here,
we're going to output all of those bytes
into the audio file.
Once that is done,
the with statement will automatically close the file,
and now we'll have that audio file stored locally
on our system.