コード例 #1
0
async def hello(uri):
    async with websockets.connect(uri) as websocket:
        print('Recording')
        stream = p.open(format=sample_format,
                        channels=channels,
                        rate=fs,
                        frames_per_buffer=chunk,
                        input=True,
                        input_device_index=input_device_info["index"])

        RATE = 48000
        CHANNELS = 2
        encoder = opus_encoder.Encoder(RATE, CHANNELS, 'voip')

        try:
            # Run until user press Ctrl-C
            while True:
                #frames = b''  # Initialize byte array to store frames
                #ttl_bytes = 0

                # Store data in chunks for 3 seconds
                #for i in range(0, int(fs / chunk * seconds)):
                #    data = stream.read(chunk)
                #    #stream.write(data)
                #    frames = frames + data
                #    ttl_bytes += len(data)

                data = stream.read(chunk)
                encoded_data = encoder.encode(data, chunk)

                print("Sending some frames")
                #await websocket.send(frames)
                await websocket.send(encoded_data)
        except:
            print("This is a test")
            exit()

        # Stop and close the stream
        stream.stop_stream()
        stream.close()
        # Terminate the PortAudio interface
        p.terminate()

        print('Finished recording')
コード例 #2
0
    def open_audio_input(self):
        if not self.audio_input_open:
            self.encoder = encoder.Encoder(self.encoder_sample_rate,
                                           self.number_of_input_channel,
                                           'audio')

            # PyAudio Multiprocessing Wrapper
            self.audio_packet_queue = multiprocessing.Queue()
            self.pyaudio_process = pyaudio_asynchronous.PyAudioAsync(
                audio_packet_queue=self.audio_packet_queue, audio_input=True)
            self.pyaudio_process.number_of_input_channel = self.number_of_input_channel
            self.pyaudio_process.sample_rate = self.sample_rate
            self.pyaudio_process.chunk_frame_length = self.chunk_frame_length
            self.pyaudio_process.start()

            self.worker_thread_terminated = threading.Event()
            self.worker_thread_terminated.clear()
            self.worker_thread = threading.Thread(
                target=get_audio_packet_and_send,
                args=(self.audio_packet_queue, self,
                      self.worker_thread_terminated))
            self.worker_thread.start()

            self.audio_input_open = True
コード例 #3
0
async def hello(uri):
    from opus import encoder
    async with websockets.connect(uri) as websocket:
        CHANNELS = 2
        RATE = 48000
        encoder = encoder.Encoder(RATE, CHANNELS, 'voip')
        #await websocket.send("Hello world!")
        data = wf.readframes(CHUNK)
        #await websocket.send(data)
        #text = await websocket.recv()
        #print("Should wait: ", text)
        ##time.sleep(CHUNK/44100)
        #time.sleep(int(text))
        #data = wf.readframes(CHUNK)
        #await websocket.send(data)
        #text = await websocket.recv()
        #print("Should wait: ", text)
        #time.sleep(int(text))
        #time.sleep(2)
        total = 0

        while data:
            #stream.write(data)
            encoded_data = encoder.encode(data, CHUNK)
            #await websocket.send(data)
            await websocket.send(encoded_data)
            data = wf.readframes(CHUNK)
            #response = await websocket.recv()
            #time.sleep(int(response))
            #if len(data) < CHUNK:
            #    print(data)
            total = total + len(data)
        # wait a bit more for the buffered data to finish
        time.sleep(2)
        print("Read complete")
        print(total)
コード例 #4
0
output_device_info = p.get_default_output_device_info()
bit_width = 16
byte_width = int(bit_width / 8)
number_of_output_channel = 2
sample_rate = 44100
stream = p.open(format=pyaudio.paInt16,
                channels=number_of_output_channel,
                rate=sample_rate,
                output=True,
                output_device_index=output_device_info["index"])

# convert wav to opus
opus_frames = []
CHANNELS = 2
RATE = 48000
enc = encoder.Encoder(RATE, CHANNELS, 'voip')
dec = decoder.Decoder(RATE, CHANNELS)
for data in frames:
    opus_frames.append(enc.encode(data, CHUNK))
print("DATA LENGTH :", len(b''.join(frames)))
print("ENCDATA LENGTH :", len(b''.join(opus_frames)))

frame_size_time = CHUNK / RATE
audio_packet_queue = deque()
period_sync_event = threading.Event()
pyaudio_asynchronous.set_stream_object(stream)
pyaudio_asynchronous.start(audio_packet_queue, period_sync_event)
opus_dec_data = b''
total_drift_time = 0
for opus_data in opus_frames:
    if total_drift_time > frame_size_time: