Skip to content

Create fingerprints

Sergiu Ciumac edited this page Dec 27, 2021 · 3 revisions

Audio Fingerprints

I've written an extensive explanation of how audio fingerprinting works here. In short, every audio signal has unique spectral properties that distinguish it from others. Like human fingerprints that uniquely identify a person, audio fingerprints can detect and uniquely identify audio tracks.

Below is a short snippet that generates audio fingerprints from an audio file.

public async Task CreateFingerprintsFromFile()
{
    string path = "chopin_short.wav";
    var avHashes = await FingerprintCommandBuilder.Instance
        .BuildFingerprintCommand()
        .From(path)
        .Hash();
    
    Console.WriteLine($"Generate hashes {avHashes}");
}

With default fingerprinting configuration, a 10-second audio snippet will generate 88 fingerprints. These are kept in instances of the HashedFingerprint class.

Notice that the result of a Hash method is an instance of the AVHashes class, which contains Audio and Video hashes. Since we are fingerprinting a file that does not contain video frames, the value of the Video property will be null. If you are interested in Video fingerprints, read how to generate them here

Configuration

Generally, I recommend using library defaults for fingerprint generation and query. Nevertheless, the fingerprinting algorithm can be significantly parametrized if you seek advanced options.

The most important configuration that impacts how many fingerprints the algorithm generates is stride. Stride is the distance measured in samples between consecutive fingerprints. The smaller the stride, the better the recognition rate, as more fingerprints are generated per one second of audio.

Configuring the stride can be done in the following way. Notice that we configure Audio properties only, since we are fingerprinting an audio file:

string path = "chopin_short.wav";
var fingerprints = await FingerprintCommandBuilder.Instance
                .BuildFingerprintCommand()
                .From(path)
                .WithFingerprintConfig(config =>
                {
                    // set stride between consecutive fingerprints to 1_024 samples
                    config.Audio.Stride = new IncrementalStaticStride(1_024);
                    return config;
                })
                .Hash();

The default stride used during fingerprinting is 512, as set in the DefaultFingerprintConfiguration class. The full list of query configuration options can be found here.

Support for different audio formats

Notice how the above example raw wave file was specified at the input. If you want to provide an mp3, ogg, ts, or any other format, you will need to initialize a different audio service. More information about supported audio formats and services can be found here: Audio Services.

Read the following page to learn more about saving and retrieving fingerprints from storage.