0

currently trying to get my AKSampler to play sounds that I send it but not having much luck getting audio to output. My AKMidiCallbackInstrument is properly logging the notes playing (although I'm seeing the print for each note twice..) However, the call to my sampler is not producing any audio and I can't figure out why.

class Sequencer {
    
    var sampler: AKSampler
    var sequencer: AKAppleSequencer
    var mixer: AKMixer
    
    init() {
        sampler = AKSampler()
        sequencer = AKAppleSequencer()
        mixer=AKMixer(sampler)
        let midicallback = AKMIDICallbackInstrument()
        let url = Bundle.main.url(forResource: "UprightPianoKW-20190703", withExtension: "sfz")!;
        let track = sequencer.newTrack()
        track?.setMIDIOutput(midicallback.midiIn)

        sampler.loadSFZ(url: url)
   
        //generate some notes and add thtem to the track
        generateSequence()
        
        midicallback >>> mixer
        AudioKit.output = mixer
        AKSettings.playbackWhileMuted = true
        AKSettings.audioInputEnabled = true


        midicallback.callback = { status, note, vel in
            guard let status = AKMIDIStatus(byte: status),
                let type = status.type,
                type == .noteOn else { return print("note off: \(note)") }
            print("note on: \(note)")
            self.sampler.play(noteNumber: note, velocity: vel)        }

    }
    
    
    func play() {
        try? AudioKit.start()

        sequencer.rewind()
        sequencer.play()
        try? AudioKit.stop()
    }
    
    func stop() {
        sequencer.stop()
    }

1 Answers1

0

you need to connect your sampler to the mixer:

sampler >>> mixer

Fwiw, midicallback >>> mixer isn't necessary with AKAppleSequencer/AKMIDICallbackInstrument although it would be with AKSequencer/AKCallbackInstrument

c_booth
  • 2,185
  • 1
  • 13
  • 22
  • thanks! I'll try that out! Is that not being accomplished already with AKMixer(sampler)? And good to keep in mind on midicallbacks, I've just had many iterations where I feel everything is connected on my signal chain and I'm seeing notes play for many different iterations and haven't heard sound on any of my attempts. – Scott Johnson Sep 11 '20 at 00:29
  • Sorry I missed AKMixer(sampler), that might be sufficient, but its still worth trying. A good debugging strategy would be making sure you can get a sound independently from the sequencer (e.g., trigger with a button), perhaps without the sfz. Or alternatively try the sequencer with AKAppleSampler (with the default synth) to verify that you can get a sound that way and try to isolate where the issue lies. – c_booth Sep 11 '20 at 02:13
  • thanks for the advice! Changing to AKAppleSampler with default worked perfectly (although I am still calling every note twice, I think I need to properly set it up like a conductor) ... now I have no idea why the other one didn't work... – Scott Johnson Sep 11 '20 at 02:39