使用虚拟声卡播放音频文件作为麦克风声源

一、安装虚拟声卡

VB-CABLE虚拟声卡是一种将电脑播放的声音当作虚拟麦克风输入的工具,可用于语音聊天、直播或录制系统声音等场景。

1、下载安装 VB-Audio Cable

下载地址:

VB-Audio Virtual Apps

  • 下载VBCABLE_Driver_Pack45.zip,解压后,双击打开VBCABLE_Setup_x64.exe,点击 “Install Driver” 进行安装。

  • 安装完成后,重启系统以使驱动生效。

2、配置系统音频输入输出

  • 打开“设置”->“系统”->“声音”,在 “输出” 栏,选择 “CABLE Input” ,在 “录制” 栏,选择 “CABLE Output” 。

  • 打开“设置”->“隐私和安全性”->“麦克风”,开启“麦克风访问权限”,开启游戏的麦克风权限。

二、播放音频到虚拟声卡

1、安装依赖

pip install pyaudio sounddevice numpy==1.26.4
pip install pydub

2、列出所有可用的音频输出设备

def list_audio_output_devices():
    p = pyaudio.PyAudio()
    for i in range(p.get_device_count()):
        device_info = p.get_device_info_by_index(i)
        if device_info["maxOutputChannels"] > 0:  # 只列出支持输出的设备
            device_name = device_info['name']
            print(f"设备索引: {i}, 设备名称: {device_name}, 采样率: {device_info['defaultSampleRate']}")

3、在指定的音频输出设备上播放音频

def play_audio_on_device(file_path, device_index):
    audio_data = None
    # 加载音频文件
    with wave.open(file_path, 'rb') as wf:
        # 获取音频参数
        channels = wf.getnchannels()
        sample_width = wf.getsampwidth()
        frame_rate = wf.getframerate()
        frames = wf.readframes(-1)

        # 将字节数据转换为 NumPy 数组
        dtype = None
        if sample_width == 2:
            dtype = np.int16
        elif sample_width == 4:
            dtype = np.int32
        else:
            raise ValueError(f"不支持的采样宽度: {sample_width}")

        audio_data = np.frombuffer(frames, dtype=dtype)
        audio_data = audio_data.reshape(-1, channels)

    # 播放音频
    try:
        sd.play(audio_data, samplerate=frame_rate,device=device_index, blocking=False)
        sd.wait()
        print("音频播放完成!")
    except Exception as e:
        print(f"播放音频时出错: {e}")

用于无声卡电脑- Virtual Audio Cable At first, please unpack the distribution package into an empty folder on a local drive, making sure that local folder path does not contain special characters like "#", "&" or similar. Unpacking the package, preserve folder structure (after unpacking, destination folder should contain "x86" and "x64" subfolders). If you are new to Virtual Audio Cable, please read the user manual before installation. The best way to open the manual is to run installer application named Setup and click the "Help" button in the first installation dialog. The user manual file (vac.chm, a file named "vac" with a sheet and question mark icon) is an HTML Help format file. Do not open manual file from a network path or directly from the package otherwise help pages may not be displayed due to security restrictions. Copy the file to a local drive before opening. Please make sure that local file path does not contain special characters. If user manual file is opened from a local drive but pages on the right pane are not displayed properly, please check if vac.chm file has the "Unblock" button in its Properties form (right-click the file to open its properties). If yes, unblock it. See also "How blocking some attachments helps protect your computer" topic in Windows Help (Start - Help and Support) and http://support.microsoft.com/kb/902225. Reading the manual, don't forget that there is the "Search" tab. Having a problem, enter some words related to the problem and appropriate pages will be displayed. Of course, search feature will not help if you enter a question like "how can I use it?". It only finds pages containing given keywords.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值