使用 AVFoundation 和 Swift 访问多个音频硬件输出/通道

2024-05-11

如何使用 AVFoundation 访问除 1-2 之外的其他音频硬件输出?我正在为 Mac OS-X 应用程序编写快速代码,该应用程序通过各种输出设备(USB 接口、dante、soundflower)播放 mp3 文件,如下所示:

myPlayer = AVPlayer(URL: myFilePathURL)
myPlayer.audioOutputDeviceUniqueID = myAudioOutputDevices[1].deviceUID()
myPlayer.play()

但是,我不确定如何将音频文件播放到 1-2 以外的频道。例如,我想播放 mp3 到输出 3-4。

我可以通过 AVPlayer 执行此操作吗?或者我需要去别的地方看看吗?也许 AVAudioEngine 与混音器节点一起?我查看了 AVAudioEngine 示例,但找不到任何地方引用的硬件通道。谢谢你的帮助!


随着时间的推移,我已经迭代了这段代码 - 但基本轮廓是有效的。这是我当前将音频发送到多通道设置的设置代码。我目前正在使用具有 16 个立体声实例流的 Dante Virtual Soundcard 使用以下代码执行此操作:

func setupAudioPath(){
    //print("setupAudioPath")

    // get output hardware format
    let output = engine.outputNode
    outputHWFormat = output.outputFormat(forBus: 0)

    //print("outputHWFormat = \(outputHWFormat)")
    //print("outputHWFormat.channelCount = \(outputHWFormat.channelCount)")

    // connect mixer to output
    mixer = engine.mainMixerNode

    //then work on the player end by first attaching the player to the engine
    engine.attach(player)

    engine.connect(mixer, to: output, format: outputHWFormat)

    var channelMap: [sint32] = []
    //UInt32 numOfChannels = fileFormat.NumberChannels();
    let numOfChannels: UInt32 = UInt32(numberOfStreams) * UInt32(2);    // Number of output device channels
    let mapSize: UInt32 = numOfChannels * UInt32(MemoryLayout<sint32>.size);
    for _ in 0...(numOfChannels-1) {
        channelMap.append(-1)
    }
    //channelMap[desiredInputChannel] = deviceOutputChannel;
    channelMap[leftChannel - 1] = 0;
    channelMap[leftChannel]     = 1;


    //print(channelMap)
    //print("number of channels in my map: \(channelMap.count)")

    let code: OSStatus = AudioUnitSetProperty((engine.outputNode.audioUnit)!,
                                              kAudioOutputUnitProperty_ChannelMap,
                                              kAudioUnitScope_Global,
                                              1,
                                              channelMap,
                                              mapSize);


    print("osstatus = \(code)")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 AVFoundation 和 Swift 访问多个音频硬件输出/通道 的相关文章

随机推荐