How can I specify the format of AVAudioEngine Mic-Input?

Georg

I'd like to record the some audio using AVAudioEngine and the users Microphone. I already have a working sample, but just can't figure out how to specify the format of the output that I want...

My requirement would be that I need the AVAudioPCMBuffer as I speak which it currently does...

Would I need to add a seperate node that does some transcoding? I can't find much documentation/samples on that problem...

And I am also a noob when it comes to Audio-Stuff. I know that I want NSData containing PCM-16bit with a max sample-rate of 16000 (8000 would be better)

Here's my working sample:

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 let' say

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

then if will produce an error saying that the sample rate needs to be the same as the hwInput...

Any help is very much appreciated!!!

EDIT: I just found AVAudioConverter but I need to be compatible with iOS8 as well...

Josh

You cannot change audio format directly on input nor output nodes. In the case of the microphone, the format will always be 44KHz, 1 channel, 32bits. To do so, you need to insert a mixer in between. Then when you connect inputNode > changeformatMixer > mainEngineMixer, you can specify the details of the format you want.

Something like:

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

I would recommend using an open framework such as EZAudio, instead, though.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to use computers mic as Sound Input in Pharo

分類Dev

How do I turn the mic off?

分類Dev

How can I specify the gulpfile to use?

分類Dev

How can I specify `Sized` for boxed value

分類Dev

How Can I Specify A Minimum Ruby Version in a Gemfile?

分類Dev

How can I specify an angular environment on ionic build?

分類Dev

How can i specify a discord text channel in my code?

分類Dev

How can I specify per flavor buildType sourceSets?

分類Dev

how can I limit specify filed in rest api result in sailjs

分類Dev

How can I specify the type of the field that an Annotation should be applied to in Java?

分類Dev

dpkg-deb, how can I specify the architecture

分類Dev

How can I format an NSDecimalNumber into a Swift string?

分類Dev

How can I get summary in scientific format

分類Dev

Speakers are coming up as mic input?

分類Dev

How can I format date_space_hour to a time format

分類Dev

How can i link a user input to a string

分類Dev

How can I validate scanf numeric input

分類Dev

How can I format a linux liveusb so that grub can read it?

分類Dev

Can't format date in <input>

分類Dev

How to specify column format in SELECT INTO statements in SQL?

分類Dev

How can I calculate the median of a range of numbers that I input?

分類Dev

How do I specify the timezone and date object format on moment correct such that I do not get Invalid date and a moment is deprecated warning?

分類Dev

Can I explicitly specify multiple tests in PyTest?

分類Dev

Can I specify the response directly in Swagger 2.0?

分類Dev

How can I convert a date into another format with moment.js?

分類Dev

How can I parse a C format string in Python?

分類Dev

How can i add my data properly in csv format?

分類Dev

How can I conditionally format my HTML table?

分類Dev

How can i format the date with Momentjs to look like this?

Related 関連記事

  1. 1

    How to use computers mic as Sound Input in Pharo

  2. 2

    How do I turn the mic off?

  3. 3

    How can I specify the gulpfile to use?

  4. 4

    How can I specify `Sized` for boxed value

  5. 5

    How Can I Specify A Minimum Ruby Version in a Gemfile?

  6. 6

    How can I specify an angular environment on ionic build?

  7. 7

    How can i specify a discord text channel in my code?

  8. 8

    How can I specify per flavor buildType sourceSets?

  9. 9

    how can I limit specify filed in rest api result in sailjs

  10. 10

    How can I specify the type of the field that an Annotation should be applied to in Java?

  11. 11

    dpkg-deb, how can I specify the architecture

  12. 12

    How can I format an NSDecimalNumber into a Swift string?

  13. 13

    How can I get summary in scientific format

  14. 14

    Speakers are coming up as mic input?

  15. 15

    How can I format date_space_hour to a time format

  16. 16

    How can i link a user input to a string

  17. 17

    How can I validate scanf numeric input

  18. 18

    How can I format a linux liveusb so that grub can read it?

  19. 19

    Can't format date in <input>

  20. 20

    How to specify column format in SELECT INTO statements in SQL?

  21. 21

    How can I calculate the median of a range of numbers that I input?

  22. 22

    How do I specify the timezone and date object format on moment correct such that I do not get Invalid date and a moment is deprecated warning?

  23. 23

    Can I explicitly specify multiple tests in PyTest?

  24. 24

    Can I specify the response directly in Swagger 2.0?

  25. 25

    How can I convert a date into another format with moment.js?

  26. 26

    How can I parse a C format string in Python?

  27. 27

    How can i add my data properly in csv format?

  28. 28

    How can I conditionally format my HTML table?

  29. 29

    How can i format the date with Momentjs to look like this?

ホットタグ

アーカイブ