How to load an audio file in pytorch

This recipe helps you load an audio file in pytorch

Recipe Objective

How to load an audio file in pytorch?

This is achieved by using touch audio function, which will advantage pytorch's GPU support, it makes data loading easy and more readable by providing many tools for it. Lets understand with practical implementation how to load audio in PyTorch.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
import torchaudio
import requests
import matplotlib.pyplot as plt

Step 2 - Audio url

audio_url = "https://pytorch.org/tutorials/_static/img/steam-train-whistle-daniel_simon-converted-from-mp3.wav"
request_url = requests.get(audio_url)

Step 3 - Open the audio file

with open('steam-train-whistle-daniel_simon-converted-from-mp3.wav', 'wb') as file:
    file.write(request_url.content)
audio_file = "steam-train-whistle-daniel_simon-converted-from-mp3.wav"
data_waveform, rate_of_sample = torchaudio.load(audio_file)

Step 4 - Print shape of waveform

print("This is the shape of the waveform: {}".format(data_waveform.size()))
print("This is the output for Sample rate of the waveform: {}".format(rate_of_sample))

This is the shape of the waveform: torch.Size([2, 276858])
This is the output for Sample rate of the waveform: 44100

Step 5 - Plot the waveform

plt.figure()
plt.plot(data_waveform.t().numpy())

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling