Как выбрать входные каналы, такие как `` mapping`` в `` sounddevice`` с `` pyaudio``? - PullRequest
0 голосов
/ 04 февраля 2020

Как выбрать входные каналы, такие как mapping в sounddevice с помощью pyaudio?

import pyaudio
import numpy as np

CHUNK = 2**11
RATE = 44100

p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=2,rate=RATE,input=True,
              frames_per_buffer=CHUNK, input_device_index=16)

Как выбрать здесь входные каналы?

В sounddevice вы можете используйте mapping:

import sounddevice as sd
from scipy.io.wavfile import write

sd.default.device = 16
sd.default.samplerate = 44100
input_mapping = [4, 5]  # Input Channels 4 and 5

duration = 2  # seconds
recording = sd.rec(int(duration * sd.default.samplerate), mapping=input_mapping)
sd.wait()
write('test.wav', sd.default.samplerate, recording)  # Save as WAV file

или AsioSettings channel_selectors в потоке:

import queue
import sounddevice as sd
import soundfile as sf
import numpy
import os

sd.default.device = 16  # Zoom L12 ASIO
sd.default.samplerate = 44100
sd.default.channels = 2
input_channels = sd.default.channels[0]
output_channels = sd.default.channels[1]
sd.default.extra_settings = sd.AsioSettings(channel_selectors=[3, 4])  # Input Channels 4 and 5; Starts at 0 ( Zero )
subtype = 'PCM_24'
file = 'testStream.wav'

if os.path.exists(file):
    os.remove(file)

q = queue.Queue()

def callback(indata, frames, time, status):
    if status:
        print(status)
    q.put(indata.copy())

with sf.SoundFile(file, mode='x', samplerate=sd.default.samplerate, channels=input_channels, subtype=subtype) as file:
    with sd.InputStream(callback=callback):
        print('#' * 80)
        print('press Ctrl+C to stop the recording')
        print('#' * 80)
        while True:
            file.write(q.get())

Как это работает в PyAudio?

...