17

AVAudioEngineユーザーのマイクを使用して音声を録音したいと思います。既に動作するサンプルがありますが、必要な出力の形式を指定する方法がわかりません...

私の要件は、AVAudioPCMBuffer私が話すように、それが現在行っていることを必要とすることです...

トランスコーディングを行う別のノードを追加する必要がありますか? その問題に関する多くのドキュメント/サンプルが見つかりません...

また、オーディオ関連の初心者でもあります。NSData最大サンプルレートが 16000 の PCM-16bit が必要であることはわかっています (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にも対応する必要があります...

4

6 に答える 6

24

入力ノードまたは出力ノードでオーディオ形式を直接変更することはできません。マイクの場合、フォーマットは常に 44KHz、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 などのオープン フレームワークを使用することをお勧めします。

于 2016-05-31T13:42:29.993 に答える
0

入力ノードの構成を変更することはできません。必要な形式でミキサー ノードを作成し、それをエンジンに接続してから入力ノードに接続し、作成したノードに mainMixer を接続します。これで、このノードにタップをインストールして PCM データを取得できます。

いくつかの奇妙な理由により、サンプルレートの選択肢があまりないことに注意してください! 少なくとも iOS 9.1 では、標準の 11025、22050、または 44100 を使用してください。他のサンプル レートは失敗します。

于 2015-11-24T07:55:35.120 に答える