Python Tutorial : Converting sound wave bytes to integers
Key Takeaways
This video tutorial demonstrates how to convert sound wave bytes to integers using Python and the NumPy library, specifically using the frombuffer method to convert byte data into a one-dimensional array of integers.
Full Transcript
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 will use to convert our bytes to integers is from buffer from buffer turns a series of data into a one-dimensional array of a specified data type remember we save the goodmorning audio file bytes to the variable sound wave GM since this is an array of data we can pass it to from buffer as the first parameter and then we can set the D type parameter to the data type we'd like to get back there are multiple data types we could pass in but for our case in 16 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 goodmorning sound wave has a frequency of 48 kilohertz a length of 2 seconds and thus 96 thousand pieces of information so this array only shows the first 10 of those 96 thousand ok we know our good morning sound wave as 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 pythons wave module has a programmatic way get frame wait on a wave object will return its framerate let's use it on our good morning wave object excellent the method returns the number we were expecting 48 thousand or 48 kilohertz we can use this framerate 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 framerate we can get the duration of our audio file with this value we can leverage num pies linspace method to figure out the timestamp where each sound wave value occurs the linspace method takes start/stop & 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 now is 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 time stance 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 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 where each sound wave bite occurred we'll be able to use these timestamp values later to see what our sound wave looks like ok
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/spoken-language-processing-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
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 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.
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 gets frame rate on a wave object will return its frame rate.
Let's use it on our good morning wave object.
Excel
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
🎓
Tutor Explanation
DeepCamp AI