Click here to Skip to main content
15,885,278 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.Diagnostics;

namespace DuplicateTracks.Model
{
    /// <summary>
    ///   Track entity object
    /// </summary>
    [Serializable]
    [CLSCompliant(true)]
    [DebuggerDisplay("Id={_id}, Title={_title}, Artist={_artist}, Path={_path}")]
    public class Track
    {
        #region Constants

        /// <summary>
        ///   Maximum artist's length
        /// </summary>
        private const int MAX_ARTIST_LENGTH = 255;

        /// <summary>
        ///   Maximum title's length
        /// </summary>
        private const int MAX_TITLE_LENGTH = 255;

        /// <summary>
        ///   Maximum path's length
        /// </summary>
        private const int MAX_PATH_LENGTH = 255;

        #endregion

        /// <summary>
        ///   Lock object used for concurrency purposes
        /// </summary>
        private static readonly object LockObj = new object();

        /// <summary>
        ///   Incremental Id
        /// </summary>
        private static Int32 _increment;

        #region Private fields

        /// <summary>
        ///   Artist of the track
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        private string _artist;

        /// <summary>
        ///   Id of the track
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        private Int32 _id;

        /// <summary>
        ///   Track length
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        private string _path;

        /// <summary>
        ///   Title of the track
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        private string _title;

        /// <summary>
        ///   Track length
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        private double _trackLength;

        #endregion

        #region Constructors

        /// <summary>
        ///   Parameter less Constructor
        /// </summary>
        public Track()
        {
            lock (LockObj)
            {
                _id = _increment++;
            }
        }

        /// <summary>
        ///   Track constructor
        /// </summary>
        /// <param name = "artist">Artist's name</param>
        /// <param name = "title">Title</param>
        /// <param name = "path">Path to file to local system</param>
        /// <param name = "length">Length of the file</param>
        public Track(string artist, string title, string path, int length)
            : this()
        {
            Artist = artist;
            Title = title;
            Path = path;
            TrackLength = length;
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Track's id
        /// </summary>
        public Int32 Id
        {
            get { return _id; }
            private set { _id = value; }
        }

        /// <summary>
        ///   Artist's name
        /// </summary>
        public string Artist
        {
            get { return _artist; }
            set { _artist = value.Length > MAX_ARTIST_LENGTH ? value.Substring(0, MAX_ARTIST_LENGTH) : value; }
        }

        /// <summary>
        ///   Track's title
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { _title = value.Length > MAX_TITLE_LENGTH ? value.Substring(0, MAX_TITLE_LENGTH) : value; }
        }

        /// <summary>
        ///   Track's Length
        /// </summary>
        public double TrackLength
        {
            get { return _trackLength; }
            set
            {
                if (value < 0)
                    _trackLength = 0;
                _trackLength = value;
            }
        }

        /// <summary>
        ///   Path to file on local system
        /// </summary>
        public string Path
        {
            get { return _path; }
            set { _path = value.Length > MAX_PATH_LENGTH ? value.Substring(0, MAX_PATH_LENGTH) : value; }
        }

        #endregion

        /// <summary>
        ///   Returns hash code of a track object.
        /// </summary>
        /// <returns>Id is returned as it is unique</returns>
        public override int GetHashCode()
        {
            return _id;
        }
    }
}

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