dpwe / audfprint

Landmark-based audio fingerprinting
MIT License
547 stars 123 forks source link

Import Usage #38

Closed dovidgef closed 5 years ago

dovidgef commented 6 years ago

Hi, if I were to import this library into a larger Python script, how would I go about running the equivalent of the following commands: python audfprint.py match -x 10 --dbase test.pklz path/to/possible/match.wav python audfprint.py add -x 10 --dbase test.pklz path/to/new/file.wav

I did not see anything in the documentation about how to use the program within a Python script.

By the way, thanks for writing such a useful program.

dpwe commented 6 years ago

For audfprint match, you could do something like the code below (which is incomplete, but hopefully intelligible). You could do add similarly, but calling analyzer.ingest(hash_tab, filename).

from audfprint import audfprint
from audfprint import audfprint_analyze
from audfprint import hash_table
import numpy as np

hash_tab = hash_table.HashTable()
hash_tab.load_pkl(input_reference_filename)
analyzer = audfprint.setup_analyzer({
        '--density': str(AUDFPRINT_DENSITY),
        '--pks-per-frame': str(AUDFPRINT_PKS_PER_FRAME),
        '--fanout': str(AUDFPRINT_FANOUT),
        '--freq-sd': str(AUDFPRINT_FREQ_SD),
        '--shifts': str(AUDFPRINT_SHIFTS),
        '--samplerate': str(int(SAMPLE_RATE)),  # Expects an int.
        '--continue-on-error': False
})
matcher = audfprint.setup_matcher({
        '--match-win': str(AUDFPRINT_MATCH_WIN),
        '--min-count': str(AUDFPRINT_MIN_COUNT),
        '--max-matches': '1',  # Unused in this application.
        '--search-depth': str(AUDFPRINT_SEARCH_DEPTH),
        '--sortbytime': False,   # Unused in this application.
        '--exact-count': AUDFPRINT_EXACT_COUNT,
        '--illustrate': False,
        '--illustrate-hpf': False,
        '--verbose': False,
        '--find-time-range': True,  # Needed for this application.
        '--time-quantile': str(AUDFPRINT_TIME_QUANTILE)
})
samples, sample_rate = wavread(query_wavfile)   # However you read in the audio to an np.array.
time_hash_pairs = np.array(
      audfprint_analyze.landmarks2hashes(
          analyzer.peaks2landmarks(analyzer.find_peaks(samples, sample_rate))),
      dtype=np.int32)
results = matcher.match_hashes(hash_tab, time_hash_pairs)
# Each element of results contains
# (id, filteredmatches, timoffs, rawmatches, origrank, mintime, maxtime)