How to check shape and summary of an audio file in Pytorch

This recipe helps you check shape and summary of an audio file in Pytorch

Recipe Objective

How to check shape and summary of an audio file in Pytorch?

This is achieved by using the torchaudio function only, we have seen before how to load a audio file using PyTorch. For finding the shape of the audio file we need to find out the Waveform which are nothing but the audio signals, the pattern of the sound. By using .size function we can find out the shape of the audio file and for the summary we can use .info function which will return each and every information regarding the audio file. Lets understand this with practical implementation.

Build a Multi Touch Attribution Model in Python with Source Code

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 audio file

print("This is the shape of the audio file: {}".format(data_waveform.size()))

This is the shape of the audio file: torch.Size([2, 276858])

Step 5 - Print the summary of audio file

print("This is the summary of the audio file: {}".format(torchaudio.info(audio_file)))

This is the summary of the audio file: (sox_signalinfo_t {
  rate-> 44100
  channels-> 2
  precision-> 16
  length-> 553716
  mult-> 0
}
, sox_encodinginfo_t {
  encoding-> 1
  bits_per_sample-> 16
  compression-> inf
  reverse_bytes-> 0
  reverse_nibbles-> 0
  reverse_bits-> 0
  opposite_endian-> 0
}
)

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

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

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.