How to specify AVAudioEngine Mic-Input format?

I would like to record some sound using a AVAudioEnginecustom microphone as well. I already have a working sample, but I just can't figure out how to specify the output format I want ...

My requirement would be what I need AVAudioPCMBuffer, as I say what he is currently doing ...

Do I need to add a separate node that does some transcoding? I can not find a lot of documentation / samples on this issue ...

And so am I. When it comes to Audio-Stuff. I know that I want to NSDatacontain PCM-16bit with a maximum sampling frequency of 16000 (8000 would be better)

Here is my working example:

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 */ }
}

If I changed the format to say "

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

, , , hwInput...

!!!

EDIT: AVAudioConverter, iOS8...

+7
3

. , 44 , 1 , 32 . . , inputNode> changeformatMixer> mainEngineMixer, .

- :

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

//I think you the engine 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 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.

+11

node, node , , node, mainMixer node, . node, PCM.

, - ! , iOS 9.1, 11025, 22050 44100. !

0

, , .

AVAudioSettings.sharedInstance().setPreferredSampleRate(...)

engine.inputNode :

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 . ( , , )

iOS 12.3.1.

0
source

Source: https://habr.com/ru/post/1614186/


All Articles