Clear Filters
Clear Filters

Extract BPM from audio file

20 views (last 30 days)
Rita Campos
Rita Campos on 3 Oct 2023
Commented: Ana Campos on 17 Oct 2023
Hi,
I have a code that extracts several features from audio signals. I managed to make everything work so far but the extraction of the BPM.
Any tips on how to write code for that? I looked online before asking here but couldn't find anything that worked. An example of part of my code is shown below. The idea would be to write the code in a way that it integrates the BPM feature calculation in a similar way to what was done for spectral centroid and others. Please see code below. I've got Audio toolbox (if relevant to mention).
Thanks in advance
% select the folder path where audio files are located
folderPath = 'C:\Users\xxxxx\Documents\MATLAB\xxxxExperiment3';
%get a list of all audio files in the folder
fileList = dir(fullfile(folderPath,'*.wav')); % Change the file extension if needed
%Specify the chosen audio features
chosenFeatures = {'mfcc','spectralCentroid','spectralRolloff','BPMinute'};
% Loop through each audio file
for i = 1:length(fileList)
% Read the audio file
filePath = fullfile(folderPath,fileList(i).name);
[audio,sampleRate] = audioread(filePath);
% Extract chosen audio features
features = extractAudioFeatures(audio,sampleRate,chosenFeatures);
% Do something with extracted features (e.g. save to a file)
saveFilePath = fullfile(folderPath,[fileList(i).name '_features.mat']);
save(saveFilePath,'features');
end
% Function to extract the chosen audio features
function features = extractAudioFeatures(audio,sampleRate,chosenFeatures)
% Convert audio to mono if it is stereo
if size(audio,2) > 1
audio = mean(audio,2);
end
% Normalize the audio signal
audio = audio / max(abs(audio));
% Extract the chosen audio features
features = struct();
if ismember('mfcc', chosenFeatures)
mfccCoeffs = mfcc(audio,sampleRate);
features.mfcc = mean(mfccCoeffs,2); % Take the mean across time
end
if ismember('spectralCentroid',chosenFeatures)
spectCentroid = spectralCentroid(audio,sampleRate);
features.spectCentroid = mean(spectCentroid); %single value
end
end
  3 Comments
Ana Campos
Ana Campos on 17 Oct 2023
Thanks all for your help! And apologies for the delay.

Sign in to comment.

Answers (1)

akshatsood
akshatsood on 13 Oct 2023
Edited: akshatsood on 13 Oct 2023
Hi Rita,
I understand that you want to extract BPM from an audio file. Since you have the Audio Toolbox, you can leverage the Beat Detector plugin from the Audio Plugin Example Gallery that estimates and displays a BPM decision using the specified onset detection method. All the example plugins including the Beat Detector can be accessed as follows
Redirect to the above page and scroll down to the Beat Detector plugin
The above plugin can be executed by running the following in the MATLAB command window
>> audioTestBench('audiopluginexample.BeatDetector')
It will open up the Audio Test Bench. Now, from the Test Bench View, the required audio file can be selected by clicking on the gear icon in the Input block
To visualize the BPM value, follow these steps:
  1. Select the desired onset detection method from the available options.
  2. Click the 'Run' button to initiate the analysis.
  3. Click on the 'Visualize Plugin' button located in the Toolstrip.
The results will be displayed in the following format:
I hope this helps.
  1 Comment
Ana Campos
Ana Campos on 17 Oct 2023
Thanks very much! And sorry for the delay.

Sign in to comment.

Categories

Find more on Audio Plugin Creation and Hosting in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!