[
  {
    "path": ".gitignore",
    "content": "*.pyc\nwav\nmp3\n*.wav\n*.mp3\n.DS_Store\n*.cnf\n"
  },
  {
    "path": "INSTALLATION.md",
    "content": "# Installation of Dejavu\n\nSo far Dejavu has only been tested on Unix systems.\n\n* [`pyaudio`](http://people.csail.mit.edu/hubert/pyaudio/) for grabbing audio from microphone\n* [`ffmpeg`](https://github.com/FFmpeg/FFmpeg) for converting audio files to .wav format\n* [`pydub`](http://pydub.com/), a Python `ffmpeg` wrapper\n* [`numpy`](http://www.numpy.org/) for taking the FFT of audio signals\n* [`scipy`](http://www.scipy.org/), used in peak finding algorithms\n* [`matplotlib`](http://matplotlib.org/), used for spectrograms and plotting\n* [`MySQLdb`](http://mysql-python.sourceforge.net/MySQLdb.html) for interfacing with MySQL databases\n\nFor installing `ffmpeg` on Mac OS X, I highly recommend [this post](http://jungels.net/articles/ffmpeg-howto.html).\n\n## Fedora 20+\n\n### Dependency installation on Fedora 20+\n\nInstall the dependencies:\n\n    sudo yum install numpy scipy python-matplotlib ffmpeg portaudio-devel\n    pip install PyAudio\n    pip install pydub\n    \nNow setup virtualenv ([howto?](http://www.pythoncentral.io/how-to-install-virtualenv-python/)):\n\n    pip install virtualenv\n    virtualenv --system-site-packages env_with_system\n\nInstall from PyPI:\n\n    source env_with_system/bin/activate\n    pip install PyDejavu\n\n\nYou can also install the latest code from GitHub:\n\n    source env_with_system/bin/activate\n    pip install https://github.com/worldveil/dejavu/zipball/master\n\n## Max OS X\n\n### Dependency installation for Mac OS X\n\nTested on OS X Mavericks. An option is to install [Homebrew](http://brew.sh) and do the following:\n\n```\nbrew install portaudio\nbrew install ffmpeg\n\nsudo easy_install pyaudio\nsudo easy_install pydub\nsudo easy_install numpy\nsudo easy_install scipy\nsudo easy_install matplotlib\nsudo easy_install pip\n\nsudo pip install MySQL-python\n\nsudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib\n```\n\nHowever installing `portaudio` and/or `ffmpeg` from source is also doable. \n"
  },
  {
    "path": "LICENSE.md",
    "content": "### MIT License\n\nCopyright (c) 2013 Will Drevo\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
  },
  {
    "path": "MANIFEST.in",
    "content": "include requirements.txt\n"
  },
  {
    "path": "README.md",
    "content": "dejavu\n==========\n\nAudio fingerprinting and recognition algorithm implemented in Python, see the explanation here:  \n[How it works](http://willdrevo.com/fingerprinting-and-audio-recognition-with-python/)\n\nDejavu can memorize audio by listening to it once and fingerprinting it. Then by playing a song and recording microphone input or reading from disk, Dejavu attempts to match the audio against the fingerprints held in the database, returning the song being played. \n\nNote: for voice recognition, *Dejavu is not the right tool!* Dejavu excels at recognition of exact signals with reasonable amounts of noise.\n\n## Quickstart with Docker\n\nFirst, install [Docker](https://docs.docker.com/get-docker/).\n\n```shell\n# build and then run our containers\n$ docker-compose build\n$ docker-compose up -d\n\n# get a shell inside the container\n$ docker-compose run python /bin/bash\nStarting dejavu_db_1 ... done\nroot@f9ea95ce5cea:/code# python example_docker_postgres.py \nFingerprinting channel 1/2 for test/woodward_43s.wav\nFingerprinting channel 1/2 for test/sean_secs.wav\n...\n\n# connect to the database and poke around\nroot@f9ea95ce5cea:/code# psql -h db -U postgres dejavu\nPassword for user postgres:  # type \"password\", as specified in the docker-compose.yml !\npsql (11.7 (Debian 11.7-0+deb10u1), server 10.7)\nType \"help\" for help.\n\ndejavu=# \\dt\n            List of relations\n Schema |     Name     | Type  |  Owner   \n--------+--------------+-------+----------\n public | fingerprints | table | postgres\n public | songs        | table | postgres\n(2 rows)\n\ndejavu=# select * from fingerprints limit 5;\n          hash          | song_id | offset |        date_created        |       date_modified        \n------------------------+---------+--------+----------------------------+----------------------------\n \\x71ffcb900d06fe642a18 |       1 |    137 | 2020-06-03 05:14:19.400153 | 2020-06-03 05:14:19.400153\n \\xf731d792977330e6cc9f |       1 |    148 | 2020-06-03 05:14:19.400153 | 2020-06-03 05:14:19.400153\n \\x71ff24aaeeb55d7b60c4 |       1 |    146 | 2020-06-03 05:14:19.400153 | 2020-06-03 05:14:19.400153\n \\x29349c79b317d45a45a8 |       1 |    101 | 2020-06-03 05:14:19.400153 | 2020-06-03 05:14:19.400153\n \\x5a052144e67d2248ccf4 |       1 |    123 | 2020-06-03 05:14:19.400153 | 2020-06-03 05:14:19.400153\n(10 rows)\n\n# then to shut it all down...\n$ docker-compose down\n```\n\nIf you want to be able to use the microphone with the Docker container, you'll need to do a [little extra work](https://stackoverflow.com/questions/43312975/record-sound-on-ubuntu-docker-image). I haven't had the time to write this up, but if anyone wants to make a PR, I'll happily merge.\n\n## Docker alternative on local machine\n\nFollow instructions in [INSTALLATION.md](INSTALLATION.md)\n\nNext, you'll need to create a MySQL database where Dejavu can store fingerprints. For example, on your local setup:\n\t\n\t$ mysql -u root -p\n\tEnter password: **********\n\tmysql> CREATE DATABASE IF NOT EXISTS dejavu;\n\nNow you're ready to start fingerprinting your audio collection! \n\nYou may also use Postgres, of course. The same method applies.\n\n## Fingerprinting\n\nLet's say we want to fingerprint all of July 2013's VA US Top 40 hits. \n\nStart by creating a Dejavu object with your configurations settings (Dejavu takes an ordinary Python dictionary for the settings).\n\n```python\n>>> from dejavu import Dejavu\n>>> config = {\n...     \"database\": {\n...         \"host\": \"127.0.0.1\",\n...         \"user\": \"root\",\n...         \"password\": <password above>, \n...         \"database\": <name of the database you created above>,\n...     }\n... }\n>>> djv = Dejavu(config)\n```\n\nNext, give the `fingerprint_directory` method three arguments:\n* input directory to look for audio files\n* audio extensions to look for in the input directory\n* number of processes (optional)\n\n```python\n>>> djv.fingerprint_directory(\"va_us_top_40/mp3\", [\".mp3\"], 3)\n```\n\nFor a large amount of files, this will take a while. However, Dejavu is robust enough you can kill and restart without affecting progress: Dejavu remembers which songs it fingerprinted and converted and which it didn't, and so won't repeat itself. \n\nYou'll have a lot of fingerprints once it completes a large folder of mp3s:\n```python\n>>> print djv.db.get_num_fingerprints()\n5442376\n```\n\nAlso, any subsequent calls to `fingerprint_file` or `fingerprint_directory` will fingerprint and add those songs to the database as well. It's meant to simulate a system where as new songs are released, they are fingerprinted and added to the database seemlessly without stopping the system. \n\n## Configuration options\n\nThe configuration object to the Dejavu constructor must be a dictionary. \n\nThe following keys are mandatory:\n\n* `database`, with a value as a dictionary with keys that the database you are using will accept. For example with MySQL, the keys must can be anything that the [`MySQLdb.connect()`](http://mysql-python.sourceforge.net/MySQLdb.html) function will accept. \n\nThe following keys are optional:\n\n* `fingerprint_limit`: allows you to control how many seconds of each audio file to fingerprint. Leaving out this key, or alternatively using `-1` and `None` will cause Dejavu to fingerprint the entire audio file. Default value is `None`.\n* `database_type`: `mysql` (the default value) and `postgres` are supported. If you'd like to add another subclass for `BaseDatabase` and implement a new type of database, please fork and send a pull request!\n\nAn example configuration is as follows:\n\n```python\n>>> from dejavu import Dejavu\n>>> config = {\n...     \"database\": {\n...         \"host\": \"127.0.0.1\",\n...         \"user\": \"root\",\n...         \"password\": \"Password123\", \n...         \"database\": \"dejavu_db\",\n...     },\n...     \"database_type\" : \"mysql\",\n...     \"fingerprint_limit\" : 10\n... }\n>>> djv = Dejavu(config)\n```\n\n## Tuning\n\nInside `config/settings.py`, you may want to adjust following parameters (some values are given below).\n\n    FINGERPRINT_REDUCTION = 30\n    PEAK_SORT = False\n    DEFAULT_OVERLAP_RATIO = 0.4\n    DEFAULT_FAN_VALUE = 5\n    DEFAULT_AMP_MIN = 10\n    PEAK_NEIGHBORHOOD_SIZE = 10\n    \nThese parameters are described within the file in detail. Read that in-order to understand the impact of changing these values.\n\n## Recognizing\n\nThere are two ways to recognize audio using Dejavu. You can recognize by reading and processing files on disk, or through your computer's microphone.\n\n### Recognizing: On Disk\n\nThrough the terminal:\n\n```bash\n$ python dejavu.py --recognize file sometrack.wav \n{'total_time': 2.863781690597534, 'fingerprint_time': 2.4306554794311523, 'query_time': 0.4067542552947998, 'align_time': 0.007731199264526367, 'results': [{'song_id': 1, 'song_name': 'Taylor Swift - Shake It Off', 'input_total_hashes': 76168, 'fingerprinted_hashes_in_db': 4919, 'hashes_matched_in_input': 794, 'input_confidence': 0.01, 'fingerprinted_confidence': 0.16, 'offset': -924, 'offset_seconds': -30.00018, 'file_sha1': b'3DC269DF7B8DB9B30D2604DA80783155912593E8'}, {...}, ...]}\n```\n\nor in scripting, assuming you've already instantiated a Dejavu object: \n\n```python\n>>> from dejavu.logic.recognizer.file_recognizer import FileRecognizer\n>>> song = djv.recognize(FileRecognizer, \"va_us_top_40/wav/Mirrors - Justin Timberlake.wav\")\n```\n\n### Recognizing: Through a Microphone\n\nWith scripting:\n\n```python\n>>> from dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer\n>>> song = djv.recognize(MicrophoneRecognizer, seconds=10) # Defaults to 10 seconds.\n```\n\nand with the command line script, you specify the number of seconds to listen:\n\n```bash\n$ python dejavu.py --recognize mic 10\n```\n\n## Testing\n\nTesting out different parameterizations of the fingerprinting algorithm is often useful as the corpus becomes larger and larger, and inevitable tradeoffs between speed and accuracy come into play. \n\n![Confidence](plots/confidence.png)\n\nTest your Dejavu settings on a corpus of audio files on a number of different metrics:\n\n* Confidence of match (number fingerprints aligned)\n* Offset matching accuracy\n* Song matching accuracy\n* Time to match\n\n![Accuracy](plots/matching_graph.png)\n\nAn example script is given in `test_dejavu.sh`, shown below:\n\n```bash\n#####################################\n### Dejavu example testing script ###\n#####################################\n\n###########\n# Clear out previous results\nrm -rf ./results ./temp_audio\n\n###########\n# Fingerprint files of extension mp3 in the ./mp3 folder\npython dejavu.py --fingerprint ./mp3/ mp3\n\n##########\n# Run a test suite on the ./mp3 folder by extracting 1, 2, 3, 4, and 5 \n# second clips sampled randomly from within each song 8 seconds \n# away from start or end, sampling offset with random seed = 42, and finally, \n# store results in ./results and log to ./results/dejavu-test.log\npython run_tests.py \\\n    --secs 5 \\\n    --temp ./temp_audio \\\n    --log-file ./results/dejavu-test.log \\\n    --padding 8 \\\n    --seed 42 \\\n    --results ./results \\\n    ./mp3\n```\n\nThe testing scripts are as of now are a bit rough, and could certainly use some love and attention if you're interested in submitting a PR! For example, underscores in audio filenames currently [breaks](https://github.com/worldveil/dejavu/issues/63) the test scripts. \n\n## How does it work?\n\nThe algorithm works off a fingerprint based system, much like:\n\n* [Shazam](http://www.ee.columbia.edu/~dpwe/papers/Wang03-shazam.pdf)\n* [MusicRetrieval](http://www.cs.cmu.edu/~yke/musicretrieval/)\n* [Chromaprint](https://oxygene.sk/2011/01/how-does-chromaprint-work/)\n\nThe \"fingerprints\" are locality sensitive hashes that are computed from the spectrogram of the audio. This is done by taking the FFT of the signal over overlapping windows of the song and identifying peaks. A very robust peak finding algorithm is needed, otherwise you'll have a terrible signal to noise ratio.\n\nHere I've taken the spectrogram over the first few seconds of \"Blurred Lines\". The spectrogram is a 2D plot and shows amplitude as a function of time (a particular window, actually) and frequency, binned logrithmically, just as the human ear percieves it. In the plot below you can see where local maxima occur in the amplitude space:\n\n![Spectrogram](plots/spectrogram_peaks.png)\n\nFinding these local maxima is a combination of a high pass filter (a threshold in amplitude space) and some image processing techniques to find maxima. A concept of a \"neighboorhood\" is needed - a local maxima with only its directly adjacent pixels is a poor peak - one that will not survive the noise of coming through speakers and through a microphone.\n\nIf we zoom in even closer, we can begin to imagine how to bin and discretize these peaks. Finding the peaks itself is the most computationally intensive part, but it's not the end. Peaks are combined using their discrete time and frequency bins to create a unique hash for that particular moment in the song - creating a fingerprint.\n\n![Spectgram zoomed](plots/spectrogram_zoomed.png)\n\nFor a more detailed look at the making of Dejavu, see my blog post [here](https://willdrevo.com/fingerprinting-and-audio-recognition-with-python/).\n\n## How well it works\n\nTo truly get the benefit of an audio fingerprinting system, it can't take a long time to fingerprint. It's a bad user experience, and furthermore, a user may only decide to try to match the song with only a few precious seconds of audio left before the radio station goes to a commercial break.\n\nTo test Dejavu's speed and accuracy, I fingerprinted a list of 45 songs from the US VA Top 40 from July 2013 (I know, their counting is off somewhere). I tested in three ways:\n\n1. Reading from disk the raw mp3 -> wav data, and\n1. Playing the song over the speakers with Dejavu listening on the laptop microphone.\n1. Compressed streamed music played on my iPhone\n\nBelow are the results.\n\n### 1. Reading from Disk\n\nReading from disk was an overwhelming 100% recall - no mistakes were made over the 45 songs I fingerprinted. Since Dejavu gets all of the samples from the song (without noise), it would be nasty surprise if reading the same file from disk didn't work every time!\n\n### 2. Audio over laptop microphone\n\nHere I wrote a script to randomly chose `n` seconds of audio from the original mp3 file to play and have Dejavu listen over the microphone. To be fair I only allowed segments of audio that were more than 10 seconds from the starting/ending of the track to avoid listening to silence. \n\nAdditionally my friend was even talking and I was humming along a bit during the whole process, just to throw in some noise.\n\nHere are the results for different values of listening time (`n`):\n\n![Matching time](plots/accuracy.png)\n\nThis is pretty rad. For the percentages:\n\nNumber of Seconds | Number Correct | Percentage Accuracy\n----|----|----\n1 | 27 / 45 | 60.0%\n2 | 43 / 45 | 95.6%\n3 | 44 / 45 | 97.8%\n4 | 44 / 45 | 97.8%\n5 | 45 / 45 | 100.0%\n6 | 45 / 45 | 100.0%\n\nEven with only a single second, randomly chosen from anywhere in the song, Dejavu is getting 60%! One extra second to 2 seconds get us to around 96%, while getting perfect only took 5 seconds or more. Honestly when I was testing this myself, I found Dejavu beat me - listening to only 1-2 seconds of a song out of context to identify is pretty hard. I had even been listening to these same songs for two days straight while debugging...\n\nIn conclusion, Dejavu works amazingly well, even with next to nothing to work with. \n\n### 3. Compressed streamed music played on my iPhone\n\nJust to try it out, I tried playing music from my Spotify account (160 kbit/s compressed) through my iPhone's speakers with Dejavu again listening on my MacBook mic. I saw no degredation in performance; 1-2 seconds was enough to recognize any of the songs.\n\n## Performance\n\n### Speed\n\nOn my MacBook Pro, matching was done at 3x listening speed with a small constant overhead. To test, I tried different recording times and plotted the recording time plus the time to match. Since the speed is mostly invariant of the particular song and more dependent on the length of the spectrogram created, I tested on a single song, \"Get Lucky\" by Daft Punk:\n\n![Matching time](plots/matching_time.png)\n\nAs you can see, the relationship is quite linear. The line you see is a least-squares linear regression fit to the data, with the corresponding line equation:\n\n    1.364757 * record_time - 0.034373 = time_to_match\n    \nNotice of course since the matching itself is single threaded, the matching time includes the recording time. This makes sense with the 3x speed in purely matching, as:\n    \n    1 (recording) + 1/3 (matching) = 4/3 ~= 1.364757\n    \nif we disregard the miniscule constant term.\n\nThe overhead of peak finding is the bottleneck - I experimented with multithreading and realtime matching, and alas, it wasn't meant to be in Python. An equivalent Java or C/C++ implementation would most likely have little trouble keeping up, applying FFT and peakfinding in realtime.\n\nAn important caveat is of course, the round trip time (RTT) for making matches. Since my MySQL instance was local, I didn't have to deal with the latency penalty of transfering fingerprint matches over the air. This would add RTT to the constant term in the overall calculation, but would not effect the matching process. \n\n### Storage\n\nFor the 45 songs I fingerprinted, the database used 377 MB of space for 5.4 million fingerprints. In comparison, the disk usage is given below:\n\nAudio Information Type | Storage in MB \n----|----\nmp3 | 339\nwav | 1885\nfingerprints | 377\n\nThere's a pretty direct trade-off between the necessary record time and the amount of storage needed. Adjusting the amplitude threshold for peaks and the fan value for fingerprinting will add more fingerprints and bolster the accuracy at the expense of more space. "
  },
  {
    "path": "dejavu/__init__.py",
    "content": "import multiprocessing\nimport os\nimport sys\nimport traceback\nfrom itertools import groupby\nfrom time import time\nfrom typing import Dict, List, Tuple\n\nimport dejavu.logic.decoder as decoder\nfrom dejavu.base_classes.base_database import get_database\nfrom dejavu.config.settings import (DEFAULT_FS, DEFAULT_OVERLAP_RATIO,\n                                    DEFAULT_WINDOW_SIZE, FIELD_FILE_SHA1,\n                                    FIELD_TOTAL_HASHES,\n                                    FINGERPRINTED_CONFIDENCE,\n                                    FINGERPRINTED_HASHES, HASHES_MATCHED,\n                                    INPUT_CONFIDENCE, INPUT_HASHES, OFFSET,\n                                    OFFSET_SECS, SONG_ID, SONG_NAME, TOPN)\nfrom dejavu.logic.fingerprint import fingerprint\n\n\nclass Dejavu:\n    def __init__(self, config):\n        self.config = config\n\n        # initialize db\n        db_cls = get_database(config.get(\"database_type\", \"mysql\").lower())\n\n        self.db = db_cls(**config.get(\"database\", {}))\n        self.db.setup()\n\n        # if we should limit seconds fingerprinted,\n        # None|-1 means use entire track\n        self.limit = self.config.get(\"fingerprint_limit\", None)\n        if self.limit == -1:  # for JSON compatibility\n            self.limit = None\n        self.__load_fingerprinted_audio_hashes()\n\n    def __load_fingerprinted_audio_hashes(self) -> None:\n        \"\"\"\n        Keeps a dictionary with the hashes of the fingerprinted songs, in that way is possible to check\n        whether or not an audio file was already processed.\n        \"\"\"\n        # get songs previously indexed\n        self.songs = self.db.get_songs()\n        self.songhashes_set = set()  # to know which ones we've computed before\n        for song in self.songs:\n            song_hash = song[FIELD_FILE_SHA1]\n            self.songhashes_set.add(song_hash)\n\n    def get_fingerprinted_songs(self) -> List[Dict[str, any]]:\n        \"\"\"\n        To pull all fingerprinted songs from the database.\n\n        :return: a list of fingerprinted audios from the database.\n        \"\"\"\n        return self.db.get_songs()\n\n    def delete_songs_by_id(self, song_ids: List[int]) -> None:\n        \"\"\"\n        Deletes all audios given their ids.\n\n        :param song_ids: song ids to delete from the database.\n        \"\"\"\n        self.db.delete_songs_by_id(song_ids)\n\n    def fingerprint_directory(self, path: str, extensions: str, nprocesses: int = None) -> None:\n        \"\"\"\n        Given a directory and a set of extensions it fingerprints all files that match each extension specified.\n\n        :param path: path to the directory.\n        :param extensions: list of file extensions to consider.\n        :param nprocesses: amount of processes to fingerprint the files within the directory.\n        \"\"\"\n        # Try to use the maximum amount of processes if not given.\n        try:\n            nprocesses = nprocesses or multiprocessing.cpu_count()\n        except NotImplementedError:\n            nprocesses = 1\n        else:\n            nprocesses = 1 if nprocesses <= 0 else nprocesses\n\n        pool = multiprocessing.Pool(nprocesses)\n\n        filenames_to_fingerprint = []\n        for filename, _ in decoder.find_files(path, extensions):\n            # don't refingerprint already fingerprinted files\n            if decoder.unique_hash(filename) in self.songhashes_set:\n                print(f\"{filename} already fingerprinted, continuing...\")\n                continue\n\n            filenames_to_fingerprint.append(filename)\n\n        # Prepare _fingerprint_worker input\n        worker_input = list(zip(filenames_to_fingerprint, [self.limit] * len(filenames_to_fingerprint)))\n\n        # Send off our tasks\n        iterator = pool.imap_unordered(Dejavu._fingerprint_worker, worker_input)\n\n        # Loop till we have all of them\n        while True:\n            try:\n                song_name, hashes, file_hash = next(iterator)\n            except multiprocessing.TimeoutError:\n                continue\n            except StopIteration:\n                break\n            except Exception:\n                print(\"Failed fingerprinting\")\n                # Print traceback because we can't reraise it here\n                traceback.print_exc(file=sys.stdout)\n            else:\n                sid = self.db.insert_song(song_name, file_hash, len(hashes))\n\n                self.db.insert_hashes(sid, hashes)\n                self.db.set_song_fingerprinted(sid)\n                self.__load_fingerprinted_audio_hashes()\n\n        pool.close()\n        pool.join()\n\n    def fingerprint_file(self, file_path: str, song_name: str = None) -> None:\n        \"\"\"\n        Given a path to a file the method generates hashes for it and stores them in the database\n        for later be queried.\n\n        :param file_path: path to the file.\n        :param song_name: song name associated to the audio file.\n        \"\"\"\n        song_name_from_path = decoder.get_audio_name_from_path(file_path)\n        song_hash = decoder.unique_hash(file_path)\n        song_name = song_name or song_name_from_path\n        # don't refingerprint already fingerprinted files\n        if song_hash in self.songhashes_set:\n            print(f\"{song_name} already fingerprinted, continuing...\")\n        else:\n            song_name, hashes, file_hash = Dejavu._fingerprint_worker(\n                file_path,\n                self.limit,\n                song_name=song_name\n            )\n            sid = self.db.insert_song(song_name, file_hash)\n\n            self.db.insert_hashes(sid, hashes)\n            self.db.set_song_fingerprinted(sid)\n            self.__load_fingerprinted_audio_hashes()\n\n    def generate_fingerprints(self, samples: List[int], Fs=DEFAULT_FS) -> Tuple[List[Tuple[str, int]], float]:\n        f\"\"\"\n        Generate the fingerprints for the given sample data (channel).\n\n        :param samples: list of ints which represents the channel info of the given audio file.\n        :param Fs: sampling rate which defaults to {DEFAULT_FS}.\n        :return: a list of tuples for hash and its corresponding offset, together with the generation time.\n        \"\"\"\n        t = time()\n        hashes = fingerprint(samples, Fs=Fs)\n        fingerprint_time = time() - t\n        return hashes, fingerprint_time\n\n    def find_matches(self, hashes: List[Tuple[str, int]]) -> Tuple[List[Tuple[int, int]], Dict[str, int], float]:\n        \"\"\"\n        Finds the corresponding matches on the fingerprinted audios for the given hashes.\n\n        :param hashes: list of tuples for hashes and their corresponding offsets\n        :return: a tuple containing the matches found against the db, a dictionary which counts the different\n         hashes matched for each song (with the song id as key), and the time that the query took.\n\n        \"\"\"\n        t = time()\n        matches, dedup_hashes = self.db.return_matches(hashes)\n        query_time = time() - t\n\n        return matches, dedup_hashes, query_time\n\n    def align_matches(self, matches: List[Tuple[int, int]], dedup_hashes: Dict[str, int], queried_hashes: int,\n                      topn: int = TOPN) -> List[Dict[str, any]]:\n        \"\"\"\n        Finds hash matches that align in time with other matches and finds\n        consensus about which hashes are \"true\" signal from the audio.\n\n        :param matches: matches from the database\n        :param dedup_hashes: dictionary containing the hashes matched without duplicates for each song\n        (key is the song id).\n        :param queried_hashes: amount of hashes sent for matching against the db\n        :param topn: number of results being returned back.\n        :return: a list of dictionaries (based on topn) with match information.\n        \"\"\"\n        # count offset occurrences per song and keep only the maximum ones.\n        sorted_matches = sorted(matches, key=lambda m: (m[0], m[1]))\n        counts = [(*key, len(list(group))) for key, group in groupby(sorted_matches, key=lambda m: (m[0], m[1]))]\n        songs_matches = sorted(\n            [max(list(group), key=lambda g: g[2]) for key, group in groupby(counts, key=lambda count: count[0])],\n            key=lambda count: count[2], reverse=True\n        )\n\n        songs_result = []\n        for song_id, offset, _ in songs_matches[0:topn]:  # consider topn elements in the result\n            song = self.db.get_song_by_id(song_id)\n\n            song_name = song.get(SONG_NAME, None)\n            song_hashes = song.get(FIELD_TOTAL_HASHES, None)\n            nseconds = round(float(offset) / DEFAULT_FS * DEFAULT_WINDOW_SIZE * DEFAULT_OVERLAP_RATIO, 5)\n            hashes_matched = dedup_hashes[song_id]\n\n            song = {\n                SONG_ID: song_id,\n                SONG_NAME: song_name.encode(\"utf8\"),\n                INPUT_HASHES: queried_hashes,\n                FINGERPRINTED_HASHES: song_hashes,\n                HASHES_MATCHED: hashes_matched,\n                # Percentage regarding hashes matched vs hashes from the input.\n                INPUT_CONFIDENCE: round(hashes_matched / queried_hashes, 2),\n                # Percentage regarding hashes matched vs hashes fingerprinted in the db.\n                FINGERPRINTED_CONFIDENCE: round(hashes_matched / song_hashes, 2),\n                OFFSET: offset,\n                OFFSET_SECS: nseconds,\n                FIELD_FILE_SHA1: song.get(FIELD_FILE_SHA1, None).encode(\"utf8\")\n            }\n\n            songs_result.append(song)\n\n        return songs_result\n\n    def recognize(self, recognizer, *options, **kwoptions) -> Dict[str, any]:\n        r = recognizer(self)\n        return r.recognize(*options, **kwoptions)\n\n    @staticmethod\n    def _fingerprint_worker(arguments):\n        # Pool.imap sends arguments as tuples so we have to unpack\n        # them ourself.\n        try:\n            file_name, limit = arguments\n        except ValueError:\n            pass\n\n        song_name, extension = os.path.splitext(os.path.basename(file_name))\n\n        fingerprints, file_hash = Dejavu.get_file_fingerprints(file_name, limit, print_output=True)\n\n        return song_name, fingerprints, file_hash\n\n    @staticmethod\n    def get_file_fingerprints(file_name: str, limit: int, print_output: bool = False):\n        channels, fs, file_hash = decoder.read(file_name, limit)\n        fingerprints = set()\n        channel_amount = len(channels)\n        for channeln, channel in enumerate(channels, start=1):\n            if print_output:\n                print(f\"Fingerprinting channel {channeln}/{channel_amount} for {file_name}\")\n\n            hashes = fingerprint(channel, Fs=fs)\n\n            if print_output:\n                print(f\"Finished channel {channeln}/{channel_amount} for {file_name}\")\n\n            fingerprints |= set(hashes)\n\n        return fingerprints, file_hash\n"
  },
  {
    "path": "dejavu/base_classes/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/base_classes/base_database.py",
    "content": "import abc\nimport importlib\nfrom typing import Dict, List, Tuple\n\nfrom dejavu.config.settings import DATABASES\n\n\nclass BaseDatabase(object, metaclass=abc.ABCMeta):\n    # Name of your Database subclass, this is used in configuration\n    # to refer to your class\n    type = None\n\n    def __init__(self):\n        super().__init__()\n\n    def before_fork(self) -> None:\n        \"\"\"\n        Called before the database instance is given to the new process\n        \"\"\"\n        pass\n\n    def after_fork(self) -> None:\n        \"\"\"\n        Called after the database instance has been given to the new process\n\n        This will be called in the new process.\n        \"\"\"\n        pass\n\n    def setup(self) -> None:\n        \"\"\"\n        Called on creation or shortly afterwards.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def empty(self) -> None:\n        \"\"\"\n        Called when the database should be cleared of all data.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def delete_unfingerprinted_songs(self) -> None:\n        \"\"\"\n        Called to remove any song entries that do not have any fingerprints\n        associated with them.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def get_num_songs(self) -> int:\n        \"\"\"\n        Returns the song's count stored.\n\n        :return: the amount of songs in the database.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def get_num_fingerprints(self) -> int:\n        \"\"\"\n        Returns the fingerprints' count stored.\n\n        :return: the number of fingerprints in the database.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def set_song_fingerprinted(self, song_id: int):\n        \"\"\"\n        Sets a specific song as having all fingerprints in the database.\n\n        :param song_id: song identifier.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def get_songs(self) -> List[Dict[str, str]]:\n        \"\"\"\n        Returns all fully fingerprinted songs in the database\n\n        :return: a dictionary with the songs info.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def get_song_by_id(self, song_id: int) -> Dict[str, str]:\n        \"\"\"\n        Brings the song info from the database.\n\n        :param song_id: song identifier.\n        :return: a song by its identifier. Result must be a Dictionary.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def insert(self, fingerprint: str, song_id: int, offset: int):\n        \"\"\"\n        Inserts a single fingerprint into the database.\n\n        :param fingerprint: Part of a sha1 hash, in hexadecimal format\n        :param song_id: Song identifier this fingerprint is off\n        :param offset: The offset this fingerprint is from.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def insert_song(self, song_name: str, file_hash: str, total_hashes: int) -> int:\n        \"\"\"\n        Inserts a song name into the database, returns the new\n        identifier of the song.\n\n        :param song_name: The name of the song.\n        :param file_hash: Hash from the fingerprinted file.\n        :param total_hashes: amount of hashes to be inserted on fingerprint table.\n        :return: the inserted id.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def query(self, fingerprint: str = None) -> List[Tuple]:\n        \"\"\"\n        Returns all matching fingerprint entries associated with\n        the given hash as parameter, if None is passed it returns all entries.\n\n        :param fingerprint: part of a sha1 hash, in hexadecimal format\n        :return: a list of fingerprint records stored in the db.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def get_iterable_kv_pairs(self) -> List[Tuple]:\n        \"\"\"\n        Returns all fingerprints in the database.\n\n        :return: a list containing all fingerprints stored in the db.\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def insert_hashes(self, song_id: int, hashes: List[Tuple[str, int]], batch_size: int = 1000) -> None:\n        \"\"\"\n        Insert a multitude of fingerprints.\n\n        :param song_id: Song identifier the fingerprints belong to\n        :param hashes: A sequence of tuples in the format (hash, offset)\n            - hash: Part of a sha1 hash, in hexadecimal format\n            - offset: Offset this hash was created from/at.\n        :param batch_size: insert batches.\n        \"\"\"\n\n    @abc.abstractmethod\n    def return_matches(self, hashes: List[Tuple[str, int]], batch_size: int = 1000) \\\n            -> Tuple[List[Tuple[int, int]], Dict[int, int]]:\n        \"\"\"\n        Searches the database for pairs of (hash, offset) values.\n\n        :param hashes: A sequence of tuples in the format (hash, offset)\n            - hash: Part of a sha1 hash, in hexadecimal format\n            - offset: Offset this hash was created from/at.\n        :param batch_size: number of query's batches.\n        :return: a list of (sid, offset_difference) tuples and a\n        dictionary with the amount of hashes matched (not considering\n        duplicated hashes) in each song.\n            - song id: Song identifier\n            - offset_difference: (database_offset - sampled_offset)\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    def delete_songs_by_id(self, song_ids: List[int], batch_size: int = 1000) -> None:\n        \"\"\"\n        Given a list of song ids it deletes all songs specified and their corresponding fingerprints.\n\n        :param song_ids: song ids to be deleted from the database.\n        :param batch_size: number of query's batches.\n        \"\"\"\n        pass\n\n\ndef get_database(database_type: str = \"mysql\") -> BaseDatabase:\n    \"\"\"\n    Given a database type it returns a database instance for that type.\n\n    :param database_type: type of the database.\n    :return: an instance of BaseDatabase depending on given database_type.\n    \"\"\"\n    try:\n        path, db_class_name = DATABASES[database_type]\n        db_module = importlib.import_module(path)\n        db_class = getattr(db_module, db_class_name)\n        return db_class\n    except (ImportError, KeyError):\n        raise TypeError(\"Unsupported database type supplied.\")\n"
  },
  {
    "path": "dejavu/base_classes/base_recognizer.py",
    "content": "import abc\nfrom time import time\nfrom typing import Dict, List, Tuple\n\nimport numpy as np\n\nfrom dejavu.config.settings import DEFAULT_FS\n\n\nclass BaseRecognizer(object, metaclass=abc.ABCMeta):\n    def __init__(self, dejavu):\n        self.dejavu = dejavu\n        self.Fs = DEFAULT_FS\n\n    def _recognize(self, *data) -> Tuple[List[Dict[str, any]], int, int, int]:\n        fingerprint_times = []\n        hashes = set()  # to remove possible duplicated fingerprints we built a set.\n        for channel in data:\n            fingerprints, fingerprint_time = self.dejavu.generate_fingerprints(channel, Fs=self.Fs)\n            fingerprint_times.append(fingerprint_time)\n            hashes |= set(fingerprints)\n\n        matches, dedup_hashes, query_time = self.dejavu.find_matches(hashes)\n\n        t = time()\n        final_results = self.dejavu.align_matches(matches, dedup_hashes, len(hashes))\n        align_time = time() - t\n\n        return final_results, np.sum(fingerprint_times), query_time, align_time\n\n    @abc.abstractmethod\n    def recognize(self) -> Dict[str, any]:\n        pass  # base class does nothing\n"
  },
  {
    "path": "dejavu/base_classes/common_database.py",
    "content": "import abc\nfrom typing import Dict, List, Tuple\n\nfrom dejavu.base_classes.base_database import BaseDatabase\n\n\nclass CommonDatabase(BaseDatabase, metaclass=abc.ABCMeta):\n    # Since several methods across different databases are actually just the same\n    # I've built this class with the idea to reuse that logic instead of copy pasting\n    # over and over the same code.\n\n    def __init__(self):\n        super().__init__()\n\n    def before_fork(self) -> None:\n        \"\"\"\n        Called before the database instance is given to the new process\n        \"\"\"\n        pass\n\n    def after_fork(self) -> None:\n        \"\"\"\n        Called after the database instance has been given to the new process\n\n        This will be called in the new process.\n        \"\"\"\n        pass\n\n    def setup(self) -> None:\n        \"\"\"\n        Called on creation or shortly afterwards.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.CREATE_SONGS_TABLE)\n            cur.execute(self.CREATE_FINGERPRINTS_TABLE)\n            cur.execute(self.DELETE_UNFINGERPRINTED)\n\n    def empty(self) -> None:\n        \"\"\"\n        Called when the database should be cleared of all data.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.DROP_FINGERPRINTS)\n            cur.execute(self.DROP_SONGS)\n\n        self.setup()\n\n    def delete_unfingerprinted_songs(self) -> None:\n        \"\"\"\n        Called to remove any song entries that do not have any fingerprints\n        associated with them.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.DELETE_UNFINGERPRINTED)\n\n    def get_num_songs(self) -> int:\n        \"\"\"\n        Returns the song's count stored.\n\n        :return: the amount of songs in the database.\n        \"\"\"\n        with self.cursor(buffered=True) as cur:\n            cur.execute(self.SELECT_UNIQUE_SONG_IDS)\n            count = cur.fetchone()[0] if cur.rowcount != 0 else 0\n\n        return count\n\n    def get_num_fingerprints(self) -> int:\n        \"\"\"\n        Returns the fingerprints' count stored.\n\n        :return: the number of fingerprints in the database.\n        \"\"\"\n        with self.cursor(buffered=True) as cur:\n            cur.execute(self.SELECT_NUM_FINGERPRINTS)\n            count = cur.fetchone()[0] if cur.rowcount != 0 else 0\n\n        return count\n\n    def set_song_fingerprinted(self, song_id):\n        \"\"\"\n        Sets a specific song as having all fingerprints in the database.\n\n        :param song_id: song identifier.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.UPDATE_SONG_FINGERPRINTED, (song_id,))\n\n    def get_songs(self) -> List[Dict[str, str]]:\n        \"\"\"\n        Returns all fully fingerprinted songs in the database\n\n        :return: a dictionary with the songs info.\n        \"\"\"\n        with self.cursor(dictionary=True) as cur:\n            cur.execute(self.SELECT_SONGS)\n            return list(cur)\n\n    def get_song_by_id(self, song_id: int) -> Dict[str, str]:\n        \"\"\"\n        Brings the song info from the database.\n\n        :param song_id: song identifier.\n        :return: a song by its identifier. Result must be a Dictionary.\n        \"\"\"\n        with self.cursor(dictionary=True) as cur:\n            cur.execute(self.SELECT_SONG, (song_id,))\n            return cur.fetchone()\n\n    def insert(self, fingerprint: str, song_id: int, offset: int):\n        \"\"\"\n        Inserts a single fingerprint into the database.\n\n        :param fingerprint: Part of a sha1 hash, in hexadecimal format\n        :param song_id: Song identifier this fingerprint is off\n        :param offset: The offset this fingerprint is from.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.INSERT_FINGERPRINT, (fingerprint, song_id, offset))\n\n    @abc.abstractmethod\n    def insert_song(self, song_name: str, file_hash: str, total_hashes: int) -> int:\n        \"\"\"\n        Inserts a song name into the database, returns the new\n        identifier of the song.\n\n        :param song_name: The name of the song.\n        :param file_hash: Hash from the fingerprinted file.\n        :param total_hashes: amount of hashes to be inserted on fingerprint table.\n        :return: the inserted id.\n        \"\"\"\n        pass\n\n    def query(self, fingerprint: str = None) -> List[Tuple]:\n        \"\"\"\n        Returns all matching fingerprint entries associated with\n        the given hash as parameter, if None is passed it returns all entries.\n\n        :param fingerprint: part of a sha1 hash, in hexadecimal format\n        :return: a list of fingerprint records stored in the db.\n        \"\"\"\n        with self.cursor() as cur:\n            if fingerprint:\n                cur.execute(self.SELECT, (fingerprint,))\n            else:  # select all if no key\n                cur.execute(self.SELECT_ALL)\n            return list(cur)\n\n    def get_iterable_kv_pairs(self) -> List[Tuple]:\n        \"\"\"\n        Returns all fingerprints in the database.\n\n        :return: a list containing all fingerprints stored in the db.\n        \"\"\"\n        return self.query(None)\n\n    def insert_hashes(self, song_id: int, hashes: List[Tuple[str, int]], batch_size: int = 1000) -> None:\n        \"\"\"\n        Insert a multitude of fingerprints.\n\n        :param song_id: Song identifier the fingerprints belong to\n        :param hashes: A sequence of tuples in the format (hash, offset)\n            - hash: Part of a sha1 hash, in hexadecimal format\n            - offset: Offset this hash was created from/at.\n        :param batch_size: insert batches.\n        \"\"\"\n        values = [(song_id, hsh, int(offset)) for hsh, offset in hashes]\n\n        with self.cursor() as cur:\n            for index in range(0, len(hashes), batch_size):\n                cur.executemany(self.INSERT_FINGERPRINT, values[index: index + batch_size])\n\n    def return_matches(self, hashes: List[Tuple[str, int]],\n                       batch_size: int = 1000) -> Tuple[List[Tuple[int, int]], Dict[int, int]]:\n        \"\"\"\n        Searches the database for pairs of (hash, offset) values.\n\n        :param hashes: A sequence of tuples in the format (hash, offset)\n            - hash: Part of a sha1 hash, in hexadecimal format\n            - offset: Offset this hash was created from/at.\n        :param batch_size: number of query's batches.\n        :return: a list of (sid, offset_difference) tuples and a\n        dictionary with the amount of hashes matched (not considering\n        duplicated hashes) in each song.\n            - song id: Song identifier\n            - offset_difference: (database_offset - sampled_offset)\n        \"\"\"\n        # Create a dictionary of hash => offset pairs for later lookups\n        mapper = {}\n        for hsh, offset in hashes:\n            if hsh.upper() in mapper.keys():\n                mapper[hsh.upper()].append(offset)\n            else:\n                mapper[hsh.upper()] = [offset]\n\n        values = list(mapper.keys())\n\n        # in order to count each hash only once per db offset we use the dic below\n        dedup_hashes = {}\n\n        results = []\n        with self.cursor() as cur:\n            for index in range(0, len(values), batch_size):\n                # Create our IN part of the query\n                query = self.SELECT_MULTIPLE % ', '.join([self.IN_MATCH] * len(values[index: index + batch_size]))\n\n                cur.execute(query, values[index: index + batch_size])\n\n                for hsh, sid, offset in cur:\n                    if sid not in dedup_hashes.keys():\n                        dedup_hashes[sid] = 1\n                    else:\n                        dedup_hashes[sid] += 1\n                    #  we now evaluate all offset for each  hash matched\n                    for song_sampled_offset in mapper[hsh]:\n                        results.append((sid, offset - song_sampled_offset))\n\n            return results, dedup_hashes\n\n    def delete_songs_by_id(self, song_ids: List[int], batch_size: int = 1000) -> None:\n        \"\"\"\n        Given a list of song ids it deletes all songs specified and their corresponding fingerprints.\n\n        :param song_ids: song ids to be deleted from the database.\n        :param batch_size: number of query's batches.\n        \"\"\"\n        with self.cursor() as cur:\n            for index in range(0, len(song_ids), batch_size):\n                # Create our IN part of the query\n                query = self.DELETE_SONGS % ', '.join(['%s'] * len(song_ids[index: index + batch_size]))\n\n                cur.execute(query, song_ids[index: index + batch_size])\n"
  },
  {
    "path": "dejavu/config/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/config/settings.py",
    "content": "# Dejavu\n\n# DEJAVU JSON RESPONSE\nSONG_ID = \"song_id\"\nSONG_NAME = 'song_name'\nRESULTS = 'results'\n\nHASHES_MATCHED = 'hashes_matched_in_input'\n\n# Hashes fingerprinted in the db.\nFINGERPRINTED_HASHES = 'fingerprinted_hashes_in_db'\n# Percentage regarding hashes matched vs hashes fingerprinted in the db.\nFINGERPRINTED_CONFIDENCE = 'fingerprinted_confidence'\n\n# Hashes generated from the input.\nINPUT_HASHES = 'input_total_hashes'\n# Percentage regarding hashes matched vs hashes from the input.\nINPUT_CONFIDENCE = 'input_confidence'\n\nTOTAL_TIME = 'total_time'\nFINGERPRINT_TIME = 'fingerprint_time'\nQUERY_TIME = 'query_time'\nALIGN_TIME = 'align_time'\nOFFSET = 'offset'\nOFFSET_SECS = 'offset_seconds'\n\n# DATABASE CLASS INSTANCES:\nDATABASES = {\n    'mysql': (\"dejavu.database_handler.mysql_database\", \"MySQLDatabase\"),\n    'postgres': (\"dejavu.database_handler.postgres_database\", \"PostgreSQLDatabase\")\n}\n\n# TABLE SONGS\nSONGS_TABLENAME = \"songs\"\n\n# SONGS FIELDS\nFIELD_SONG_ID = 'song_id'\nFIELD_SONGNAME = 'song_name'\nFIELD_FINGERPRINTED = \"fingerprinted\"\nFIELD_FILE_SHA1 = 'file_sha1'\nFIELD_TOTAL_HASHES = 'total_hashes'\n\n# TABLE FINGERPRINTS\nFINGERPRINTS_TABLENAME = \"fingerprints\"\n\n# FINGERPRINTS FIELDS\nFIELD_HASH = 'hash'\nFIELD_OFFSET = 'offset'\n\n# FINGERPRINTS CONFIG:\n# This is used as connectivity parameter for scipy.generate_binary_structure function. This parameter\n# changes the morphology mask when looking for maximum peaks on the spectrogram matrix.\n# Possible values are: [1, 2]\n# Where 1 sets a diamond morphology which implies that diagonal elements are not considered as neighbors (this\n# is the value used in the original dejavu code).\n# And 2 sets a square mask, i.e. all elements are considered neighbors.\nCONNECTIVITY_MASK = 2\n\n# Sampling rate, related to the Nyquist conditions, which affects\n# the range frequencies we can detect.\nDEFAULT_FS = 44100\n\n# Size of the FFT window, affects frequency granularity\nDEFAULT_WINDOW_SIZE = 4096\n\n# Ratio by which each sequential window overlaps the last and the\n# next window. Higher overlap will allow a higher granularity of offset\n# matching, but potentially more fingerprints.\nDEFAULT_OVERLAP_RATIO = 0.5\n\n# Degree to which a fingerprint can be paired with its neighbors. Higher values will\n# cause more fingerprints, but potentially better accuracy.\nDEFAULT_FAN_VALUE = 5  # 15 was the original value.\n\n# Minimum amplitude in spectrogram in order to be considered a peak.\n# This can be raised to reduce number of fingerprints, but can negatively\n# affect accuracy.\nDEFAULT_AMP_MIN = 10\n\n# Number of cells around an amplitude peak in the spectrogram in order\n# for Dejavu to consider it a spectral peak. Higher values mean less\n# fingerprints and faster matching, but can potentially affect accuracy.\nPEAK_NEIGHBORHOOD_SIZE = 10  # 20 was the original value.\n\n# Thresholds on how close or far fingerprints can be in time in order\n# to be paired as a fingerprint. If your max is too low, higher values of\n# DEFAULT_FAN_VALUE may not perform as expected.\nMIN_HASH_TIME_DELTA = 0\nMAX_HASH_TIME_DELTA = 200\n\n# If True, will sort peaks temporally for fingerprinting;\n# not sorting will cut down number of fingerprints, but potentially\n# affect performance.\nPEAK_SORT = True\n\n# Number of bits to grab from the front of the SHA1 hash in the\n# fingerprint calculation. The more you grab, the more memory storage,\n# with potentially lesser collisions of matches.\nFINGERPRINT_REDUCTION = 20\n\n# Number of results being returned for file recognition\nTOPN = 2\n"
  },
  {
    "path": "dejavu/database_handler/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/database_handler/mysql_database.py",
    "content": "import queue\n\nimport mysql.connector\nfrom mysql.connector.errors import DatabaseError\n\nfrom dejavu.base_classes.common_database import CommonDatabase\nfrom dejavu.config.settings import (FIELD_FILE_SHA1, FIELD_FINGERPRINTED,\n                                    FIELD_HASH, FIELD_OFFSET, FIELD_SONG_ID,\n                                    FIELD_SONGNAME, FIELD_TOTAL_HASHES,\n                                    FINGERPRINTS_TABLENAME, SONGS_TABLENAME)\n\n\nclass MySQLDatabase(CommonDatabase):\n    type = \"mysql\"\n\n    # CREATES\n    CREATE_SONGS_TABLE = f\"\"\"\n        CREATE TABLE IF NOT EXISTS `{SONGS_TABLENAME}` (\n            `{FIELD_SONG_ID}` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT\n        ,   `{FIELD_SONGNAME}` VARCHAR(250) NOT NULL\n        ,   `{FIELD_FINGERPRINTED}` TINYINT DEFAULT 0\n        ,   `{FIELD_FILE_SHA1}` BINARY(20) NOT NULL\n        ,   `{FIELD_TOTAL_HASHES}` INT NOT NULL DEFAULT 0\n        ,   `date_created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP\n        ,   `date_modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n        ,   CONSTRAINT `pk_{SONGS_TABLENAME}_{FIELD_SONG_ID}` PRIMARY KEY (`{FIELD_SONG_ID}`)\n        ,   CONSTRAINT `uq_{SONGS_TABLENAME}_{FIELD_SONG_ID}` UNIQUE KEY (`{FIELD_SONG_ID}`)\n        ) ENGINE=INNODB;\n    \"\"\"\n\n    CREATE_FINGERPRINTS_TABLE = f\"\"\"\n        CREATE TABLE IF NOT EXISTS `{FINGERPRINTS_TABLENAME}` (\n            `{FIELD_HASH}` BINARY(10) NOT NULL\n        ,   `{FIELD_SONG_ID}` MEDIUMINT UNSIGNED NOT NULL\n        ,   `{FIELD_OFFSET}` INT UNSIGNED NOT NULL\n        ,   `date_created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP\n        ,   `date_modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n        ,   INDEX `ix_{FINGERPRINTS_TABLENAME}_{FIELD_HASH}` (`{FIELD_HASH}`)\n        ,   CONSTRAINT `uq_{FINGERPRINTS_TABLENAME}_{FIELD_SONG_ID}_{FIELD_OFFSET}_{FIELD_HASH}`\n                UNIQUE KEY  (`{FIELD_SONG_ID}`, `{FIELD_OFFSET}`, `{FIELD_HASH}`)\n        ,   CONSTRAINT `fk_{FINGERPRINTS_TABLENAME}_{FIELD_SONG_ID}` FOREIGN KEY (`{FIELD_SONG_ID}`)\n                REFERENCES `{SONGS_TABLENAME}`(`{FIELD_SONG_ID}`) ON DELETE CASCADE\n    ) ENGINE=INNODB;\n    \"\"\"\n\n    # INSERTS (IGNORES DUPLICATES)\n    INSERT_FINGERPRINT = f\"\"\"\n        INSERT IGNORE INTO `{FINGERPRINTS_TABLENAME}` (\n                `{FIELD_SONG_ID}`\n            ,   `{FIELD_HASH}`\n            ,   `{FIELD_OFFSET}`)\n        VALUES (%s, UNHEX(%s), %s);\n    \"\"\"\n\n    INSERT_SONG = f\"\"\"\n        INSERT INTO `{SONGS_TABLENAME}` (`{FIELD_SONGNAME}`,`{FIELD_FILE_SHA1}`,`{FIELD_TOTAL_HASHES}`)\n        VALUES (%s, UNHEX(%s), %s);\n    \"\"\"\n\n    # SELECTS\n    SELECT = f\"\"\"\n        SELECT `{FIELD_SONG_ID}`, `{FIELD_OFFSET}`\n        FROM `{FINGERPRINTS_TABLENAME}`\n        WHERE `{FIELD_HASH}` = UNHEX(%s);\n    \"\"\"\n\n    SELECT_MULTIPLE = f\"\"\"\n        SELECT HEX(`{FIELD_HASH}`), `{FIELD_SONG_ID}`, `{FIELD_OFFSET}`\n        FROM `{FINGERPRINTS_TABLENAME}`\n        WHERE `{FIELD_HASH}` IN (%s);\n    \"\"\"\n\n    SELECT_ALL = f\"SELECT `{FIELD_SONG_ID}`, `{FIELD_OFFSET}` FROM `{FINGERPRINTS_TABLENAME}`;\"\n\n    SELECT_SONG = f\"\"\"\n        SELECT `{FIELD_SONGNAME}`, HEX(`{FIELD_FILE_SHA1}`) AS `{FIELD_FILE_SHA1}`, `{FIELD_TOTAL_HASHES}`\n        FROM `{SONGS_TABLENAME}`\n        WHERE `{FIELD_SONG_ID}` = %s;\n    \"\"\"\n\n    SELECT_NUM_FINGERPRINTS = f\"SELECT COUNT(*) AS n FROM `{FINGERPRINTS_TABLENAME}`;\"\n\n    SELECT_UNIQUE_SONG_IDS = f\"\"\"\n        SELECT COUNT(`{FIELD_SONG_ID}`) AS n\n        FROM `{SONGS_TABLENAME}`\n        WHERE `{FIELD_FINGERPRINTED}` = 1;\n    \"\"\"\n\n    SELECT_SONGS = f\"\"\"\n        SELECT\n            `{FIELD_SONG_ID}`\n        ,   `{FIELD_SONGNAME}`\n        ,   HEX(`{FIELD_FILE_SHA1}`) AS `{FIELD_FILE_SHA1}`\n        ,   `{FIELD_TOTAL_HASHES}`\n        ,   `date_created`\n        FROM `{SONGS_TABLENAME}`\n        WHERE `{FIELD_FINGERPRINTED}` = 1;\n    \"\"\"\n\n    # DROPS\n    DROP_FINGERPRINTS = f\"DROP TABLE IF EXISTS `{FINGERPRINTS_TABLENAME}`;\"\n    DROP_SONGS = f\"DROP TABLE IF EXISTS `{SONGS_TABLENAME}`;\"\n\n    # UPDATE\n    UPDATE_SONG_FINGERPRINTED = f\"\"\"\n        UPDATE `{SONGS_TABLENAME}` SET `{FIELD_FINGERPRINTED}` = 1 WHERE `{FIELD_SONG_ID}` = %s;\n    \"\"\"\n\n    # DELETES\n    DELETE_UNFINGERPRINTED = f\"\"\"\n        DELETE FROM `{SONGS_TABLENAME}` WHERE `{FIELD_FINGERPRINTED}` = 0;\n    \"\"\"\n\n    DELETE_SONGS = f\"\"\"\n        DELETE FROM `{SONGS_TABLENAME}` WHERE `{FIELD_SONG_ID}` IN (%s);\n    \"\"\"\n\n    # IN\n    IN_MATCH = f\"UNHEX(%s)\"\n\n    def __init__(self, **options):\n        super().__init__()\n        self.cursor = cursor_factory(**options)\n        self._options = options\n\n    def after_fork(self) -> None:\n        # Clear the cursor cache, we don't want any stale connections from\n        # the previous process.\n        Cursor.clear_cache()\n\n    def insert_song(self, song_name: str, file_hash: str, total_hashes: int) -> int:\n        \"\"\"\n        Inserts a song name into the database, returns the new\n        identifier of the song.\n\n        :param song_name: The name of the song.\n        :param file_hash: Hash from the fingerprinted file.\n        :param total_hashes: amount of hashes to be inserted on fingerprint table.\n        :return: the inserted id.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.INSERT_SONG, (song_name, file_hash, total_hashes))\n            return cur.lastrowid\n\n    def __getstate__(self):\n        return self._options,\n\n    def __setstate__(self, state):\n        self._options, = state\n        self.cursor = cursor_factory(**self._options)\n\n\ndef cursor_factory(**factory_options):\n    def cursor(**options):\n        options.update(factory_options)\n        return Cursor(**options)\n    return cursor\n\n\nclass Cursor(object):\n    \"\"\"\n    Establishes a connection to the database and returns an open cursor.\n    # Use as context manager\n    with Cursor() as cur:\n        cur.execute(query)\n        ...\n    \"\"\"\n    def __init__(self, dictionary=False, **options):\n        super().__init__()\n\n        self._cache = queue.Queue(maxsize=5)\n\n        try:\n            conn = self._cache.get_nowait()\n            # Ping the connection before using it from the cache.\n            conn.ping(True)\n        except queue.Empty:\n            conn = mysql.connector.connect(**options)\n\n        self.conn = conn\n        self.dictionary = dictionary\n\n    @classmethod\n    def clear_cache(cls):\n        cls._cache = queue.Queue(maxsize=5)\n\n    def __enter__(self):\n        self.cursor = self.conn.cursor(dictionary=self.dictionary)\n        return self.cursor\n\n    def __exit__(self, extype, exvalue, traceback):\n        # if we had a MySQL related error we try to rollback the cursor.\n        if extype is DatabaseError:\n            self.cursor.rollback()\n\n        self.cursor.close()\n        self.conn.commit()\n\n        # Put it back on the queue\n        try:\n            self._cache.put_nowait(self.conn)\n        except queue.Full:\n            self.conn.close()\n"
  },
  {
    "path": "dejavu/database_handler/postgres_database.py",
    "content": "import queue\n\nimport psycopg2\nfrom psycopg2.extras import DictCursor\n\nfrom dejavu.base_classes.common_database import CommonDatabase\nfrom dejavu.config.settings import (FIELD_FILE_SHA1, FIELD_FINGERPRINTED,\n                                    FIELD_HASH, FIELD_OFFSET, FIELD_SONG_ID,\n                                    FIELD_SONGNAME, FIELD_TOTAL_HASHES,\n                                    FINGERPRINTS_TABLENAME, SONGS_TABLENAME)\n\n\nclass PostgreSQLDatabase(CommonDatabase):\n    type = \"postgres\"\n\n    # CREATES\n    CREATE_SONGS_TABLE = f\"\"\"\n        CREATE TABLE IF NOT EXISTS \"{SONGS_TABLENAME}\" (\n            \"{FIELD_SONG_ID}\" SERIAL\n        ,   \"{FIELD_SONGNAME}\" VARCHAR(250) NOT NULL\n        ,   \"{FIELD_FINGERPRINTED}\" SMALLINT DEFAULT 0\n        ,   \"{FIELD_FILE_SHA1}\" BYTEA\n        ,   \"{FIELD_TOTAL_HASHES}\" INT NOT NULL DEFAULT 0\n        ,   \"date_created\" TIMESTAMP NOT NULL DEFAULT now()\n        ,   \"date_modified\" TIMESTAMP NOT NULL DEFAULT now()\n        ,   CONSTRAINT \"pk_{SONGS_TABLENAME}_{FIELD_SONG_ID}\" PRIMARY KEY (\"{FIELD_SONG_ID}\")\n        ,   CONSTRAINT \"uq_{SONGS_TABLENAME}_{FIELD_SONG_ID}\" UNIQUE (\"{FIELD_SONG_ID}\")\n        );\n    \"\"\"\n\n    CREATE_FINGERPRINTS_TABLE = f\"\"\"\n        CREATE TABLE IF NOT EXISTS \"{FINGERPRINTS_TABLENAME}\" (\n            \"{FIELD_HASH}\" BYTEA NOT NULL\n        ,   \"{FIELD_SONG_ID}\" INT NOT NULL\n        ,   \"{FIELD_OFFSET}\" INT NOT NULL\n        ,   \"date_created\" TIMESTAMP NOT NULL DEFAULT now()\n        ,   \"date_modified\" TIMESTAMP NOT NULL DEFAULT now()\n        ,   CONSTRAINT \"uq_{FINGERPRINTS_TABLENAME}\" UNIQUE  (\"{FIELD_SONG_ID}\", \"{FIELD_OFFSET}\", \"{FIELD_HASH}\")\n        ,   CONSTRAINT \"fk_{FINGERPRINTS_TABLENAME}_{FIELD_SONG_ID}\" FOREIGN KEY (\"{FIELD_SONG_ID}\")\n                REFERENCES \"{SONGS_TABLENAME}\"(\"{FIELD_SONG_ID}\") ON DELETE CASCADE\n        );\n\n        CREATE INDEX IF NOT EXISTS \"ix_{FINGERPRINTS_TABLENAME}_{FIELD_HASH}\" ON \"{FINGERPRINTS_TABLENAME}\"\n        USING hash (\"{FIELD_HASH}\");\n    \"\"\"\n\n    CREATE_FINGERPRINTS_TABLE_INDEX = f\"\"\"\n        CREATE INDEX \"ix_{FINGERPRINTS_TABLENAME}_{FIELD_HASH}\" ON \"{FINGERPRINTS_TABLENAME}\"\n        USING hash (\"{FIELD_HASH}\");\n    \"\"\"\n\n    # INSERTS (IGNORES DUPLICATES)\n    INSERT_FINGERPRINT = f\"\"\"\n        INSERT INTO \"{FINGERPRINTS_TABLENAME}\" (\n                \"{FIELD_SONG_ID}\"\n            ,   \"{FIELD_HASH}\"\n            ,   \"{FIELD_OFFSET}\")\n        VALUES (%s, decode(%s, 'hex'), %s) ON CONFLICT DO NOTHING;\n    \"\"\"\n\n    INSERT_SONG = f\"\"\"\n        INSERT INTO \"{SONGS_TABLENAME}\" (\"{FIELD_SONGNAME}\", \"{FIELD_FILE_SHA1}\",\"{FIELD_TOTAL_HASHES}\")\n        VALUES (%s, decode(%s, 'hex'), %s)\n        RETURNING \"{FIELD_SONG_ID}\";\n    \"\"\"\n\n    # SELECTS\n    SELECT = f\"\"\"\n        SELECT \"{FIELD_SONG_ID}\", \"{FIELD_OFFSET}\"\n        FROM \"{FINGERPRINTS_TABLENAME}\"\n        WHERE \"{FIELD_HASH}\" = decode(%s, 'hex');\n    \"\"\"\n\n    SELECT_MULTIPLE = f\"\"\"\n        SELECT upper(encode(\"{FIELD_HASH}\", 'hex')), \"{FIELD_SONG_ID}\", \"{FIELD_OFFSET}\"\n        FROM \"{FINGERPRINTS_TABLENAME}\"\n        WHERE \"{FIELD_HASH}\" IN (%s);\n    \"\"\"\n\n    SELECT_ALL = f'SELECT \"{FIELD_SONG_ID}\", \"{FIELD_OFFSET}\" FROM \"{FINGERPRINTS_TABLENAME}\";'\n\n    SELECT_SONG = f\"\"\"\n        SELECT\n            \"{FIELD_SONGNAME}\"\n        ,   upper(encode(\"{FIELD_FILE_SHA1}\", 'hex')) AS \"{FIELD_FILE_SHA1}\"\n        ,   \"{FIELD_TOTAL_HASHES}\"\n        FROM \"{SONGS_TABLENAME}\"\n        WHERE \"{FIELD_SONG_ID}\" = %s;\n    \"\"\"\n\n    SELECT_NUM_FINGERPRINTS = f'SELECT COUNT(*) AS n FROM \"{FINGERPRINTS_TABLENAME}\";'\n\n    SELECT_UNIQUE_SONG_IDS = f\"\"\"\n        SELECT COUNT(\"{FIELD_SONG_ID}\") AS n\n        FROM \"{SONGS_TABLENAME}\"\n        WHERE \"{FIELD_FINGERPRINTED}\" = 1;\n    \"\"\"\n\n    SELECT_SONGS = f\"\"\"\n        SELECT\n            \"{FIELD_SONG_ID}\"\n        ,   \"{FIELD_SONGNAME}\"\n        ,   upper(encode(\"{FIELD_FILE_SHA1}\", 'hex')) AS \"{FIELD_FILE_SHA1}\"\n        ,   \"{FIELD_TOTAL_HASHES}\"\n        ,   \"date_created\"\n        FROM \"{SONGS_TABLENAME}\"\n        WHERE \"{FIELD_FINGERPRINTED}\" = 1;\n    \"\"\"\n\n    # DROPS\n    DROP_FINGERPRINTS = F'DROP TABLE IF EXISTS \"{FINGERPRINTS_TABLENAME}\";'\n    DROP_SONGS = F'DROP TABLE IF EXISTS \"{SONGS_TABLENAME}\";'\n\n    # UPDATE\n    UPDATE_SONG_FINGERPRINTED = f\"\"\"\n        UPDATE \"{SONGS_TABLENAME}\" SET\n            \"{FIELD_FINGERPRINTED}\" = 1\n        ,   \"date_modified\" = now()\n        WHERE \"{FIELD_SONG_ID}\" = %s;\n    \"\"\"\n\n    # DELETES\n    DELETE_UNFINGERPRINTED = f\"\"\"\n        DELETE FROM \"{SONGS_TABLENAME}\" WHERE \"{FIELD_FINGERPRINTED}\" = 0;\n    \"\"\"\n\n    DELETE_SONGS = f\"\"\"\n        DELETE FROM \"{SONGS_TABLENAME}\" WHERE \"{FIELD_SONG_ID}\" IN (%s);\n    \"\"\"\n\n    # IN\n    IN_MATCH = f\"decode(%s, 'hex')\"\n\n    def __init__(self, **options):\n        super().__init__()\n        self.cursor = cursor_factory(**options)\n        self._options = options\n\n    def after_fork(self) -> None:\n        # Clear the cursor cache, we don't want any stale connections from\n        # the previous process.\n        Cursor.clear_cache()\n\n    def insert_song(self, song_name: str, file_hash: str, total_hashes: int) -> int:\n        \"\"\"\n        Inserts a song name into the database, returns the new\n        identifier of the song.\n\n        :param song_name: The name of the song.\n        :param file_hash: Hash from the fingerprinted file.\n        :param total_hashes: amount of hashes to be inserted on fingerprint table.\n        :return: the inserted id.\n        \"\"\"\n        with self.cursor() as cur:\n            cur.execute(self.INSERT_SONG, (song_name, file_hash, total_hashes))\n            return cur.fetchone()[0]\n\n    def __getstate__(self):\n        return self._options,\n\n    def __setstate__(self, state):\n        self._options, = state\n        self.cursor = cursor_factory(**self._options)\n\n\ndef cursor_factory(**factory_options):\n    def cursor(**options):\n        options.update(factory_options)\n        return Cursor(**options)\n    return cursor\n\n\nclass Cursor(object):\n    \"\"\"\n    Establishes a connection to the database and returns an open cursor.\n    # Use as context manager\n    with Cursor() as cur:\n        cur.execute(query)\n        ...\n    \"\"\"\n    def __init__(self, dictionary=False, **options):\n        super().__init__()\n\n        self._cache = queue.Queue(maxsize=5)\n\n        try:\n            conn = self._cache.get_nowait()\n            # Ping the connection before using it from the cache.\n            conn.ping(True)\n        except queue.Empty:\n            conn = psycopg2.connect(**options)\n\n        self.conn = conn\n        self.dictionary = dictionary\n\n    @classmethod\n    def clear_cache(cls):\n        cls._cache = queue.Queue(maxsize=5)\n\n    def __enter__(self):\n        if self.dictionary:\n            self.cursor = self.conn.cursor(cursor_factory=DictCursor)\n        else:\n            self.cursor = self.conn.cursor()\n        return self.cursor\n\n    def __exit__(self, extype, exvalue, traceback):\n        # if we had a PostgreSQL related error we try to rollback the cursor.\n        if extype is psycopg2.DatabaseError:\n            self.cursor.rollback()\n\n        self.cursor.close()\n        self.conn.commit()\n\n        # Put it back on the queue\n        try:\n            self._cache.put_nowait(self.conn)\n        except queue.Full:\n            self.conn.close()\n"
  },
  {
    "path": "dejavu/logic/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/logic/decoder.py",
    "content": "import fnmatch\nimport os\nfrom hashlib import sha1\nfrom typing import List, Tuple\n\nimport numpy as np\nfrom pydub import AudioSegment\nfrom pydub.utils import audioop\n\nfrom dejavu.third_party import wavio\n\n\ndef unique_hash(file_path: str, block_size: int = 2**20) -> str:\n    \"\"\" Small function to generate a hash to uniquely generate\n    a file. Inspired by MD5 version here:\n    http://stackoverflow.com/a/1131255/712997\n\n    Works with large files.\n\n    :param file_path: path to file.\n    :param block_size: read block size.\n    :return: a hash in an hexagesimal string form.\n    \"\"\"\n    s = sha1()\n    with open(file_path, \"rb\") as f:\n        while True:\n            buf = f.read(block_size)\n            if not buf:\n                break\n            s.update(buf)\n    return s.hexdigest().upper()\n\n\ndef find_files(path: str, extensions: List[str]) -> List[Tuple[str, str]]:\n    \"\"\"\n    Get all files that meet the specified extensions.\n\n    :param path: path to a directory with audio files.\n    :param extensions: file extensions to look for.\n    :return: a list of tuples with file name and its extension.\n    \"\"\"\n    # Allow both with \".mp3\" and without \"mp3\" to be used for extensions\n    extensions = [e.replace(\".\", \"\") for e in extensions]\n\n    results = []\n    for dirpath, dirnames, files in os.walk(path):\n        for extension in extensions:\n            for f in fnmatch.filter(files, f\"*.{extension}\"):\n                p = os.path.join(dirpath, f)\n                results.append((p, extension))\n    return results\n\n\ndef read(file_name: str, limit: int = None) -> Tuple[List[List[int]], int, str]:\n    \"\"\"\n    Reads any file supported by pydub (ffmpeg) and returns the data contained\n    within. If file reading fails due to input being a 24-bit wav file,\n    wavio is used as a backup.\n\n    Can be optionally limited to a certain amount of seconds from the start\n    of the file by specifying the `limit` parameter. This is the amount of\n    seconds from the start of the file.\n\n    :param file_name: file to be read.\n    :param limit: number of seconds to limit.\n    :return: tuple list of (channels, sample_rate, content_file_hash).\n    \"\"\"\n    # pydub does not support 24-bit wav files, use wavio when this occurs\n    try:\n        audiofile = AudioSegment.from_file(file_name)\n\n        if limit:\n            audiofile = audiofile[:limit * 1000]\n\n        data = np.fromstring(audiofile.raw_data, np.int16)\n\n        channels = []\n        for chn in range(audiofile.channels):\n            channels.append(data[chn::audiofile.channels])\n\n        audiofile.frame_rate\n    except audioop.error:\n        _, _, audiofile = wavio.readwav(file_name)\n\n        if limit:\n            audiofile = audiofile[:limit * 1000]\n\n        audiofile = audiofile.T\n        audiofile = audiofile.astype(np.int16)\n\n        channels = []\n        for chn in audiofile:\n            channels.append(chn)\n\n    return channels, audiofile.frame_rate, unique_hash(file_name)\n\n\ndef get_audio_name_from_path(file_path: str) -> str:\n    \"\"\"\n    Extracts song name from a file path.\n\n    :param file_path: path to an audio file.\n    :return: file name\n    \"\"\"\n    return os.path.splitext(os.path.basename(file_path))[0]\n"
  },
  {
    "path": "dejavu/logic/fingerprint.py",
    "content": "import hashlib\nfrom operator import itemgetter\nfrom typing import List, Tuple\n\nimport matplotlib.mlab as mlab\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.ndimage.filters import maximum_filter\nfrom scipy.ndimage.morphology import (binary_erosion,\n                                      generate_binary_structure,\n                                      iterate_structure)\n\nfrom dejavu.config.settings import (CONNECTIVITY_MASK, DEFAULT_AMP_MIN,\n                                    DEFAULT_FAN_VALUE, DEFAULT_FS,\n                                    DEFAULT_OVERLAP_RATIO, DEFAULT_WINDOW_SIZE,\n                                    FINGERPRINT_REDUCTION, MAX_HASH_TIME_DELTA,\n                                    MIN_HASH_TIME_DELTA,\n                                    PEAK_NEIGHBORHOOD_SIZE, PEAK_SORT)\n\n\ndef fingerprint(channel_samples: List[int],\n                Fs: int = DEFAULT_FS,\n                wsize: int = DEFAULT_WINDOW_SIZE,\n                wratio: float = DEFAULT_OVERLAP_RATIO,\n                fan_value: int = DEFAULT_FAN_VALUE,\n                amp_min: int = DEFAULT_AMP_MIN) -> List[Tuple[str, int]]:\n    \"\"\"\n    FFT the channel, log transform output, find local maxima, then return locally sensitive hashes.\n\n    :param channel_samples: channel samples to fingerprint.\n    :param Fs: audio sampling rate.\n    :param wsize: FFT windows size.\n    :param wratio: ratio by which each sequential window overlaps the last and the next window.\n    :param fan_value: degree to which a fingerprint can be paired with its neighbors.\n    :param amp_min: minimum amplitude in spectrogram in order to be considered a peak.\n    :return: a list of hashes with their corresponding offsets.\n    \"\"\"\n    # FFT the signal and extract frequency components\n    arr2D = mlab.specgram(\n        channel_samples,\n        NFFT=wsize,\n        Fs=Fs,\n        window=mlab.window_hanning,\n        noverlap=int(wsize * wratio))[0]\n\n    # Apply log transform since specgram function returns linear array. 0s are excluded to avoid np warning.\n    arr2D = 10 * np.log10(arr2D, out=np.zeros_like(arr2D), where=(arr2D != 0))\n\n    local_maxima = get_2D_peaks(arr2D, plot=False, amp_min=amp_min)\n\n    # return hashes\n    return generate_hashes(local_maxima, fan_value=fan_value)\n\n\ndef get_2D_peaks(arr2D: np.array, plot: bool = False, amp_min: int = DEFAULT_AMP_MIN)\\\n        -> List[Tuple[List[int], List[int]]]:\n    \"\"\"\n    Extract maximum peaks from the spectogram matrix (arr2D).\n\n    :param arr2D: matrix representing the spectogram.\n    :param plot: for plotting the results.\n    :param amp_min: minimum amplitude in spectrogram in order to be considered a peak.\n    :return: a list composed by a list of frequencies and times.\n    \"\"\"\n    # Original code from the repo is using a morphology mask that does not consider diagonal elements\n    # as neighbors (basically a diamond figure) and then applies a dilation over it, so what I'm proposing\n    # is to change from the current diamond figure to a just a normal square one:\n    #       F   T   F           T   T   T\n    #       T   T   T   ==>     T   T   T\n    #       F   T   F           T   T   T\n    # In my local tests time performance of the square mask was ~3 times faster\n    # respect to the diamond one, without hurting accuracy of the predictions.\n    # I've made now the mask shape configurable in order to allow both ways of find maximum peaks.\n    # That being said, we generate the mask by using the following function\n    # https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.generate_binary_structure.html\n    struct = generate_binary_structure(2, CONNECTIVITY_MASK)\n\n    #  And then we apply dilation using the following function\n    #  http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.iterate_structure.html\n    #  Take into account that if PEAK_NEIGHBORHOOD_SIZE is 2 you can avoid the use of the scipy functions and just\n    #  change it by the following code:\n    #  neighborhood = np.ones((PEAK_NEIGHBORHOOD_SIZE * 2 + 1, PEAK_NEIGHBORHOOD_SIZE * 2 + 1), dtype=bool)\n    neighborhood = iterate_structure(struct, PEAK_NEIGHBORHOOD_SIZE)\n\n    # find local maxima using our filter mask\n    local_max = maximum_filter(arr2D, footprint=neighborhood) == arr2D\n\n    # Applying erosion, the dejavu documentation does not talk about this step.\n    background = (arr2D == 0)\n    eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)\n\n    # Boolean mask of arr2D with True at peaks (applying XOR on both matrices).\n    detected_peaks = local_max != eroded_background\n\n    # extract peaks\n    amps = arr2D[detected_peaks]\n    freqs, times = np.where(detected_peaks)\n\n    # filter peaks\n    amps = amps.flatten()\n\n    # get indices for frequency and time\n    filter_idxs = np.where(amps > amp_min)\n\n    freqs_filter = freqs[filter_idxs]\n    times_filter = times[filter_idxs]\n\n    if plot:\n        # scatter of the peaks\n        fig, ax = plt.subplots()\n        ax.imshow(arr2D)\n        ax.scatter(times_filter, freqs_filter)\n        ax.set_xlabel('Time')\n        ax.set_ylabel('Frequency')\n        ax.set_title(\"Spectrogram\")\n        plt.gca().invert_yaxis()\n        plt.show()\n\n    return list(zip(freqs_filter, times_filter))\n\n\ndef generate_hashes(peaks: List[Tuple[int, int]], fan_value: int = DEFAULT_FAN_VALUE) -> List[Tuple[str, int]]:\n    \"\"\"\n    Hash list structure:\n       sha1_hash[0:FINGERPRINT_REDUCTION]    time_offset\n        [(e05b341a9b77a51fd26, 32), ... ]\n\n    :param peaks: list of peak frequencies and times.\n    :param fan_value: degree to which a fingerprint can be paired with its neighbors.\n    :return: a list of hashes with their corresponding offsets.\n    \"\"\"\n    # frequencies are in the first position of the tuples\n    idx_freq = 0\n    # times are in the second position of the tuples\n    idx_time = 1\n\n    if PEAK_SORT:\n        peaks.sort(key=itemgetter(1))\n\n    hashes = []\n    for i in range(len(peaks)):\n        for j in range(1, fan_value):\n            if (i + j) < len(peaks):\n\n                freq1 = peaks[i][idx_freq]\n                freq2 = peaks[i + j][idx_freq]\n                t1 = peaks[i][idx_time]\n                t2 = peaks[i + j][idx_time]\n                t_delta = t2 - t1\n\n                if MIN_HASH_TIME_DELTA <= t_delta <= MAX_HASH_TIME_DELTA:\n                    h = hashlib.sha1(f\"{str(freq1)}|{str(freq2)}|{str(t_delta)}\".encode('utf-8'))\n\n                    hashes.append((h.hexdigest()[0:FINGERPRINT_REDUCTION], t1))\n\n    return hashes\n"
  },
  {
    "path": "dejavu/logic/recognizer/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/logic/recognizer/file_recognizer.py",
    "content": "from time import time\nfrom typing import Dict\n\nimport dejavu.logic.decoder as decoder\nfrom dejavu.base_classes.base_recognizer import BaseRecognizer\nfrom dejavu.config.settings import (ALIGN_TIME, FINGERPRINT_TIME, QUERY_TIME,\n                                    RESULTS, TOTAL_TIME)\n\n\nclass FileRecognizer(BaseRecognizer):\n    def __init__(self, dejavu):\n        super().__init__(dejavu)\n\n    def recognize_file(self, filename: str) -> Dict[str, any]:\n        channels, self.Fs, _ = decoder.read(filename, self.dejavu.limit)\n\n        t = time()\n        matches, fingerprint_time, query_time, align_time = self._recognize(*channels)\n        t = time() - t\n\n        results = {\n            TOTAL_TIME: t,\n            FINGERPRINT_TIME: fingerprint_time,\n            QUERY_TIME: query_time,\n            ALIGN_TIME: align_time,\n            RESULTS: matches\n        }\n\n        return results\n\n    def recognize(self, filename: str) -> Dict[str, any]:\n        return self.recognize_file(filename)\n"
  },
  {
    "path": "dejavu/logic/recognizer/microphone_recognizer.py",
    "content": "import numpy as np\nimport pyaudio\n\nfrom dejavu.base_classes.base_recognizer import BaseRecognizer\n\n\nclass MicrophoneRecognizer(BaseRecognizer):\n    default_chunksize = 8192\n    default_format = pyaudio.paInt16\n    default_channels = 2\n    default_samplerate = 44100\n\n    def __init__(self, dejavu):\n        super().__init__(dejavu)\n        self.audio = pyaudio.PyAudio()\n        self.stream = None\n        self.data = []\n        self.channels = MicrophoneRecognizer.default_channels\n        self.chunksize = MicrophoneRecognizer.default_chunksize\n        self.samplerate = MicrophoneRecognizer.default_samplerate\n        self.recorded = False\n\n    def start_recording(self, channels=default_channels,\n                        samplerate=default_samplerate,\n                        chunksize=default_chunksize):\n        print(\"* start recording\")\n        self.chunksize = chunksize\n        self.channels = channels\n        self.recorded = False\n        self.samplerate = samplerate\n\n        if self.stream:\n            self.stream.stop_stream()\n            self.stream.close()\n\n        self.stream = self.audio.open(\n            format=self.default_format,\n            channels=channels,\n            rate=samplerate,\n            input=True,\n            frames_per_buffer=chunksize,\n        )\n\n        self.data = [[] for i in range(channels)]\n\n    def process_recording(self):\n        print(\"* recording\")\n        data = self.stream.read(self.chunksize)\n        nums = np.fromstring(data, np.int16)\n        # print(nums)\n        for c in range(self.channels):\n            self.data[c].extend(nums[c::self.channels])\n\n    def stop_recording(self):\n        print(\"* done recording\")\n        self.stream.stop_stream()\n        self.stream.close()\n        self.stream = None\n        self.recorded = True\n\n    def recognize_recording(self):\n        if not self.recorded:\n            raise NoRecordingError(\"Recording was not complete/begun\")\n        return self._recognize(*self.data)\n\n    def get_recorded_time(self):\n        return len(self.data[0]) / self.rate\n\n    def recognize(self, seconds=10):\n        self.start_recording()\n        for i in range(0, int(self.samplerate / self.chunksize * int(seconds))):\n            self.process_recording()\n        self.stop_recording()\n        return self.recognize_recording()\n\n\nclass NoRecordingError(Exception):\n    pass\n"
  },
  {
    "path": "dejavu/tests/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/tests/dejavu_test.py",
    "content": "import fnmatch\nimport json\nimport logging\nimport random\nimport re\nimport subprocess\nimport traceback\nfrom os import listdir, makedirs, walk\nfrom os.path import basename, exists, isfile, join, splitext\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom pydub import AudioSegment\n\nfrom dejavu.config.settings import (DEFAULT_FS, DEFAULT_OVERLAP_RATIO,\n                                    DEFAULT_WINDOW_SIZE, HASHES_MATCHED,\n                                    OFFSET, RESULTS, SONG_NAME, TOTAL_TIME)\nfrom dejavu.logic.decoder import get_audio_name_from_path\n\n\nclass DejavuTest:\n    def __init__(self, folder, seconds):\n        super().__init__()\n\n        self.test_folder = folder\n        self.test_seconds = seconds\n        self.test_songs = []\n\n        print(\"test_seconds\", self.test_seconds)\n\n        self.test_files = [\n            f for f in listdir(self.test_folder)\n            if isfile(join(self.test_folder, f))\n            and any([x for x in re.findall(\"[0-9]sec\", f) if x in self.test_seconds])\n        ]\n\n        print(\"test_files\", self.test_files)\n\n        self.n_columns = len(self.test_seconds)\n        self.n_lines = int(len(self.test_files) / self.n_columns)\n\n        print(\"columns:\", self.n_columns)\n        print(\"length of test files:\", len(self.test_files))\n        print(\"lines:\", self.n_lines)\n\n        # variable match results (yes, no, invalid)\n        self.result_match = [[0 for x in range(self.n_columns)] for x in range(self.n_lines)]\n\n        print(\"result_match matrix:\", self.result_match)\n\n        # variable match precision (if matched in the corrected time)\n        self.result_matching_times = [[0 for x in range(self.n_columns)] for x in range(self.n_lines)]\n\n        # variable matching time (query time)\n        self.result_query_duration = [[0 for x in range(self.n_columns)] for x in range(self.n_lines)]\n\n        # variable confidence\n        self.result_match_confidence = [[0 for x in range(self.n_columns)] for x in range(self.n_lines)]\n\n        self.begin()\n\n    def get_column_id(self, secs):\n        for i, sec in enumerate(self.test_seconds):\n            if secs == sec:\n                return i\n\n    def get_line_id(self, song):\n        for i, s in enumerate(self.test_songs):\n            if song == s:\n                return i\n        self.test_songs.append(song)\n        return len(self.test_songs) - 1\n\n    def create_plots(self, name, results, results_folder):\n        for sec in range(0, len(self.test_seconds)):\n            ind = np.arange(self.n_lines)\n            width = 0.25       # the width of the bars\n\n            fig = plt.figure()\n            ax = fig.add_subplot(111)\n            ax.set_xlim([-1 * width, 2 * width])\n\n            means_dvj = [x[0] for x in results[sec]]\n            rects1 = ax.bar(ind, means_dvj, width, color='r')\n\n            # add some\n            ax.set_ylabel(name)\n            ax.set_title(f\"{self.test_seconds[sec]} {name} Results\")\n            ax.set_xticks(ind + width)\n\n            labels = [0 for x in range(0, self.n_lines)]\n            for x in range(0, self.n_lines):\n                labels[x] = f\"song {x+1}\"\n            ax.set_xticklabels(labels)\n\n            box = ax.get_position()\n            ax.set_position([box.x0, box.y0, box.width * 0.75, box.height])\n\n            if name == 'Confidence':\n                autolabel(rects1, ax)\n            else:\n                autolabeldoubles(rects1, ax)\n\n            plt.grid()\n\n            fig_name = join(results_folder, f\"{name}_{self.test_seconds[sec]}.png\")\n            fig.savefig(fig_name)\n\n    def begin(self):\n        for f in self.test_files:\n            log_msg('--------------------------------------------------')\n            log_msg(f'file: {f}')\n\n            # get column\n            col = self.get_column_id([x for x in re.findall(\"[0-9]sec\", f) if x in self.test_seconds][0])\n\n            # format: XXXX_offset_length.mp3, we also take into account underscores within XXXX\n            splits = get_audio_name_from_path(f).split(\"_\")\n            song = \"_\".join(splits[0:len(get_audio_name_from_path(f).split(\"_\")) - 2])\n            line = self.get_line_id(song)\n            result = subprocess.check_output([\n                \"python\",\n                \"dejavu.py\",\n                '-r',\n                'file',\n                join(self.test_folder, f)])\n\n            if result.strip() == \"None\":\n                log_msg('No match')\n                self.result_match[line][col] = 'no'\n                self.result_matching_times[line][col] = 0\n                self.result_query_duration[line][col] = 0\n                self.result_match_confidence[line][col] = 0\n\n            else:\n                result = result.strip()\n                # we parse the output song back to a json\n                result = json.loads(result.decode('utf-8').replace(\"'\", '\"').replace(': b\"', ':\"'))\n\n                # which song did we predict? We consider only the first match.\n                match = result[RESULTS][0]\n                song_result = match[SONG_NAME]\n                log_msg(f'song: {song}')\n                log_msg(f'song_result: {song_result}')\n\n                if song_result != song:\n                    log_msg('invalid match')\n                    self.result_match[line][col] = 'invalid'\n                    self.result_matching_times[line][col] = 0\n                    self.result_query_duration[line][col] = 0\n                    self.result_match_confidence[line][col] = 0\n                else:\n                    log_msg('correct match')\n                    print(self.result_match)\n                    self.result_match[line][col] = 'yes'\n                    self.result_query_duration[line][col] = round(result[TOTAL_TIME], 3)\n                    self.result_match_confidence[line][col] = match[HASHES_MATCHED]\n\n                    # using replace in f for getting rid of underscores in name\n                    song_start_time = re.findall(\"_[^_]+\", f.replace(song, \"\"))\n                    song_start_time = song_start_time[0].lstrip(\"_ \")\n\n                    result_start_time = round((match[OFFSET] * DEFAULT_WINDOW_SIZE *\n                                               DEFAULT_OVERLAP_RATIO) / DEFAULT_FS, 0)\n\n                    self.result_matching_times[line][col] = int(result_start_time) - int(song_start_time)\n                    if abs(self.result_matching_times[line][col]) == 1:\n                        self.result_matching_times[line][col] = 0\n\n                    log_msg(f'query duration: {round(result[TOTAL_TIME], 3)}')\n                    log_msg(f'confidence: {match[HASHES_MATCHED]}')\n                    log_msg(f'song start_time: {song_start_time}')\n                    log_msg(f'result start time: {result_start_time}')\n\n                    if self.result_matching_times[line][col] == 0:\n                        log_msg('accurate match')\n                    else:\n                        log_msg('inaccurate match')\n            log_msg('--------------------------------------------------\\n')\n\n\ndef set_seed(seed=None):\n    \"\"\"\n    `seed` as None means that the sampling will be random.\n\n    Setting your own seed means that you can produce the\n    same experiment over and over.\n    \"\"\"\n    if seed is not None:\n        random.seed(seed)\n\n\ndef get_files_recursive(src, fmt):\n    \"\"\"\n    `src` is the source directory.\n    `fmt` is the extension, ie \".mp3\" or \"mp3\", etc.\n    \"\"\"\n    files = []\n    for root, dirnames, filenames in walk(src):\n        for filename in fnmatch.filter(filenames, '*' + fmt):\n            files.append(join(root, filename))\n\n    return files\n\n\ndef get_length_audio(audiopath, extension):\n    \"\"\"\n    Returns length of audio in seconds.\n    Returns None if format isn't supported or in case of error.\n    \"\"\"\n    try:\n        audio = AudioSegment.from_file(audiopath, extension.replace(\".\", \"\"))\n    except Exception:\n        print(f\"Error in get_length_audio(): {traceback.format_exc()}\")\n        return None\n    return int(len(audio) / 1000.0)\n\n\ndef get_starttime(length, nseconds, padding):\n    \"\"\"\n    `length` is total audio length in seconds\n    `nseconds` is amount of time to sample in seconds\n    `padding` is off-limits seconds at beginning and ending\n    \"\"\"\n    maximum = length - padding - nseconds\n    if padding > maximum:\n        return 0\n    return random.randint(padding, maximum)\n\n\ndef generate_test_files(src, dest, nseconds, fmts=[\".mp3\", \".wav\"], padding=10):\n    \"\"\"\n    Generates a test file for each file recursively in `src` directory\n    of given format using `nseconds` sampled from the audio file.\n\n    Results are written to `dest` directory.\n\n    `padding` is the number of off-limit seconds and the beginning and\n    end of a track that won't be sampled in testing. Often you want to\n    avoid silence, etc.\n    \"\"\"\n    # create directories if necessary\n    if not exists(dest):\n        makedirs(dest)\n\n    # find files recursively of a given file format\n    for fmt in fmts:\n        testsources = get_files_recursive(src, fmt)\n        for audiosource in testsources:\n\n            print(\"audiosource:\", audiosource)\n\n            filename, extension = splitext(basename(audiosource))\n            length = get_length_audio(audiosource, extension)\n            starttime = get_starttime(length, nseconds, padding)\n\n            test_file_name = f\"{join(dest, filename)}_{starttime}_{nseconds}sec.{extension.replace('.', '')}\"\n\n            subprocess.check_output([\n                \"ffmpeg\", \"-y\",\n                \"-ss\", f\"{starttime}\",\n                '-t', f\"{nseconds}\",\n                \"-i\", audiosource,\n                test_file_name])\n\n\ndef log_msg(msg, log=True, silent=False):\n    if log:\n        logging.debug(msg)\n    if not silent:\n        print(msg)\n\n\ndef autolabel(rects, ax):\n    # attach some text labels\n    for rect in rects:\n        height = rect.get_height()\n        ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, f'{int(height)}', ha='center', va='bottom')\n\n\ndef autolabeldoubles(rects, ax):\n    # attach some text labels\n    for rect in rects:\n        height = rect.get_height()\n        ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, f'{round(float(height), 3)}',\n                ha='center', va='bottom')\n"
  },
  {
    "path": "dejavu/third_party/__init__.py",
    "content": ""
  },
  {
    "path": "dejavu/third_party/wavio.py",
    "content": "# wavio.py\n# Author: Warren Weckesser\n# License: BSD 2-Clause (http://opensource.org/licenses/BSD-2-Clause)\n# Synopsis: A Python module for reading and writing 24 bit WAV files.\n# Github: github.com/WarrenWeckesser/wavio\n\n\"\"\"\nThe wavio module defines the functions:\nread(file)\n    Read a WAV file and return a `wavio.Wav` object, with attributes\n    `data`, `rate` and `sampwidth`.\nwrite(filename, data, rate, scale=None, sampwidth=None)\n    Write a numpy array to a WAV file.\n-----\nAuthor: Warren Weckesser\nLicense: BSD 2-Clause:\nCopyright (c) 2015, Warren Weckesser\nAll rights reserved.\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n1. Redistributions of source code must retain the above copyright notice,\n   this list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright notice,\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\nARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\nLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\nSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\nINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\nCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\nARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\nPOSSIBILITY OF SUCH DAMAGE.\n\"\"\"\n\n\nimport wave as _wave\n\nimport numpy as _np\n\n__version__ = \"0.0.5.dev1\"\n\n\ndef _wav2array(nchannels, sampwidth, data):\n    \"\"\"data must be the string containing the bytes from the wav file.\"\"\"\n    num_samples, remainder = divmod(len(data), sampwidth * nchannels)\n    if remainder > 0:\n        raise ValueError('The length of data is not a multiple of '\n                         'sampwidth * num_channels.')\n    if sampwidth > 4:\n        raise ValueError(\"sampwidth must not be greater than 4.\")\n\n    if sampwidth == 3:\n        a = _np.empty((num_samples, nchannels, 4), dtype=_np.uint8)\n        raw_bytes = _np.frombuffer(data, dtype=_np.uint8)\n        a[:, :, :sampwidth] = raw_bytes.reshape(-1, nchannels, sampwidth)\n        a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255\n        result = a.view('<i4').reshape(a.shape[:-1])\n    else:\n        # 8 bit samples are stored as unsigned ints; others as signed ints.\n        dt_char = 'u' if sampwidth == 1 else 'i'\n        a = _np.frombuffer(data, dtype=f'<{dt_char}{sampwidth}')\n        result = a.reshape(-1, nchannels)\n    return result\n\n\ndef _array2wav(a, sampwidth):\n    \"\"\"\n    Convert the input array `a` to a string of WAV data.\n    a.dtype must be one of uint8, int16 or int32.  Allowed sampwidth\n    values are:\n        dtype    sampwidth\n        uint8        1\n        int16        2\n        int32      3 or 4\n    When sampwidth is 3, the *low* bytes of `a` are assumed to contain\n    the values to include in the string.\n    \"\"\"\n    if sampwidth == 3:\n        # `a` must have dtype int32\n        if a.ndim == 1:\n            # Convert to a 2D array with a single column.\n            a = a.reshape(-1, 1)\n        # By shifting first 0 bits, then 8, then 16, the resulting output\n        # is 24 bit little-endian.\n        a8 = (a.reshape(a.shape + (1,)) >> _np.array([0, 8, 16])) & 255\n        wavdata = a8.astype(_np.uint8).tostring()\n    else:\n        # Make sure the array is little-endian, and then convert using\n        # tostring()\n        a = a.astype('<' + a.dtype.str[1:], copy=False)\n        wavdata = a.tostring()\n    return wavdata\n\n\nclass Wav(object):\n    \"\"\"\n    Object returned by `wavio.read`.  Attributes are:\n    data : numpy array\n        The array of data read from the WAV file.\n    rate : float\n        The sample rate of the WAV file.\n    sampwidth : int\n        The sample width (i.e. number of bytes per sample) of the WAV file.\n        For example, `sampwidth == 3` is a 24 bit WAV file.\n    \"\"\"\n\n    def __init__(self, data, rate, sampwidth):\n        self.data = data\n        self.rate = rate\n        self.sampwidth = sampwidth\n\n    def __repr__(self):\n        s = (f\"Wav(data.shape={self.data.shape}, data.dtype={self.data.dtype}, \"\n             f\"rate={self.rate}, sampwidth={self.sampwidth})\")\n        return s\n\n\ndef read(file):\n    \"\"\"\n    Read a WAV file.\n    Parameters\n    ----------\n    file : string or file object\n        Either the name of a file or an open file pointer.\n    Returns\n    -------\n    wav : wavio.Wav() instance\n        The return value is an instance of the class `wavio.Wav`,\n        with the following attributes:\n            data : numpy array\n                The array containing the data.  The shape of the array\n                is (num_samples, num_channels).  num_channels is the\n                number of audio channels (1 for mono, 2 for stereo).\n            rate : float\n                The sampling frequency (i.e. frame rate)\n            sampwidth : float\n                The sample width, in bytes.  E.g. for a 24 bit WAV file,\n                sampwidth is 3.\n    Notes\n    -----\n    This function uses the `wave` module of the Python standard libary\n    to read the WAV file, so it has the same limitations as that library.\n    In particular, the function does not read compressed WAV files, and\n    it does not read files with floating point data.\n    The array returned by `wavio.read` is always two-dimensional.  If the\n    WAV data is mono, the array will have shape (num_samples, 1).\n    `wavio.read()` does not scale or normalize the data.  The data in the\n    array `wav.data` is the data that was in the file.  When the file\n    contains 24 bit samples, the resulting numpy array is 32 bit integers,\n    with values that have been sign-extended.\n    \"\"\"\n    wav = _wave.open(file)\n    rate = wav.getframerate()\n    nchannels = wav.getnchannels()\n    sampwidth = wav.getsampwidth()\n    nframes = wav.getnframes()\n    data = wav.readframes(nframes)\n    wav.close()\n    array = _wav2array(nchannels, sampwidth, data)\n    w = Wav(data=array, rate=rate, sampwidth=sampwidth)\n    return w\n\n\n_sampwidth_dtypes = {1: _np.uint8,\n                     2: _np.int16,\n                     3: _np.int32,\n                     4: _np.int32}\n_sampwidth_ranges = {1: (0, 256),\n                     2: (-2**15, 2**15),\n                     3: (-2**23, 2**23),\n                     4: (-2**31, 2**31)}\n\n\ndef _scale_to_sampwidth(data, sampwidth, vmin, vmax):\n    # Scale and translate the values to fit the range of the data type\n    # associated with the given sampwidth.\n\n    data = data.clip(vmin, vmax)\n\n    dt = _sampwidth_dtypes[sampwidth]\n    if vmax == vmin:\n        data = _np.zeros(data.shape, dtype=dt)\n    else:\n        outmin, outmax = _sampwidth_ranges[sampwidth]\n        if outmin != vmin or outmax != vmax:\n            vmin = float(vmin)\n            vmax = float(vmax)\n            data = (float(outmax - outmin) * (data - vmin) /\n                    (vmax - vmin)).astype(_np.int64) + outmin\n            data[data == outmax] = outmax - 1\n        data = data.astype(dt)\n\n    return data\n\n\ndef write(file, data, rate, scale=None, sampwidth=None):\n    \"\"\"\n    Write the numpy array `data` to a WAV file.\n    The Python standard library \"wave\" is used to write the data\n    to the file, so this function has the same limitations as that\n    module.  In particular, the Python library does not support\n    floating point data.  When given a floating point array, this\n    function converts the values to integers.  See below for the\n    conversion rules.\n    Parameters\n    ----------\n    file : string, or file object open for writing in binary mode\n        Either the name of a file or an open file pointer.\n    data : numpy array, 1- or 2-dimensional, integer or floating point\n        If it is 2-d, the rows are the frames (i.e. samples) and the\n        columns are the channels.\n    rate : float\n        The sampling frequency (i.e. frame rate) of the data.\n    sampwidth : int, optional\n        The sample width, in bytes, of the output file.\n        If `sampwidth` is not given, it is inferred (if possible) from\n        the data type of `data`, as follows::\n            data.dtype     sampwidth\n            ----------     ---------\n            uint8, int8        1\n            uint16, int16      2\n            uint32, int32      4\n        For any other data types, or to write a 24 bit file, `sampwidth`\n        must be given.\n    scale : tuple or str, optional\n        By default, the data written to the file is scaled up or down to\n        occupy the full range of the output data type.  So, for example,\n        the unsigned 8 bit data [0, 1, 2, 15] would be written to the file\n        as [0, 17, 30, 255].  More generally, the default behavior is\n        (roughly)::\n            vmin = data.min()\n            vmax = data.max()\n            outmin = <minimum integer of the output dtype>\n            outmax = <maximum integer of the output dtype>\n            outdata = (outmax - outmin)*(data - vmin)/(vmax - vmin) + outmin\n        The `scale` argument allows the scaling of the output data to be\n        changed.  `scale` can be a tuple of the form `(vmin, vmax)`, in which\n        case the given values override the use of `data.min()` and\n        `data.max()` for `vmin` and `vmax` shown above.  (If either value\n        is `None`, the value shown above is used.)  Data outside the\n        range (vmin, vmax) is clipped.  If `vmin == vmax`, the output is\n        all zeros.\n        If `scale` is the string \"none\", then `vmin` and `vmax` are set to\n        `outmin` and `outmax`, respectively. This means the data is written\n        to the file with no scaling.  (Note: `scale=\"none\" is not the same\n        as `scale=None`.  The latter means \"use the default behavior\",\n        which is to scale by the data minimum and maximum.)\n        If `scale` is the string \"dtype-limits\", then `vmin` and `vmax`\n        are set to the minimum and maximum integers of `data.dtype`.\n        The string \"dtype-limits\" is not allowed when the `data` is a\n        floating point array.\n        If using `scale` results in values that exceed the limits of the\n        output sample width, the data is clipped.  For example, the\n        following code::\n            >> x = np.array([-100, 0, 100, 200, 300, 325])\n            >> wavio.write('foo.wav', x, 8000, scale='none', sampwidth=1)\n        will write the values [0, 0, 100, 200, 255, 255] to the file.\n    Example\n    -------\n    Create a 3 second 440 Hz sine wave, and save it in a 24-bit WAV file.\n    >> import numpy as np\n    >> import wavio\n    >> rate = 22050  # samples per second\n    >> T = 3         # sample duration (seconds)\n    >> f = 440.0     # sound frequency (Hz)\n    >> t = np.linspace(0, T, T*rate, endpoint=False)\n    >> x = np.sin(2*np.pi * f * t)\n    >> wavio.write(\"sine24.wav\", x, rate, sampwidth=3)\n    Create a file that contains the 16 bit integer values -10000 and 10000\n    repeated 100 times.  Don't automatically scale the values.  Use a sample\n    rate 8000.\n    >> x = np.empty(200, dtype=np.int16)\n    >> x[::2] = -10000\n    >> x[1::2] = 10000\n    >> wavio.write(\"foo.wav\", x, 8000, scale='none')\n    Check that the file contains what we expect.\n    >> w = wavio.read(\"foo.wav\")\n    >> np.all(w.data[:, 0] == x)\n    True\n    In the following, the values -10000 and 10000 (from within the 16 bit\n    range [-2**15, 2**15-1]) are mapped to the corresponding values 88 and\n    168 (in the range [0, 2**8-1]).\n    >> wavio.write(\"foo.wav\", x, 8000, sampwidth=1, scale='dtype-limits')\n    >> w = wavio.read(\"foo.wav\")\n    >> w.data[:4, 0]\n    array([ 88, 168,  88, 168], dtype=uint8)\n    \"\"\"\n\n    if sampwidth is None:\n        if not _np.issubdtype(data.dtype, _np.integer) or data.itemsize > 4:\n            raise ValueError('when data.dtype is not an 8-, 16-, or 32-bit integer type, sampwidth must be specified.')\n        sampwidth = data.itemsize\n    else:\n        if sampwidth not in [1, 2, 3, 4]:\n            raise ValueError('sampwidth must be 1, 2, 3 or 4.')\n\n    outdtype = _sampwidth_dtypes[sampwidth]\n    outmin, outmax = _sampwidth_ranges[sampwidth]\n\n    if scale == \"none\":\n        data = data.clip(outmin, outmax-1).astype(outdtype)\n    elif scale == \"dtype-limits\":\n        if not _np.issubdtype(data.dtype, _np.integer):\n            raise ValueError(\"scale cannot be 'dtype-limits' with non-integer data.\")\n        # Easy transforms that just changed the signedness of the data.\n        if sampwidth == 1 and data.dtype == _np.int8:\n            data = (data.astype(_np.int16) + 128).astype(_np.uint8)\n        elif sampwidth == 2 and data.dtype == _np.uint16:\n            data = (data.astype(_np.int32) - 32768).astype(_np.int16)\n        elif sampwidth == 4 and data.dtype == _np.uint32:\n            data = (data.astype(_np.int64) - 2**31).astype(_np.int32)\n        elif data.itemsize != sampwidth:\n            # Integer input, but rescaling is needed to adjust the\n            # input range to the output sample width.\n            ii = _np.iinfo(data.dtype)\n            vmin = ii.min\n            vmax = ii.max\n            data = _scale_to_sampwidth(data, sampwidth, vmin, vmax)\n    else:\n        if scale is None:\n            vmin = data.min()\n            vmax = data.max()\n        else:\n            # scale must be a tuple of the form (vmin, vmax)\n            vmin, vmax = scale\n            if vmin is None:\n                vmin = data.min()\n            if vmax is None:\n                vmax = data.max()\n\n        data = _scale_to_sampwidth(data, sampwidth, vmin, vmax)\n\n    # At this point, `data` has been converted to have one of the following:\n    #    sampwidth   dtype\n    #    ---------   -----\n    #        1       uint8\n    #        2       int16\n    #        3       int32\n    #        4       int32\n    # The values in `data` are in the form in which they will be saved;\n    # no more scaling will take place.\n\n    if data.ndim == 1:\n        data = data.reshape(-1, 1)\n\n    wavdata = _array2wav(data, sampwidth)\n\n    w = _wave.open(file, 'wb')\n    w.setnchannels(data.shape[1])\n    w.setsampwidth(sampwidth)\n    w.setframerate(rate)\n    w.writeframes(wavdata)\n    w.close()\n"
  },
  {
    "path": "dejavu.cnf.SAMPLE",
    "content": "{\n    \"database\": {\n        \"host\": \"127.0.0.1\",\n        \"user\": \"root\",\n        \"password\": \"rootpass\",\n        \"database\": \"dejavu\"\n    },\n    \"database_type\": \"mysql\"\n}\n"
  },
  {
    "path": "dejavu.py",
    "content": "import argparse\nimport json\nimport sys\nfrom argparse import RawTextHelpFormatter\nfrom os.path import isdir\n\nfrom dejavu import Dejavu\nfrom dejavu.logic.recognizer.file_recognizer import FileRecognizer\nfrom dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer\n\nDEFAULT_CONFIG_FILE = \"dejavu.cnf.SAMPLE\"\n\n\ndef init(configpath):\n    \"\"\"\n    Load config from a JSON file\n    \"\"\"\n    try:\n        with open(configpath) as f:\n            config = json.load(f)\n    except IOError as err:\n        print(f\"Cannot open configuration: {str(err)}. Exiting\")\n        sys.exit(1)\n\n    # create a Dejavu instance\n    return Dejavu(config)\n\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(\n        description=\"Dejavu: Audio Fingerprinting library\",\n        formatter_class=RawTextHelpFormatter)\n    parser.add_argument('-c', '--config', nargs='?',\n                        help='Path to configuration file\\n'\n                             'Usages: \\n'\n                             '--config /path/to/config-file\\n')\n    parser.add_argument('-f', '--fingerprint', nargs='*',\n                        help='Fingerprint files in a directory\\n'\n                             'Usages: \\n'\n                             '--fingerprint /path/to/directory extension\\n'\n                             '--fingerprint /path/to/directory')\n    parser.add_argument('-r', '--recognize', nargs=2,\n                        help='Recognize what is '\n                             'playing through the microphone or in a file.\\n'\n                             'Usage: \\n'\n                             '--recognize mic number_of_seconds \\n'\n                             '--recognize file path/to/file \\n')\n    args = parser.parse_args()\n\n    if not args.fingerprint and not args.recognize:\n        parser.print_help()\n        sys.exit(0)\n\n    config_file = args.config\n    if config_file is None:\n        config_file = DEFAULT_CONFIG_FILE\n\n    djv = init(config_file)\n    if args.fingerprint:\n        # Fingerprint all files in a directory\n        if len(args.fingerprint) == 2:\n            directory = args.fingerprint[0]\n            extension = args.fingerprint[1]\n            print(f\"Fingerprinting all .{extension} files in the {directory} directory\")\n            djv.fingerprint_directory(directory, [\".\" + extension], 4)\n\n        elif len(args.fingerprint) == 1:\n            filepath = args.fingerprint[0]\n            if isdir(filepath):\n                print(\"Please specify an extension if you'd like to fingerprint a directory!\")\n                sys.exit(1)\n            djv.fingerprint_file(filepath)\n\n    elif args.recognize:\n        # Recognize audio source\n        songs = None\n        source = args.recognize[0]\n        opt_arg = args.recognize[1]\n\n        if source in ('mic', 'microphone'):\n            songs = djv.recognize(MicrophoneRecognizer, seconds=opt_arg)\n        elif source == 'file':\n            songs = djv.recognize(FileRecognizer, opt_arg)\n        print(songs)\n"
  },
  {
    "path": "docker/.gitkeep",
    "content": ""
  },
  {
    "path": "docker/postgres/Dockerfile",
    "content": "FROM postgres:10.7-alpine\nCOPY init.sql /docker-entrypoint-initdb.d/"
  },
  {
    "path": "docker/postgres/init.sql",
    "content": "-- put any SQL you'd like to run on creation of the image\n-- in this file :)"
  },
  {
    "path": "docker/python/Dockerfile",
    "content": "FROM python:3.7\nRUN apt-get update -y && apt-get upgrade -y\nRUN apt-get install \\\n    gcc nano \\\n    ffmpeg libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0 \\\n    postgresql postgresql-contrib -y\nRUN pip install numpy scipy matplotlib pydub pyaudio psycopg2\nWORKDIR /code"
  },
  {
    "path": "docker-compose.yaml",
    "content": "version: '3'\nservices:\n  db:\n    build:\n      context: ./docker/postgres\n    environment:\n      - POSTGRES_DB=dejavu\n      - POSTGRES_USER=postgres\n      - POSTGRES_PASSWORD=password\n    networks:\n      - db_network\n  python:\n    build:\n      context: ./docker/python\n    volumes:\n      - .:/code\n    depends_on:\n      - db\n    networks:\n      - db_network\nnetworks:\n  db_network:"
  },
  {
    "path": "example_docker_postgres.py",
    "content": "import json\n\nfrom dejavu import Dejavu\nfrom dejavu.logic.recognizer.file_recognizer import FileRecognizer\nfrom dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer\n\n# load config from a JSON file (or anything outputting a python dictionary)\nconfig = {\n    \"database\": {\n        \"host\": \"db\",\n        \"user\": \"postgres\",\n        \"password\": \"password\",\n        \"database\": \"dejavu\"\n    },\n    \"database_type\": \"postgres\"\n}\n\nif __name__ == '__main__':\n\n    # create a Dejavu instance\n    djv = Dejavu(config)\n\n    # Fingerprint all the mp3's in the directory we give it\n    djv.fingerprint_directory(\"test\", [\".wav\"])\n\n    # Recognize audio from a file\n    results = djv.recognize(FileRecognizer, \"mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3\")\n    print(f\"From file we recognized: {results}\\n\")\n\n    # Or use a recognizer without the shortcut, in anyway you would like\n    recognizer = FileRecognizer(djv)\n    results = recognizer.recognize_file(\"mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3\")\n    print(f\"No shortcut, we recognized: {results}\\n\")\n"
  },
  {
    "path": "example_script.py",
    "content": "import json\n\nfrom dejavu import Dejavu\nfrom dejavu.logic.recognizer.file_recognizer import FileRecognizer\nfrom dejavu.logic.recognizer.microphone_recognizer import MicrophoneRecognizer\n\n# load config from a JSON file (or anything outputting a python dictionary)\nwith open(\"dejavu.cnf.SAMPLE\") as f:\n    config = json.load(f)\n\nif __name__ == '__main__':\n\n    # create a Dejavu instance\n    djv = Dejavu(config)\n\n    # Fingerprint all the mp3's in the directory we give it\n    djv.fingerprint_directory(\"test\", [\".wav\"])\n\n    # Recognize audio from a file\n    results = djv.recognize(FileRecognizer, \"mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3\")\n    print(f\"From file we recognized: {results}\\n\")\n\n    # Or recognize audio from your microphone for `secs` seconds\n    secs = 5\n    results = djv.recognize(MicrophoneRecognizer, seconds=secs)\n    if results is None:\n        print(\"Nothing recognized -- did you play the song out loud so your mic could hear it? :)\")\n    else:\n        print(f\"From mic with {secs} seconds we recognized: {results}\\n\")\n\n    # Or use a recognizer without the shortcut, in anyway you would like\n    recognizer = FileRecognizer(djv)\n    results = recognizer.recognize_file(\"mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3\")\n    print(f\"No shortcut, we recognized: {results}\\n\")\n"
  },
  {
    "path": "requirements.txt",
    "content": "pydub==0.23.1\nPyAudio==0.2.11\nnumpy==1.17.2\nscipy==1.3.1\nmatplotlib==3.1.1\nmysql-connector-python==8.0.17\npsycopg2==2.8.3\n"
  },
  {
    "path": "run_tests.py",
    "content": "import argparse\nimport logging\nimport time\nfrom os import makedirs\nfrom os.path import exists, join\nfrom shutil import rmtree\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom dejavu.tests.dejavu_test import (DejavuTest, autolabeldoubles,\n                                      generate_test_files, log_msg, set_seed)\n\n\ndef main(seconds: int, results_folder: str, temp_folder: str, log: bool, silent: bool,\n         log_file: str, padding: int, seed: int, src: str):\n\n    # set random seed if set by user\n    set_seed(seed)\n\n    # ensure results folder exists\n    if not exists(results_folder):\n        makedirs(results_folder)\n\n    # set logging\n    if log:\n        logging.basicConfig(filename=log_file, level=logging.DEBUG)\n\n    # set test seconds\n    test_seconds = [f'{i}sec' for i in range(1, seconds + 1, 1)]\n\n    # generate testing files\n    for i in range(1, seconds + 1, 1):\n        generate_test_files(src, temp_folder, i, padding=padding)\n\n    # scan files\n    log_msg(f\"Running Dejavu fingerprinter on files in {src}...\", log=log, silent=silent)\n\n    tm = time.time()\n    djv = DejavuTest(temp_folder, test_seconds)\n    log_msg(f\"finished obtaining results from dejavu in {(time.time() - tm)}\", log=log, silent=silent)\n\n    tests = 1  # djv\n    n_secs = len(test_seconds)\n\n    # set result variables -> 4d variables\n    all_match_counter = [[[0 for x in range(tests)] for x in range(3)] for x in range(n_secs)]\n    all_matching_times_counter = [[[0 for x in range(tests)] for x in range(2)] for x in range(n_secs)]\n    all_query_duration = [[[0 for x in range(tests)] for x in range(djv.n_lines)] for x in range(n_secs)]\n    all_match_confidence = [[[0 for x in range(tests)] for x in range(djv.n_lines)] for x in range(n_secs)]\n\n    # group results by seconds\n    for line in range(0, djv.n_lines):\n        for col in range(0, djv.n_columns):\n            # for dejavu\n            all_query_duration[col][line][0] = djv.result_query_duration[line][col]\n            all_match_confidence[col][line][0] = djv.result_match_confidence[line][col]\n\n            djv_match_result = djv.result_match[line][col]\n\n            if djv_match_result == 'yes':\n                all_match_counter[col][0][0] += 1\n            elif djv_match_result == 'no':\n                all_match_counter[col][1][0] += 1\n            else:\n                all_match_counter[col][2][0] += 1\n\n            djv_match_acc = djv.result_matching_times[line][col]\n\n            if djv_match_acc == 0 and djv_match_result == 'yes':\n                all_matching_times_counter[col][0][0] += 1\n            elif djv_match_acc != 0:\n                all_matching_times_counter[col][1][0] += 1\n\n    # create plots\n    djv.create_plots('Confidence', all_match_confidence, results_folder)\n    djv.create_plots('Query duration', all_query_duration, results_folder)\n\n    for sec in range(0, n_secs):\n        ind = np.arange(3)\n        width = 0.25  # the width of the bars\n\n        fig = plt.figure()\n        ax = fig.add_subplot(111)\n        ax.set_xlim([-1 * width, 2.75])\n\n        means_dvj = [round(x[0] * 100 / djv.n_lines, 1) for x in all_match_counter[sec]]\n        rects1 = ax.bar(ind, means_dvj, width, color='r')\n\n        # add some\n        ax.set_ylabel('Matching Percentage')\n        ax.set_title(f'{test_seconds[sec]} Matching Percentage')\n        ax.set_xticks(ind + width)\n\n        labels = ['yes', 'no', 'invalid']\n        ax.set_xticklabels(labels)\n\n        box = ax.get_position()\n        ax.set_position([box.x0, box.y0, box.width * 0.75, box.height])\n        autolabeldoubles(rects1, ax)\n        plt.grid()\n\n        fig_name = join(results_folder, f\"matching_perc_{test_seconds[sec]}.png\")\n        fig.savefig(fig_name)\n\n    for sec in range(0, n_secs):\n        ind = np.arange(2)\n        width = 0.25  # the width of the bars\n\n        fig = plt.figure()\n        ax = fig.add_subplot(111)\n        ax.set_xlim([-1 * width, 1.75])\n\n        div = all_match_counter[sec][0][0]\n        if div == 0:\n            div = 1000000\n\n        means_dvj = [round(x[0] * 100 / div, 1) for x in all_matching_times_counter[sec]]\n        rects1 = ax.bar(ind, means_dvj, width, color='r')\n\n        # add some\n        ax.set_ylabel('Matching Accuracy')\n        ax.set_title(f'{test_seconds[sec]} Matching Times Accuracy')\n        ax.set_xticks(ind + width)\n\n        labels = ['yes', 'no']\n        ax.set_xticklabels(labels)\n\n        box = ax.get_position()\n        ax.set_position([box.x0, box.y0, box.width * 0.75, box.height])\n        autolabeldoubles(rects1, ax)\n\n        plt.grid()\n\n        fig_name = join(results_folder, f\"matching_acc_{test_seconds[sec]}.png\")\n        fig.savefig(fig_name)\n\n    # remove temporary folder\n    rmtree(temp_folder)\n\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(description=f'Runs a few tests for dejavu to evaluate '\n                                                 f'its configuration performance. '\n                                                 f'Usage: %(prog).py [options] TESTING_AUDIOFOLDER'\n                                     )\n\n    parser.add_argument(\"-sec\", \"--seconds\", action=\"store\", default=5, type=int,\n                        help='Number of seconds starting from zero to test.')\n    parser.add_argument(\"-res\", \"--results-folder\", action=\"store\", default=\"./dejavu_test_results\",\n                        help='Sets the path where the results are saved.')\n    parser.add_argument(\"-temp\", \"--temp-folder\", action=\"store\", default=\"./dejavu_temp_testing_files\",\n                        help='Sets the path where the temp files are saved.')\n    parser.add_argument(\"-l\", \"--log\", action=\"store_true\", default=False, help='Enables logging.')\n    parser.add_argument(\"-sl\", \"--silent\", action=\"store_false\", default=False, help='Disables printing.')\n    parser.add_argument(\"-lf\", \"--log-file\", default=\"results-compare.log\",\n                        help='Set the path and filename of the log file.')\n    parser.add_argument(\"-pad\", \"--padding\", action=\"store\", default=10, type=int,\n                        help='Number of seconds to pad choice of place to test from.')\n    parser.add_argument(\"-sd\", \"--seed\", action=\"store\", default=None, type=int, help='Random seed.')\n    parser.add_argument(\"src\", type=str, help='Source folder for audios to use as tests.')\n\n    args = parser.parse_args()\n\n    main(args.seconds, args.results_folder, args.temp_folder, args.log, args.silent, args.log_file, args.padding,\n         args.seed, args.src)\n"
  },
  {
    "path": "setup.cfg",
    "content": "[flake8]\nmax-line-length = 120\n\n"
  },
  {
    "path": "setup.py",
    "content": "from setuptools import find_packages, setup\n\n\ndef parse_requirements(requirements):\n    # load from requirements.txt\n    with open(requirements) as f:\n        lines = [l for l in f]\n        # remove spaces\n        stripped = list(map((lambda x: x.strip()), lines))\n        # remove comments\n        nocomments = list(filter((lambda x: not x.startswith('#')), stripped))\n        # remove empty lines\n        reqs = list(filter((lambda x: x), nocomments))\n        return reqs\n\n\nPACKAGE_NAME = \"PyDejavu\"\nPACKAGE_VERSION = \"0.1.3\"\nSUMMARY = 'Dejavu: Audio Fingerprinting in Python'\nDESCRIPTION = \"\"\"\nAudio fingerprinting and recognition algorithm implemented in Python\n\nSee the explanation here:\n\n`http://willdrevo.com/fingerprinting-and-audio-recognition-with-python/`__\n\nDejavu can memorize recorded audio by listening to it once and fingerprinting\nit. Then by playing a song and recording microphone input or on disk file,\nDejavu attempts to match the audio against the fingerprints held in the\ndatabase, returning the song or recording being played.\n\n__ http://willdrevo.com/fingerprinting-and-audio-recognition-with-python/\n\"\"\"\nREQUIREMENTS = parse_requirements(\"requirements.txt\")\n\nsetup(\n    name=PACKAGE_NAME,\n    version=PACKAGE_VERSION,\n    description=SUMMARY,\n    long_description=DESCRIPTION,\n    author='Will Drevo',\n    author_email='will.drevo@gmail.com',\n    maintainer=\"Will Drevo\",\n    maintainer_email=\"will.drevo@gmail.com\",\n    url='http://github.com/worldveil/dejavu',\n    license='MIT License',\n    include_package_data=True,\n    packages=find_packages(),\n    platforms=['Unix'],\n    install_requires=REQUIREMENTS,\n    classifiers=[\n        'Development Status :: 4 - Beta',\n        'Environment :: Console',\n        'Intended Audience :: Developers',\n        'License :: OSI Approved :: MIT License',\n        'Operating System :: OS Independent',\n        'Topic :: Software Development :: Libraries :: Python Modules',\n    ],\n    keywords=\"python, audio, fingerprinting, music, numpy, landmark\",\n)\n"
  },
  {
    "path": "test_dejavu.sh",
    "content": "#####################################\n### Dejavu example testing script ###\n#####################################\n\n###########\n# Clear out previous results\nrm -rf ./results ./temp_audio\n\n###########\n# Fingerprint files of extension mp3 in the ./mp3 folder\npython dejavu.py -f ./mp3/ mp3\n\n##########\n# Run a test suite on the ./mp3 folder by extracting 1, 2, 3, 4, and 5 \n# second clips sampled randomly from within each song 8 seconds \n# away from start or end, sampling with random seed = 42, and finally \n# store results in ./results and log to dejavu-test.log\npython run_tests.py \\\n\t--secs 5 \\\n\t--temp ./temp_audio \\\n\t--log-file ./results/dejavu-test.log \\\n\t--padding 8 \\\n\t--seed 42 \\\n\t--results ./results \\\n\t./mp3\n"
  }
]