Beispiel #1
0
    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.thread = Thread(target=self._handle_predictions)
        self.thread.daemon = True
        self.thread.start()
def my_record():
    pa = PyAudio()  #创建一个录音对象
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=framerate,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)  #这里是input
    my_buf = []
    count = 0
    while count < TIME * 8:  #控制录音时间(16000/2000=8)
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        count += 1
        print('.')
    save_wave_file('01.wav', my_buf)  #调用音频保存音频的函数
    stream.close()
Beispiel #3
0
def start_record():
    global recordflag, stoped, my_buf
    pa = PyAudio()
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=framerate,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    my_buf = []
    recordflag = True
    stoped = False
    while recordflag:
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        print('.')
    stoped = True
Beispiel #4
0
def play():
    wf = wave.open(r"010.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 == b"":
            break
        # print(data)
        # print(type(data))
        stream.write(data)
    stream.close()
    p.terminate()
Beispiel #5
0
def my_record():
    pa = PyAudio()
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=framerate,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    my_buf = []
    count = 0  #
    while count < TIME * 8:  # 循环2*20次
        string_audio_data = stream.read(NUM_SAMPLES)  # 每读完2000个采样加1
        my_buf.append(string_audio_data)
        count += 1
        print('当前正在录音(同时录制系统内部和麦克风的声音)……')
    save_wave_file('03.wav', my_buf)  # 文件保存
    stream.close()
Beispiel #6
0
 def my_record(self, filename):
     pa = PyAudio()
     # stream = pa.open(format = paInt16, channels=1, rate=framerate, input=True, frames_pre_buffer=NUM_SAMPLES)
     stream = pa.open(format=paInt16,
                      channels=1,
                      rate=self.framerate,
                      input=True)
     my_buf = []
     count = 0
     print('.')
     while count < self.TIME * 10:
         string_audio_data = stream.read(self.NUM_SAMPLES)
         my_buf.append(string_audio_data)
         count += 1
     self.save_wav_file(filename, my_buf)
     stream.close()
Beispiel #7
0
def play_1channel_8bit(data, restframes, rate):
    pyaudio_object = PyAudio()
    stream = pyaudio_object.open(
        format=pyaudio_object.get_format_from_width(1),  # 8bit
        channels=1,  # mono
        rate=rate,
        output=True)
    for buf in data:  # 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()
    pyaudio_object.terminate()
Beispiel #8
0
 def recording(self):
     p = PyAudio()
     stream = p.open(format=self.FORMAT,
                     channels=self.CHANNELS,
                     rate=self.RATE,
                     input=True)
     frames = []
     for i in range(0, int(self.RATE / self.CHUNK)):
         data = stream.read(self.CHUNK)
         data_converted = struct.unpack(str(4 * self.CHUNK) + 'B', data)
         data_np = np.array(data_converted, dtype='b')[::2] + 128
         frames.append(data_np)
     stream.stop_stream()
     stream.close()
     p.terminate()
     return frames
 def run(self):
     audio = PyAudio()
     wavfile = wave.open(self.audiofile, 'ab')
     wavfile.setnchannels(self.channels)
     wavfile.setsampwidth(audio.get_sample_size(self.format))
     wavfile.setframerate(self.rate)
     wavstream = audio.open(format=self.format,
                            channels=self.channels,
                            rate=self.rate,
                            input=True,
                            frames_per_buffer=self.chunk)
     while self.bRecord:
         wavfile.writeframes(wavstream.read(self.chunk))
     wavstream.stop_stream()
     wavstream.close()
     audio.terminate()
Beispiel #10
0
 def play(self, name):
     wf = wave.open(name, 'rb')
     params = wf.getparams()
     nchannels, sampwidth, framerate, nframes = params[:4]
     p = PyAudio()
     stream = p.open(format=p.get_format_from_width(sampwidth),
                     channels=nchannels,
                     rate=framerate,
                     output=True)
     while True:
         data = wf.readframes(self.chunk)
         if data == b"":
             break
         stream.write(data)
     stream.close()
     p.terminate()
Beispiel #11
0
def my_record():
    pa = PyAudio()
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=framerate,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    my_buf = []
    count = 0
    while count < TIME * 20:  #控制录音时间
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        count += 1
        print('.')
    save_wave_file('01.wav', my_buf)
    stream.close()
Beispiel #12
0
 def noise_replay(self):
     """
     实时播放音波
     误差距离~=5e-5*343=0.017m
     :return:
     """
     pa = PyAudio()
     out_stream = pa.open(format=paInt16, channels=1,
                          rate=self.framerate, output=True, output_device_index=5)
     while True:
         start = time.clock()
         fromstring = np.fromstring(self.q.get(), dtype=np.int16)
         negative = np.negative(fromstring)
         negative_data = negative.tostring()
         out_stream.write(negative_data)
         print(time.clock() - start)
Beispiel #13
0
    def record_wave(self):
        while self.start_flag == 1:
            pa = PyAudio()
            stream = pa.open(format=paInt16,
                             channels=1,
                             rate=self.RATE,
                             input=True,
                             frames_per_buffer=self.NUM_SAMPLES)
            save_buffer = []
            count = 0
            print('开始录音')
            while count < self.TIME * 20:
                string_audio_data = stream.read(self.NUM_SAMPLES)
                audio_data = np.fromstring(string_audio_data, dtype=np.short)
                large_sample_count = np.sum(audio_data > self.LEVEL)
                print(large_sample_count)
                if large_sample_count < self.mute_count_limit:  # 静音
                    self.mute_begin = 1
                else:
                    save_buffer.append(string_audio_data)
                    self.mute_begin = 0
                    self.mute_end = 1
                count += 1
                if (self.mute_end - self.mute_begin) > 9:  # 留白时间过长
                    self.mute_begin = 0
                    self.mute_end = 1
                    break
                if self.mute_begin:
                    self.mute_end += 1
                print('...')

                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
                    print(filename + "已经保存")
                else:
                    print("文件未保存")
                # self.save_wave_file(filename, my_buf)
                save_buffer = []
                stream.close()
Beispiel #14
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
            # 读入NUM_SAMPLES个取样
            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)
            # 如果个数大于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中
                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 = []
                    print "Recode a piece of  voice successfully!"
                    rec.savewav('initial.wav')
                    # return True
            if time_count == 0:
                if len(save_buffer) > 0:
                    self.Voice_String = save_buffer
                    save_buffer = []
                    print "Recode a piece of  voice successfully!"
                    rec.savewav('initial.wav')
Beispiel #15
0
    def record_wave(self, temp):
        while self.start_flag == 1:
            pa = PyAudio()
            stream = pa.open(format=paInt16,
                             channels=1,
                             rate=framerate,
                             input=True,
                             frames_per_buffer=self.NUM_SAMPLES)
            my_buf = []
            count = 0
            print "* start recoding *"
            while count < self.TIME * 20:
                string_audio_data = stream.read(self.NUM_SAMPLES)
                audio_data = np.fromstring(string_audio_data, dtype=np.short)
                large_sample_count = np.sum(audio_data > self.LEVEL)
                print large_sample_count
                if large_sample_count < self.mute_count_limit:
                    self.mute_begin = 1
                else:
                    my_buf.append(string_audio_data)
                    self.mute_begin = 0
                    self.mute_end = 1
                count += 1
                if (self.mute_end - self.mute_begin) > 9:
                    self.mute_begin = 0
                    self.mute_end = 1
                    break
                if self.mute_begin:
                    self.mute_end += 1
                print '.'

            my_buf = my_buf[:]
            if my_buf:
                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=my_buf)
                self.writeQ(queue=self.wav_queue, data=filename)
                self.file_name_index += 1
                print filename, "saved"
            else:
                print '* Error: file not saved! *'
            #self.save_wave_file(filename, my_buf)
            my_buf = []
            stream.close()
Beispiel #16
0
    def recognise_forever(self):
        """
        Start recognising from the default recording device until disconnect() is
        called.

        The pause_recognition or resume_recognition methods can also be called to
        pause and resume without disconnecting and reconnecting.
        """
        # Open a PyAudio stream with the specified keyword args and get the number
        # of frames to read per buffer
        p = PyAudio()
        keyword_args = self.config.PYAUDIO_STREAM_KEYWORD_ARGS
        stream = p.open(**keyword_args)
        frames_per_buffer = keyword_args["frames_per_buffer"]

        # Start recognising in a loop
        stream.start_stream()
        self._recognising = True
        self._cancel_recognition_next_time = False
        while self.recognising:
            # Cancel current recognition if it has been requested
            if self._cancel_recognition_next_time:
                self._decoder.end_utterance()
                self._cancel_recognition_next_time = False

            # Don't read and process audio if recognition has been paused
            if self._recognition_paused:
                time.sleep(0.2)
                continue

            # Read from the audio device (default number of frames is 2048)
            buf = stream.read(frames_per_buffer)

            # Keep a list of AudioData objects for reprocessing with Pocket Sphinx
            # LM search later if the utterance matches no rules.
            self._audio_buffers.append(buf)

            # Process audio and wait a few milliseconds.
            self._decoder.process_audio(buf)

            # This improves the performance; we don't need to process as much audio
            # as the device can read, and people don't speak that fast anyway!
            time.sleep(0.1)

        stream.close()
        self._recognition_paused = False
        self._free_engine_resources()
Beispiel #17
0
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)
        # init voice
        self.NUM_SAMPLES = 1000
        par = {"image": "black.png"}
        self.voicebar = Sprite(**par)
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)
        self.label = cocos.text.RichLabel('3 LIVES',
                                          font_name='Times New Roman',
                                          font_size=32,
                                          anchor_x='center',
                                          anchor_y='center',
                                          color=(255, 0, 0, 255))

        self.label.position = 330, 400

        self.add(self.label)

        self.ppx = PPX()
        self.add(self.ppx)

        # layer order according the layer added order
        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        self.maxx = 0
        for i in range(30):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height
            if i == 29:
                self.maxx = b.x

        # open sound input
        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.schedule(self.update)
Beispiel #18
0
def read_wave_file(fileName):
    f = wave.open(fileName, "rb")
    params = f.getparams()
    #print params[:4]
    pa = PyAudio()
    stream = pa.open(format=pa.get_format_from_width(f.getsampwidth()),
                     channels=f.getnchannels(),
                     rate=f.getframerate(),
                     output=True)
    full_data = ""
    data = f.readframes(configs.NUM_SAMPLES)

    while data:
        full_data += data
        data = f.readframes(data)

    return full_data
Beispiel #19
0
def my_record():
    pa = PyAudio()
    # 打开一个新的音频stream
    stream = pa.open(format=paInt16, channels=channels,
                     rate=framerate, input=True, frames_per_buffer=num_samples)
    my_buf = []  # 存放录音数据

    t = time.time()
    print('正在录音...')

    while time.time() < t + 4:  # 设置录音时间(秒)
        # 循环read,每次read 2000frames
        string_audio_data = stream.read(num_samples)
        my_buf.append(string_audio_data)
    print('录音结束.')
    save_wave_file(FILEPATH, my_buf)
    stream.close()
Beispiel #20
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)
Beispiel #21
0
def my_record():
    pa = PyAudio()
    stream = pa.open(format = paInt16,channels = 1,
                     rate = framerate,input = True,
                     frames_per_buffer = NUM_SAMPLES)
    my_buf = []
    count = 0
    print('\n开始录音')
    time.sleep(2)
#    time.sleep(10)
    while count < TIME*1:
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        count += 1
        print('...')
    save_wave_file(filename, my_buf)
    stream.close()
Beispiel #22
0
 def __init__(self, filename):
     self.open = True
     self.rate = 44100
     self.frames_per_buffer = 1024
     self.channels = 2
     self.startTime = 0
     self.endTime = 0
     self.duration = 0
     self.format = 8
     self.audio_filename = filename+".wav"
     self.audio = PyAudio()
     self.stream = self.audio.open(format=self.format,
                                   channels=self.channels,
                                   rate=self.rate,
                                   input=True,
                                   frames_per_buffer=self.frames_per_buffer)
     self.audio_frames = []
Beispiel #23
0
    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._wrap_stream_read(self.stream)

        self.engine.start()
        self.running = True
        self.is_paused = False
        self._handle_predictions()
Beispiel #24
0
 def read_data(self):
     pa = PyAudio()
     stream = pa.open(format=paInt16,
                      channels=1,
                      rate=16000,
                      input=True,
                      frames_per_buffer=1)
     count = 0
     while True:
         if count >= 2:
             break
         print('.' + str(count))
         data = stream.read(16000)
         count += 1
         data = audioop.lin2lin(data, pa.get_sample_size(paInt16),
                                TARGET_WIDTH)
         yield data
Beispiel #25
0
 def __init__(self):
     self.buffer = []
     self.tracker = 0
     self.base = 0
     BITRATE = 16000
     p = PyAudio()
     self.stream = p.open(
         format=p.get_format_from_width(1),
         channels=1,
         rate=BITRATE,
         output=True,
     )
     self.time = []
     self.timeUnit = 0.001
     self.fig = plt.figure()
     self.ax1 = self.fig.add_subplot(111)
     plt.ion()
Beispiel #26
0
    def __init__(self, logger):
        QtCore.QObject.__init__(self)

        self.logger = logger

        self.duo_input = 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")
            self.stream = self.open_stream(device)
            self.device = device

            self.logger.push("Trying to read from input device %d" % device)
            if self.try_input_stream(self.stream):
                self.logger.push("Success")
                break
            else:
                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.buffer_timer_time = 0.
Beispiel #27
0
def my_record():

    pa = PyAudio()
    stream = pa.open(format=paInt16,
                     channels=1,
                     rate=framerate,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    my_buf = []
    print("录音开始!!!")
    while Foo.Key == False:  #控制录音结束
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        print('.')
    print("录音结束!!!按 esc 退出")
    save_wave_file('01.wav', my_buf)
    stream.close()
    def liveListen(self) -> Path:
        """ 
			Records `self.recordLen` (default: 2) seconds of live audio to be tested. Returns the path to which the 
			audio was saved.
		"""

        # The audio will be saved in `../data/tmp`, because the output is temporary,
        # and will be changing constantly.
        SAVE_PATH = Path(f"../data/tmp/llout.wav")

        # Some data needed for recording the audio
        CHUNK = 1024
        FORMAT = paInt16
        CHANNELS = 2
        RATE = 44100
        RECORD_SECONDS = self.recordLen
        OUTPUT = SAVE_PATH

        pa = PyAudio()

        with self.__openstream(pa, FORMAT, CHANNELS, RATE, True,
                               CHUNK) as stream:
            print("Recording...")

            frames = list()

            for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
                data = stream.read(CHUNK)
                frames.append(data)

            stream.stop_stream()
            stream.close()

            pa.terminate()

            print("Done!")

        with self.__openwf(str(OUTPUT), "wb") as wf:
            wf.setnchannels(CHANNELS)
            wf.setsampwidth(pa.get_sample_size(FORMAT))
            wf.setframerate(RATE)
            wf.writeframes(b"".join(frames))

        del pa

        return OUTPUT
Beispiel #29
0
    def __init__(self, port):
        super(AudioServer, self).__init__()

        self.setDaemon(True)
        self.address = ('', port)
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.recoder = PyAudio()
        self.audio_stream = None

        # parameters for recording
        self.chunk = config.VOICE_CHUNK
        self.format = config.VOICE_FORMAT
        self.channels = config.VOICE_CHANNELS
        self.rate = config.VOICE_RATE
        self.max_record_seconds = config.MAX_RECORD_SECONDS * 50

        self.is_alive = True
Beispiel #30
0
    def _write_stream_to_file(self, filename, data):
        '''
        Write contents of data to a Wave file.

        Args:
            filename  (str) : name of Wave file to be written to
            data     (list) : mono audio signal
        '''
        wave_file = wave.open(f'./assets/{filename}.wav',
                              'wb')  # Open the Wave file in binary write mode
        wave_file.setnchannels(1)  # Set details of the data being written
        wave_file.setsampwidth(PyAudio().get_sample_size(paInt16))
        wave_file.setframerate(self.rate)
        wave_file.writeframes(
            b''.join(data)
        )  # Convert the list into a binary string and (over)write to the Wave file
        wave_file.close()