ios - 如何指定 AVAudioEngine Mic-Input 的格式?

标签 ios avfoundation core-audio avaudioengine

我想使用 AVAudioEngine 和用户麦克风录制一些音频。我已经有了一个工作示例,但就是不知道如何指定我想要的输出格式...

我的要求是我需要 AVAudioPCMBuffer,正如我所说的那样......

我是否需要添加一个单独的节点来进行一些转码?我找不到关于该问题的大量文档/示例...

而且我也是 Audio-Stuff 的菜鸟。我知道我想要包含最大采样率为 16000 的 PCM-16 位的 NSData(8000 会更好)

这是我的工作示例:

private var audioEngine = AVAudioEngine()

func startRecording() {

  let format = audioEngine.inputNode!.inputFormatForBus(bus)

  audioEngine.inputNode!.installTapOnBus(bus, bufferSize: 1024, format: format) { (buffer: AVAudioPCMBuffer, time:AVAudioTime) -> Void in

     let audioFormat = PCMBuffer.format
     print("\(audioFormat)")
  }

  audioEngine.prepare()
  do {
     try audioEngine.start()
  } catch { /* Imagine some super awesome error handling here */ }
}

如果我改变格式让'说

let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)

然后 if 会产生一个错误,指出采样率需要与 hwInput 相同...

非常感谢任何帮助!!!

编辑:我刚找到 AVAudioConverter 但我还需要与 iOS8 兼容...

最佳答案

您不能直接在输入节点或输出节点上更改音频格式。对于麦克风,格式始终为 44KHz、1 channel 、32 位。为此,您需要在两者之间插入一个混音器。然后当你连接 inputNode > changeformatMixer > mainEngineMixer 时,你可以指定你想要的格式的细节。

类似于:

var inputNode = audioEngine.inputNode
var downMixer = AVAudioMixerNode()

//I think you the engine's I/O nodes are already attached to itself by default, so we attach only the downMixer here:
audioEngine.attachNode(downMixer)

//You can tap the downMixer to intercept the audio and do something with it:
downMixer.installTapOnBus(0, bufferSize: 2048, format: downMixer.outputFormatForBus(0), block:  //originally 1024
            { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
                print(NSString(string: "downMixer Tap"))
                do{
                    print("Downmixer Tap Format: "+self.downMixer.outputFormatForBus(0).description)//buffer.audioBufferList.debugDescription)

        })

//let's get the input audio format right as it is
let format = inputNode.inputFormatForBus(0)
//I initialize a 16KHz format I need:
let format16KHzMono = AVAudioFormat.init(commonFormat: AVAudioCommonFormat.PCMFormatInt16, sampleRate: 11050.0, channels: 1, interleaved: true)

//connect the nodes inside the engine:
//INPUT NODE --format-> downMixer --16Kformat--> mainMixer
//as you can see I m downsampling the default 44khz we get in the input to the 16Khz I want 
audioEngine.connect(inputNode, to: downMixer, format: format)//use default input format
audioEngine.connect(downMixer, to: audioEngine.outputNode, format: format16KHzMono)//use new audio format
//run the engine
audioEngine.prepare()
try! audioEngine.start()

不过,我建议改用 EZAudio 等开放式框架。

关于ios - 如何指定 AVAudioEngine Mic-Input 的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33484140/

相关文章:

swift - 没有音频的视频会使 AVMutableComposition() 上的应用程序崩溃

ios - 从 CMSampleBufferRef 获取 YUV 用于视频流

ios - FFT 输出与 float 缓冲器 AudioUnit

objective-c - 如何使用 avassetreader 在音轨中搜索?

iOS - 连接混音器、remoteio 和动态处理器的 AudioFormat 问题

ios - 为 UITableView 的不同部分设置按钮标签

ios - 如何分析 Swift 中方法/函数使用的 CPU

ios - 当应用程序在后台时发送 http 请求

ios - 如何使用 osx/iOS native 安全框架创建 PKCS12 blob?

ios - AudioKit/AVAudioEngine 启动时间过长