This is a quick and easy Python tutorial where I show you how to use several great Python modules and packages to record audio through a microphone or audio device on your computer with the PyAudio Python Module. I then show you how to process the data using the WAVE .wav file audio editing module. Lastly I show how to graph and display the recorded file using the numpy and matplotlib python plug in modules.
What do you want to see in a future video? Be sure to leave a comment below saying what you want in future videos or letting me know if you had any questions about this tutorial!
Thanks for watching, and be sure to leave a like or subscribe to the channel to stay up to speed with all our future projects!
what's up you guys welcome back to the
channel in today's video i'm going to
show you how to record audio and graph
wave files using python
[Music]
so i've been traveling a lot for work
lately and i'm not at home this week so
i don't have my multiple screens and
setups to do like a real detailed
line-by-line tutorial but i wanted to
bring you guys this cool quick project
that uses recording audio and the
matplotlib graphing library to do some
really cool audio visual stuff
so i'm going to do a slightly different
format for this video where i'm going to
show the code that i already have
written and explain what each section
does but i'm not actually going to type
it out line by line in real time with
you guys let me know in the comments
below if you like this format more or if
you find the tutorials where we do go
line by line and i explain it as i write
it let me know what your preference is
and i'll start doing more of that type
of tutorial if people lean one way or
another otherwise i'll probably just
keep a good mix on the channel so thank
you guys so much for your feedback and
without any further ado let's start
talking about the project
so to start
you're going to want to import the pi
audio module and you should be able to
do this just by using dash m pip install
pi audio there's a lot of stack overflow
and online help about how to install
these modules so if you're having any
trouble with any of the ones you see
here just check out the internet i'm not
going to go too in-depth about how to
run pip because hopefully by this stage
of project building you know how to
import modules but you need the pi audio
module to actually record from your
computer and then you want to import the
wave module which most of the time is a
built-in module that comes installed
with your python install and that's the
python module that lets you parse wav
files and process them and use them
which we'll need to do in this project
then import the
matplotlib.pyplot as plot i always alias
it you don't technically have to do this
but trust me it saves you a lot of
typing later on
this is the graph package that's
actually going to give us the visual of
our wave file when we record
and then import numpy as np honestly
even if you weren't doing this project
in particular this is a package i import
at the beginning of almost every project
because it's got so many shortcuts for
math and arrays and data visualization
it's a great one to have in your
projects
and then this second block of four rows
is us setting up some specifics about
our audio files and i will touch on what
each of them do real quickly but just
know you can kind of use these as a
default if you don't really understand
what we're looking at
frames per buffer is essentially how
many frames are going to be recorded per
second and you'll notice
how we use that when we get down to
actually recording um but 3200 is super
standard and we need to put it kind of
in that format because when we get to
the point where we open the computers
like stream and record from the
microphone or your computer microphone
whatever you're using that's the format
it uses if you're peaking ahead a little
bit here you'll see there are some
specific
parameters that we give the stream when
we open pi audios like microphone
recording
input and so just use frames per buffer
as 3200 for now if you don't want to
dive too deep into why that is
and then the format this is where these
next two are kind of together if you're
recording in what's called mono or
single channel recording
then you want your format to be that of
p816
if you were recording in stereo which
would have two channels you would want
the pa in 32 but we're just going to
record in mono
because every microphone can handle that
not every microphone can handle
recording in stereo
and then for rate this is going to be
1600 that is a really standard wave
recording frequency um so that's what
i'm going to use you don't have to
but again i don't want to get too deep
into the actual science of audio files i
just want to show you guys how to write
the code
so then
i just like to create this alias
variable where i say pi audio dot pi
audio with parentheses is equal to pa
because typing this every time you want
to access something from pi audio is a
bit messy and this makes it a lot
cleaner and neater to read your code but
so then this this stream is actually
where we open the computer and access
the microphone essentially
so from pi audio it's just pa.open and
then those variables we set up before
format channels rate and frames per
buffer this looks kind of like kivy
if you've designed any graphical user
interfaces in qivi there's a lot of
parameters that you just set you know
parameter name equal to and if you're
setting up a big project it's smart to
just use a variable in there so that you
can play around with the variables up
here instead of having to mess around
inside your stream
so anyways just format channels rate and
frames per buffer and then anytime
you're trying to access the microphone
set input equal to true
and then uh because i want kind of a
countdown on the screen while you're
recording we're going to just print the
word start recording on screen and now
anything you say after that gets printed
is in our kind of our recording so uh
then we set up some new variables
seconds is gonna be how long you want
the recording so for me i just picked
eight seconds because you can say full
sentences in there but it doesn't go on
too long this would be a tedious video
if you guys had just had to watch me
record minutes of data but so if you
want longer recordings you can make this
a larger value
create an empty list for your frames and
frames is actually going to be
individually capturing the audio
frequency of your recording device
every single iteration so when we set
our rate divided by our frames per
buffer that's going to be how many times
per second it's recording so that's
quite a large number
um and so you're going to store that all
in a list that we're going to use both
for storing the wav file later and for
graphing it at the end
and then these two
second tracking and second count these
are definitely optional but i like the
idea of in the terminal just showing how
much time is left in your project um in
your recording rather and so what i do
is i create this variable second
tracking that's going to add one
every single time we record a new frame
but then once we've recorded enough
frames that equal one second so remember
i said that's the rate at which we're
sampling divided by the number of frames
per buffer that's going to tell you how
many seconds you have
or that's going to tell you how many
frames you have in one second
so if second tracking is equal to that
value that means we've gone one second i
reset my second tracking code and then i
just add one to my counter for seconds
and then i show how much time is left by
printing the total seconds minus the
number of seconds that have passed this
is just a nice thing so that while you
are recording um it's showing you you
have seven seconds left six seconds left
five seconds left if you want to it's
not even a bad idea to say seconds
afterwards so you don't have to guess
about units but then once it finishes so
this means we've gotten to the point
where we've done
this many which is how many frames per
second times the total number of seconds
and once we get to that point you do
stream dot stop stream stream dot close
and then pi audio which we've aliased as
pa dot terminate and this is everything
you need to shut down the recording but
then what we want to do is we want to
store the information in a file so what
i do is i create another alias variable
that i call object or obj and then
set it equal to wave dot open so again
the wave module is like the built-in
package for parsing data
and then
give it a name so this actually doesn't
have to be an existing audio file if you
see right now i do have that but if i
delete this out because i just did a
recording so i'm gonna delete it and i'm
not gonna do the safe delete so i just
got rid of it
this code is still gonna run fine
because we're not doing a read function
right now we're doing a write function
so the two things you need to give this
wave.open argument are the name you want
to give your wav file and then if you're
writing this is wb for write binary if
you were reading from it it would be rb
which is read binary and if you're
peaking ahead we actually do that later
once we read it
into graph it so i do it that way so
that i can show you using the wave
module a couple times but technically
you do already have the data in this
file
so then what you do is
with the wav file you're already
creating a wave object by saying
wave.open so there are these built-in
functions that you need to populate with
those things we've already created so
set the number of channels and this is
what's going to tell the wave file if
it's a
mono or stereo
and then set sample width is just
telling it the total size and by doing
that it just needs to know the format so
it's able to figure out how many frames
are in it
by knowing what format so if this is a
stereo
recording that's going to be in 32 but
if it's a mono recording it's in 16 and
all you have to do is just
set the sample width by doing pi audio
dot get sample size of the format
and then object dot set frame rate and
then just tell it the frame rate that
you were recording at
and then the last thing you do is you
say object dot write frames and then you
do b
uh parenthesis parenthesis dot join and
then all of the frames and so this is
the list we just recorded so this is our
full list of all of the data while we
were recording and we just joined that
to this right frames object
module
that's not the framework function of the
wave module and then just close it so
this object.close is what actually saves
all of your work and now you're going to
have a file so if you were to stop the
program right at this block of code
you're going to have an audio file over
here everything after that code
is now just so that we can graph it on
the screen if you were to stop right
there you would have everything you
needed to get a recording
so then what i do now to kind of show
you the flip side of things if you
already had a recording and you wanted
to graph it then start with file equals
wave.open and the only reason i say file
instead of object again is to kind of
distinguish the two functions but you
could call this object again if you
needed to or you don't really have to
open it back up since it already exists
but again i'm trying to separate out the
two functionalities
so we're going to open the file right
after we record it and notice now it's
rb because it's read binary now we're
just getting the data out of it
and set up a few things so sample
frequency is going to be file.getframe
frames is going to be get number of
frames and then the the actual wave
is going to be reading the frames and
then just give it a minus one which
basically means read everything
and then we're going to close it because
now we've moved all the information we
need
into
a
into our local variables so we don't
have to mess with the file anymore we
have everything we need right here the
total time of the recording is equal to
frames divided by the sample frequency
so that tells us how long it is
and then again if one channel use int 16
here for data type otherwise if it's
stereo use int 32
but we're creating an array and here's
why numpy is great for this project
um we're just going to create an array
and then we say from a buffer and then
we give it the wave and we say what data
type
it was produced with
um and so that's all we have to do as
long as you follow
the same steps that i gave you here
that's going to give you an array of all
of the data that was recorded and it
knows how to graph that
so then the times are going to be equal
to numpy again and now this is lin space
l in space um
and then
zero up to time where the number is
equal to frames so this is saying your
start time and your end time
and then the total number of
data points to expect in that space and
so this should be logical because time
is equal to the frames divided by the
sample frequency which basically means
from zero to time
the frames are how many data points we
have so the rest of this is just
creating the graph so we use plot which
again is from map plot lib dot pi plot
so if you didn't do an alias in the
beginning you're gonna need to type that
out every time but assuming you're
following along it would be plot dot
figure
and then the figure size and i just use
15 by five because it seems about right
for an eight second recording you can
play around with this this is just
setting the parameters for the size of
your screen
and then you tell it what to put on the
x-axis and then what the actual data is
for the y-axis and so for us we want
time in the x-axis and then we want the
audio array in the y axis i just give it
some labels so y label and x label for
the signal wave and then the times
and then the x limit is from zero to
time and then give it a title and do
plot.show and that's everything so now
if we record a new one
we wait till we get start recording
there we go and now we're recording so
i'll take a pause
and now we'll talk a little bit more and
you can see right here you know this
doesn't really look like anything in
particular because i was just talking
over the video but you can see when i
said let's take a pause and then we
pretty much flat line and then we get to
the end here and i'm talking again so
you can do some kind of fun things with
this and you can sort of see let's do
that again
so
if i take big spaces
will see some fun gaps between the words
so you can see that last one it was much
closer because i was sort of talking a
normal pace but here i took big spaces
between every word and you can see you
get this kind of fun shape so you don't
have to follow along with the video any
longer and watch me just record multiple
things if you wanted to um always record
with a new
uh a new file name then you could
potentially set this up to be a little
more robust to like running multiple
times and handling different data every
time for me this is plenty um basically
what this is going to do though every
time is overwrite the previous wave file
so if you were gonna do multiple
recordings and you want it with a new
name you either gonna have to come in
here and punch something manually or
just create like a a start prompt where
you get the name of the wav file as an
input so um if you wanted to you would
just do like up here
input
you know and then get uh enter
file name something like that and then
instead of
you would call this like new name
and instead of uh just writing to le
master tech every time you would write
to
new name dot wave and you would have to
do that as a formatted string so you
would do something like
that i'm not doing it that way i did
just i guess show you how to do the
whole thing so hopefully if you would
prefer to do it that way go ahead
i will probably throw all this code in a
github and leave a link to it in the
description below so if you don't feel
like typing out everything you saw in
here that's totally fine
again let me know if you liked this kind
of format where it was just an
explanation of already written code or
if you'd like to see the code written
line by line with me so i was certainly
happy to take feedback and make the
channel better
and with that being said if you have any
ideas for future videos or things you'd
like to see on the channel be sure to
let me know in the comments below
because i've been getting a ton of great
suggestions and i am working on a lot of
those projects
so i really appreciate the support if
you've enjoyed the video please consider
leaving a like on the channel subscribe
to the channel
we're just getting started and we make a
lot of cool projects so thank you
everybody for the support good luck with
your projects and thanks for watching
thanks bye