Reputation: 6373
I am setting up an AVAudioEngine implementation to take audio from the microphone and stream it to a websocket connection. First things first, I have tapped the microphone input and an intermediate mixer that downsamples the microphone's audio from 44khz to 11khz. Somehow only the Downsampling Mixer's "print" is shown on the log, while the "print" from the tap on the microphones inputNode never happens. What am I doing wrong? Am I imagining something with the wrong mindset?
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
}
Upvotes: 3
Views: 1998
Reputation: 51
let node = audioEngine.inputNode!
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, time) in
self.audioBuffer = buffer;
}
The format should be the output format of the node you are tapping on.
Upvotes: 0