我正在尝试创建一个带有phonegap的应用程序,以识别诸如shazam之类的歌曲。我使用的是很棒的python脚本dejavu,但是当我尝试识别使用智能手机录制的歌曲样本时遇到了问题。当从PC识别录制的歌曲样本时,Dejavu效果很好,但是对于智能手机,我无法弄清楚如何使用“ micRecognition”。我用智能手机尝试了带有录制歌曲的“ fileRecognition”:我将样本保存在文件中,然后将其发送到dejavu进行工作的服务器上,但是显然,由于质量/背景噪音,它可以识别不同的歌曲。
因此,我尝试使用两个插件: -PhoneGap媒体流插件,但是我不知道在录制音频时如何使用它; -cordova-plugin-audioinput,但在这种情况下,我无法理解此插件是否可以实现我想要的功能。
这是dejavu MicrophoneRecognizer,我正在使用python 2.7在centos 6.9上运行
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()