From 371742a3140aeda6cba66c92e0962ccef2e4568f Mon Sep 17 00:00:00 2001 From: Vin Date: Tue, 17 Dec 2013 20:55:20 +0000 Subject: [PATCH] Began moving recognizer functionality into separate classes --- dejavu/recognize.py | 66 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 18 deletions(-) mode change 100644 => 100755 dejavu/recognize.py diff --git a/dejavu/recognize.py b/dejavu/recognize.py old mode 100644 new mode 100755 index bcb5d3c..f6a5de5 --- a/dejavu/recognize.py +++ b/dejavu/recognize.py @@ -1,5 +1,7 @@ from multiprocessing import Queue, Process from dejavu.database import SQLDatabase +import dejavu.fingerprint +from dejavu import Dejavu from scipy.io import wavfile import wave import numpy as np @@ -8,6 +10,52 @@ import sys import time import array + +class BaseRecognizer(object): + + def __init__(self, dejavu): + self.dejavu = dejavu + self.Fs = dejavu.fingerprint.DEFAULT_FS + + def recognize(self, *data): + matches = [] + for d in data: + matches.extend(self.dejavu.find_matches(data, Fs=self.Fs)) + return self.dejavu.align_matches(matches) + + +class WaveFileRecognizer(BaseRecognizer): + + def __init__(self, dejavu): + super(BaseRecognizer, self).__init__(dejavu) + + def recognize_file(self, filepath): + Fs, frames = wavfile.read(filename) + self.Fs = Fs + + wave_object = wave.open(filename) + nchannels, sampwidth, framerate, num_frames, comptype, compname = wave_object.getparams() + + channels = [] + for channel in range(nchannels): + channels.append(frames[:, channel]) + + t = time.time() + match = self.recognize(*channels) + t = time.time() - t + + if match: + match['match_time'] = t + + return match + + +class MicrophoneRecognizer(BaseRecognizer): + pass + + + + class Recognizer(object): CHUNK = 8192 # 44100 is a multiple of 1225 @@ -20,24 +68,6 @@ class Recognizer(object): self.fingerprinter = fingerprinter self.config = config self.audio = pyaudio.PyAudio() - - def read(self, filename, verbose=False): - - # read file into channels - channels = [] - Fs, frames = wavfile.read(filename) - wave_object = wave.open(filename) - nchannels, sampwidth, framerate, num_frames, comptype, compname = wave_object.getparams() - for channel in range(nchannels): - channels.append(frames[:, channel]) - - # get matches - starttime = time.time() - matches = [] - for channel in channels: - matches.extend(self.fingerprinter.match(channel)) - - return self.fingerprinter.align_matches(matches, starttime, verbose=verbose) def listen(self, seconds=10, verbose=False):