работает с двумя каналами как для локальных файлов, так и для потока данных.
Локальный файл.
# audio channel balance local file
import numpy as np
import pyaudio
import wave
import time
wf = wave.open('2chan_audio.wav', 'r')
p = pyaudio.PyAudio()
# two channels
channel_balance = [0.1, 1]
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
data_as_np = np.frombuffer(data, dtype=np.int16)
data_as_np = data_as_np.reshape(frame_count, wf.getnchannels())
data_as_np = np.int16(data_as_np * channel_balance)
return data_as_np, pyaudio.paContinue
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.01)
stream.stop_stream()
stream.close()
wf.close()
p.terminate()
Поток данных.
# audio channel balance stream
WIDTH = 2
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
channel_balance = [0.1, 1]
def callback(in_data, frame_count, time_info, status):
data_as_np = np.frombuffer(in_data, dtype=np.int16)
data_as_np = data_as_np.reshape(frame_count, CHANNELS)
data_as_np = np.int16(data_as_np * channel_balance)
return data_as_np, pyaudio.paContinue
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.01)
stream.stop_stream()
stream.close()
p.terminate()
Надеюсь, что это работает!