Audio recording and playback - python

I am going to implement voice chat using python. So I saw some examples of how to play sound and how to record. In many examples, they used the library pyAudio.
I can record a voice and save it in a file .wav. And I can play the file .wav. But I search for a record voice for 5 seconds and then play. I don’t want to save it to a file and then play, this is not good for voice chat.

Here is my recording code:

p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT, channels=1, rate=RATE,
        input=True, output=True,
        frames_per_buffer=CHUNK_SIZE)
num_silent = 0
snd_started = False

r = array('h')

while 1:
    # little endian, signed short
    snd_data = array('h', stream.read(CHUNK_SIZE))
    if byteorder == 'big':
        snd_data.byteswap()
    r.extend(snd_data)

    silent = is_silent(snd_data)

    if silent and snd_started:
        num_silent += 1
    elif not silent and not snd_started:
        snd_started = True

    if snd_started and num_silent > 30:
        break

Now I want to play without saving. I do not know how to do that.

+4
source share
1 answer

PyAudio Documentation, , , , stream . , ( stream.read), , ( stream.write).

, :

# Play back collected sound.
stream.write(r)

# Cleanup the stream and stop PyAudio
stream.stop_stream()
stream.close()
p.terminate()
+1

All Articles