.NET
BPM detection
audio processing
MP3
wave files

programmatically get BPM of a wave or MP3 from .Net

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Getting BPM from an MP3 or WAV file in .NET is possible, but it is not a metadata lookup unless the file already contains tempo tags. Real BPM detection means decoding the audio, extracting an onset or energy envelope, and estimating the repeating beat interval from that signal.

Decode the Audio to PCM First

MP3 is compressed, so the first step is to decode it into sample data. WAV is often already in PCM form, but using the same decoding path keeps the workflow simple.

In .NET, NAudio is a common choice:

bash
dotnet add package NAudio

Then read the file as mono floating-point samples:

csharp
1using NAudio.Wave;
2using System.Collections.Generic;
3
4static List<float> ReadMonoSamples(string path)
5{
6    using var reader = new AudioFileReader(path);
7    int channels = reader.WaveFormat.Channels;
8    float[] buffer = new float[reader.WaveFormat.SampleRate * channels];
9    var samples = new List<float>();
10
11    int read;
12    while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
13    {
14        for (int i = 0; i < read; i += channels)
15        {
16            float mono = 0f;
17            for (int c = 0; c < channels; c++)
18            {
19                mono += buffer[i + c];
20            }
21            samples.Add(mono / channels);
22        }
23    }
24
25    return samples;
26}

This gives you a mono signal, which is a good starting point for beat estimation.

Build a Simple Energy Envelope

A basic tempo detector does not start by looking for exact drum hits. It usually starts by tracking short-term energy over windows of audio.

csharp
1using System;
2using System.Collections.Generic;
3
4static List<double> ComputeEnergyEnvelope(List<float> samples, int windowSize)
5{
6    var envelope = new List<double>();
7
8    for (int i = 0; i + windowSize <= samples.Count; i += windowSize)
9    {
10        double energy = 0.0;
11        for (int j = 0; j < windowSize; j++)
12        {
13            double s = samples[i + j];
14            energy += s * s;
15        }
16        envelope.Add(energy);
17    }
18
19    return envelope;
20}

The envelope is much smaller than the raw waveform and is easier to analyze for periodic beat structure.

Estimate BPM From Repeating Peaks

Once you have the energy envelope, you can search for a repeating lag. One simple method is autocorrelation across a sensible BPM range.

csharp
1using System;
2using System.Collections.Generic;
3
4static double EstimateBpm(List<double> envelope, int sampleRate, int windowSize)
5{
6    double envelopeRate = (double)sampleRate / windowSize;
7    int minLag = (int)(envelopeRate * 60.0 / 200.0);
8    int maxLag = (int)(envelopeRate * 60.0 / 60.0);
9
10    double bestScore = double.MinValue;
11    int bestLag = minLag;
12
13    for (int lag = minLag; lag <= maxLag; lag++)
14    {
15        double score = 0.0;
16        for (int i = lag; i < envelope.Count; i++)
17        {
18            score += envelope[i] * envelope[i - lag];
19        }
20
21        if (score > bestScore)
22        {
23            bestScore = score;
24            bestLag = lag;
25        }
26    }
27
28    return 60.0 * envelopeRate / bestLag;
29}

And use it like this:

csharp
1var samples = ReadMonoSamples("song.mp3");
2int sampleRate = 44100;
3int windowSize = 1024;
4var envelope = ComputeEnergyEnvelope(samples, windowSize);
5double bpm = EstimateBpm(envelope, sampleRate, windowSize);
6
7Console.WriteLine($"Estimated BPM: {bpm:F1}");

This is a simplified estimator, but it demonstrates the actual shape of the problem.

Expect Approximation, Not Magic

Real music is messy. Tempo detection gets harder when tracks contain:

  • weak drum transients
  • tempo drift
  • swing or syncopation
  • long intros without obvious beat
  • half-time or double-time rhythmic ambiguity

That is why a simple detector may return 70 when a DJ would call the track 140. Both can be rhythmically consistent interpretations.

For production-grade analysis, you may eventually want a specialized DSP library or a stronger onset-detection pipeline. But for many internal tools, an energy-envelope approach is enough to get a useful estimate.

Consider Metadata Before DSP

Some audio files or external music libraries may already contain tempo metadata. If that is available and trustworthy, reading it is much cheaper than analyzing the waveform. However, you cannot depend on metadata for arbitrary MP3 or WAV files, so signal analysis is still the general solution.

Common Pitfalls

  • Expecting MP3 files to expose BPM directly without decoding is usually wrong because tempo is not guaranteed to be stored as metadata.
  • Analyzing stereo samples without converting to mono first can complicate simple beat-detection logic unnecessarily.
  • Using a window size that is too large or too small can make the energy envelope too noisy or too coarse for useful BPM estimation.
  • Treating the returned BPM as exact truth is risky because beat trackers often confuse half-time and double-time patterns.
  • Assuming every song has a stable fixed tempo leads to poor results on live recordings or heavily expressive performances.

Summary

  • BPM detection in .NET is mainly an audio-analysis problem, not a file-format problem.
  • Decode MP3 or WAV audio into PCM samples first.
  • Build an energy or onset envelope to reduce the waveform into something rhythmically meaningful.
  • Estimate the repeating beat interval, often with autocorrelation over a plausible BPM range.
  • Expect an approximation and validate the result for music with weak or ambiguous rhythm.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.