1

I'm trying to create an app with phonegap that could recognize songs like shazam. I'm using dejavu, a great python script, but I'm facing problems when I try to recognise songs' samples recorded with my smartphone. Dejavu works well when recognises recorded songs' samples from pc but with smartphone I can't figure out how to use the "micRecognition". I tried the"fileRecognition" with a recorded songs with my smartphone: I saved the sample in a file and send it to the server where dejavu do the work but, obviously, it recognises a different songs because of the quality/background noise.

So I'm trying to use two plugins: - the PhoneGap Media Stream Plugin but I have no idea on how use it when recording the audio; - the cordova-plugin-audioinput but in this case I can't understand if this plugin do what I want.

This is dejavu MicrophoneRecognizer and I'm running it on a centos 6.9 with python 2.7

class MicrophoneRecognizer(BaseRecognizer):
default_chunksize   = 8192
default_format      = pyaudio.paInt16
default_channels    = 2
default_samplerate  = 44100
def __init__(self, dejavu):
    super(MicrophoneRecognizer, self).__init__(dejavu)
    self.audio = pyaudio.PyAudio()
    self.stream = None
    self.data = []
    self.channels = MicrophoneRecognizer.default_channels
    self.chunksize = MicrophoneRecognizer.default_chunksize
    self.samplerate = MicrophoneRecognizer.default_samplerate
    self.recorded = False

def start_recording(self, channels=default_channels,
                    samplerate=default_samplerate,
                    chunksize=default_chunksize):
    self.chunksize = chunksize
    self.channels = channels
    self.recorded = False
    self.samplerate = samplerate

    if self.stream:
        self.stream.stop_stream()
        self.stream.close()

    self.stream = self.audio.open(
        format=self.default_format,
        channels=channels,
        rate=samplerate,
        input=True,
        frames_per_buffer=chunksize,
    )

    self.data = [[] for i in range(channels)]

def process_recording(self):
    data = self.stream.read(self.chunksize)
    nums = np.fromstring(data, np.int16)
    for c in range(self.channels):
        self.data[c].extend(nums[c::self.channels])

def stop_recording(self):
    self.stream.stop_stream()
    self.stream.close()
    self.stream = None
    self.recorded = True

def recognize_recording(self):
    if not self.recorded:
        raise NoRecordingError("Recording was not complete/begun")
    return self._recognize(*self.data)

def get_recorded_time(self):
    return len(self.data[0]) / self.rate

def recognize(self, seconds=10):
    self.start_recording()
    for i in range(0, int(self.samplerate / self.chunksize
                          * seconds)):
        self.process_recording()
    self.stop_recording()
    return self.recognize_recording()

0 Answers0