I'm trying to implement a simple recording feature using AudioKit's NodeRecorder. The idea is that a user taps a button to record, taps again to stop recording, and taps yet again to sample their new recording.
I've looked into the AudioKit Cookbook Recipe for this class, which uses AudioPlayer, whereas my code uses AppleSampler.
When I try and run the start/stop methods, though, my print statement shows that the length of the audioFile is 0, and that the "File read returned zero frames". Printing recorder.isRecording during the record operation shows the variable is being set properly.
Did I set the input and output correctly for the AudioEngine? Thank you!
import UIKit
import AudioKit
import AVFoundation
class MIDIButton: UIButton {
let engine = AudioEngine()
var sampler = AppleSampler()
var recorder: NodeRecorder?
var audioFile: AVAudioFile?
init() {
guard let input = engine.input else {
fatalError()
}
do {
recorder = try NodeRecorder(node: input)
} catch let err {
fatalError("\(err)")
}
engine.output = sampler
setup() //Starts the audioEngine
}
func startRecording() {
do {
try recorder?.record()
} catch let err {
print(err)
}
}
func stopRecording() {
recorder?.stop()
if let file = recorder?.audioFile {
//This is returning 0
print("audiofile has length \(file.length)")
handleNewRecording(file) //Handles the recording file
}
}
}