Converting sound wave bytes to integers
1. Converting sound wave bytes to integers
Excellent effort, you've imported the good morning audio file and seen what it looks like in byte form. Now let's see if we can make those bytes even more useful.2. Converting bytes to integers
To make our audio data more useful, we're going to convert it from byte form to integers. To do this, we'll use NumPy. NumPy is a numerical Python library full of helpful functions. First, we'll import it with the common alias ehn-pee to avoid typing NumPy every time. Then, the NumPy method we'll use to convert our bytes to integers is frombuffer. frombuffer turns a series of data into a 1-dimensional array of a specified data type. Remember, we saved the good morning audio file bytes to the variable soundwave gm. Since this is an array of data, we can pass it to frombuffer as the first parameter. And then we can set the dtype parameter to the data type we'd like to get back. There are multiple datatype's we could pass in but for our case, int16 is what we're after. So if we wanted to see the first values of our soundwave in integer form, what do they look like? Much better. But, we're only looking at the first 10. Can you guess how long the whole array is? Remember how frequency is a measure of information per second? Our good morning soundwave has a frequency of 48 kilohertz, a length of 2-seconds and thus 96,000 pieces of information. So, this array only shows the first 10 of those 96,000.3. Finding the frame rate
Okay, we know our good morning sound wave has a frequency of 48 kilohertz. But what if we didn't? To find it, we could divide the length of the wave object array by the duration of the sound wave in seconds. But Python's wave module has a programmatic way. Calling get frame rate on a wave object will return its frame rate. Let's use it on our good morning wave object. Excellent, the method returns the number we were expecting, 48,000, or, 48 kilohertz. We can use this frame rate variable for one more thing which will be handy for visualizing our sound waves later. By dividing the number of items in the sound wave array by the frame rate, we can get the duration of our audio file.4. Finding sound wave timestamps
With this value, we can leverage NumPy's linspace method to figure out the timestamp where each sound wave value occurs. The linspace method takes start, stop and num integers as parameters. Calling it will return num evenly spaced values between start and stop. Let's try it with start as 1, stop as 10 and num as 10. As you can see, it returns an array of evenly spaced numbers between 1 and 10. Let's try it on our own values to get the timestamps of pieces of information in our sound wave. Start will be 0 for the beginning of the audio file. Stop will be the length of our sound wave array over the framerate, or in other words, the duration. And num will be the length of our sound wave array, since each item in the array is a sound wave value.5. Finding sound wave timestamps
Let's check out the first 10 timestamps. Each of these values is the time in seconds where each sound wave byte occurred. We'll be able to use these timestamp values later to see what our sound wave looks like.6. Let's practice!
Okay, it's your turn to make our bytes more useful!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.