def fill(self, data): """ After successfully connecting to the server with start(), pump PCM samples through this method. data is 16-bit, 8 KHz/16 KHz little-endian PCM samples. Returns True if the server detected the end of audio and is processing the data or False if the server is still accepting audio """ if self.audioFinished: # buffer gets flushed on next call to start() return True self.buffer += data # 20ms 16-bit audio frame = (2 * 0.02 * sampleRate) bytes frame_size = int(2 * 0.02 * self.sampleRate) while len(self.buffer) > frame_size: frame = self.buffer[:frame_size] if self.useSpeex: frame = pySHSpeex.EncodeFrame(frame) self._send(frame) self.buffer = self.buffer[frame_size:] return False
def fill(self, data): """ After successfully connecting to the server with start(), pump PCM samples through this method. data is 16-bit, 8 KHz/16 KHz little-endian PCM samples. Returns True if the server detected the end of audio and is processing the data or False if the server is still accepting audio """ if self.audioFinished: return True if self.useSpeex: self.buffer += data while len(self.buffer) >= self.speexFrameSize: frame = self.buffer[:self.speexFrameSize] self.buffer = self.buffer[self.speexFrameSize:] self._send(pySHSpeex.EncodeFrame(frame)) else: self._send(data) return self.audioFinished
def finish(self): """ Once fill returns True, call finish() to finalize the transaction. finish will wait for all the data to be received from the server. After finish() is called, you can start another request with start() but each start() call should have a corresponding finish() to wait for the threads """ try: try: if len(self.buffer) > 0: padding_size = self.speexFrameSize - len(self.buffer) frame = self.buffer + b'\x00' * padding_size self._send(pySHSpeex.EncodeFrame(frame)) self._send("") except socket.timeout as e: raise HoundifyTimeoutError(e) from e except Exception as e: raise HoundifySDKError() from e self.callbackTID.join(self.timeout) if self.callbackTID.isAlive(): # join timed out but thread is still running self.killCallbackThread = True raise HoundifyTimeoutError() elif self.lastException is not None: if isinstance(self.lastException, socket.timeout): raise HoundifyTimeoutError( self.lastException) from self.lastException else: raise HoundifySDKError() from self.lastException else: return self.lastResult finally: self._close()
def fill(self, data): """ After successfully connecting to the server with start(), pump PCM samples through this method. data is 16-bit, 16 KHz little-endian PCM samples. Returns True if the server detected the end of audio and is processing the data or False if the server is still accepting audio """ if self.audioFinished: # buffer gets flushed on next call to start() return True self.buffer += data while len(self.buffer) > 640: speexFrame = pySHSpeex.EncodeFrame(self.buffer[:640]) self.conn.SendMessage( HTPMessage(HTPMessage.HTP_TYPE_BINARY, speexFrame)) self.buffer = self.buffer[640:] return False
def finish(self): """ Once fill returns True, call finish() to finalize the transaction. finish will wait for all the data to be received from the server. After finish() is called, you can start another request with start() but each start() call should have a corresponding finish() to wait for the threads """ if len(self.buffer) > 0: frame = self.buffer if self.useSpeex: padding_size = int(2 * 0.02 * self.sampleRate) - len(self.buffer) frame = frame + b'\x00' * padding_size frame = pySHSpeex.EncodeFrame(frame) self._send(frame) self._send("") self.callbackTID.join() return self.lastResult