def core_loop(self): self.__reset_position_flag() # Prepare a work buffer (It might be avaliable only in this process) self.__workBuffer = None #np.zeros([self.__width,],dtype="int16") while True: # prepare a chunk of frames if not self.__prepare_chunk_frame(): break self.__workBuffer.flags.writeable = False # Detect if necessary # activity can be a bool value or a list of bool value if self.__tailIndex > 0: activity = self.vad_function( self.__workBuffer[:self.__tailIndex] ) else: activity = True self.__workBuffer.flags.writeable = True # print(activity) # append data into pipe and do some processes if isinstance(activity,(bool,int)): ### If activity, add all frames in to new PIPE if activity: for i in range(self.__tailIndex): self.put_packet( Packet({self.oKey[0]:self.__workBuffer[i].copy()},cid=self.__id_count,idmaker=self.objid) ) self.__silenceCounter = 0 ### If not else: self.__silenceCounter += 1 if self.__silenceCounter < self.__patience: for i in range(self.__tailIndex): self.put_packet( Packet({self.oKey[0]:self.__workBuffer[i].copy()},cid=self.__id_count,idmaker=self.objid) ) elif (self.__silenceCounter == self.__patience) and self.__truncate: self.put_packet( Endpoint(cid=self.__id_count,idmaker=self.objid) ) else: pass ## if this is a list or tuple of bool value elif isinstance(activity,(list,tuple)): assert len(activity) == self.__tailIndex, f"{self.name}: If VAD detector return mutiple results, " + \ "it must has the same numbers with chunk frames." for i, act in enumerate(activity): if act: self.put_packet( Packet({self.oKey[0]:self.__workBuffer[i].copy()},cid=self.__id_count,idmaker=self.objid) ) self.__silenceCounter = 0 else: self.__silenceCounter += 1 if self.__silenceCounter < self.__patience: self.put_packet( Packet({self.oKey[0]:self.__workBuffer[i].copy()},cid=self.__id_count,idmaker=self.objid) ) elif (self.__silenceCounter == self.__patience) and self.__truncate: self.put_packet( Endpoint(cid=self.__id_count,idmaker=self.objid) ) else: raise Exception(f"{self.name}: VAD function must return a bool value or a list of bool value.") # If arrived endpoint if self.__endpointStep: self.put_packet( Endpoint(cid=self.__id_count,idmaker=self.objid) ) self.__reset_position_flag() # If over if self.__finalStep: break
def core_loop(self): ''' The core thread funtion to batch. ''' while True: # Decide action action = self.decide_action() if action is True: # get a packet pack = self.get_packet() if not pack.is_empty(): iKey = self.iKey if self.iKey is not None else pack.mainKey mat = pack[iKey] assert isinstance(mat, np.ndarray) and len(mat.shape) == 2 cSize = len(mat) // self.__nChunk assert cSize * self.__nChunk == len(mat) # Split matrix for i in range(self.__nChunk): self.put_packet( Packet(items={ self.oKey[0]: mat[i * cSize:(i + 1) * cSize] }, cid=self.__id_count, idmaker=pack.idmaker)) # add endpoint if is_endpoint(pack): self.put_packet( Endpoint(cid=self.__id_count, idmaker=pack.idmaker)) else: break
def core_loop(self): ''' The core thread funtion to cut frames. ''' self.__reset_position_flag() # Prepare a work buffer (It might be avaliable only in this process) self.__streamBuffer = None #np.zeros([self.__width,],dtype="int16") while True: # prepare a frame of stream if not self.__prepare_chunk_stream(): break ## If there are new data generated if self.__hadData: if self.__batchSize == 1: self.put_packet( Packet( items={self.oKey[0]:self.__streamBuffer[0].copy()}, cid=self.__id_count, idmaker=self.objid ) ) else: self.put_packet( Packet( items={self.oKey[0]:self.__streamBuffer.copy()}, cid=self.__id_count, idmaker=self.objid ) ) ## check whether arrived endpoint if self.__endpointStep: self.put_packet( Endpoint( cid=self.__id_count,idmaker=self.objid ) ) self.__reset_position_flag() ## check whether end if self.__finalStep: break
def core_loop(self): ''' The thread function to record stream from microphone. ''' readTimes = math.ceil(self.__totalframes / self.__points) wf = wave.open(self.__recource, "rb") try: i = 0 while i < readTimes: # Decide state master, state = self.decide_state() #print("master:",master,"state:",state,"inPIPE state:",self.inPIPE.state,"outPIPT state:",self.outPIPE.state) # If state is silent (although unlikely) if state in [mark.wrong, mark.terminated]: break elif state == mark.stranded: time.sleep(info.TIMESCALE) if self.__redirect_flag: break continue # #print( "try to read stream" ) st = time.time() # read a chunk of stream data = wf.readframes(self.__points) # detcet if necessary if self.__vad is not None: if len(data) != self.__width * self.__points: data += np.zeros( (self.__width * self.__points - len(data)) // 2, dtype="int16").tobytes() valid = self.__vad.detect(data) else: valid = True # add data if valid is True: ## append data for ele in np.frombuffer(data, dtype=self.__format): if self.outPIPE.state_is_(mark.silent, mark.active): self.put_packet( Packet(items={self.oKey[0]: ele}, cid=self.__id_count, idmaker=self.objid)) elif valid is None: self.put_packet( Endpoint(cid=self.__id_count, idmaker=self.objid)) ## if reader has been stopped by force if state == mark.terminated: break #print( "sleep" ) # wait if necessary if self.__simulate: internal = self.__timeSpan - round((time.time() - st), 4) if internal > 0: time.sleep(internal) i += 1 finally: wf.close()
def core_loop(self): ''' The thread function to record stream from microphone. ''' pa = pyaudio.PyAudio() stream = pa.open(format=self.__paFormat, channels=self.__channels, rate=self.__rate, input=True, output=False) try: while True: # master, state = self.decide_state() # if state in [mark.wrong, mark.terminated]: break elif state == mark.stranded: time.sleep(info.TIMESCALE) if self.__redirect_flag: break continue data = stream.read(self.__points) # detcet if necessary if self.__vad is not None: valid = self.__vad.detect(data) else: valid = True # add data if valid is True: ## append data for ele in np.frombuffer(data, dtype=self.__format): if self.outPIPE.state_is_(mark.silent, mark.active): self.put_packet( Packet(items={self.oKey[0]: ele}, cid=self.__id_count, idmaker=self.objid)) elif valid is None: self.put_packet( Endpoint(cid=self.__id_count, idmaker=self.objid)) ## if reader has been stopped by force if state == mark.terminated: break finally: stream.stop_stream() stream.close() pa.terminate()
def core_loop(self): while True: action = self.decide_action() if action is True: packet = self.get_packet() if not packet.is_empty(): iKey = self.iKey if self.iKey is not None else packet.mainKey data = packet[iKey] assert isinstance( data, np.ndarray ), f"{self.name}: Can only dissolve vector and matrix packet but got: {type(data)}." for element in data.reshape(-1): self.put_packet( Packet({self.oKey[0]: element}, cid=self.__id_count, idmaker=packet.idmaker)) if is_endpoint(packet): self.put_packet( Endpoint(cid=self.__id_count, idmaker=packet.idmaker)) else: break
def core_loop(self): while True: action = self.decide_action() if action is True: packet = self.get_packet() if not packet.is_empty(): items = dict(packet.items()) items = self.__map_function(items) if is_endpoint(packet): packet = Endpoint(items=items, cid=packet.cid, idmaker=packet.idmaker) else: packet = Packet(items=items, cid=packet.cid, idmaker=packet.idmaker) self.put_packet(packet) elif is_endpoint(packet): self.put_packet(packet) else: break
def __read_result_from_subprocess(self): ''' This function is used to open a thread to read result from main decoding process. ''' timecost = 0 try: while True: # decide state and action master, state = self.decide_state() if state == mark.wrong: break elif state == mark.stranded: time.sleep(info.TIMESCALE) continue elif state == mark.terminated: if master == mark.outPIPE: break # if state is active or terminated (master is inPIPE) # do the following steps # Read line = self.__decodeProcess.stdout.readline().decode().strip() # nothing is received if line == "": time.sleep(info.TIMESCALE) timecost += info.TIMESCALE if timecost > info.TIMEOUT: raise Exception( f"{self.name}: Timeout! Receiving thread has not received any data for a long time!" ) else: if line.startswith("-1"): packet = self.__packetCache.get() line = line[2:].strip().split( ) # discard the flag "-1" if len(line) > 0: packet.add(self.oKey[0], self.ids_to_words(line), asMainKey=True) else: packet.add(self.oKey[0], " ", asMainKey=True) self.put_packet(packet) ## Endpoint elif line.startswith("-2"): packet = self.__packetCache.get() endpoint = Endpoint.from_packet(packet) line = line[2:].strip() if len(line) == 0: endpoint.add(self.oKey[0], " ", asMainKey=True) else: lines = line[2:].strip().split( "-1") # discard the flag "-2 -1" lines = [ line.strip().split() for line in lines if len(line.strip()) > 0 ] if len(lines) == 0: endpoint.add(self.oKey[0], " ", asMainKey=True) elif len(lines) == 1: endpoint.add(self.oKey[0], self.ids_to_words(lines[0]), asMainKey=True) else: # do not need to rescore if self.rescore_function is None: for i, line in enumerate(lines): outKey = self.oKey[0] if i == 0 else ( self.oKey[0] + f"-{i+1}") endpoint.add(outKey, self.ids_to_words(line), asMainKey=True) else: nbestsInt = [[ int(ID) for ID in line.split() ] for line in lines] nResults = self.rescore_function(nbestsInt) assert isinstance( nbestsInt, (list, tuple)) and len(nbestsInt) > 0 for i, re in enumerate(nResults): assert isinstance( re, (list, tuple)) and len(nbestsInt) > 0 outKey = self.oKey[0] if i == 0 else ( self.oKey[0] + f"-{i+1}") endpoint.add(outKey, self.ids_to_words(re), asMainKey=True) self.put_packet(endpoint) ## Final step elif line.startswith("-3"): break else: raise Exception( f"{self.name}: Expected flag (-1 -> partial) (-2 endpoint) (-3 termination) but got: {line}" ) except Exception as e: if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.kill() if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.kill() raise e else: if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.terminate() if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.terminate() finally: self.__decodeProcess.stdout.close() self.__decodeProcess.kill()