首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何指定AVAudioEngine Mic输入的格式?

如何指定AVAudioEngine Mic输入的格式?
EN

Stack Overflow用户
提问于 2015-11-02 18:10:55
回答 6查看 11.4K关注 0票数 17

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

我的要求是,我需要的AVAudioPCMBuffer,因为我说,它现在做.

我是否需要添加一个执行转码的独立节点?我找不到很多关于这个问题的文件/样本.

在音频方面,我也是个菜鸟。我知道我希望NSData包含PCM-16位,最大采样率为16000 (8000会更好)。

这是我的工作样本:

代码语言:javascript
复制
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 */ }
}

如果我改变了格式

代码语言:javascript
复制
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)

如果会产生错误,说明样本率需要与hwInput相同.

任何帮助都非常感谢!

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-05-31 13:42:29

您不能在输入或输出节点上直接更改音频格式。在麦克风的情况下,格式将始终为44 the,1通道,32位。为此,您需要在中间插入一个混合器。然后,在连接inputNode > changeformatMixer > mainEngineMixer时,可以指定所需格式的详细信息。

类似于:

代码语言:javascript
复制
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这样的开放框架。

票数 24
EN

Stack Overflow用户

发布于 2019-07-19 00:20:56

我发现唯一能改变采样率的方法是

代码语言:javascript
复制
AVAudioSettings.sharedInstance().setPreferredSampleRate(...)

您可以点击engine.inputNode并使用输入节点的输出格式:

代码语言:javascript
复制
engine.inputNode.installTap(onBus: 0, bufferSize: 2048,
                            format: engine.inputNode.outputFormat(forBus: 0))

不幸的是,这并不能保证你能得到你想要的样本率,尽管这看起来像是8000个,12000,16000,22050,44100都起作用了。

以下几点不起作用:

  1. 在关闭engine.inputNode的点击中设置我的自定义格式。(例外)
  2. 添加一个混音器与我的自定义格式和点击。(例外)
  3. 添加一个混频器,将其与inputNode格式连接,将混频器与我的自定义格式连接到主混频器,然后删除outputNode的输入,以免将音频发送到扬声器并获得即时反馈。(成功了,但得到了所有的零)
  4. 在AVAudioEngine中完全不使用我的自定义格式,并且使用AVAudioConverter从我的tap中的硬件速率进行转换。(没有设置缓冲区的长度,无法判断结果是否正确)

这是与iOS 12.3.1。

票数 3
EN

Stack Overflow用户

发布于 2021-01-27 04:55:08

为了改变输入节点的采样率,必须首先将输入节点连接到混频器节点,并在参数中指定新的格式。

代码语言:javascript
复制
let input = avAudioEngine.inputNode
let mainMixer = avAudioEngine.mainMixerNode
let newAudioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)
avAudioEngine.connect(input, to: mainMixer, format: newAudioFormat)

现在,您可以使用installTap调用输入节点上的newAudioFormat函数。

我还要指出的另一件事是,自从iPhone12新推出以来,输入节点的默认采样率不再是44100。它已经升级到48000了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33484140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档