Python Tutorial : Converting sound wave bytes to integers

Convert raw audio bytes from a WAV file into integers using NumPy's frombuffer with int16, then extract the frame rate (48 kHz) and calculate duration by dividing sample count by frame rate. Use linspace to create timestamps for each sample so you can visualize the waveform.
Chapters
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. 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 NP to avoid typing numpy every time. Then the numpy method we'll use to convert our bytes to integers is from Buffer. FromBuffer turns a series of data into a one dimensional array of a specified data type. Remember, we save the Good Morning audio file bytes to the variable soundwavegm. Since this is an array of data, we can pass it to from buffer 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 data types we could parse in, but for our case, INT16 is what we're after. So if we wanted to see the first values of our sound wave 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 sound wave 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. OK, we know our Good morning sound wave has a frequency of 48kHz, 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 frameweight 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 kHz. 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 soundwave array by the frame rate, we can get the duration of our audio file. 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 zero for the beginning of the audio file. Stop will be the length of our sound wave array over the frame rate. 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, let's check out the first 10 timestamps. Each of these values is the time in seconds which where each sound wave byte occurred. We'll be able to use these timestamp values later to see what our sound wave looks like. Okay.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







