AVAudioEngine 実装をセットアップして、マイクからオーディオを取得し、それを websocket 接続にストリーミングします。まず最初に、マイク入力と、マイクのオーディオを 44khz から 11khz にダウンサンプリングする中間ミキサーをタップしました。どういうわけか、ダウンサンプリング ミキサーの「印刷」のみがログに表示されますが、マイク inputNode のタップからの「印刷」は発生しません。私は何を間違っていますか?私は間違った考え方で何かを想像していますか?
func initializeBlastEngine(){
var listOfInputs = AVAudioSession.sharedInstance().availableInputs
print("LIST OF INPUTS: "+(listOfInputs?.description)!)
do{
//pick which one you want (change index)
var availableInput: AVAudioSessionPortDescription = listOfInputs![0] as AVAudioSessionPortDescription
//set the Preffered Input
try! AVAudioSession.sharedInstance().setPreferredInput(availableInput)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
let ioBufferDuration = 128.0 / 44100.0
try AVAudioSession.sharedInstance().setPreferredIOBufferDuration(ioBufferDuration)
}
catch{
print("AudioSession Init ERROR")
}
audioEngine.stop()
audioEngine = AVAudioEngine.init()
playerNode = AVAudioPlayerNode.init()
inputNode = audioEngine.inputNode!
mainMixer = audioEngine.mainMixerNode
//uncommenting causes: Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: node != _mixer && node != _outputNode && node != _inputNode'
//audioEngine.attachNode(inputNode)
audioEngine.attachNode(downMixer)
audioEngine.attachNode(playerNode)
inputNode.installTapOnBus(0, bufferSize: 4096, format: inputNode.inputFormatForBus(0), block:
{ (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
//THIS CODE NEVER EXECUTES!!!
print(NSString(string: "MIC Tap"))
print(buffer.format.description)
var micFormat = self.inputNode.inputFormatForBus(0).description
print("mic Format: "+micFormat)
})
downMixer.installTapOnBus(0, bufferSize: 4096, 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)
print(NSString(string: "writing"))
}
catch let error as NSError{
print(NSString(string: "Write failed: "+error.description));
}
})
let format = inputNode.inputFormatForBus(0)
audioEngine.connect(inputNode, to: downMixer, format: format)
audioEngine.connect(downMixer, to: audioEngine.outputNode, format: format16KHzMono)
audioEngine.connect(playerNode, to: mainMixer, format: mainMixer.outputFormatForBus(0))
//ENGINE IGNITION!!!!
audioEngine.prepare()
try! audioEngine.start()
// connectWebSocket() //This is a tale for another day, first lets get the audio engine running
}