Click here to Skip to main content
15,894,907 members
Articles / Desktop Programming / WPF

Duplicate songs detector via audio fingerprinting

Rate me:
Please Sign up or sign in to vote.
4.96/5 (337 votes)
23 Jun 2020MIT44 min read 1.3M   20.4K   533  
Explains sound fingerprinting algorithm, with a practical example of detecting duplicate files on the user's local drive.
The aim of this article is to show an efficient algorithm of signal processing which will allow one to have a competent system of sound fingerprinting and signal recognition. I'll try to come with some explanations of the article's algorithm, and also speak about how it can be implemented using the C# programming language. Additionally, I'll try to cover topics of digital signal processing that are used in the algorithm, thus you'll be able to get a clearer image of the entire system. And as a proof of concept, I'll show you how to develop a simple WPF MVVM application.
// Sound Fingerprinting framework
// https://code.google.com/p/soundfingerprinting/
// Code license: GNU General Public License v2
// ciumac.sergiu@gmail.com

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Encog.Neural.Data.Basic;
using Encog.Neural.Networks;
using SoundfingerprintingLib.Hashing;
using SoundfingerprintingLib.NeuralHashing.ActivationFunctions;

namespace SoundfingerprintingLib.NeuralHashing
{
    /// <summary>
    ///   Network - represent a collection of connected layers
    /// </summary>
    /// <remarks>
    ///   The network can be saved or loaded, thus serializable
    /// </remarks>
    [Serializable]
    public class Network : BasicNetwork
    {
        /// <summary>
        /// Median responses used in assembling the hasher
        /// </summary>
        public double[] MedianResponces { get; protected set; }


        /// <summary>
        ///   Compute median responses of the network
        /// </summary>
        /// <param name = "inputs">Inputs</param>
        /// <param name = "granularity">Number of fingerprints per input</param>
        /// <remarks>
        ///   After propagation for each of the 10 network outputs, if the output was greater than the
        ///   median response of that output (as ascertained from the training set) it was assigned +1, otherwise 0
        /// </remarks>
        public virtual void ComputeMedianResponses(double[][] inputs, int granularity)
        {
            int outputsCount =  GetLayerNeuronCount(LayerCount - 1); /*10 - Output length*/
            double[][] responses = new double[outputsCount][];
            int inputsLength = inputs.Length;
            for (int i = 0; i < granularity /*10 Fingerprints*/; i++)
            {
                responses[i] = new double[inputsLength]; /*10240*/
            }

            for (int i = 0; i < inputsLength /*10240*/; i++)
            {
                double[] currentOutputs = Compute(new BasicNeuralData(inputs[i])).Data;
                for (int j = 0; j < granularity /*10*/; j++)
                {
                    responses[j][i] = currentOutputs[j]; /*10240*/
                }
            }

            MedianResponces = new double[outputsCount];

            for (int i = 0; i < outputsCount /*10*/; i++)
            {
                MedianResponces[i] = SignalUtils.Median(responses[i]);
            }
        }

        
        #region I/O Operations

        /// <summary>
        ///   Save network to specified file.
        /// </summary>
        /// <param name = "stream">Stream to save network into.</param>
        /// <remarks>
        ///   <para>The neural network is saved using .NET serialization (binary formatter is used).</para>
        /// </remarks>
        public virtual void Save(Stream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
        }

        public virtual void Save(string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            Save(stream);
            stream.Close();
        }

        /// <summary>
        ///   Load network from specified file.
        /// </summary>
        /// <param name = "fileName">File name to load network from.</param>
        /// <returns>Returns instance of <see cref = "Network" /> class with all properties initialized from file.</returns>
        /// <remarks>
        ///   <para>Neural network is loaded from file using .NET serialization (binary formater is used).</para>
        /// </remarks>
        public static Network Load(string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            Network network = Load(stream);
            stream.Close();

            return network;
        }

        /// <summary>
        ///   Load network from specified file.
        /// </summary>
        /// <param name = "stream">Stream to load network from.</param>
        /// <returns>Returns instance of <see cref = "Network" /> class with all properties initialized from file.</returns>
        /// <remarks>
        ///   <para>Neural network is loaded from file using .NET serialization (binary formater is used).</para>
        /// </remarks>
        public static Network Load(Stream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            Network network = (Network) formatter.Deserialize(stream);
            return network;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Moldova (Republic of) Moldova (Republic of)
Interested in computer science, math, research, and everything that relates to innovation. Fan of agnostic programming, don't mind developing under any platform/framework if it explores interesting topics. In search of a better programming paradigm.

Comments and Discussions