我想用AVAudioEngine和用户麦克风录制一些音频。我已经有了一个工作样例,但是不知道如何指定我想要的输出的格式.
我的要求是,我需要的AVAudioPCMBuffer,因为我说,它现在做.
我是否需要添加一个执行转码的独立节点?我找不到很多关于这个问题的文件/样本.
在音频方面,我也是个菜鸟。我知道我希望NSData包含PCM-16位,最大采样率为16000 (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)如果会产生错误,说明样本率需要与hwInput相同.
任何帮助都非常感谢!
编辑:,我刚找到了AVAudioConverter,但我也需要与iOS8兼容.
发布于 2016-05-31 13:42:29
您不能在输入或输出节点上直接更改音频格式。在麦克风的情况下,格式将始终为44 the,1通道,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这样的开放框架。
发布于 2019-07-19 00:20:56
我发现唯一能改变采样率的方法是
AVAudioSettings.sharedInstance().setPreferredSampleRate(...)您可以点击engine.inputNode并使用输入节点的输出格式:
engine.inputNode.installTap(onBus: 0, bufferSize: 2048,
format: engine.inputNode.outputFormat(forBus: 0))不幸的是,这并不能保证你能得到你想要的样本率,尽管这看起来像是8000个,12000,16000,22050,44100都起作用了。
以下几点不起作用:
这是与iOS 12.3.1。
发布于 2021-01-27 04:55:08
为了改变输入节点的采样率,必须首先将输入节点连接到混频器节点,并在参数中指定新的格式。
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了。
https://stackoverflow.com/questions/33484140
复制相似问题