A
A
Antongard2019-05-28 18:14:25
Python
Antongard, 2019-05-28 18:14:25

How to play stereo sound without saving to file (Python)?

Hello.
There is a program that creates stereo sound and saves it to a file

import wave
import numpy as np

time = 25
di = 44100
wavef = wave.open('audio.wav','w')
wavef.setnchannels(2)
wavef.setsampwidth(2) 
wavef.setframerate(44100)
t = np.linspace(0, 25, di*25, endpoint = False)
l = 10000*np.sin(2*np.pi*t*500)
r = 10000*np.sin(2*np.pi*t*2000)
for i in range(di*time):
    wavef.writeframesraw( struct.pack('<hh', int(l[i]), int(r[i]) ) )

But it is necessary that the audio recording (stereo) is immediately output to the headphones, and not saved to a file.
How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2019-05-28
@Zarom

It seems to me that you can use this option to not use the file

memfile = io.BytesIO()
wavef = wave.open(memfile, 'wb')

Well, use pyaudio to play it https://stackoverflow.com/questions/6951046/pyaudi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question