Sound Fingerprinting in .Net and C# PArt Two | CodeGuru

If you have read the first installment of this series, you know by now what audio fingerprinting is, but just to recap: an acoustic fingerprint is a condensed digital summary, a fingerprint, deterministically generated from an audio signal, that can be used to identify an audio sample or quickly locate similar items in an audio database.

To visualize an audio fingerprint, we could make use of a spectrogram, which is a visual representation of the frequencies in audio signals, as mentioned in the first article. You can refamiliarize yourself – or read it for the first time – by visiting: sound fingerprinting and .Net part 1.

So, what are we going to do today? Well, we will create a spectrogram from sounds! Sounds exciting, doesn’t it?

Let’s start by creating a C# Windows Forms project in Visual Studio. Name it anything you like – I have named mine: Specto. For simplicity’s sake, I recommend you do the same. When the form’s design view is displayed, add a PictureBox to the form. You could give it a name if you like, but we will only use it for display purposes, so it doesn’t actually need one for our purposes.

Next, ensure the PictureBox’s SizeMode is set to AutoSize. Then, set the WindowState property of the Form to Maximized.

Now that we are finished with the design and setup of the form, we need to add some NuGet packages to our project. We will need to add the following packages:

  • Spectrogram
  • MP3Sharp

To add the NuGet Packages, follow these steps:

  • Click Project.
  • Click Manage NuGet Packages.
  • Click the Browse tab.
  • Enter Spectrogram.

It will search and after it has been found, click install on the right side, as shown in the image below:

Package Installing with C# and .Net

Figure 1 – Spectrogram NuGet Package

Next, add the NuGet package for MP3Sharp.

Net and C# Package Installers

Figure 2 – MP3Sharp NuGet Package

Before we continue, let’s have a look at the two packages we’ve just added.

Spectrogram is a .NET library for creating spectrograms from pre-recorded signals or live audio from the sound card. Spectrogram uses FFT algorithms and window functions provided by the FftSharp project, and it targets .NET Standard so it can be used in .NET Framework and .NET Core projects. FftSharp is a collection of Fast Fourier Transform (FFT) tools for .NET. MP3Sharp enables you to decode MP3 files to PCM bitstreams entirely in .NET managed code.

Now that we understand their purpose, let’s move on to writing some code!

Coding a Spectrogram in C#

Add the following Namespaces to your code:

using Spectrogram;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

Notice the inclusion of the Spectrogram namespace. Add the following code inside the Form’s Load event:

        private void Form1_Load(object sender, EventArgs e)
        {

            double[] audio = ReadMP3("Song.mp3");
            int sampleRate = 44100;
            int fftSize = 16384;
            int targetWidthPx = 3000;
            int stepSize = audio.Length / targetWidthPx;

            spec = new SpectrogramGenerator(sampleRate, fftSize, stepSize, maxFreq: 2200);
            spec.Add(audio);

            Bitmap bmp = spec.GetBitmap(intensity: .4);
            pictureBox1.Image?.Dispose();
            pictureBox1.Image = bmp;

            spec.SaveImage("Output.png", intensity: 5, dB: true);
        }

A double array object named audio is created. It will hold the result of the ReadMP3 method which we will add a bit later. The sample rate as well as some objects to specify the size of the resulting spectrogram.

Later a Bitmap object is created. This is the spectrogram. It will be shown partially inside the PictureBox but will be saved as Output.png. Let’s add the ReadMP3 method.

        public static double[] ReadMP3(string filePath, int bufferSize = 4096)
        {
            List audio = new List();
            MP3Sharp.MP3Stream stream = new MP3Sharp.MP3Stream(filePath);
            byte[] buffer = new byte[bufferSize];
            int bytesReturned = 1;
            while (bytesReturned > 0)
            {
                bytesReturned = stream.Read(buffer, 0, bufferSize);
                for (int i = 0; i < bytesReturned / 2 - 1; i += 2)
                    audio.Add(BitConverter.ToInt16(buffer, i * 2));
            }
            stream.Close();
            return audio.ToArray();
        }

This program reads an MP3 file and adds it to a list. This list gets used by the Spectrogram to display it bit by bit.

Below are two different outputs from two different songs:

Radioactive by Imagine Dragons

.Net Spectrograms

Figure 3 – Output for song 1

And We Run by Within Temptation ft. Xzibit

C# Spectrogram example

Figure 4 – Output for song 2

You can clearly see the difference. Each song starts relatively slowly – the first one (Radioactive) a bit slower than the second (And we run). These two songs are from two different genres making the changes apparent, then each song ends by fading out.

Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read