Git Product home page Git Product logo

chromaprint.scala's Introduction

Chromaprint.scala

An implementation of the Chromaprint/AcoustID audio fingerprinting algorithm for the JVM, created originally in C++ by Lukáš Lalinský.

Download CircleCI codecov

What does it do?

It creates an audio fingerprint designed to identify near-identical audio tracks in the AcoustID and MusicBrainz databases. This can be used to identify unknown tracks, find duplicates and look up metadata.

How does it work?

The algorithm performs a pipeline of operations on an audio stream to extract its fingerprint:

  • The audio is resampled to mono at 11025Hz
  • It is split into a series of short frames
  • A Fast Fourier Transform is performed on each frame to extract components of different frequencies
  • Audio features such as notes are extracted from each frame
  • An image is created from the series of features
  • A fingerprint is extracted from the image as a long series of numbers
  • This raw fingerprint is compressed into a shorter string
  • The string can be compared with other fingerprints to obtain a similarity score

Installation and usage

Requirements

This package is build for Scala 2.11 and 2.12. The FFmpeg library must be installed, along with codecs for files to be analyzed, the libavcodec-extra package on Debian should provide all of these.

In your build.sbt, add:

resolvers += Resolver.jcenterRepo

libraryDependencies += "com.github.mgdigital" %% "chromaprint" % "0.3.1"

Then in your Scala application:

import chromaprint.quick._

val file = new java.io.File("/audio/Pocket Calculator.flac")
val fingerprint = Fingerprinter(file).unsafeRunSync()

println(fingerprint.compressed)
// AQADtNQYhYkYnGhw7X...

Note: The fingerprint is generated using a FS2 stream, and the Fingerprinter returns a type of IO[Fingerprint], which is a Fingerprint type wrapped in a Cats Effect IO type. Running ùnsafeRunSync() on the IO[Fingerprint] object is the quickest way to have it return the Fingerprint, a different IO method may be better suited to your application.

Java interop

In your pom.xml, add:

    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter</name>
            <url>https://jcenter.bintray.com/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.mgdigital</groupId>
            <artifactId>chromaprint_2.12</artifactId>
            <version>0.3.1</version>
        </dependency>
    </dependencies>

Then in your Java application:

import java.io.File;
import chromaprint.AudioSource;
import chromaprint.Fingerprint;
import chromaprint.quick.Fingerprinter;

File file = new File("/audio/Pocket Calculator.flac");
AudioSource source = new AudioSource.AudioFileSource(file);
Fingerprinter fingerprinter = new Fingerprinter();
Fingerprint fingerprint = fingerprinter.apply(source).unsafeRunSync();
System.out.println(fingerprint.compressed());
// AQADtNQYhYkYnGhw7X...

Build the Docker image

$ docker build -t chromaprint-scala .

Then run the command line app (assuming audio file in current folder, double quotes needed for filenames containing spaces).

$ docker run -ti -v $(pwd):/audio/ chromaprint-scala '"/audio/Pocket Calculator.flac"'

Identify track with AcoustID

Get an AcoustID client ID and provide the acoustid-client option in the command line app to lookup matches in the AcoustID database:

$ sbt run "/audio/Pocket Calculator.flac" --acoustid-client=MyClientId
[info] Running chromaprint.Main /audio/Pocket Calculator.flac --acoustid-client=MyClientId
Fingerprinting...
Created fingerprint in 1.583s
Duration: 296.80008
Fingerprint (compressed): AQADtNQYhYkYnGhw7X....
AcoustID response received
c0c5a16f-64e0-4571-aa29-ba80e6cb7874: Result 1 with score 0.923195:
867cf1a1-c46a-4ad5-916f-0c5397ff65e5: 'Pocket Calculator' by 'Kraftwerk'
...

See the code for the command line app under chromaprint.cli for further examples.

Note that the fingerprinter requires an implicit for the FFT (Fast Fourier Transform) implementation. Adapters are provided for Breeze and through FFTW via JavaCPP Presets. I have seen intermittent errors in the FFTW adapter so would recommend using Breeze.

Performance

Performance was never going to be as good as the C++ library, which can calculate a fingerprint almost instantly - but it does need to be fast enough to be conveniently usable. On my laptop I can generate a first fingerprint in around 1.6 seconds from a standing start, with subsequent fingerprints generated in around 0.5 seconds (assuming audio files which need transcoding to PCM and resampling).


Copyright (c) 2019 Mike Gibson, https://github.com/mgdigital. Original Chromaprint algorithm Copyright (c) Lukáš Lalinský.


For Chris.

chromaprint.scala's People

Contributors

mgdigital avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

chromaprint.scala's Issues

java.lang.ArrayIndexOutOfBoundsException in unsafeRunSync()

Hey there!
Im writing a Java application wich supposed to receive audio files, calculate fingerprint, and search track based on it.
But Im facing a problem that the sample Java interop code you mention in README doesnt work.

Here is my code

        File file = new File("song.mp3");
        System.out.println(file.exists());
        System.out.println(file.length());
        AudioSource source = new AudioSource.AudioFileSource(file);
        Fingerprinter fingerprinter = new Fingerprinter();
        Fingerprint fingerprint = fingerprinter.apply(source).unsafeRunSync();
        System.out.println(fingerprint.compressed());

Here is the exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
	at javazoom.spi.mpeg.sampled.convert.DecodedMpegAudioInputStream$DMAISObuffer.append(DecodedMpegAudioInputStream.java:294)
	at javazoom.jl.decoder.Obuffer.appendSamples(Obuffer.java:59)
	at javazoom.jl.decoder.SynthesisFilter.compute_pcm_samples(SynthesisFilter.java:1510)
	at javazoom.jl.decoder.SynthesisFilter.calculate_pcm_samples(SynthesisFilter.java:1550)
	at javazoom.jl.decoder.LayerIIIDecoder.decode(LayerIIIDecoder.java:324)
	at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(LayerIIIDecoder.java:219)
	at javazoom.jl.decoder.Decoder.decodeFrame(Decoder.java:147)
	at javazoom.spi.mpeg.sampled.convert.DecodedMpegAudioInputStream.execute(DecodedMpegAudioInputStream.java:185)
	at org.tritonus.share.TCircularBuffer.read(TCircularBuffer.java:138)
	at org.tritonus.share.sampled.convert.TAsynchronousFilteredAudioInputStream.read(TAsynchronousFilteredAudioInputStream.java:197)
	at org.tritonus.share.sampled.convert.TAsynchronousFilteredAudioInputStream.read(TAsynchronousFilteredAudioInputStream.java:182)
	at chromaprint.AudioSource$.$anonfun$audioInputStreamToByteStream$8(AudioSource.scala:59)
	at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:87)
	at cats.effect.internals.IORunLoop$RestartCallback.signal(IORunLoop.scala:355)
	at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:376)
	at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:316)
	at cats.effect.internals.IOShift$Tick.run(IOShift.scala:36)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

Also, here is the track: https://mega.nz/file/mO5DTbRA#f-3EUFlPbHs47eYfNMLiElaA3gbA4yJzP-8RtZR1V1c

Java version: 1.8
Library version: 0.3.1

What am I doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.