Click here to Skip to main content
15,887,812 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.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace SoundfingerprintingLib.Hashing
{
    public class DbPermutations : IPermutations
    {
        private readonly string _connString;

        /// <summary>
        /// Cached permutations
        /// </summary>
        private int[][] _cachedPermutations;

        /// <summary>
        ///   Instantiate data access manager using FingerprintConnectionString read from the App.Config.
        ///   If no such string is found in the App.Config, connection string is set to Empty
        /// </summary>
        public DbPermutations(string connectionstring)
        {
            _connString = connectionstring;
            _connection = new SqlConnection(_connString);
        }

        #region Connection Management

        private readonly IDbConnection _connection;

        /// <summary>
        ///   Gets the connection to MSSQL Server
        /// </summary>
        /// <returns>SQL Connection object</returns>
        private IDbConnection GetConnection()
        {
            return _connection;
        }

        #endregion

        private const int COMMAND_TIMEOUT = 60 * 10; /*600 sec*/
        private const string SP_READ_ALL_PERMUTATIONS = "sp_ReadPermutations";
        private const string FIELD_PERMUTATIONS_ID = "Id";
        private const string FIELD_PERMUTATIONS_PERMUTATION = "Permutation";

        /// <summary>
        ///   Gets stored procedure with the assigned name
        /// </summary>
        /// <param name = "cmdTxt">Name of the stored procedure</param>
        /// <returns>SQL command to be executed</returns>
        /// <param name = "connection">Connection object to the database</param>
        private static SqlCommand GetStoredProcedureCommand(string cmdTxt, IDbConnection connection)
        {
            if (String.IsNullOrEmpty(cmdTxt)) throw new ArgumentException("cmdTest parameter is null or empty");
            if (!(connection is SqlConnection)) throw new ArgumentException("connection is not of type SqlConnection");

            SqlCommand sqlComm = new SqlCommand(cmdTxt)
            {
                CommandType = CommandType.StoredProcedure,
                CommandTimeout = COMMAND_TIMEOUT,
                Connection = (SqlConnection)connection
            };
            return sqlComm;
        }

        /// <summary>
        ///   Get a command to read all the permutations from the database
        /// </summary>
        /// <param name = "connection">SQL connection used to execute the command</param>
        /// <returns>SQL command to be executed</returns>
        public IDbCommand GetReadPermutations(IDbConnection connection)
        {
            SqlCommand sqlCommand = GetStoredProcedureCommand(SP_READ_ALL_PERMUTATIONS, connection);
            return sqlCommand;
        }



        /// <summary>
        ///   Read all permutations from the database
        /// </summary>
        /// <returns>Dictionary of permutations</returns>
        public int[][] ReadPermutations()
        {
            if (_cachedPermutations != null)
                return _cachedPermutations;
            IDbCommand sqlCommand = GetReadPermutations(GetConnection());
            IDataReader reader = null;
            Dictionary<int, int[]> result = null;
            try
            {
                sqlCommand.Connection.Open();
                reader = sqlCommand.ExecuteReader();
                if (reader != null)
                {
                    result = new Dictionary<int, int[]>();
                    while (reader.Read())
                    {
                        int id = (int)reader[FIELD_PERMUTATIONS_ID];
                        string permutation = (string)reader[FIELD_PERMUTATIONS_PERMUTATION];
                        string[] elementsOfPermutation = permutation.Split(new char[1] { ';' },
                                                                           StringSplitOptions.RemoveEmptyEntries);
                        int[] arrayOfPermutations = new int[elementsOfPermutation.Length];
                        for (int i = 0; i < elementsOfPermutation.Length; i++)
                            arrayOfPermutations[i] = Convert.ToInt32(elementsOfPermutation[i]);
                        result.Add(id, arrayOfPermutations);
                    }
                }
            }
            finally
            {
                if (reader != null) reader.Close();
                sqlCommand.Connection.Close();
            }

            if (result != null)
            {
                _cachedPermutations = new int[result.Count][];
                for (int i = 0; i < result.Count; i++)
                {
                    _cachedPermutations[i] = result.ElementAt(i).Value;
                }
            }
            return _cachedPermutations;
        }

        /// <summary>
        /// Get permutations
        /// </summary>
        /// <returns></returns>
        public int[][] GetPermutations()
        {
            return ReadPermutations();
        }
    }
}

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