我怎样才能将 python pyaudio 适配到我创建的虚拟麦克风

我怎样才能将 python pyaudio 适配到我创建的虚拟麦克风

我正在使用 ubuntu 18.04,我想编写一个可以转换和转换麦克风输出的 python 脚本。这是我用来创建虚拟麦克风的命令

pactl load-module module-null-sink sink_name=virtspk sink_properties=device.description=Virtual_Speaker
pactl load-module module-null-sink sink_name=virtmic sink_properties=device.description=Virtual_Microphone_Sink

#Remap source

pactl load-module module-remap-source master=virtmic.monitor source_name=virtmic source_properties=device.description=Virtual_Microphone

这是我转换虚拟麦克风的 Python 代码

import pyaudio as pa
import numpy as np
from lib.audio_equalizer import AudioEqualizer
from lib.audio_effect import AudioEffect


class AudioStream(object):
    def __init__(self):
        self.FORMAT = pa.paFloat32
        self.CHANNEL_INPUT = 1
        self.CHANNEL_OUTPUT = 1
        self.RATE = 44100
        self.CHUNK = 1024 * 10
        self.p = None
        self.in_stream = None
        self.audio_effect = AudioEffect()
        self.audio_equalizer = AudioEqualizer()

    def configure_stream(
        self,
        FORMAT=pa.paFloat32,
        CHANNEL_INPUT=1,
        CHANNEL_OUTPUT=1,
        RATE=44100,
        CHUNK=1024 * 2,
    ):
        # Stop the stream
        self.stop(self)

        # Update the stream parameters
        self.FORMAT = FORMAT
        self.CHANNEL_INPUT = CHANNEL_INPUT
        self.CHANNEL_OUTPUT = CHANNEL_OUTPUT
        self.RATE = RATE
        self.CHUNK = CHUNK

        # Start the stream
        self.start(self)

    def start(self):
        try:
            # Initialize PyAudio for Output
            self.p_out = pa.PyAudio()
            self.out_stream = self.p_out.open(
                format=self.FORMAT,
                channels=self.CHANNEL_OUTPUT,
                rate=self.RATE,
                output=True,
                frames_per_buffer=self.CHUNK,
            )
            self.out_stream.start_stream()

            # Initialize PyAudio for Input
            self.p = pa.PyAudio()
            self.in_stream = self.p.open(
                format=self.FORMAT,
                channels=self.CHANNEL_INPUT,
                rate=self.RATE,
                input=True,
                frames_per_buffer=self.CHUNK,
                stream_callback=self._process_stream,
            )
            self.in_stream.start_stream()
            # while self.in_stream.is_active():
            #     time.sleep(0.1)
        except KeyboardInterrupt:
            self.stop()
            pass

    def stop(self):
        self.in_stream.close()
        self.p.terminate()
        self.out_stream.close()
        self.p_out.terminate()

    def _process_stream(self, in_data, frame_count, time_info, flag):
        data = np.frombuffer(in_data, dtype=np.float32)
        data = self.use_audio_effect(data)
        data = self.audio_equalizer.equalizer_10band(data=data, fs=self.RATE)
        self.out_stream.write(np.array(data, dtype=np.float32).tobytes())
        return in_data, pa.paContinue

    def use_audio_effect(self, signal):
        signal = self.audio_effect.add_white_noise(signal)
        signal = self.audio_effect.time_stretch(signal)
        signal = self.audio_effect.pitch_scale(signal, self.CHUNK)
        signal = self.audio_effect.invert_polarity(signal)
        signal = self.audio_effect.set_volume(signal)
        return signal

但是,当我使用像 google meet 这样的视频会议应用程序时,python 语音转换的输出只能在我的电脑扬声器上听到,而不能在虚拟麦克风中听到。

相关内容