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 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 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 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 core_loop(self): self.__proto = ReceiveProtocol(bport=self.__bport) try: while True: if self.outPIPE.state_is_(mark.wrong): _ = self.__proto.receive(feedback=ErrorMark) if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.kill() break elif self.outPIPE.state_is_(mark.terminated): _ = self.__proto.receive(feedback=TerminatedMark) if not self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.stop() break elif self.outPIPE.state_is_(mark.stranded): message = self.__proto.receive( feedback=StrandedMark + double_to_bytes(self.outPIPE.timestamp)) if message[0:1] == ErrorMark: self.outPIPE.kill() if self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.kill() break elif message[0:1] == TerminatedMark: self.outPIPE.stop() if self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.stop() break elif message[0:1] == StrandedMark: time.sleep(info.TIMESCALE) continue elif message[0:1] == ActiveMark: remoteTimeStamp = double_from_bytes(message[1:]) if self.outPIPE.timestamp < remoteTimeStamp: self.outPIPE.activate() if self.inPIPE.state_is_(mark.silent, mark.stranded): self.inPIPE.activate() else: message = self.__proto.receive( feedback=ActiveMark + double_to_bytes(self.inPIPE.timestamp)) if message[0:1] == ErrorMark: self.outPIPE.kill() if self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.kill() break elif message[0:1] == TerminatedMark: self.outPIPE.stop() if self.inPIPE.state_is_(mark.wrong, mark.terminated): self.inPIPE.stop() break elif message[0:1] == StrandedMark: remoteTimeStamp = double_from_bytes(message[1:]) if self.outPIPE.timestamp < remoteTimeStamp: self.outPIPE.pause() if self.inPIPE.state_is_(mark.active): self.inPIPE.pause() continue elif message[0:1] == ActiveMark: message = self.__proto.receive() assert message[0:1] == PacketMark packet = Packet.decode(message[1:]) self.put_packet(packet) finally: self.__proto.close()