Пример #1
33
def record():

	pa = PyAudio()
	in_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=BUFFER_SIZE)
	out_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE,output=True)
	save_count = 0
	save_buffer = []
	save_data   = []

	save_count = SAVE_LENGTH
	
	print 'start recording'

	while save_count>0:
		string_audio_data = in_stream.read(BUFFER_SIZE)
		audio_data = np.fromstring(string_audio_data, dtype=np.short)
		
		print type(audio_data)
		save_buffer.append( string_audio_data )
		save_data.append( audio_data )

		save_count = save_count - 1

#print 'save %s' % (wav.fileName)
#save_wave_file(wav.fileName, save_buffer)
		save_wave_file("test.wav", save_buffer)
		pa.terminate()
Пример #2
25
Файл: Wav.py Проект: ej0cl6/ciao
	def record(self):
		pa = PyAudio()
		in_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=BUFFER_SIZE)
		save_count = 0
		save_buffer = []

		save_count = SAVE_LENGTH
		
		print 'start recording'
		while save_count>0:
			string_audio_data = in_stream.read(BUFFER_SIZE)
			audio_data = np.fromstring(string_audio_data, dtype=np.short)
			save_buffer.append( string_audio_data )
			save_count = save_count - 1

		print 'save %s' % (self.fileName)
		pa.terminate()
		
		wf = wave.open(self.fileName, 'wb')
		wf.setnchannels(1)
		wf.setsampwidth(2)
		wf.setframerate(SAMPLING_RATE)
		wf.writeframes("".join(save_buffer))
		wf.close()
		
		self.stringAudioData = "".join(save_buffer)
		save_data = np.fromstring(self.stringAudioData, dtype=np.short)
		self.audioData = save_data[10000:10000+4608*4]
		self.stringAudioData = self.audioData.tostring()
		self.cutAudio = self.audioData
		# self.cut2()
		self.getFeature()
Пример #3
17
 def record(self, time):
     audio = PyAudio()
     stream = audio.open(input_device_index=self.device_index,
                         output_device_index=self.device_index,
                         format=self.format,
                         channels=self.channel,
                         rate=self.rate,
                         input=True,
                         frames_per_buffer=self.chunk
                         )
     print "Recording..."
     frames = []
     for i in range(0, self.rate / self.chunk * time):
         data = stream.read(self.chunk)
         frames.append(data)
     stream.stop_stream()
     print "Recording Complete"
     stream.close()
     audio.terminate()
     write_frames = open_audio(self.file, 'wb')
     write_frames.setnchannels(self.channel)
     write_frames.setsampwidth(audio.get_sample_size(self.format))
     write_frames.setframerate(self.rate)
     write_frames.writeframes(''.join(frames))
     write_frames.close()
     self.convert()
Пример #4
6
class AudioBackend(QtCore.QObject):

    underflow = QtCore.pyqtSignal()
    new_data_available_from_callback = QtCore.pyqtSignal(
        bytes, int, float, int)
    new_data_available = QtCore.pyqtSignal(ndarray, float, int)

    def callback(self, in_data, frame_count, time_info, status):
        # do the minimum from here to prevent overflows, just pass the data to the main thread

        input_time = time_info['input_buffer_adc_time']

        # some API drivers in PortAudio do not return a valid time, so fallback to the current stream time
        if input_time == 0.:
            input_time = time_info['current_time']
        if input_time == 0.:
            input_time = self.stream.get_time()

        self.new_data_available_from_callback.emit(in_data, frame_count,
                                                   input_time, status)

        return (None, 0)

    def __init__(self, logger):
        QtCore.QObject.__init__(self)

        self.logger = logger

        self.duo_input = False
        self.terminated = False

        self.logger.push("Initializing PyAudio")
        self.pa = PyAudio()

        # look for devices
        self.input_devices = self.get_input_devices()
        self.output_devices = self.get_output_devices()

        self.device = None
        self.first_channel = None
        self.second_channel = None

        # we will try to open all the input devices until one
        # works, starting by the default input device
        for device in self.input_devices:
            self.logger.push("Opening the stream")
            try:
                self.stream = self.open_stream(device)
                self.stream.start_stream()
                self.device = device
                self.logger.push("Success")
                break
            except:
                self.logger.push("Fail")

        if self.device is not None:
            self.first_channel = 0
            nchannels = self.get_current_device_nchannels()
            if nchannels == 1:
                self.second_channel = 0
            else:
                self.second_channel = 1

        # counter for the number of input buffer overflows
        self.xruns = 0

        self.chunk_number = 0

        self.new_data_available_from_callback.connect(self.handle_new_data)

    def close(self):
        if self.stream is not None:
            self.stream.stop_stream()
            self.stream.close()
            self.stream = None

        if not self.terminated:
            # call terminate on PortAudio
            self.logger.push("Terminating PortAudio")
            self.pa.terminate()
            self.logger.push("PortAudio terminated")

            # avoid calling PortAudio methods in the callback/slots
            self.terminated = True

    # method
    def get_readable_devices_list(self):
        devices_list = []

        default_device_index = self.get_default_input_device()

        for device in self.input_devices:
            dev_info = self.pa.get_device_info_by_index(device)
            api = self.pa.get_host_api_info_by_index(
                dev_info['hostApi'])['name']

            if device is default_device_index:
                extra_info = ' (system default)'
            else:
                extra_info = ''

            nchannels = self.pa.get_device_info_by_index(
                device)['maxInputChannels']

            desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels,
                                                 api, extra_info)

            devices_list += [desc]

        return devices_list

    # method
    def get_readable_output_devices_list(self):
        devices_list = []

        default_device_index = self.get_default_output_device()

        for device in self.output_devices:
            dev_info = self.pa.get_device_info_by_index(device)
            api = self.pa.get_host_api_info_by_index(
                dev_info['hostApi'])['name']

            if device is default_device_index:
                extra_info = ' (system default)'
            else:
                extra_info = ''

            nchannels = self.pa.get_device_info_by_index(
                device)['maxOutputChannels']

            desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels,
                                                 api, extra_info)

            devices_list += [desc]

        return devices_list

    # method
    def get_default_input_device(self):
        try:
            index = self.pa.get_default_input_device_info()['index']
        except IOError:
            index = None

        return index

    # method
    def get_default_output_device(self):
        try:
            index = self.pa.get_default_output_device_info()['index']
        except IOError:
            index = None
        return index

    # method
    def get_device_count(self):
        return self.pa.get_device_count()

    # method
    # returns a list of input devices index, starting with the system default
    def get_input_devices(self):
        device_count = self.get_device_count()
        device_range = list(range(0, device_count))

        default_input_device = self.get_default_input_device()

        if default_input_device is not None:
            # start by the default input device
            device_range.remove(default_input_device)
            device_range = [default_input_device] + device_range

        # select only the input devices by looking at the number of input channels
        input_devices = []
        for device in device_range:
            n_input_channels = self.pa.get_device_info_by_index(
                device)['maxInputChannels']
            if n_input_channels > 0:
                input_devices += [device]

        return input_devices

    # method
    # returns a list of output devices index, starting with the system default
    def get_output_devices(self):
        device_count = self.get_device_count()
        device_range = list(range(0, device_count))

        default_output_device = self.get_default_output_device()

        if default_output_device is not None:
            # start by the default input device
            device_range.remove(default_output_device)
            device_range = [default_output_device] + device_range

        # select only the output devices by looking at the number of output channels
        output_devices = []
        for device in device_range:
            n_output_channels = self.pa.get_device_info_by_index(
                device)['maxOutputChannels']
            if n_output_channels > 0:
                output_devices += [device]

        return output_devices

    # method.
    # The index parameter is the index in the self.input_devices list of devices !
    # The return parameter is also an index in the same list.
    def select_input_device(self, index):
        device = self.input_devices[index]

        # save current stream in case we need to restore it
        previous_stream = self.stream
        previous_device = self.device

        self.logger.push("Trying to open input device #%d" % (index))

        try:
            self.stream = self.open_stream(device)
            self.device = device
            self.stream.start_stream()
            success = True
        except:
            self.logger.push("Fail")
            success = False
            if self.stream is not None:
                self.stream.close()
            # restore previous stream
            self.stream = previous_stream
            self.device = previous_device

        if success:
            self.logger.push("Success")
            previous_stream.close()

            self.first_channel = 0
            nchannels = self.get_current_device_nchannels()
            if nchannels == 1:
                self.second_channel = 0
            else:
                self.second_channel = 1

        return success, self.input_devices.index(self.device)

    # method
    def select_first_channel(self, index):
        self.first_channel = index
        success = True
        return success, self.first_channel

    # method
    def select_second_channel(self, index):
        self.second_channel = index
        success = True
        return success, self.second_channel

    # method
    def open_stream(self, device):
        # by default we open the device stream with all the channels
        # (interleaved in the data buffer)
        max_input_channels = self.pa.get_device_info_by_index(
            device)['maxInputChannels']
        stream = self.pa.open(format=paInt16,
                              channels=max_input_channels,
                              rate=SAMPLING_RATE,
                              input=True,
                              input_device_index=device,
                              stream_callback=self.callback,
                              frames_per_buffer=FRAMES_PER_BUFFER)

        lat_ms = 1000 * stream.get_input_latency()
        self.logger.push("Device claims %d ms latency" % (lat_ms))

        return stream

    # method
    def open_output_stream(self, device, callback):
        # by default we open the device stream with all the channels
        # (interleaved in the data buffer)
        max_output_channels = self.pa.get_device_info_by_index(
            device)['maxOutputChannels']
        stream = self.pa.open(format=paInt16,
                              channels=max_output_channels,
                              rate=SAMPLING_RATE,
                              output=True,
                              frames_per_buffer=FRAMES_PER_BUFFER,
                              output_device_index=device,
                              stream_callback=callback)
        return stream

    def is_output_format_supported(self, device, output_format):
        max_output_channels = self.pa.get_device_info_by_index(
            device)['maxOutputChannels']
        success = self.pa.is_format_supported(
            SAMPLING_RATE,
            output_device=device,
            output_channels=max_output_channels,
            output_format=output_format)
        return success

    # method
    # return the index of the current input device in the input devices list
    # (not the same as the PortAudio index, since the latter is the index
    # in the list of *all* devices, not only input ones)
    def get_readable_current_device(self):
        return self.input_devices.index(self.device)

    # method
    def get_readable_current_channels(self):
        dev_info = self.pa.get_device_info_by_index(self.device)
        nchannels = dev_info['maxInputChannels']

        if nchannels == 2:
            channels = ['L', 'R']
        else:
            channels = []
            for channel in range(0, dev_info['maxInputChannels']):
                channels += ["%d" % channel]

        return channels

    # method
    def get_current_first_channel(self):
        return self.first_channel

    # method
    def get_current_second_channel(self):
        return self.second_channel

    # method
    def get_current_device_nchannels(self):
        return self.pa.get_device_info_by_index(
            self.device)['maxInputChannels']

    def get_device_outputchannels_count(self, device):
        return self.pa.get_device_info_by_index(device)['maxOutputChannels']

    def handle_new_data(self, in_data, frame_count, input_time, status):
        if self.terminated:
            return

        if status & paInputOverflow:
            print("Stream overflow!")
            self.xruns += 1
            self.underflow.emit()

        intdata_all_channels = fromstring(in_data, int16)

        int16info = iinfo(int16)
        norm_coeff = max(abs(int16info.min), int16info.max)
        floatdata_all_channels = intdata_all_channels.astype(float64) / float(
            norm_coeff)

        channel = self.get_current_first_channel()
        nchannels = self.get_current_device_nchannels()
        if self.duo_input:
            channel_2 = self.get_current_second_channel()

        if len(floatdata_all_channels) != frame_count * nchannels:
            print(
                "Incoming data is not consistent with current channel settings."
            )
            return

        floatdata1 = floatdata_all_channels[channel::nchannels]

        if self.duo_input:
            floatdata2 = floatdata_all_channels[channel_2::nchannels]
            floatdata = vstack((floatdata1, floatdata2))
        else:
            floatdata = floatdata1
            floatdata.shape = (1, floatdata.size)

        self.new_data_available.emit(floatdata, input_time, status)

        self.chunk_number += 1

    def set_single_input(self):
        self.duo_input = False

    def set_duo_input(self):
        self.duo_input = True

    # returns the stream time in seconds
    def get_stream_time(self):
        try:
            return self.stream.get_time()
        except OSError:
            return 0

    def pause(self):
        self.stream.stop_stream()

    def restart(self):
        self.stream.start_stream()
Пример #5
0
def play():
	
	wavName = 'test.wav'
	print "play %s" % (wavName)
	wf = wave.open(wavName, 'rb')

	pa = PyAudio()

	stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
					channels=wf.getnchannels(),
					rate=wf.getframerate(),
					output=True)

	data = wf.readframes(CHUNK)
	td = threading.Thread(target=startGame)
	td.start()
	while data != '':
		stream.write(data)
		data = wf.readframes(CHUNK)
		
		audio_data = np.fromstring(data, dtype=np.short)
		print data

	stream.stop_stream()
	stream.close()

	pa.terminate()
Пример #6
0
def record():


	pa = PyAudio()
	in_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=NUM_SAMPLES)

	out_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE,output=True)

	save_count = 0
	save_buffer = []
	save_data   = []

	save_count = SAVE_LENGTH


	while save_count>0:
		string_audio_data = in_stream.read(NUM_SAMPLES)
		audio_data = np.fromstring(string_audio_data, dtype=np.short)

		save_buffer.append( string_audio_data )
		save_data.append( audio_data )

		save_count = save_count - 1

	print 'save to test.wav'
	save_wave_file("test.wav",save_buffer)
Пример #7
0
def sine_tone(frequency, duration, volume=1, sample_rate=22050):
  n_samples = int(sample_rate * duration)
  restframes = n_samples % sample_rate

  p = PyAudio()
  stream = p.open(format=p.get_format_from_width(2), # 16 bit
                  channels=2,
                  rate=sample_rate,
                  output=True)

  for i in xrange(0, 10):
    if i % 2 == 0:
      frequency = ZERO_FREQUENCY
    else:
      frequency = ONE_FREQUENCY

    s = lambda t: volume * math.sin(2 * math.pi * frequency * t / sample_rate)
    samples = (int(s(t) * 0x7f + 0x80) for t in xrange(n_samples))
    for buf in izip(*[samples]*sample_rate): # write several samples at a time
      stream.write(bytes(bytearray(buf)))

  # fill remainder of frameset with silence
  stream.write(b'\x80' * restframes)

  stream.stop_stream()
  stream.close()
  p.terminate()
Пример #8
0
	def __init__(self):
		self.THRESHOLD = 200
		self.CHUNK_SIZE = 1024
		self.RATE = 22100

		p = PyAudio()
		self.stream = p.open(format=paInt16, channels=1, rate=self.RATE, input=True, output=True, frames_per_buffer=self.CHUNK_SIZE)
Пример #9
0
class pybeeptone:
    def __init__(self, rate=44100):
        self.rate = 44100
        self.pyaudio = PyAudio()
        self.stream = self.pyaudio.open(
                                format = self.pyaudio.get_format_from_width(1),
                                channels = 1, rate = self.rate, output = True)

    def play_tone(self, freq=1000, duration=0.3):
        rate = self.rate
        length = int(math.ceil(self.rate*duration))
        data = ''.join( [chr(int(math.sin(x/((rate/freq)/math.pi))*127+128)) 
                            for x in xrange(length)] )
        self.stream.write(data)

    def play_rest(self, duration):
        rate = self.rate
        length = int(math.ceil(self.rate*duration))
        data = ''.join( [chr(int(128)) for x in xrange(length)] )
        self.stream.write(data)

    def close(self):
        self.stream.stop_stream()
        self.stream.close()
        self.pyaudio.terminate()
Пример #10
0
def rec_audio(stat,filename,queue):
	NUM_SAMPLES = 200
	SAMPLING_RATE = 8000
	pa = PyAudio()
	stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=NUM_SAMPLES)
	save_count = 0 
	save_buffer = [] 

	while True:
		signal=queue.get()
		if(signal=="audio_start"):
			break

	time_start=clock()

	while True:
		string_audio_data = stream.read(NUM_SAMPLES)
		save_buffer.append( string_audio_data )
		if(stat.value==1):
			break
	
	time_finish=clock()
	wf = wave.open("./temp_frame/"+filename+".wav", 'wb') 
	wf.setnchannels(1) 
	wf.setsampwidth(2) 
	wf.setframerate(SAMPLING_RATE) 
	wf.writeframes("".join(save_buffer)) 
	wf.close() 
	save_buffer = [] 

	print("audio_start: "+str(time_start))
	print("audio_end: "+str(time_finish))
	print("audio_duration (sec): "+str(time_finish-time_start))   #duration (second)
	print ("audio_file: ", filename, "saved" )
	queue.put("wav_sav_ok")
Пример #11
0
def sine_tone(frequencies, amplitudes, duration, volume=1.0, sample_rate=22050):
    n_samples = int(sample_rate * duration)
    restframes = n_samples % sample_rate

    p = PyAudio()
    stream = p.open(format=p.get_format_from_width(1), # 8bit
                    channels=1, # mono
                    rate=sample_rate,
                    output=True)

    def s(t):
        r = 0
        for i in range(0, len(frequencies)):
            r += volume * amplitudes[i] * math.sin(2 * math.pi * frequencies[i] * t / sample_rate)
        return r

    samples = (int(s(t) * 0x7f + 0x80) for t in range(n_samples))
    for buf in zip(*[samples]*sample_rate): # write several samples at a time
        stream.write(bytes(bytearray(buf)))

    # fill remainder of frameset with silence
    stream.write(b'\x80' * restframes)

    stream.stop_stream()
    stream.close()
    p.terminate()
Пример #12
0
	def __init__(self):
		super(VCGame, self).__init__(255, 255, 255, 255, 800, 600)
		# 初始化参数
		# frames_per_buffer
		self.numSamples = 1000
		# 声控条
		self.vbar = Sprite('black.png')
		self.vbar.position = 20, 450
		self.vbar.scale_y = 0.1
		self.vbar.image_anchor = 0, 0
		self.add(self.vbar)
		# 皮卡丘类
		self.pikachu = Pikachu()
		self.add(self.pikachu)
		# cocosnode精灵类
		self.floor = cocos.cocosnode.CocosNode()
		self.add(self.floor)
		position = 0, 100
		for i in range(120):
			b = Block(position)
			self.floor.add(b)
			position = b.x + b.width, b.height
		# 声音输入
		audio = PyAudio()
		SampleRate = int(audio.get_device_info_by_index(0)['defaultSampleRate'])
		self.stream = audio.open(format=paInt16, 
								 channels=1, 
								 rate=SampleRate, 
								 input=True, 
								 frames_per_buffer=self.numSamples)
		self.schedule(self.update)
Пример #13
0
def main():
    # read in some block data from pyaudio
    RATE=44100
    INPUT_BLOCK_TIME=0.2
    INPUT_FRAMES_PER_BLOCK=int(RATE*INPUT_BLOCK_TIME)
    pa=PyAudio()
         
    data=True
    fmt="%dh"%INPUT_FRAMES_PER_BLOCK
    total_rms=0
    total_blocks=0
    while data:
        for dr,subdr,fnames in os.walk(path):
            for filename in fnames:
                try:
                    print filename
                    wf=wave.open("%s/%s"%(path,filename),'rb')
                    strm=pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        input=True)
                    strm.stop_stream()
                    strm.close()
                    d=wf.readframes(INPUT_FRAMES_PER_BLOCK)
                    d=struct.unpack(fmt,d)
                    wf.close()
                    total_rms+=calc_rms(d)
                    total_blocks+=1
                except:
                    #print e
                    print "*** ERROR ***"
        data=False
    avg=total_rms/total_blocks
    print "The average is %f"%avg
Пример #14
0
    def record(self):
        #open the input of wave
        pa = PyAudio()
        stream = pa.open(format = paInt16, channels = 1,
            rate = self.getRate(pa), input = True,
            frames_per_buffer = self.NUM_SAMPLES)
        save_buffer = []

        record_start = False
        record_end = False

        no_record_times = 0


        while 1:
            #read NUM_SAMPLES sampling data
            string_audio_data = stream.read(self.NUM_SAMPLES)
            if record_start == True :save_buffer.append(string_audio_data)
            print max(array('h', string_audio_data))
            if max(array('h', string_audio_data)) >5000:
                record_start = True
                no_record_times = 0
            else:
                no_record_times += 1
            if record_start == False:continue

            if no_record_times >10:
                break
        stream.close()
        pa.terminate()
        return save_buffer
Пример #15
0
class Stream(Thread):
    def __init__(self, f, on_terminated):
        self.__active = True
        self.__path = f
        self.__paused = True
        self.on_terminated = on_terminated
        self.__position = 0
        self.__chunks = []
        self.__pyaudio = PyAudio()
        Thread.__init__(self)
        self.start()

    def play(self):
        self.__paused = False

    def seek(self, seconds):
        self.__position = int(seconds * 10)

    def is_playing(self):
        return self.__active and not self.__paused

    def get_position(self):
        return int(self.__position / 10)

    def get_duration(self):
        return int(len(self.__chunks) / 10)

    def pause(self):
        self.__paused = True

    def kill(self):
        self.__active = False

    def __get_stream(self):
        self.__segment = AudioSegment.from_file(self.__path)
        self.__chunks = make_chunks(self.__segment, 100)
        return self.__pyaudio.open(format=self.__pyaudio.get_format_from_width(self.__segment.sample_width),
                                   channels=self.__segment.channels,
                                   rate=self.__segment.frame_rate,
                                   output=True)

    def run(self):
        stream = self.__get_stream()
        while self.__position < len(self.__chunks):
            if not self.__active:
                break
            if not self.__paused:
                # noinspection PyProtectedMember
                data = self.__chunks[self.__position]._data
                self.__position += 1
            else:
                free = stream.get_write_available()
                data = chr(0) * free
            stream.write(data)

        stream.stop_stream()
        self.__pyaudio.terminate()
        if self.__active:
            self.on_terminated()
Пример #16
0
 def worker():
     p = PyAudio()
     stream = p.open(format=p.get_format_from_width(2),
                     channels=1, rate=44100, output=True)
     while True:
         self.lock.acquire()
         stream.write(self.wavdata.tostring())
         self.lock.release()
Пример #17
0
def Audio_record_play(seconds,play,filename):
    '''
    This function include record and play, if you want to play and record,
    please set the play is True.
    The sample rate is 44100
    Bit:16
    '''
    CHUNK = 1024
    CHANNELS = 2
    SAMPLING_RATE = 44100
    FORMAT = paInt16
    NUM = int(SAMPLING_RATE/CHUNK * seconds)

    save_buffer = []

    if play is True:
        source_file = autohandle_directory + '/audio_lib/'+'source1.wav'
        swf = wave.open(source_file, 'rb')
    
    #open audio stream
    pa = PyAudio()
    default_input = pa.get_default_host_api_info().get('defaultInputDevice')
    stream = pa.open(
                    format   = FORMAT, 
                    channels = CHANNELS, 
                    rate     = SAMPLING_RATE, 
                    input    = True,
                    output   = play,
                    frames_per_buffer  = CHUNK,
                    input_device_index = default_input
                    )

    logging.info(">> START TO RECORD AUDIO")
    while NUM:
        save_buffer.append(stream.read(CHUNK))
        NUM -= 1
        if play is True:
            data = swf.readframes(CHUNK)
            stream.write(data)
            if data == " ": break

    #close stream
    stream.stop_stream()
    stream.close()
    pa.terminate()

    # save wav file
    def save_wave_file(filename,data):
        wf_save = wave.open(filename, 'wb')
        wf_save.setnchannels(CHANNELS)
        wf_save.setsampwidth(pa.get_sample_size(FORMAT))
        wf_save.setframerate(SAMPLING_RATE)
        wf_save.writeframes("".join(data))
        wf_save.close()

    save_wave_file(filename, save_buffer)

    del save_buffer[:]
Пример #18
0
class RadioServer:
	def __init__(self):
		self.pa=PyAudio()
		self.input_names={}
		self.output_names={}
		self.listeners=[]

	def registerDevices(self,inputDevice=None,outputDevice=None):
		if inputDevice==None:
			self.inputDevice=InputDevice(self.pa)
		else:
			self.inputDevice=inputDevice

		if outputDevice==None:
			self.outputDevice=OutputDevice(self.pa)
		else:
			self.outputDevice=outputDevice



	def registerInput(self,descriptor,name):
		self.input_names[name]=descriptor

	def registerOutput(self,descriptor,name):
		self.output_names[name]=descriptor

	def subscribeToInput(self,name,queue):
		self.inputDevice.subscribe(queue,self.input_names[name])

	def subscribeToOutput(self,name,queue):
		self.outputDevice.subscribe(queue,self.output_names[name])

	def addListener(self,listener):
		self.listeners.append(listener)
		listener.bind(self)

	def start(self):
		for l in self.listeners:
			l.start()
		self.inputDevice.start()
		self.outputDevice.start()

	def stop(self):
		self.inputDevice.stop()
		self.outputDevice.stop()
		for l in self.listeners:
			l.stop()
		self.pa.terminate()

	def sigint(self,signal,frame):
		self.stop()

	def run_forever(self):
		self.start()
		signal.signal(signal.SIGINT,lambda s,f:self.sigint(s,f))
		signal.pause()
Пример #19
0
 def playWaveData(self, waveData):
     p = PyAudio()
     stream = p.open(format = p.get_format_from_width(1),
                     channels = 1,
                     rate = self.bitRate,
                     output = True)
     stream.write(waveData)
     stream.stop_stream()
     stream.close()
     p.terminate()
Пример #20
0
    def record_wave(self):
        #open the input of wave 
        pa = PyAudio() 
        stream = pa.open(format = paInt16, channels = 1, 
        rate = self.framerate, input = True, 
        frames_per_buffer = self.NUM_SAMPLES) 
        save_buffer = [] 
        count = 0 
        while count < self.TIME*5:
            string_audio_data = stream.read(self.NUM_SAMPLES)
            audio_data = np.fromstring(string_audio_data, dtype=np.short)
            # 得到audio_data中大约level的数据个数
            large_sample_count = np.sum( audio_data > self.LEVEL )
            #print large_sample_count
            #print 'mute_begin' + str(self.mute_begin)
            #print 'mute_end' + str(self.mute_end)
            #未开始计时,出现静音
            # 如果一帧数据数据的有效数据小于mute_count_limit,则认为是静音
            if large_sample_count < self.mute_count_limit :
                # 初始化静音计数
                self.mute_begin=1
            else:
                # 如果有声音出现
                save_buffer.append(string_audio_data)  
                # 静音标记为否
                self.mute_begin=0
                # 静音时长为0
                self.mute_end=1
            count += 1
            # 如果静音时长大于5
            if (self.mute_end - self.mute_begin) > 3:
                # 还原变量
                self.mute_begin=0
                # 还原变量
                self.mute_end=1
                # 结束本次从声卡取值,本次录音结束
                break
            # 如果是静音,那么自增静音时长mute_end
            if self.mute_begin:
                self.mute_end+=1  
        
        save_buffer=save_buffer[:]
        if save_buffer:
            if self.file_name_index < 11:
                pass
            else:
                self.file_name_index = 1
            filename = str(self.file_name_index)+'.wav' 
            self.save_wave_file(filename=filename, data=save_buffer)
            self.writeQ(queue=self.wav_queue, data=filename)
            self.file_name_index+=1

        save_buffer = []
        #在嵌入式设备上必须加这一句,否则只能录音一次,下次录音时提示stram overflow 错误。
        stream.close()
Пример #21
0
Файл: Wav.py Проект: ej0cl6/ciao
	def play(self):
		print "play %s" % (self.fileName)
		pa = PyAudio()
		stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, output=True, frames_per_buffer=BUFFER_SIZE)
		
		stream.write(self.stringAudioData)
		# stream.write(self.cutAudio)
		
		stream.stop_stream()
		stream.close()
		pa.terminate()
Пример #22
0
    def Record(self):
        global CHANNELS

        # 开启声音输入
        pa = PyAudio()
        stream = pa.open(format=paInt16, channels=CHANNELS, rate=self.sampling_rate, input=True, 
                        frames_per_buffer=self.cacheblock_size)

        save_count = 0          # 已经保存的样本块
        silence_count = 0       # 持续无声音的样本块
        save_buffer = []        # 音频缓冲

        try:
            print "start recording"
            while True:
                # 录音、取样
                string_audio_data = stream.read(self.cacheblock_size)
                # 将读入的数据转换为数组
                audio_data = np.fromstring(string_audio_data, dtype=np.short)
                # 样本值大于LEVEL的取样为成功取样,计算成功取样的样本的个数
                large_sample_count = np.sum(audio_data > self.level)
                print "Peak:",np.max(audio_data),"    Sum:",large_sample_count
                # 如果成功取样数大于SAMPLING_NUM,则当前数据块取样都成功
                if large_sample_count > self.sampling_num:
                    # 有成功取样的数据块时,样本计数+1
                    save_count += 1
                else:
                    # 有成功录取的块后,若取样失败,此时可能处于静音状态,静音计数+1
                    if(save_count > 0):
                        silence_count += 1

                # 取样失败次数是否超过最大值
                if (save_count <= self.max_save_length) and (silence_count <= self.max_silence_length):
                    # 将要保存的数据存放到save_buffer中
                    save_buffer.append(string_audio_data)
                else:
                    # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻
                    if len(save_buffer) > 0:
                        self.filename = datetime.now().strftime("%Y-%m-%d_%H_%M_%S") + ".wav"
                        self.__Save_wave_file(self.filename, save_buffer)
                        save_buffer = []
                        print self.filename, "saved"
                    break
        except KeyboardInterrupt:
            print "manual exit"
        finally:
            # stop stream
            stream.stop_stream()  
            stream.close()
            # close PyAudio  
            pa.terminate() 
            print "exit recording"

        return self.filename
Пример #23
0
 def recode(self):
  pa = PyAudio() 
  stream = pa.open(format=paInt16, channels=self.nchannel, rate=self.SAMPLING_RATE, input=True, frames_per_buffer=self.NUM_SAMPLES) 
  save_count = 0 
  save_buffer = [] 
  time_out = self.TIME_OUT
  NO_WORDS=self.NO_WORDS
  
  while True and NO_WORDS:
   time_out -= 1
   print 'time_out in', time_out # 读入NUM_SAMPLES个取样
   string_audio_data = stream.read(self.NUM_SAMPLES) # 将读入的数据转换为数组
   audio_data = np.fromstring(string_audio_data, dtype=np.short) 

   # 查看是否没有语音输入
   NO_WORDS -= 1
   if np.max(audio_data) > self.UPPER_LEVEL:
    NO_WORDS=self.NO_WORDS
   print 'self.NO_WORDS ', NO_WORDS
   print 'np.max(audio_data) ', np.max(audio_data)

   # 计算大于LOWER_LEVEL的取样的个数
   large_sample_count = np.sum( audio_data > self.LOWER_LEVEL )
   
   # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
   if large_sample_count > self.COUNT_NUM:
    save_count = self.SAVE_LENGTH 
   else: 
    save_count -= 1
   #print 'save_count',save_count
   
   # 将要保存的数据存放到save_buffer中
   if save_count < 0:
    save_count = 0 
   elif save_count > 0 : 
    save_buffer.append( string_audio_data ) 
   else:
    pass
    
   # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻
   if len(save_buffer) > 0 and NO_WORDS==0: 
    self.Voice_String = save_buffer
    save_buffer = [] 
    rospy.loginfo( "Recode a piece of voice successfully!")
    #return self.Voice_String
    
   elif len(save_buffer) > 0 and time_out==0: 
    self.Voice_String = save_buffer
    save_buffer = [] 
    rospy.loginfo( "Recode a piece of voice successfully!")
    #return self.Voice_String
   else: 
    pass
Пример #24
0
def main():
	# 开启声音输入
	pa = PyAudio() 
	stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, 
		frames_per_buffer=NUM_SAMPLES);

	time = np.arange(0, NUM_SAMPLES) * (1.0/SAMPLING_RATE);
	delta = 0
	
	while True: 
		print "delta = ", delta
		delta += NUM_SAMPLES * (1.0/SAMPLING_RATE)
		# 读入NUM_SAMPLES个取样
		string_audio_data = stream.read(NUM_SAMPLES) 
		# 将读入的数据转换为数组
		audio = np.fromstring(string_audio_data, dtype=np.short) 
		
		curr = []
		for i in range(1, len(time)):
			dy = audio[i] - audio[i-1]
			if (abs(dy) > THRESHOLD):
		#		print abs(dy)
				curr.append(delta + time[i])

		blk_sum = 0
		blk_num = 0
		for i in range(1, len(curr)):
			if (curr[i] - curr[i-1] < MAXDELTA):
				blk_sum += curr[i]
				blk_num += 1
			else:
				if (blk_num > 60):
					print "blk::sum, num = ", blk_sum/blk_num, blk_num
					push(blk_sum/blk_num)
				blk_sum = 0
				blk_num = 0
		if (blk_num > 60):
			print "blk::sum, num = ", blk_sum/blk_num, blk_num
			push(blk_sum/blk_num)
			
		cnt = 0
		for i in range(1, len(event)):
			if (event[i] - event[i-1] < 0.4):
				cnt += 1
			else:
				cnt = 0

			if (cnt >= 2):
				thread.start_new_thread(play, ())
				print "-- shuia"
				del event[0:i+1]
				break 
		print event
Пример #25
0
 def record_wav(self,save_file_name):
     pa = PyAudio()
     stream = pa.open(format = paInt16
                      ,channels = 1
                      ,rate = self.framerate
                      ,input = True
                      ,frames_per_buffer = self.NUM_SAMPLES)
     buf = []
     while self.isRecording:
         audio_data = stream.read(self.NUM_SAMPLES)
         buf.append(audio_data)
     self.save_wav_file(save_file_name, buf)
     stream.close()
Пример #26
0
 def __init__(self, file="audio"):
     self.format = paInt16
     audio = PyAudio()
     if hasattr(settings, 'AUDIO_DEVICE_INDEX'):
         self.device_index = settings.AUDIO_DEVICE_INDEX
     elif hasattr(settings, 'AUDIO_DEVICE'):
         for i in range(audio.get_device_count()):
             curr_device = audio.get_device_info_by_index(i)
             print 'Found device: %s' % curr_device['name']
             if curr_device['name'] == settings.AUDIO_DEVICE:
                 print 'Assigning %s (Index: %s)' % (
                     settings.AUDIO_DEVICE, i
                 )
                 self.device_index = i
     elif not hasattr(self, 'device_index'):
         print 'No Audio device specified. Discovering...'
         for i in range(audio.get_device_count()):
             curr_device = audio.get_device_info_by_index(i)
             print 'Found device: %s' % curr_device['name']
             if curr_device['maxInputChannels'] > 0:
                 self.device_index = curr_device['index']
                 print 'Using device: %s' % curr_device['name']
                 break
     print audio.get_device_info_by_index(self.device_index)
     try:
         device = audio.get_device_info_by_index(self.device_index)
         calc_rate = device['defaultSampleRate']
         print 'Discovered Sample Rate: %s' % calc_rate
         self.rate = int(calc_rate)
     except:
         print 'Guessing sample rate of 44100'
         self.rate = 44100
     self.channel = 1
     self.chunk = 1024
     self.file = file
Пример #27
0
    def openWav(self):

        chunk = 1024
        wf = wave.open(r"result.wav", 'rb')
        p = PyAudio()

        stream = p.open(format = p.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = wf.getframerate(), output = True)
        while True:
            data = wf.readframes(chunk)
            if data == "":break
            stream.write(data)

        stream.close()
        p.terminate()
Пример #28
0
class AudioStream(object):

    def __init__(self, sample_rate=44100, channels=1, width=2, chunk=1024,
                 input_device_index=None):
        self.sample_rate = sample_rate
        self.channels = channels
        self.width = width
        self.chunk = chunk
        self.input_device_index = input_device_index

    def __enter__(self):
        self._pa = PyAudio()
        if self.input_device_index is None:
            self.input_device_index = \
                self._pa.get_default_input_device_info()['index']
        self._stream = self._pa.open(
            format=self._pa.get_format_from_width(self.width),
            channels=self.channels,
            rate=self.sample_rate,
            input=True,
            frames_per_buffer=self.chunk,
            input_device_index=self.input_device_index)
        self._stream.start_stream()
        return self

    def read(self):
        ''' On a buffer overflow this returns 0 bytes. '''
        try:
            return self._stream.read(self.chunk)
        except IOError:
            return ''
        except AttributeError:
            raise Exception('Must be used as a context manager.')

    def stream(self):
        try:
            while True:
                bytes = self.read()
                if bytes:
                    self.handle(bytes)
        except (KeyboardInterrupt, SystemExit):
            pass

    def __exit__(self, type, value, traceback):
        self._stream.stop_stream()
        self._stream.close()
        self._pa.terminate()

    def handle(self, bytes):
        pass
Пример #29
0
Файл: Wav.py Проект: ej0cl6/ciao
	def loaddb(self):
		print 'load', self.fileName
		pa = PyAudio()
		wf = wave.open(self.fileName, 'rb')
		save_buffer = []
		string_audio_data = wf.readframes(BUFFER_SIZE)
		while string_audio_data != '':
			audio_data = np.fromstring(string_audio_data, dtype=np.short)
			save_buffer.append( string_audio_data )
			string_audio_data = wf.readframes(BUFFER_SIZE)

		pa.terminate()
		self.stringAudioData = "".join(save_buffer)
		save_data = np.fromstring(self.stringAudioData, dtype=np.short)
		self.audioData = save_data
Пример #30
0
def play(wave_data):
    chunk_size = BITRATE/10

    p = PyAudio()
    stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)

    for chunk in itertools.islice(wave_data, chunk_size):
        stream.write(chunk)

    stream.stop_stream()
    stream.close()
    p.terminate()
Пример #31
0
    def throw_process_loop(self, q: Queue):
        """ A sound loop. """

        import sounddevice
        p = PyAudio()
        stream = p.open(
            format=self.formatting,
            channels=self.channels,
            rate=self.rate,
            output=True
        )
        while q.empty():
            stream.write(self.data)
        p.terminate()
        stream.close()
Пример #32
0
from pyaudio import PyAudio
import math
import time

p = PyAudio()
BITRATE = 16000 #number of frames per second/frameset.
LENGTH = 3 #seconds to play sound
THRESHOLD = -1.0

def play_tone(frequency, delay):
    SILENCEFRAMES = int(BITRATE * LENGTH * delay)
    NUMBEROFFRAMES = int(BITRATE * LENGTH)
    RESTFRAMES = (SILENCEFRAMES + NUMBEROFFRAMES) % BITRATE
    WAVEDATA = ''

    for x in xrange(SILENCEFRAMES):
        WAVEDATA = WAVEDATA+chr(128)

    for x in xrange(NUMBEROFFRAMES):
        WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/frequency)/math.pi))*127+128))

    for x in xrange(RESTFRAMES):
        WAVEDATA = WAVEDATA+chr(128)

    return WAVEDATA

def actually_play(wavedatas):
    wavedatas = [data for data in wavedatas if data]
    n_datas = len(wavedatas)
    actual_data = ''
    for data in zip(*wavedatas):
Пример #33
0
    def recoder(self):
        pa = PyAudio()
        stream = pa.open(format=paInt16,
                         channels=1,
                         rate=self.SAMPLING_RATE,
                         input=True,
                         frames_per_buffer=self.NUM_SAMPLES)
        save_count = 0
        save_buffer = []
        time_count = self.TIME_COUNT

        while True:
            time_count -= 1
            # print time_count
            # 读入NUM_SAMPLES个取样
            # print('又回来了')
            string_audio_data = stream.read(self.NUM_SAMPLES)
            # 将读入的数据转换为数组
            audio_data = np.fromstring(string_audio_data, dtype=np.short)
            # 计算大于LEVEL的取样的个数
            large_sample_count = np.sum(audio_data > self.LEVEL)
            print(np.max(audio_data))
            # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
            if large_sample_count > self.COUNT_NUM:
                save_count = self.SAVE_LENGTH
            else:
                save_count -= 1

            if save_count < 0:
                save_count = 0

            if save_count > 0:
                # 将要保存的数据存放到save_buffer中
                #print  save_count > 0 and time_count >0
                save_buffer.append(string_audio_data)
            else:
                #print save_buffer
                # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻
                #print "debug"
                if len(save_buffer) > 0:
                    self.Voice_String = save_buffer
                    save_buffer = []
                    # modified by jxc 20180912
                    client = AipSpeech(self.BAIDU_APP_ID, self.BAIDU_API_KEY,
                                       self.BAIDU_SECRET_KEY)
                    voice_buffer = np.array(self.Voice_String).tostring()
                    result = client.asr(voice_buffer, 'wav', 16000, {
                        'dev_pid': 1936,
                    })
                    '''
                    1536    普通话(支持简单的英文识别)  搜索模型    无标点 支持自定义词库
                    1537    普通话(纯中文识别)  输入法模型   有标点 不支持自定义词库
                    1737    英语      有标点 不支持自定义词库
                    1637    粤语      有标点 不支持自定义词库
                    1837    四川话     有标点 不支持自定义词库
                    1936    普通话远场   远场模型    有标点 不支持
                    '''
                    print("hear : ")
                    # print(result['result'])
                    ppt_send = ''
                    # 需要判断是否为空!
                    # ppt 区域,专门设置ppt需要的内容
                    if (result.get('result') == None):
                        print(' get none ')
                        break
                    for item in self.key_words:
                        if item in result.get('result')[0]:
                            ppt_send = item
                            '''
                            if(item == '手作'):
                                ppt_send = 'diy'
                            elif(item == '订阅'):
                                ppt_send = 'RSS'
                            '''
                            ws = create_connection("ws://127.0.0.1:9001")
                            ws.send(ppt_send)
                            ws.close()
                        '''
                        # server.send_message_to_all('北京欢迎你')
                        ws = create_connection("ws://127.0.0.1:9001")
                        ws.send("Hello, PeKing! ")
                        ws.close()
                        '''
                    # modified end by jxc
                    print("Recode a piece of  voice successfully! 1 ")
                    time_count = self.TIME_COUNT
                    return True  # modified by jxc 20180912 for loop again and again
                    # return True
            if time_count == 0:
                if len(save_buffer) > 0:
                    self.Voice_String = save_buffer
                    save_buffer = []
                    # added by jxc 20180913
                    client = AipSpeech(self.BAIDU_APP_ID, self.BAIDU_API_KEY,
                                       self.BAIDU_SECRET_KEY)
                    voice_buffer = np.array(self.Voice_String).tostring()
                    result = client.asr(voice_buffer, 'wav', 16000, {
                        'dev_pid': 1936,
                    })
                    '''
                    1536    普通话(支持简单的英文识别)  搜索模型    无标点 支持自定义词库
                    1537    普通话(纯中文识别)  输入法模型   有标点 不支持自定义词库
                    1737    英语      有标点 不支持自定义词库
                    1637    粤语      有标点 不支持自定义词库
                    1837    四川话     有标点 不支持自定义词库
                    1936    普通话远场   远场模型    有标点 不支持
                    '''
                    print("hear : ")
                    # print(result['result'])
                    ppt_send = ''
                    # 需要判断是否为空!
                    # ppt 区域,专门设置ppt需要的内容
                    if (result.get('result') == None):
                        print(' get none ')
                        break
                    for item in self.key_words:
                        if item in result.get('result')[0]:
                            ppt_send = item
                            '''
                            if(item == '手作'):
                                ppt_send = 'diy'
                            elif(item == '订阅'):
                                ppt_send = 'RSS'
                            '''
                            ws = create_connection("ws://127.0.0.1:9001")
                            ws.send(ppt_send)
                            ws.close()
                        '''
                        # server.send_message_to_all('北京欢迎你')
                        ws = create_connection("ws://127.0.0.1:9001")
                        ws.send("Hello, PeKing! ")
                        ws.close()
                        '''

                    time_count = self.TIME_COUNT
                    # add end by jxc
                    print("Recode a piece of  voice successfully! 2 ")
                    time_count = self.TIME_COUNT
                    return True
                else:
                    return True
Пример #34
0
def robot_state_abnormal():

    rospy.init_node('robot_state_abnormal', anonymous=True)
    pub = rospy.Publisher('robot_state/abnormal_detection',
                          RobotState,
                          queue_size=1)
    rate = rospy.Rate(100)  # 10hz

    seq = 0

    background_eng = 0

    Fs = 11026  # 采样频率

    pt_step = math.floor(Fs / 2)  # 0.5s的采样点数,求能量点的数据长度

    delta_time = 2.5  # 以2.5s为间隔进行平均
    avrg_time = 10  # 选取10个能量点求平均能量

    # last_time=10# 求10次录音进行监测,求完终止整个程序!!!即最大的录音次数。
    # last_time=2  # 10+5=15s录音
    last_time = 16  # 10s + 40s =50s录音

    alarm_cnt = 0  # 报警计数

    NUM_SAMPLES = int(Fs * delta_time)  # pyAudio内部缓存的块的大小,2.5s读取点数
    NUM_TEST = 4 * NUM_SAMPLES  # 一次读取4个2.5s,即10s数据的总点数

    # 初始化变量为列表, list
    save_count = 0
    save_buffer = []
    eng_step = []  # 0.5s为间隔计算能量
    avrg_eng_step = []  # 10个能量点求平均能量
    alarm_all = []  # 每个时间点报警状态记录
    bkgrd_eng = []  # 背景数据汇总数组记录每个背景点的值

    ########################################################################
    # 先读10s录音,进行初次报警监测
    ########################################################################
    # 开启声音输入 & 读取录音数据
    pa = PyAudio()
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=Fs,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    string_audio_data = stream.read(NUM_TEST)  # !!!!!! 读取10s录音 !!!!!!
    y1 = np.fromstring(string_audio_data, dtype=np.short)  # 读取录音数据:y1

    ny = len(y1)  # 10s录音数据长度
    N_step = int(math.floor(ny / pt_step))  # 10s录音中的能量点数 (20)

    # 求所有能量点 & 求平均能量
    # for循环,求10s内所有能量点
    for i_step in range(0, N_step):  # 循环从0开始,循环次数为 N_step(20)
        abs_y1_smth = np.abs(y1[i_step * pt_step:(i_step + 1) *
                                pt_step])  # 0.5s内能量取绝对值
        eng_step.append(np.sum(abs_y1_smth))  # 再求和

    # for循环,求平均能量(2.5s内平均) (5为2.5/0.5 2.5秒内的能量点数)
    for i_avrg in range(0, int(math.floor(
            N_step / 5))):  # 循环从0开始,循环次数为后面int结果(20/5 = 4)
        # avrg_eng_step.append(np.sum(eng_step[(i_avrg * 5):(i_avrg + 1) * 5]) / avrg_time)
        avrg_eng_step.append(
            np.sum(eng_step[(i_avrg * 5):(i_avrg + 1) * 5]) /
            avrg_time)  # ??为什么求平均用10

    # 对10s录音的平均能量点进行报警监测
    # if判断,根据左右两边点比较确定当前点的报警状态
    # if (avrg_eng_step[2]>(avrg_eng_step[0]*5)):# 报警点
    if avrg_eng_step[2] > (avrg_eng_step[0] * 2):  # 报警点
        alarm_sign = 1
        background_eng = avrg_eng_step[0]

    # elseif (avrg_eng_step(3)>(background_eng*2))% possible changing point
    # elif (avrg_eng_step[2]>(avrg_eng_step[0]*2)):  # 过渡点
    elif avrg_eng_step[2] > (avrg_eng_step[0] * 1.5):  # 过渡点
        alarm_sign = -1
        background_eng = avrg_eng_step[0]

    else:  # 背景点
        alarm_sign = 0
        background_eng = avrg_eng_step[1]

    # 报警状态记录,报警次数累加
    alarm_all.append(alarm_sign)
    alarm_cnt += 1

    # bkgrd_cnt=1# 记录背景点点数
    # bkgrd_cnt = bkgrd_cnt+1# 背景点数累加一次
    # background_eng = avrg_eng_step[0]# 背景能量初始化
    # bkgrd_eng[bkgrd_cnt] = background_eng# 背景数据汇总数组记录每个背景点的值
    bkgrd_eng.append(background_eng)

    while not rospy.is_shutdown():

        n_eng_step = len(eng_step)  # 已计算的能量点序列的当前长度
        n_avrg_eng_step = len(avrg_eng_step)  # 已计算的平均能量点序列的当前长度

        # 开启录音2.5s & 求新的平均能量点
        print "读取2.5s"
        string_audio_data = stream.read(
            NUM_SAMPLES)  # !!!!!! 读取 2.5s 录音 !!!!!!
        print "读取结束"
        y_next = np.fromstring(string_audio_data,
                               dtype=np.short)  # 读取录音数据:y_next
        ny = len(y_next)  # 新的录音数据长度
        N_step = int(math.floor(ny / pt_step))  # 新的录音数据包含的能量点数

        # 输出当前录音数据的能量点数和录音长度
        # print "!!!!"
        # print N_step
        # print ny
        # print "!!!!"

        # for循环,求新的2.5s录音数据的所有能量点
        # for i_step in range(1,N_step):
        for i_step in range(0, N_step):  # 循环从0开始,次数为 N_step
            abs_y1_smth = np.abs(y_next[i_step * pt_step:(i_step + 1) *
                                        pt_step])
            eng_step.append(np.sum(abs_y1_smth))  # 将新计算点加入list的尾部

        # 输出所求新的能量点
        # print "****eng_step"
        # print(eng_step)

        # 求新的平均能量点
        # avrg_eng_step.append(np.sum(eng_step[((n_avrg_eng_step-1)*5):(n_eng_step+4)])/ avrg_time)
        avrg_eng_step.append(
            np.sum(eng_step[n_avrg_eng_step * 5:(n_avrg_eng_step + 1) * 5]) /
            avrg_time)

        print "#######"
        print avrg_eng_step[-1]
        print background_eng
        print "&&&&&&&"
        # 输出新求的能量点和平均能量点
        # print "****"
        # print eng_step[(n_avrg_eng_step * 5):(n_eng_step + 4)]# 新求的能量点
        # print "****avrg_eng_step"
        # print (avrg_eng_step)# 新求的平均能量点

        # 判断新点报警状态
        # if判断,决定新的平均能量点的报警状态
        if alarm_sign == 0:  # 当前是非报警状态
            # if (avrg_eng_step[n_avrg_eng_step] > (avrg_eng_step[n_avrg_eng_step - 1] * 2)):
            # if (avrg_eng_step[n_avrg_eng_step] > (avrg_eng_step[n_avrg_eng_step-2]*5)):# 是报警点
            # if (avrg_eng_step[n_avrg_eng_step] > (background_eng*5)):# 是报警点
            if avrg_eng_step[n_avrg_eng_step] > (background_eng * 2.4):  # 是报警点
                alarm_sign = 1  # 报警

            # elif (avrg_eng_step[n_avrg_eng_step-1] > (background_eng*2)):# 是过渡点
            elif avrg_eng_step[n_avrg_eng_step] > (background_eng *
                                                   1.5):  # 是过渡点
                alarm_sign = -1  # 过渡点

            else:  # 是非报警点
                alarm_sign = 0  # 非报警

                # background_eng = avrg_eng_step[n_avrg_eng_step]
                background_eng = avrg_eng_step[n_avrg_eng_step]  # 更新背景值
                bkgrd_eng.append(background_eng)  # 增添新的背景值

        elif alarm_sign == 1:  # 当前是报警状态
            # if (avrg_eng_step[n_avrg_eng_step] > (background_eng * 2)):
            # if (avrg_eng_step[n_avrg_eng_step - 1] > (background_eng * 2)):
            # if (avrg_eng_step[n_avrg_eng_step-1] > (background_eng*5)):# 是报警点
            if avrg_eng_step[n_avrg_eng_step] > (background_eng * 2.4):  # 是报警点
                alarm_sign = 1  # 报警

            # elif (avrg_eng_step[n_avrg_eng_step-1] > (background_eng*2)):  # 是过渡点
            elif avrg_eng_step[n_avrg_eng_step] > (background_eng *
                                                   1.5):  # 是过渡点
                alarm_sign = -1  # 过渡点

            else:  # 是非报警点
                alarm_sign = 0  # 非报警

                # background_eng = avrg_eng_step[n_avrg_eng_step]
                background_eng = avrg_eng_step[n_avrg_eng_step]  # 更新背景值
                bkgrd_eng.append(background_eng)  # 增添新的背景值

        elif alarm_sign == -1:  # 当前是过渡点状态
            # if (avrg_eng_step[n_avrg_eng_step-1] > (background_eng*5)):  # 是报警点
            if avrg_eng_step[n_avrg_eng_step] > (background_eng * 2.4):  # 是报警点
                alarm_sign = 1  # 报警

            # elif (avrg_eng_step[n_avrg_eng_step-1] > (background_eng*2)):  # 是过渡点
            elif avrg_eng_step[n_avrg_eng_step] > (background_eng *
                                                   1.5):  # 是过渡点
                alarm_sign = -1  # 过渡点

            else:  # 是非报警点
                alarm_sign = 0  # 非报警

                # background_eng = avrg_eng_step[n_avrg_eng_step]
                background_eng = avrg_eng_step[n_avrg_eng_step]  # 更新背景值
                bkgrd_eng.append(background_eng)  # 增添新的背景值

                # else:
                # alarm_sign = 0
                # background_eng = avrg_eng_step[n_avrg_eng_step - 1]

        # 增添新的报警状态记录 & 报警次数累加
        alarm_all.append(alarm_sign)
        alarm_cnt += 1

        if alarm_sign == 1:
            print "AAAAAAAAAAAAA"
            robot_state.abnormal_detection = True
        else:
            robot_state.abnormal_detection = False

            # if判断,决定while循环何时终止
            # if alarm_cnt  > 20:
            # if alarm_cnt  > 10:
            # if alarm_cnt > last_time:
            #    loop_sign = 1

        robot_state.header.stamp = rospy.Time.now()
        robot_state.header.frame_id = ''
        robot_state.header.seq = seq

        pub.publish(robot_state)
        seq = seq + 1

        #print array
        rate.sleep()
Пример #35
0
class Listener:
    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.sample_rate = kwargs.get('sample_rate', 44100)
        self.buffer_size = kwargs.get('buffer_size', 1024)

        self.pa = PyAudio()
        self.pitch = pitch('default', self.buffer_size, self.buffer_size // 2,
                           self.sample_rate)
        self.pitch.set_unit('midi')
        self.pitch.set_tolerance(0.7)
        self.tempo = tempo('default', self.buffer_size, self.buffer_size // 2,
                           self.sample_rate)
        low_filter = digital_filter(3)
        low_filter.set_biquad(7.89276652e-4, 0.00157855, 7.89276652e-4,
                              -1.94146067, 0.94461777)
        mid_filter = digital_filter(3)
        mid_filter.set_biquad(0.13357629, 0.0, -0.13357629, -1.64841689,
                              0.73284743)
        high_filter = digital_filter(3)
        high_filter.set_biquad(0.72530667, -1.45061333, 0.72530667,
                               -1.32614493, 0.57508174)
        self.filters = [low_filter, mid_filter, high_filter]
        self.stream = None

    def start_stream(self):
        self.stream = self.pa.open(format=paFloat32,
                                   channels=1,
                                   rate=self.sample_rate,
                                   input=True,
                                   frames_per_buffer=self.buffer_size)

    def stop_stream(self):
        self.stream.stop_stream()
        self.stream.close()
        self.pa.terminate()
        self.stream = None

    def listen(self, data_queues, stop_event: Event):
        if self.stream is None:
            self.start_stream()
        try:

            def loop():
                # Get microphone data
                data = self.stream.read(self.buffer_size // 2)
                signal = np.fromstring(data, dtype=np.float32)
                for queue in data_queues:
                    queue.put_nowait(signal)
                # for i, signal_filter in enumerate(self.filters):
                #     f_sig = signal_filter(signal)
                #     pitch = self.pitch(f_sig)[0] / 128.0
                #     is_beat = self.tempo(f_sig)
                #     if is_beat > 0.:
                #         # print(f'[{", ".join(str(p) for p in pitches)}]')
                #         callback(pitch, i)

            while True:
                try:
                    loop()
                except KeyboardInterrupt:
                    print("Ctrl-C Terminating...")
                    break
                except Exception as e:
                    print(e)
                    print("ERROR Terminating...")
                    break
        finally:
            stop_event.set()
            # stop_callback()
            self.stop_stream()
Пример #36
0
class Noise:
    chunk = 1024

    def __init__(self):
        """Init noise"""
        self.wf = wave.open(os.path.join(os.path.dirname(__file__), "noise/noise.wav"), 'rb');
        self.p = PyAudio()
        self.fftArray = []
        self.fft()
        self.curentIndex = 0

        def callback(in_data, frame_count, time_info, status):
            data = self.wf.readframes(frame_count)
            self.curentIndex += 1
            if len(data) < 2*frame_count:
                self.wf.rewind()
                self.curentIndex = 0
                data = self.wf.readframes(frame_count)
            return (data, paContinue)


        self.stream = self.p.open(
            format = self.p.get_format_from_width(self.wf.getsampwidth()),
            channels = self.wf.getnchannels(),
            rate = self.wf.getframerate(),
            output = True,
            frames_per_buffer=self.chunk,
            start=False,
            stream_callback=callback
        )

    def wav2array(self, nchannels, sampwidth, data):
        num_samples, remainder = divmod(len(data), sampwidth * nchannels)
        if remainder > 0:
            raise ValueError('The length of data is not a multiple of '
                            'sampwidth * num_channels.')
        if sampwidth > 4:
            raise ValueError("sampwidth must not be greater than 4.")

        if sampwidth == 3:
            a = np.empty((num_samples, nchannels, 4), dtype=np.uint8)
            raw_bytes = np.fromstring(data, dtype=np.uint8)
            a[:, :, :sampwidth] = raw_bytes.reshape(-1, nchannels, sampwidth)
            a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255
            result = a.view('<i4').reshape(a.shape[:-1])
        else:
            dt_char = 'u' if sampwidth == 1 else 'i'
            a = np.fromstring(data, dtype='<%s%d' % (dt_char, sampwidth))
            result = a.reshape(-1, nchannels)
        return result

    def fft(self):
        self.wf.rewind()
        while True:
            data = self.wf.readframes(self.chunk)
            if len(data) < 2*self.chunk:
                break
            array = self.wav2array(self.wf.getnchannels(), self.wf.getsampwidth(), data)
            fs = self.wf.getframerate()
            ff = np.fft.fft(array)
            freq = np.fft.fftfreq(len(array), d=1./fs)
            ff += 0.00001 # da ne bi desio log od 0
            ffDec = 20*np.log10(np.abs(ff[0:ff.shape[0]//2]))
            ffDec -= np.max(ffDec)
            freq = freq[0:len(freq)//2]
            result = dict(zip(freq.tolist(), ffDec.tolist()));
            self.fftArray.append(result)
        self.wf.rewind()

    def close(self):
        self.stream.close()
        self.p.terminate()
Пример #37
0
 def __init__(self, args):
     super().__init__(args)
     self.orig_settings = tcgetattr(stdin)
     self.p = PyAudio()
Пример #38
0
class PreciseRunner(object):
    """
    Wrapper to use Precise. Example:
    >>> def on_act():
    ...     print('Activation!')
    ...
    >>> p = PreciseRunner(PreciseEngine('./precise-engine'), on_activation=on_act)
    >>> p.start()
    >>> from time import sleep; sleep(10)
    >>> p.stop()

    Args:
        engine (Engine): Object containing info on the binary engine
        trigger_level (int): Number of chunk activations needed to trigger on_activation
                       Higher values add latency but reduce false positives
        sensitivity (float): From 0.0 to 1.0, relates to the network output level required
                             to consider a chunk "active"
        stream (BinaryIO): Binary audio stream to read 16000 Hz 1 channel int16
                           audio from. If not given, the microphone is used
        on_prediction (Callable): callback for every new prediction
        on_activation (Callable): callback for when the wake word is heard
    """
    def __init__(self,
                 engine,
                 trigger_level=3,
                 sensitivity=0.5,
                 stream=None,
                 on_prediction=lambda x: None,
                 on_activation=lambda: None):
        self.engine = engine
        self.trigger_level = trigger_level
        self.sensitivity = sensitivity
        self.stream = stream
        self.on_prediction = on_prediction
        self.on_activation = on_activation
        self.chunk_size = engine.chunk_size

        self.pa = None
        self.thread = None
        self.running = False
        self.is_paused = False
        atexit.register(self.stop)

    def start(self):
        """Start listening from stream"""
        if self.stream is None:
            from pyaudio import PyAudio, paInt16
            self.pa = PyAudio()
            self.stream = self.pa.open(16000,
                                       1,
                                       paInt16,
                                       True,
                                       frames_per_buffer=self.chunk_size)

        self.engine.start()
        self.running = True
        self.is_paused = False
        self.thread = Thread(target=self._handle_predictions)
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        """Stop listening and close stream"""
        if self.thread:
            self.running = False
            self.thread.join()
            self.thread = None

        self.engine.stop()

        if self.pa:
            self.pa.terminate()
            self.stream.stop_stream()
            self.stream = self.pa = None

    def pause(self):
        self.is_paused = True

    def play(self):
        self.is_paused = False

    def _handle_predictions(self):
        """Continuously check Precise process output"""
        activation = 0
        while self.running:
            chunk = self.stream.read(self.chunk_size // 2)

            if self.is_paused:
                continue

            prob = self.engine.get_prediction(chunk)
            self.on_prediction(prob)

            if prob > 1 - self.sensitivity or activation < 0:
                activation += 1
                if activation > self.trigger_level:
                    activation = -self.chunk_size // 50
                    self.on_activation()
            elif activation > 0:
                activation -= 1
Пример #39
0
from pprint import pprint

from pyaudio import PyAudio

pyaud = PyAudio()

for i in range(pyaud.get_device_count()):
    dev = pyaud.get_device_info_by_index(i)
    pprint(dev)
Пример #40
0
 def __init__(self, postProcessCallback=None):
     super().__init__(postProcessCallback=postProcessCallback)
     self.pyaudio = PyAudio()
     self.stream = None
Пример #41
0
class PreciseRunner(object):
    """
    Wrapper to use Precise. Example:
    >>> def on_act():
    ...     print('Activation!')
    ...
    >>> p = PreciseRunner(PreciseEngine('./precise-engine'), on_activation=on_act)
    >>> p.start()
    >>> from time import sleep; sleep(10)
    >>> p.stop()

    Args:
        engine (Engine): Object containing info on the binary engine
        trigger_level (int): Number of chunk activations needed to trigger on_activation
                       Higher values add latency but reduce false positives
        sensitivity (float): From 0.0 to 1.0, relates to the network output level required
                             to consider a chunk "active"
        stream (BinaryIO): Binary audio stream to read 16000 Hz 1 channel int16
                           audio from. If not given, the microphone is used
        on_prediction (Callable): callback for every new prediction
        on_activation (Callable): callback for when the wake word is heard
    """
    def __init__(self,
                 engine,
                 trigger_level=3,
                 sensitivity=0.5,
                 stream=None,
                 on_prediction=lambda x: None,
                 on_activation=lambda: None):
        self.engine = engine
        self.trigger_level = trigger_level
        self.sensitivity = sensitivity
        self.stream = stream
        self.on_prediction = on_prediction
        self.on_activation = on_activation
        self.chunk_size = engine.chunk_size
        self.read_divisor = 1

        print('sensitivity: ', self.sensitivity)

        self.pa = None
        self.thread = None
        self.running = False
        self.is_paused = False
        self.detector = TriggerDetector(self.chunk_size, sensitivity,
                                        trigger_level)
        atexit.register(self.stop)

    def _calc_read_divisor(self):
        """
        pyaudio.Stream.read takes samples as n, not bytes
        so read(n) should be read(n // sample_depth
        """
        try:
            import pyaudio
            if isinstance(self.stream, pyaudio.Stream):
                return 2
        except ImportError:
            pass
        return 1

    def start(self):
        """Start listening from stream"""
        if self.stream is None:
            from pyaudio import PyAudio, paInt16
            self.pa = PyAudio()

            print("================================================")
            info = self.pa.get_host_api_info_by_index(0)
            numdevices = info.get('deviceCount')
            for i in range(0, numdevices):
                if (self.pa.get_device_info_by_host_api_device_index(
                        0, i).get('maxInputChannels')) > 0:
                    print(
                        "Input Device id ", i, " - ",
                        self.pa.get_device_info_by_host_api_device_index(
                            0, i).get('name'),
                        self.pa.get_device_info_by_index(i).get(
                            'defaultSampleRate'))
            print("================================================")

            self.stream = self.pa.open(rate=16000,
                                       channels=1,
                                       format=paInt16,
                                       input=True,
                                       frames_per_buffer=self.chunk_size,
                                       input_device_index=2)

        self.read_divisor = self._calc_read_divisor()

        self.engine.start()
        self.running = True
        self.is_paused = False
        self.thread = Thread(target=self._handle_predictions)
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        """Stop listening and close stream"""
        if self.thread:
            self.running = False
            if isinstance(self.stream, ReadWriteStream):
                self.stream.write(b'\0' * self.chunk_size)
            self.thread.join()
            self.thread = None

        self.engine.stop()

        if self.pa:
            self.pa.terminate()
            self.stream.stop_stream()
            self.stream = self.pa = None

    def pause(self):
        self.is_paused = True

    def play(self):
        self.is_paused = False

    def _handle_predictions(self):
        """Continuously check Precise process output"""
        while self.running:
            # print('chunk_size: ', self.chunk_size, ' read_divisor: ', self.read_divisor)
            chunk = self.stream.read(self.chunk_size // self.read_divisor,
                                     exception_on_overflow=False)

            if self.is_paused:
                continue

            prob = self.engine.get_prediction(chunk)
            self.on_prediction(prob)
            if self.detector.update(prob):
                self.on_activation()
Пример #42
0
#Actualizción de la leyenda que actúa como frecuencímetro
def set_legend(values):
    legend.clear()
    for item in values:
        legend.addItem(traces['spectrum'], "f = " + str(floor(item)) + " Hz")


#Configuración de canal de entrada de audio
BUFF = 1024 * 3
FORMAT = paInt16
bits = 16
bitFormat = 'h'
RATE = 44100
ts = 1 / RATE

audio = PyAudio()
stream = audio.open(format=FORMAT,
                    channels=1,
                    rate=RATE,
                    input=True,
                    output=False,
                    frames_per_buffer=BUFF)

#Inicialización de vectores de tiempo y frecuencia
t = arange(0, ts * BUFF, ts)
F = fft.fftfreq(BUFF, ts)
F = split(F, 2)[0]
Threshold = 50


#Funciones de identificación y ordenamiento de picos
Пример #43
0
        voice_fp = open(filename, 'wb+')
        voice_fp.write(voice_data)
        voice_fp.close()
        pass


#百度语音合成--------------------------------------

NUM_SAMPLES = 2000  # pyAudio内部缓存的块的大小
SAMPLING_RATE = 8000  # 取样频率
LEVEL = 1500  # 声音保存的阈值
COUNT_NUM = 20  # NUM_SAMPLES个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音
SAVE_LENGTH = 8  # 声音记录的最小长度:SAVE_LENGTH * NUM_SAMPLES 个取样
exception_on_overflow = False
# 开启声音输入pyaudio对象
pa = PyAudio()
stream = pa.open(format=paInt16,
                 channels=1,
                 rate=SAMPLING_RATE,
                 input=True,
                 frames_per_buffer=NUM_SAMPLES)
token = get_token()  #获取token
key = '35ff2856b55e4a7f9eeb86e3437e23fe'
api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info='

#百度语音合成--------------------------------------
if __name__ == "__main__":
    # 我的api_key,供大家测试用,在实际工程中请换成自己申请的应用的key和secert
    api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
    api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
    # 初始化
########################################################################################################################
# Author: Enders Kong                                                                                                  #
# Some very basic framework double buffering for "real time" audio processing, need to use this as main and pass funs  #
# for trueRealTime no need for buffers. Functions may not perform well on single threaded CPUS, try multithread        #
########################################################################################################################

from pyaudio import PyAudio
import threading

# TODO: update chunks1 val
chunks1 = 0
# opening and initializing audio streams, should be using .wav files
wavStreamOpen = PyAudio()
wavStream = wavStreamOpen.open(format="format1",
                               channels="channels1",
                               rate="rate1",
                               input="input1",
                               frames_per_buffer="chunks1")
# initializing both buffers
buff1 = [0 for n in range(chunks1)]
buff2 = [0 for n in range(chunks1)]
# write to buffer1 before loop
buff1 = wavStream.read(chunks1)


def foo():
    i = 0
    print("you're still using foo, swap it with your actual function")
    return i

Пример #45
0
gtL = [[96], [8, 4, 2, 2], [4, 4, 8], gtD[3], gtD[4], gtD[5], gtD[6], gtD[7],
       [b, c, b, c, b, b], [4, 4, 8]]
gt = [
    0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 5, 4, 4, 4, 6, 4, 4, 4, 5, 4, 4,
    4, 7, 1, 1, 1, 1, 2, 2, 2, 2, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8
]
sg["bd"] = [bd, bdN, bdD, bdL]
sg["mn"] = [mn, mnN, mnD, mnL]
sg["cr"] = [cr, crN, crD, crL]
sg["rc"] = [rc, rcN, rcD, rcL]
sg["bs"] = [bs, bsN, bsD, bsL]
sg["sy"] = [sy, syN, syD, syL]
sg["sc"] = [sc, scN, scD, scL]
sg["gt"] = [gt, gtN, gtD, gtL]
z = [[] for i in X(8)]
p = PyAudio()
st = p.open(format=p.get_format_from_width(1), channels=1, rate=R, output=True)
os.system("clear")
for i in X(R):
    sD.append(math.sin(tp * (float(i) / R)))
tP = 0
for tk in sg:
    A = z[tP].append
    for pt in sg[tk][0]:
        pP = 0
        nP = 0
        for n in sg[tk][1][pt]:
            ss = int(qL * sg[tk][3][pt][nP] * R)
            du = int(qL * sg[tk][2][pt][nP] * R)
            if n != g:
                F = 440 * pow(1.059463094, n)
Пример #46
0
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, WIDTH, HEIGHT)
        pygame.mixer.init()
        global random_seed
        self.random_seed = random_seed

        self.gameover = None

        self.score = 0  #记录分数
        self.txt_score = cocos.text.Label(u'分数:0',
                                          font_name=FONTS,
                                          font_size=24,
                                          color=BLACK)
        self.txt_score.position = 500, 440
        self.add(self.txt_score, 99999)

        self.top = '', 0
        self.top_notice = cocos.text.Label(u'',
                                           font_name=FONTS,
                                           font_size=18,
                                           color=BLACK)
        self.top_notice.position = 400, 410
        self.add(self.top_notice, 99999)

        self.name = ''

        # init voice
        self.NUM_SAMPLES = 2048  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ruc = RUC(self)
        self.add(self.ruc)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        self.last_block = 0, 100
        for i in range(5):
            b = Block(self)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=SAMPLING_RATE,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)
        self.stream.stop_stream()

        # pygame.mixer.music.load('bgm.wav')
        # pygame.mixer.music.play(-1)

        self.schedule(self.update)

        self.vol_manager = socket_vol()
        self.vol_manager.start()
Пример #47
0
class Playlist:
    def __init__(self, filepath, repeat=True, verbose=True):
        self.alive, self.lock = True, Lock()
        self.player, self.files = PyAudio(), []
        self.current, self.index = None, 0
        self.repeat, self.repeated = repeat, 0
        self.verbose = verbose
        self._skipper = deque((False, ), maxlen=1)
        self._scaler = deque((0.5, ), maxlen=1)
        self.scan(filepath)
        self.current = self.files[0]

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.stop()

    def __iter__(self):
        self.index = 0
        self.current = self.sounds[self.index]
        return self

    def __next__(self):
        try:
            self.current = self.sounds[self.index]
        except IndexError:
            if self.repeat is True:
                pass
            elif self.repeated < self.repeat:
                self.repeated += 1
            else:
                raise StopIteration()
            self.index = 0
            return next(self)
        else:
            self.index += 1
            return self.current

    def __len__(self):
        return len(self.sounds)

    def __gt__(self, other):
        return self.sounds > other.sounds

    def __ge__(self, other):
        return self.sounds >= other.sounds

    def __lt__(self, other):
        return self.sounds < other.sounds

    def __le__(self, other):
        return self.sounds <= other.sounds

    @property
    def sounds(self):
        return sorted(self.files, key=lambda f: path.basename(f))

    @property
    def repeat(self):
        return self._repeat

    @repeat.setter
    def repeat(self, val):
        if val is True:
            self._repeat = True
        elif not val:
            self._repeat = 0
        elif isinstance(val, int):
            self._repeat = val
        elif isinstance(val, float):
            self._repeat = int(val)
        else:
            raise IndexError('Must be bool, int, or float')

    @property
    def scale(self):
        with self.lock:
            return self._scaler[0]

    @scale.setter
    def scale(self, val):
        assert isinstance(val, (int, float)), 'Must be int or float'
        if val <= 0:
            scaled = 0
        elif val >= 1:
            scaled = 1
        else:
            scaled = round(val, 2)
            if scaled > 0:
                self.lastScaled = scaled
        with self.lock:
            self._scaler.append(scaled)
        if self.verbose:
            print('Scale: {}'.format(self._scaler[0]))

    @property
    def skipTrack(self):
        return self._skipper[0]

    @skipTrack.setter
    def skipTrack(self, val):
        self._skipper.append(bool(val))

    def increment_scale(self, increment=.1):
        self.scale += increment

    def scan(self, filepath, recursive=True):
        assert isinstance(filepath, str), 'Must be str'
        try:
            for f in scandir(filepath):
                if not f.is_dir():
                    self.add(f.path)
                elif recursive:
                    self.scan(f.path)
        except NotADirectoryError:
            if path.exists:
                self.add(filepath)

    def add(self, filepath):
        assert isinstance(filepath, str), 'Must be str'
        self.files.append(filepath)

    def play(self, filepath, **kwargs):
        assert self.alive
        self.skipTrack = False
        try:
            with Sound(self.player, filepath, **kwargs) as sound:
                if self.verbose:
                    print('Playing {}'.format(path.basename(filepath)))
                sound.play(self._skipper, scaler=self._scaler)
        except SkipTrack:
            return

    def skip(self):
        if self.verbose:
            print('Skipping')
        self.skipTrack = True

    def mute(self):
        print('Muting')
        self.lastScale = self.scale
        self.scale = 0

    def unmute(self):
        print('Unmuting')
        self.scale = self.lastScale

    def toggle_mute(self):
        if self.scale > 0:
            self.mute()
        else:
            self.unmute()

    def stop(self):
        self.alive = False
        self.player.terminate()

    def start(self, loop=True):
        if not self.sounds:
            raise MissingAssetsException('Must add files to play')
        for sound in self:
            try:
                self.play(self.current, loop=loop)
            except KeyboardInterrupt:
                return
from pyaudio import PyAudio, paContinue, paFloat32, paInt16
from time import sleep
import numpy as np
from ctypes import *

pa = PyAudio()
#audio = CDLL('./RealTimeAudioFilter.so')

NORM_CONST = 32768.0
D_TYPE = np.int16

def callback(in_data, frame_count, time_info, status):
#    	audio_data = np.fromstring(in_data, dtype=D_TYPE)
#	out = audio.RealTimeAudioFilter(in_data)
	return (in_data, paContinue)

stream = pa.open(format = paInt16,
	channels = 1,
        rate = 44100,
	input = True,
        output = True,
        frames_per_buffer = 1024,
        stream_callback = callback)

while stream.is_active():
	sleep(0.1)

stream.close()
pa.terminate()
Пример #49
0
 def __init__(self):
     self._recorder = PyAudio()
     self.recording = False
     self.playing = False
    #plot.add_plot("cross_sec6", yname="Macvec6", ydata=td)

    #plot.add_img_plot(mag_vec, yok, linspace(0, len(anr)-1, len(anr)))
    #zplot.add_img_plot(zname="blah", zdata=mag_vec)#z, ydata=linspace(0, len(anr)-1, len(anr)), xdata=linspace(0, len(yok)-1, len(yok)))

    #plot.add_plot("cross_sec", yname="Macvec1", ydata=c)
    #    plot.add_plot("cross_se2", yname="Macvec2", ydata=mag_vec[:, 75])
    plot.show()
for x in xrange(NUMBEROFFRAMES):
    WAVEDATA = WAVEDATA + chr(c[x])

    #fill remainder of frameset with silence
for x in xrange(RESTFRAMES):
    WAVEDATA = WAVEDATA + chr(128)

p = PyAudio()
FORMAT = p.get_format_from_width(1)

#pyaudio.paInt16 = 8 corresponds to FORMAT=p.get_format_from_width(2)

#pyaudio.paInt24 = 4  corresponds to FORMAT=p.get_format_from_width(3)

print FORMAT
if 1:
    stream = p.open(format=p.get_format_from_width(1),
                    channels=1,
                    rate=sample_rate,
                    output=True)
    stream.write(WAVEDATA)
    stream.stop_stream()
    stream.close()
Пример #51
0
def get_audio_info():
    audio = PyAudio()
    info = audio.get_host_api_info_by_index(0)
    print(info, '\n')
    for i in range(audio.get_device_count()):
        print(audio.get_device_info_by_index(i))
def Monitor():
    print ("声音检测实现语音识别和合成,Press Ctrl+C to exit ")
    pa = PyAudio()  # 实例化pyaudio
    stream = pa.open(format = paInt16,
                     channels=1,
                     rate=16000,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    print('开始缓存录音')
	
	# 初始化缓存数组
    audioBuffer = []
    rec = []
	# 初始化标志位
    audioFlag = False
    t = False
	
    while True:
        try:
            # 读取采样音频并获取特征信息
            data = stream.read(NUM_SAMPLES,exception_on_overflow = False) #add exception para
            audioBuffer.append(data)     #录音源文件
            audioData = np.fromstring(data,dtype=np.short) #字符串创建矩阵
            largeSampleCount = np.sum(audioData >1000)  # 采样变量
            temp = np.max(audioData)    # 获取声音强度
            #print (temp)
		
            if temp >3000 and t == False:  # 设置声音检测阈值,需根据麦克风适当调整
                t = 1  #  开始录音
                print ("检测到语音信号,开始录音")
                begin = time.time()
                print (temp)
			
            if t:
                end = time.time()
                if end-begin > 5:
                    timeFlag = 1 #  5s录音结束
                if largeSampleCount > 20:
                    saveCount = 4
                else:
                    saveCount -=1
                if saveCount <0:
                    saveCount =0
                if saveCount >0:
                    rec.append(data)  # 合成数据
                else:
                    if len(rec) >0 or timeFlag:
				
                        save_wave_file('detected_voice.wav',rec) # 保存检测的语音
                        voice.identify_synthesize('detected_voice.wav') # 调用百度语音实现语音识别和语音合成
                        #os.system('mplayer %s' % 'synthesis.mp3')	# play synthesis
                        # 清除缓存
                        rec = []
                        t = 0
                        timeFlag = 0

        except KeyboardInterrupt:
            break
    
    # 释放资源
    stream.stop_stream()
    stream.close()
    pa.terminate()
Пример #53
0
#Python 2.x program for Speech Recognition

import speech_recognition as sr
from pyaudio import PyAudio
import sys
#enter the name of usb microphone that you found
#using lsusb
#the following name is only used as an example
mic_name = PyAudio().get_default_input_device_info()["name"]
text = ""
#Sample rate is how often values are recorded
sample_rate = 48000
#Chunk is like a buffer. It stores 2048 samples (bytes of data)
#here.
#it is advisable to use powers of 2 such as 1024 or 2048
chunk_size = 2048
#Initialize the recognizer
r = sr.Recognizer()

#generate a list of all audio cards/microphones
mic_list = sr.Microphone.list_microphone_names()

#the following loop aims to set the device ID of the mic that
#we specifically want to use to avoid ambiguity.
for i, microphone_name in enumerate(mic_list):
    if microphone_name == mic_name:
        device_id = i

#use the microphone as source for input. Here, we also specify
#which device ID to specifically look for incase the microphone
#is not working, an error will pop up saying "device_id undefined"
Пример #54
0
class window(QDialog):
    def __init__(self):
        super().__init__()
        # 视频参数
        self.video_capture = cv2.VideoCapture(0)
        self.video_thread = True
        self.audio_thread = True
        self.width = int(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        self.height = int(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
        self.video_count = 0
        self.audio_count = 0
        self.audio_file = "media/audio.mp3"
        self.video_file = "media/input.mp4"

        # 音频参数

        self.framerate = 44100  # 采样率
        self.CHUNK = 4096  # 采样点
        self.channels = 1  # 一个声道
        self.sampwidth = 2  # 两个字节十六位
        self.TIME = 2  # 条件变量,可以设置定义录音的时间
        self.filename = datetime.now().strftime("%Y-%m-%d_%H_%M_%S") + ".wav"

        self.intUI()
        self.path = os.getcwd()
        print("当前路径:" + self.path)

    def intUI(self):

        self.btnStart = QPushButton('Start', self)
        self.btnStop = QPushButton('Stop', self)
        self.btnPlay = QPushButton('Play', self)
        self.btnStopVideo = QPushButton('sockStart', self)
        self.label = QLabel()
        self.label.resize(self.width, self.height)

        # 布局设定
        layout = QGridLayout(self)
        layout.addWidget(self.label, 0, 1, 4, 4)
        layout.addWidget(self.btnStart, 4, 1, 1, 1)
        layout.addWidget(self.btnStop, 4, 2, 1, 1)
        layout.addWidget(self.btnPlay, 4, 3, 1, 1)
        layout.addWidget(self.btnStopVideo, 4, 4, 1, 1)

        # 信号与槽进行连接,信号可绑定普通成员函数
        self.btnStart.clicked.connect(self.Start)
        self.btnStop.clicked.connect(self.Stop)
        self.btnPlay.clicked.connect(self.Play)
        self.btnStopVideo.clicked.connect(self.stopVideo)

    def openSlot(self):
        # 调用存储文件
        fileName, tmp = QFileDialog.getOpenFileName(self, 'Open Image',
                                                    'Image',
                                                    '*.png *.jpg *.bmp')
        if fileName is '':
            return
        # 采用OpenCV函数读取数据
        self.img = cv2.imread(fileName, -1)
        if self.img.size == 1:
            return
        self.refreshShow()

    def saveSlot(self):
        # 调用存储文件dialog
        fileName, tmp = QFileDialog.getSaveFileName(self, 'Save Image',
                                                    'Image',
                                                    '*.png *.jpg *.bmp')
        if fileName is '':
            return
        if self.img.size == 1:
            return
        # 调用OpenCV写入函数
        cv2.imwrite(fileName, self.img)

    def videoTask(self, *args, **kwargs):

        print("开始录像")

        success, frame = self.video_capture.read()
        while success and self.video_thread:
            self.video_count += 1
            print("vvvvvvvv:" + str(self.video_count))
            current_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            self.video_writer.write(frame)
            self.img = QImage(current_image.data, current_image.shape[1],
                              current_image.shape[0], QImage.Format_RGB888)
            self.label.setPixmap(QPixmap.fromImage(self.img))
            success, frame = self.video_capture.read()

    def startVideo(self):
        # 打开摄像头
        self.video_capture = cv2.VideoCapture(0)
        self.video_thread = True
        self.size = (self.width, self.height)
        self.fps = int(self.video_capture.get(cv2.CAP_PROP_FPS))
        self.fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', '2')
        # self.fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')

        # 创建视频写入对象
        self.video_writer = cv2.VideoWriter()
        self.video_writer.open(self.video_file, self.fourcc, self.fps,
                               self.size, True)
        self.video_count = 0
        self.tVideo = threading.Thread(target=self.videoTask)
        self.tVideo.start()
        pass

        # self.label.show()
        # sys.sleep(100)

        # if self.img.size == 1:
        #     return
        # # 对图像做模糊处理,窗口设定为5*5
        # self.img = cv2.blur(self.img, (5, 5))
        # self.refreshShow()

    def stopVideo(self):
        threading.Thread(target=scokt_start).start()
        #scokt_start()

        # self.video_thread = False
        # time.sleep(0.2)
        # self.tVideo._stop()

        # self.video_writer.release()
        # self.video_capture.release()

    # def save_wave_file(self, filename, data):
    #     wf = wave.open(filename, 'wb')  # 二进制写入模式
    #     wf.setnchannels(self.channels)
    #     wf.setsampwidth(self.sampwidth)  # 两个字节16位
    #     wf.setframerate(self.framerate)  # 帧速率
    #     # 把数据加进去,就会存到硬盘上去wf.writeframes(b"".join(data))
    #     wf.writeframes(b"".join(data))
    #     wf.close()

    def video_play(self):

        cap = cv2.VideoCapture(self.video_file)
        ret, frame = cap.read()

        while (ret):
            current_image = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            #current_image  =frame
            # get a frame
            # show a frame
            self.img = QImage(current_image.data, current_image.shape[1],
                              current_image.shape[0], QImage.Format_RGB888)
            self.label.setPixmap(QPixmap.fromImage(self.img))
            #sed_udp_data(frame)
            ret, frame = cap.read()
            if cv2.waitKey(100) & 0xFF == ord('q'):
                break

    def audioStart(self):

        print("开始录音")
        self.pa = PyAudio()
        self.audioStream = self.pa.open(format=paInt16,
                                        channels=self.channels,
                                        rate=self.framerate,
                                        input=True,
                                        frames_per_buffer=self.CHUNK,
                                        output=True)
        self.wf = wave.open(self.audio_file, 'wb')  # 二进制写入模式
        self.wf.setnchannels(self.channels)
        self.wf.setsampwidth(self.sampwidth)  # 两个字节16位
        self.wf.setframerate(self.framerate)  # 帧速率
        self.audio_thread = True
        self.audio_count = 0
        self.tAudio = threading.Thread(target=self.audioTask)
        self.tAudio.start()

    def audioTask(self):

        while self.audioStream.is_active() and self.audio_thread:
            self.audio_count += 1
            print("aaaaaaaaa:" + str(self.audio_count))
            # 采集数据
            string_audio_data = self.audioStream.read(self.CHUNK)
            # 播放数据
            self.audioStream.write(string_audio_data)
            # 写入文件
            #my_buf.append(string_audio_data)
            self.wf.writeframes(b"".join([string_audio_data]))
        # sed_udp_data(string_audio_data)

        #self.wf.writeframes(b"".join(my_buf))

    def audioStop(self):
        self.audio_thread = False
        time.sleep(0.2)
        self.audioStream.close()
        self.wf.close()
        #self.pa.terminate()
        self.tAudio._stop()
        print("录音完成")

    def audio_play(self):
        pa = PyAudio()
        wf = wave.open(self.audio_file, 'rb')
        stream = pa.open(format=paInt16,
                         channels=self.channels,
                         rate=self.framerate,
                         input=True,
                         output=True)

        while True:
            # string_audio_data = stream.read(self.NUM_SAMPLES)
            # audio_data = np.fromstring(string_audio_data, dtype=np.short)
            data = wf.readframes(self.NUM_SAMPLES)
            if data == b'':
                break
            stream.write(data)
        wf.close()
        stream.close()
        pa.terminate()

    def mediaPlay(self):
        self.video_capture = cv2.VideoCapture(0)
        #cv2.namedWindow("src")
        cuccess, frame = self.video_capture.read()
        while cuccess:
            cv2.imshow("src", frame)
            #cv2.imshow("sec",frame)

            # print(frame.shape)
            # print(type(frame))
            send_data(frame)
            cuccess, frame = self.video_capture.read()
            cv2.waitKey(1)

    def mediaMuxer(self):
        ff = ffmpegEx()
        ff.video_add_mp3(self.video_file, self.audio_file)

    def refreshShow(self):
        # 提取图像的通道和尺寸,用于将OpenCV下的image转换成Qimage
        # height, width, channel = self.img.shape
        # bytesPerline = 3 * width
        # self.qImg = QImage(self.img.data, width, height, bytesPerline, QImage.Format_RGB888).rgbSwapped()
        # 将QImage显示出来
        self.label.setPixmap(QPixmap.fromImage(self.img))

    def Start(self):
        self.startVideo()
        self.audioStart()

    def Stop(self):
        self.stopVideo()
        self.audioStop()
        self.mediaMuxer()

    def Play(self):
        #threading.Thread(target=self.video_play).start()
        #threading.Thread(target=self.audio_play).start()
        #self.video_play()
        #self.audio_play()
        threading.Thread(target=self.mediaPlay).start()
Пример #55
0
class CollectScript(BaseScript):
    RECORD_KEY = ' '
    EXIT_KEY_CODE = 27

    usage = Usage('''
        Record audio samples for use with precise

        :-w --width int 2
            Sample width of audio

        :-r --rate int 16000
            Sample rate of audio

        :-c --channels int 1
            Number of audio channels
    ''')
    usage.add_argument('file_label',
                       nargs='?',
                       help='File label (Ex. recording-##)')

    def __init__(self, args):
        super().__init__(args)
        self.orig_settings = tcgetattr(stdin)
        self.p = PyAudio()

    def key_pressed(self):
        return select([stdin], [], [], 0) == ([stdin], [], [])

    def show_input(self):
        tcsetattr(stdin, TCSADRAIN, self.orig_settings)

    def hide_input(self):
        tty.setcbreak(stdin.fileno())

    def next_name(self, name):
        name += '.wav'
        pos, num_digits = None, None
        try:
            pos = name.index('#')
            num_digits = name.count('#')
        except ValueError:
            print(
                "Name must contain at least one # to indicate where to put the number."
            )
            raise

        def get_name(i):
            nonlocal name, pos
            return name[:pos] + str(i).zfill(num_digits) + name[pos +
                                                                num_digits:]

        i = 0
        while True:
            if not isfile(get_name(i)):
                break
            i += 1

        return get_name(i)

    def wait_to_continue(self):
        while True:
            c = stdin.read(1)
            if c == self.RECORD_KEY:
                return True
            elif ord(c) == self.EXIT_KEY_CODE:
                return False

    def record_until_key(self):
        def should_return():
            return self.key_pressed() and stdin.read(1) == self.RECORD_KEY

        return record_until(self.p, should_return, self.args)

    def _run(self):
        args = self.args
        self.show_input()
        args.file_label = args.file_label or input(
            "File label (Ex. recording-##): ")
        args.file_label = args.file_label + ('' if '#' in args.file_label else
                                             '-##')
        self.hide_input()

        while True:
            print('Press space to record (esc to exit)...')

            if not self.wait_to_continue():
                break

            print('Recording...')
            d = self.record_until_key()
            name = self.next_name(args.file_label)
            save_audio(name, d, args)
            print('Saved as ' + name)

    def run(self):
        try:
            self.hide_input()
            self._run()
        finally:
            tcsetattr(stdin, TCSADRAIN, self.orig_settings)
            self.p.terminate()
Пример #56
0
from MkIOT.imp import iot_device, messaging
from MkIOT.discovery import broadcast
from MkIOT import Get_Device_IPS

import json, time

from pyaudio import PyAudio, paInt16

DEVICE_NAME = "MK_IOT_MIC"  # Change this to anything you want
DEVICE_PASSWORD = "******"  # Password for this speaker
DEVICE_IP = Get_Device_IPS()['ext'][0]  # Get This Devices IP
DEVICE_PORT = 7000  # The Port for audio data to be sent back and forth
DISCOVER_PORT = 7001  # The Port where Speaker will be expecting a connect request
HOTPHRASE = "mkiot_network_device"  # What the speaker will respond to when a connect request is sent out

AUDIO_API = PyAudio()  # Init PyAudio

# Mic config
CHUNK = 1024
FORMAT = paInt16
CHANNELS = 2
RATE = 44100

DEVICE_MIC = AUDIO_API.open(  # Setup our mic
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    frames_per_buffer=CHUNK,
    input=True)

AUDIO_CONFIG = {
Пример #57
0
from pyaudio import PyAudio, paFloat32, paContinue
import numpy as np
import time

CHANNLES = 1
RATE = 44100

pa = PyAudio()
full_data = np.array([])


def callback(in_data, frame_count, time_info, flag):
    if flag:
        print("Playback Error: %i" % flag)
    global full_data
    audio_data = np.fromstring(in_data, dtype=np.float32)
    #full_data = np.append(full_data, audio_data)
    return (audio_data, paContinue)


def main():
    stream = pa.open(format=paFloat32,
                     channels=2,
                     rate=44100,
                     input=True,
                     output=True,
                     frames_per_buffer=1024,
                     stream_callback=callback)

    stream.start_stream()
Пример #58
0
def _pyaudio() -> Generator[PyAudio, None, None]:
    p = PyAudio()
    try:
        yield p
    finally:
        p.terminate()
Пример #59
0
        hdr = ftc.getHeader()
    except IOError:
        pass

    if hdr is None:
        print 'Invalid Header... waiting'
        sleep(1)
    else:
        print hdr
        print hdr.labels

fSample = hdr.fSample

# set  up pygame and PyAudio
pygame.init()
p = PyAudio()

# set up the window
if fullscreen:
    windowSurface = pygame.display.set_mode(pygame.display.list_modes()[0],
                                            pygame.FULLSCREEN, 32)
else:
    windowSurface = pygame.display.set_mode((640, 480), 1, 32)

pygame.display.set_caption('BCI Music Experiment')

## LOADING GLOBAL VARIABLES

# Loading Music data
nrStimuli = 7
Пример #60
0
def voice_capture():
    flag = False
    num_samples = 2000  # pyaudio内置缓冲大小
    sampling_rate = 16000  # 取样频率
    level = 2000  # 声音保存的阈值
    count_num_amp1 = 10
    count_num_amp2 = 15
    silence_length = 4
    min_length = 7

    dev_to_capture = PyAudio()
    stream = dev_to_capture.open(format=paInt16,
                                 channels=1,
                                 rate=sampling_rate,
                                 input=True,
                                 frames_per_buffer=num_samples)
    save_count = 0
    save_buffer = []
    length = 0
    silence = 0

    while True:
        string_audio_data = stream.read(num_samples)
        audio_data = np.fromstring(string_audio_data, dtype=np.short)
        large_sample_count = np.sum(audio_data > level)

        if not flag:
            if large_sample_count < count_num_amp1:
                save_buffer = []
                length = 0
            elif count_num_amp1 < large_sample_count < count_num_amp2:
                save_buffer.append(string_audio_data)
                length += 1
            else:
                save_buffer.append(string_audio_data)
                length += 1
                flag = True
                silence = 0
        else:
            if large_sample_count > count_num_amp1:
                save_buffer.append(string_audio_data)
                length += 1
            else:
                silence += 1
                if silence < silence_length:
                    save_buffer.append(string_audio_data)
                    length += 1
                elif length < min_length:
                    flag = False
                    length = 0
                    save_buffer = []
                    silence = 0
                else:
                    voice_string = save_buffer
                    wf = wave.open(captured_voice, 'wb')
                    wf.setnchannels(1)
                    wf.setsampwidth(2)
                    wf.setframerate(sampling_rate)
                    wf.writeframes(np.array(voice_string).tostring())
                    wf.close()
                    stream.stop_stream()
                    stream.close()
                    dev_to_capture.terminate()
                    print("Recorded a piece of voice successfully!")
                    rospy.set_param(param_is_ready_to_capture, False)
                    rospy.set_param(param_is_ready_to_translate, True)
                    flag = False
                    length = 0
                    save_buffer = []
                    silence = 0
                    return True