I’ve been building an App for MacOS (and also iOS) using AudioKit.
The App will play sounds with a MIDISampler
, and this bit works!
It will also listen with a device microphone and use a PitchTap
to provide tuning information. I have not been able to get the microphone input to work.
My audio graph setup looks like this…
final class AudioGraph {
let engine = AudioEngine()
let sampler = MIDISampler(name: "sampler")
let pitchTracker: PitchTap
init(file: SamplerFile? = nil) throws {
let mic = engine.input!
engine.output = Mixer(
sampler,
Fader(mic, gain: 0.0)
)
pitchTracker = PitchTap(mic) { f, a in
guard let f = f.first, let a = a.first else { return }
print("Frequency (f) – Amplitude (a)")
}
if let file {
try setFile(file)
}
}
func setFile(_ file: SamplerFile) throws {
try sampler.loadInstrument(url: file.url)
}
}
// MARK: -
extension AudioGraph: NotePlayer {
func startAudioEngine() throws {
print("### Start engine")
try engine.start()
pitchTracker.start()
}
func stopAudioEngine() {
pitchTracker.stop()
engine.stop()
}
}
When I run this lot on MacOS, I can play notes (yay). However, the PitchTap
callback is called with frequency and magnitude information, but the pitch is always 100.0
and the magnitude always 2.4266092e-05
(like – pretty zero ish).
I’ve done a couple of experiments…
- I’ve attached the
PitchTap
to theMIDISampler
instead usingpitchTracker = PitchTap(sampler) { … }
, and this works. When I play notes their frequency is printed out ????. - I’ve added
engine.input to the output by setting
gainon the
Mixerto 1.0
engine.output = Mixer(sampler, Fader(mic, gain: 1.0))`. I’d expect to hear horrible feedback squeals when I do this, but I don’t get anything. Note playback still works though.
I’ve built and run the cook book app of all the things, and the Tuner in this works great on both MacOS and iOS, which was very encouraging!
My suspicion is that I have not set up microphone input correctly on MacOS so engine.input
is just a stream of silence?
I wondered if there is a supper minimal “Hello, World!” level of demo application showing just how to configure the microphone for use on MacOS?
I’m also wanting to get this to run on iOS where the PitchTap
callback isn’t firing. I’ll get to in another question.
Many Thanks!