Beispiel #1
0
    def match_file_db(self, audio_file):
        try:
            array, sr = audio_read(audio_file, sr=self.sample_rate, channels=1)
            hashes = self.fingerprint_array(array)
            matches = self.db.return_matches(hashes)
            if len(matches) > 0:
                print("Number of hashes: %d\nNumber of matches: %d" %
                      (len(hashes), len(matches)))
                best_sids = self.db.get_best_sids(matches)
                results = self.db.align_matches(matches, best_sids)
            else:
                results = []
        except UnsupportedError:
            results = None

        matches = dict()
        if results is not None:
            for result in results:
                station, time = result['song_name'].split('.')
                if station not in matches.keys():
                    matches[station] = {
                        'hashes': [result['confidence']],
                        'time': [time]
                    }
                else:
                    matches[station]['hashes'] += [result['confidence']]
                    matches[station]['time'] += [time]
        return matches
Beispiel #2
0
    def match_file_hash_table(self, audio_file):
        """
        Read file into numpy array, fingerprint to hashes and match from hash table
        """
        try:
            array, sr = audio_read(audio_file, sr=self.sample_rate, channels=1)
            hashes = self.fingerprint_array(array)
            result = self.matcher.match_hashes(self.hash_tab, hashes)
        except UnsupportedError:
            result = []

        # The audio clip is likely spanning recordings so we will return multiple results with high score
        matches = dict()
        for r in result:
            id, nhashaligned, aligntime, nhashraw, rank, min_time, max_time = r
            if nhashaligned > 10:
                station, time = self.hash_tab.names[id].split('.')
                if station not in matches.keys():
                    matches[station] = {
                        'hashes': [nhashaligned],
                        'time': [time]
                    }
                else:
                    matches[station]['hashes'] += [nhashaligned]
                    matches[station]['time'] += [time]

        return matches
Beispiel #3
0
 def illustrate_match(self, analyzer, ht, filename):
     """ Show the query fingerprints and the matching ones
         plotted over a spectrogram """
     # Make the spectrogram
     #d, sr = librosa.load(filename, sr=analyzer.target_sr)
     d, sr = audio_read.audio_read(filename, sr=analyzer.target_sr, channels=1)
     sgram = np.abs(librosa.stft(d, n_fft=analyzer.n_fft,
                                 hop_length=analyzer.n_hop,
                                 window=np.hanning(analyzer.n_fft+2)[1:-1]))
     sgram = 20.0*np.log10(np.maximum(sgram, np.max(sgram)/1e6))
     sgram = sgram - np.mean(sgram)
     # High-pass filter onset emphasis
     # [:-1,] discards top bin (nyquist) of sgram so bins fit in 8 bits
     # spectrogram enhancement
     if self.illustrate_hpf:
         HPF_POLE = 0.98
         sgram = np.array([scipy.signal.lfilter([1, -1],
                                                [1, -HPF_POLE], s_row)
                           for s_row in sgram])[:-1,]
     sgram = sgram - np.max(sgram)
     librosa.display.specshow(sgram, sr=sr, hop_length=analyzer.n_hop,
                              y_axis='linear', x_axis='time',
                              cmap='gray_r', vmin=-80.0, vmax=0)
     # Do the match?
     q_hashes = analyzer.wavfile2hashes(filename)
     # Run query, get back the hashes for match zero
     results, matchhashes = self.match_hashes(ht, q_hashes, hashesfor=0)
     if self.sort_by_time:
         results = sorted(results, key=lambda x: -x[2])
     # Convert the hashes to landmarks
     lms = audfprint_analyze.hashes2landmarks(q_hashes)
     mlms = audfprint_analyze.hashes2landmarks(matchhashes)
     # Overplot on the spectrogram
     plt.plot(np.array([[x[0], x[0]+x[3]] for x in lms]).T,
              np.array([[x[1], x[2]] for x in lms]).T,
              '.-g')
     plt.plot(np.array([[x[0], x[0]+x[3]] for x in mlms]).T,
              np.array([[x[1], x[2]] for x in mlms]).T,
              '.-r')
     # Add title
     plt.title(filename + " : Matched as " + ht.names[results[0][0]]
               + (" with %d of %d hashes" % (len(matchhashes),
                                             len(q_hashes))))
     # Display
     plt.show()
     # Return
     return results
Beispiel #4
0
    def wavfile2peaks(self, filename, shifts=None):
        """ Read a soundfile and return its landmark peaks as a
            list of (time, bin) pairs.  If specified, resample to sr first.
            shifts > 1 causes hashes to be extracted from multiple shifts of
            waveform, to reduce frame effects.  """
        ext = os.path.splitext(filename)[1]
        if ext == PRECOMPPKEXT:
            # short-circuit - precomputed fingerprint file
            peaks = peaks_load(filename)
            dur = np.max(peaks, axis=0)[0] * self.n_hop / float(self.target_sr)
        else:
            try:
                #[d, sr] = librosa.load(filename, sr=self.target_sr)
                d, sr = audio_read.audio_read(filename,
                                              sr=self.target_sr,
                                              channels=1)
            except:  # audioread.NoBackendError:
                message = "wavfile2peaks: Error reading " + filename
                if self.fail_on_error:
                    raise IOError(message)
                print(message, "skipping")
                d = []
                sr = self.target_sr
            # Store duration in a global because it's hard to handle
            dur = float(len(d)) / sr
            if shifts is None or shifts < 2:
                peaks = self.find_peaks(d, sr)
            else:
                # Calculate hashes with optional part-frame shifts
                peaklists = []
                for shift in range(shifts):
                    shiftsamps = int(float(shift) / self.shifts * self.n_hop)
                    peaklists.append(self.find_peaks(d[shiftsamps:], sr))
                peaks = peaklists

        # instrumentation to track total amount of sound processed
        self.soundfiledur = dur
        self.soundfiletotaldur += dur
        self.soundfilecount += 1
        return peaks
Beispiel #5
0
from audfprint_connector import Connector
from audfprint.audio_read import audio_read
from glob import glob
import time

file_path = "recordings"
files = glob(file_path + '\*.wav')

afp = Connector()

time_then = time.clock()
print(files[-10])
array, sr = audio_read(files[-10], sr=8000, channels=1)

# Ingest
hashes = afp.fingerprint_array(array)
# hashes1 = hashes[:10]
# hashes2 = hashes[5:25]
# afp.db.store("tmp", hashes1)

#Match
matches = afp.db.return_matches(hashes)
best_sids = afp.db.get_best_sids(matches) if len(matches) > 0 else []
result = afp.db.align_matches(matches, best_sids)
print(result)

print("Elapsed time: %f" % (time.clock() - time_then))