Click here to Skip to main content
15,892,737 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.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;

namespace DuplicateTracks.ViewModel
{
    public static class Helper
    {
        /// <summary>
        ///   Count the number of music files in the a specific folder and it's subdirectories
        /// </summary>
        /// <param name = "path">Path of the root folder</param>
        /// <param name = "filters">Search filters</param>
        /// <param name = "includeSubdirectories">Include subdirectories in the search</param>
        /// <returns>Number of music files</returns>
        public static int CountNumberOfMusicFiles(string path, IEnumerable<string> filters, bool includeSubdirectories)
        {
            int files = 0;
            DirectoryInfo root = new DirectoryInfo(path);
            foreach (string filter in filters)
            {
                try
                {
                    foreach (FileInfo file in root.GetFiles(filter, SearchOption.TopDirectoryOnly))
                    {
                        try
                        {
                            string temp = file.FullName; /*Dummy call to see if path does not exceed the limit*/
                        }
                        catch (PathTooLongException)
                        {
                            continue;
                        }
                        files++;
                    }
                }
                catch (SecurityException)
                {
                    /*you don't have the permission of viewing this files*/
                    files += 0;
                }
                catch (DirectoryNotFoundException)
                {
                    /*directory not found*/
                    files += 0;
                }
                catch (ArgumentNullException)
                {
                    /*bad parameters*/
                    files += 0;
                }
            }
            if (includeSubdirectories)
            {
                DirectoryInfo[] directories = null;
                try
                {
                    directories = root.GetDirectories();
                }
                catch (DirectoryNotFoundException)
                {
                    /*directory wasn't found*/
                    return files;
                }
                foreach (DirectoryInfo directory in directories)
                {
                    try
                    {
                        files += CountNumberOfMusicFiles(directory.FullName, filters, true);
                    }
                    catch (PathTooLongException)
                    {
                        continue;
                    }
                }
            }
            return files;
        }

        /// <summary>
        ///   Get music files from root folder and it's subdirectories
        /// </summary>
        /// <param name = "path">Path of the root folder</param>
        /// <param name = "filters">Search filters</param>
        /// <param name = "includeSubdirectories">Include subdirectories in the search</param>
        /// <returns>List with the music files</returns>
        public static List<string> GetMusicFiles(string path, IEnumerable<string> filters, bool includeSubdirectories)
        {
            List<string> files = new List<string>();
            DirectoryInfo root = new DirectoryInfo(path);
            foreach (string filter in filters)
            {
                try
                {
                    foreach (FileInfo file in root.GetFiles(filter, SearchOption.TopDirectoryOnly))
                    {
                        try
                        {
                            files.Add(file.FullName);
                        }
                        catch (PathTooLongException)
                        {
                            /*255 item range is too long*/
                            continue;
                        }
                    }
                }
                catch (SecurityException)
                {
                    /*you don't have the permission of viewing this files*/
                }
                catch (DirectoryNotFoundException)
                {
                    /*directory not found*/
                }
                catch (ArgumentNullException)
                {
                    /*bad parameters*/
                }
            }
            if (includeSubdirectories)
            {
                DirectoryInfo[] directories = null;
                try
                {
                    directories = root.GetDirectories();
                }
                catch (DirectoryNotFoundException)
                {
                    /*directory wasn't found*/
                    return files;
                }
                foreach (DirectoryInfo directory in directories)
                {
                    try
                    {
                        files.AddRange(GetMusicFiles(directory.FullName, filters, true));
                    }
                    catch (PathTooLongException)
                    {
                        continue;
                    }
                }
            }
            return files;
        }

        /// <summary>
        ///   Get multiple filter for open file dialog
        /// </summary>
        /// <param name = "caption">Caption</param>
        /// <param name = "filters">List of filters</param>
        /// <returns>Multiple filter</returns>
        public static string GetMultipleFilter(string caption, IEnumerable<string> filters)
        {
            StringBuilder filter = new StringBuilder(caption);
            filter.Append(" (");
            for (int i = 0; i < filters.Count(); i++)
            {
                filter.Append(filters.ElementAt(i));
                if (i != filters.Count() - 1 /*last*/)
                    filter.Append(";");
                else
                {
                    filter.Append(")|");
                    for (int j = 0; j < filters.Count(); j++)
                    {
                        filter.Append(filters.ElementAt(j));
                        if (j != filters.Count() - 1 /*last*/)
                            filter.Append(";");
                    }
                }
            }
            return filter.ToString();
        }
    }
}

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