dejavu

repository·master·Indexed 26 days ago

https://github.com/worldveil/dejavu

An audio fingerprinting and recognition algorithm implemented in Python. Dejavu allows users to memorize audio by storing fingerprints in a MySQL or PostgreSQL database and identifying songs by matching microphone input or disk files against those fingerprints. It includes a Python API and CLI for recognition, as well as Docker Compose support for deployment.

Tokens
2.7K
Snippets
9
Records
14
Agent score
92%

What's inside dejavu

  1. Understand Dejavu performance and accuracy

    master

    Dejavu is an audio fingerprinting system designed for speed and accuracy. Performance characteristics include:

    • Accuracy: High recall even with short audio segments. Testing showed ~60% accuracy with 1 second of audio, ~96% with 2 seconds, and 100% with 5+ seconds. Accuracy remains stable even with compressed streams (e.g., Spotify 160 kbit/s) and environmental noise.
    • Speed: Matching speed is roughly linear relative to the recording time. On tested hardware, matching occurs at approximately 3x listening speed. The bottleneck is the peak finding process, which is single-threaded in the Python implementation.
    • Storage: Fingerprint storage scales with the number of fingerprints generated. For 45 songs, 5.4 million fingerprints occupied 377 MB.

    Note on Latency: Total time to match includes recording time and the Round Trip Time (RTT) to the MySQL database. While RTT adds to the constant overhead, it does not affect the core matching process speed.

  2. Quickstart with Docker

    master

    To run Dejavu using Docker Compose, follow these steps to build the containers, access a shell, and interact with the database.

    1. Build and start containers:

      docker-compose build
      docker-compose up -d
    2. Access the Python container shell:

      docker-compose run python /bin/bash

      Once inside, you can run the example script:

      python example_docker_postgres.py
    3. Connect to the database (Postgres): Inside the container, use psql to connect to the dejavu database. The default password is password (as defined in docker-compose.yml).

      psql -h db -U postgres dejavu
    4. Shut down the environment:

      docker-compose down
    # build and then run our containers
    $ docker-compose build
    $ docker-compose up -d
    
    # get a shell inside the container
    $ docker-compose run python /bin/bash
    
    # connect to the database and poke around
    root@f9ea95ce5cea:/code# psql -h db -U postgres dejavu
    
    # then to shut it all down...
    $ docker-compose down
  3. Install Dejavu on macOS

    master

    For macOS (tested on OS X Mavericks), it is recommended to use Homebrew to install portaudio and ffmpeg. Follow the steps below to install the necessary Python dependencies and link the MySQL client library.

    brew install portaudio
    brew install ffmpeg
    
    sudo easy_install pyaudio
    sudo easy_install pydub
    sudo easy_install numpy
    sudo easy_install scipy
    sudo easy_install matplotlib
    sudo easy_install pip
    
    sudo pip install MySQL-python
    
    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
  4. Fingerprint an audio directory

    master

    To memorize an audio collection, instantiate a Dejavu object with a configuration dictionary and use the fingerprint_directory method. Dejavu is robust; if interrupted, it will skip already fingerprinted songs upon restart.

    fingerprint_directory arguments:

    • input_directory: Path to the directory containing audio files.
    • extensions: A list of audio extensions to look for (e.g., ['.mp3']).
    • num_processes (optional): Number of processes to use for fingerprinting.
    from dejavu import Dejavu
    
    config = {
        "database": {
            "host": "127.0.0.1",
            "user": "root",
            "password": "your_password", 
            "database": "your_database",
        }
    }
    
    djv = Dejavu(config)
    
    # Fingerprint all mp3s in the directory using 3 processes
    djv.fingerprint_directory("va_us_top_40/mp3", [".mp3"], 3)
  5. Tune fingerprint storage and accuracy

    master

    There is a direct trade-off between the required recording time, fingerprint accuracy, and database storage usage. You can adjust the following parameters to balance these needs:

    • Amplitude threshold for peaks: Adjusting this affects how many peaks are stored.
    • Fan value for fingerprinting: Adjusting this affects the density of fingerprints.

    Increasing these values will result in more fingerprints, which bolsters accuracy but increases the storage footprint in the database.

  6. Install Dejavu on Fedora 20+

    master

    To install Dejavu on Fedora 20+, you must first install system dependencies via yum, set up a virtual environment with system site packages enabled to access those dependencies, and then install the PyDejavu package via PyPI or GitHub.

    # Install system dependencies
    sudo yum install numpy scipy python-matplotlib ffmpeg portaudio-devel
    pip install PyAudio
    pip install pydub
    
    # Setup virtualenv with system site packages
    pip install virtualenv
    virtualenv --system-site-packages env_with_system
    
    # Install from PyPI
    source env_with_system/bin/activate
    pip install PyDejavu
    
    # OR install latest code from GitHub
    source env_with_system/bin/activate
    pip install https://github.com/worldveil/dejavu/zipball/master
  7. Configure Dejavu settings

    master

    Dejavu is configured via a Python dictionary passed to the Dejavu constructor.

    Mandatory Keys:

    • database: A dictionary containing connection parameters required by your database driver (e.g., host, user, password, database).

    Optional Keys:

    • database_type: Supports mysql (default) and postgres.
    • fingerprint_limit: Controls how many seconds of each audio file to fingerprint. Use -1 or None to fingerprint the entire file.
    from dejavu import Dejavu
    
    config = {
        "database": {
            "host": "127.0.0.1",
            "user": "root",
            "password": "Password123", 
            "database": "dejavu_db",
        },
        "database_type" : "mysql",
        "fingerprint_limit" : 10
    }
    
    djv = Dejavu(config)
  8. Run Dejavu using Docker Compose

    master

    You can deploy the Dejavu stack (PostgreSQL database and Python environment) using the provided docker-compose.yaml file. This setup creates a private network db_network to allow the python service to communicate with the db service.

    To start the services, run:

    docker-compose up
  9. Recognize audio from a microphone

    master

    You can recognize live audio through a microphone using the CLI or the Python API.

    Via CLI: Specify the number of seconds to listen after the mic keyword.

    python dejavu.py --recognize mic <seconds>

    Via Python API: Use the MicrophoneRecognizer class with the djv.recognize method.

    from dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer
    song = djv.recognize(MicrophoneRecognizer, seconds=10)
    # CLI
    $ python dejavu.py --recognize mic 10
    
    # Python
    from dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer
    song = djv.recognize(MicrophoneRecognizer, seconds=10)
  10. Recognize audio from a file

    master

    You can recognize audio files using either the CLI or the Python API.

    Via CLI:

    python dejavu.py --recognize file <path_to_file>

    Via Python API: Use the FileRecognizer class with the djv.recognize method.

    from dejavu.logic.recognizer.file_recognizer import FileRecognizer
    song = djv.recognize(FileRecognizer, "path/to/audio.wav")
    # CLI
    $ python dejavu.py --recognize file sometrack.wav 
    
    # Python
    from dejavu.logic.recognizer.file_recognizer import FileRecognizer
    song = djv.recognize(FileRecognizer, "va_us_top_40/wav/Mirrors - Justin Timberlake.wav")
  11. Configure the Dejavu Python service via Docker Compose

    master

    The python service is built from ./docker/python and depends on the db service being available. It mounts the current working directory to /code inside the container, allowing for live development or access to local files.

    services:
      python:
        build:
          context: ./docker/python
        volumes:
          - .:/code
        depends_on:
          - db
  12. Configure Dejavu PostgreSQL database via Docker Compose

    master

    The db service uses a PostgreSQL image built from ./docker/postgres. You can configure the database credentials and name using the following environment variables:

    • POSTGRES_DB: The name of the database (defaults to dejavu).
    • POSTGRES_USER: The database user (defaults to postgres).
    • POSTGRES_PASSWORD: The password for the database user (defaults to password).
    services:
      db:
        environment:
          - POSTGRES_DB=dejavu
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=password