init() {
isRecording = false
data = Data()
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 4096, format: format) { (buffer, time) in
// Retrieve audio buffer and append it to saved data
let channels = UnsafeBufferPointer(start: buffer.int16ChannelData, count: 1)
let bufferData = NSData(bytes: channels.first, length: Int(buffer.frameCapacity * buffer.format.streamDescription.pointee.mBytesPerFrame)) as Data
if let callback = self.passBufferCallback {
callback(bufferData)
}
self.data.append(bufferData)
}
}
private func startRecording() {
data.removeAll()
do {
try audioEngine.start()
DispatchQueue.main.async {
self.delegate?.recordingStarted()
}
}
catch {
print("Some troubles with starting engine..")
}
}