AudioKit engine not starting immediately after interruption (call) ended

25 Views Asked by At

I am using AudioKit to run a MIDISampler with an .exs and an AppleSequencer. Everything works as expected.

My nodes:

output = mixer {
  MIDISampler
}

I am handing interruptions using NotificationCenter, I pause AudioKit engine when interruption begins and .start() the engine when it ends. However, if I restart the engine immediately, it doesn't seem to start properly. I get no exceptions, but the following console output:

     SamplerBase.cpp:272   SamplerBase::Cleanup
      SamplerBase.cpp:307   SamplerBase::Reset(0, 0)
  InstrumentState.cpp:248   InstrumentState::Cleanup(0x129fbf7f0)
InstrumentManager.cpp:269   InstrumentManager::ReleaseSamples(0x280c205e0)
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 2975638501413859767: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b50b940
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b50b940) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3048820765903262385: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b41f2c0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b41f2c0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3086279672035189812: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b41d2a0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b41d2a0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 2938179595281932340: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b51ddc0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b51ddc0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 2900720689150004913: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b60f340
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b60f340) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3123738578167117239: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b60fba0
       FileSample.cpp:354   FileSample::Unload(this=0x12b41f2c0):  Freeing stream cache
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b60fba0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3161197484299044666: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b30b4f0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b30b4f0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 2974626098545429296: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x12b30b7d0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12b30b7d0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3012085004677356723: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x129d51cb0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x129d51cb0) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3049543910809284150: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x129d0cf00
       FileSample.cpp:354   FileSample::Unload(this=0x129d51cb0):  Freeing stream cache
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 3087002816941211577: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x129fc6860
       RefCounted.cpp:28    RefCounted::Unref(this = 0x129fc6860) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 2900286802206391854: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x129fcba80
       FileSample.cpp:354   FileSample::Unload(this=0x129fc6860):  Freeing stream cache
       RefCounted.cpp:28    RefCounted::Unref(this = 0x129fcba80) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 4645063524637825519: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x2831ed040
       RefCounted.cpp:28    RefCounted::Unref(this = 0x2831ed040) count going to 0
    SampleManager.cpp:545   SampleManager::ReleaseSample: Holder count for sample ID 16342581291295425434: 1
    SampleManager.cpp:548   SampleManager::ReleaseSample: Holder count now zero -- unref'ing and removing sample 0x2831ecf70
       FileSample.cpp:354   FileSample::Unload(this=0x129fcba80):  Freeing stream cache
       RefCounted.cpp:28    RefCounted::Unref(this = 0x2831ecf70) count going to 0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12e608108) count going to 0
       FileSample.cpp:354   FileSample::Unload(this=0x12b30b7d0):  Freeing stream cache
         LFOState.cpp:40    LFOState::~LFOState(0x2821926f0)
       RefCounted.cpp:28    RefCounted::Unref(this = 0x12e608000) count going to 0
       RefCounted.cpp:28    RefCounted::Unref(this = 0x129d0cf00) count going to 0
       FileSample.cpp:354   FileSample::Unload(this=0x129d0cf00):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b30b4f0):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b60fba0):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b60f340):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b51ddc0):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b41d2a0):  Freeing stream cache
       FileSample.cpp:354   FileSample::Unload(this=0x12b50b940):  Freeing stream cache
    AudioStreamer.cpp:356   AudioStreamerImpl::Destroy: killing worker thread
    AudioStreamer.cpp:363   AudioStreamerImpl::Destroy: waiting for worker thread to die...
    AudioStreamer.cpp:838   ==== AudioStreamerImpl::sIOWorkerProcess: exiting thread
    AudioStreamer.cpp:375   AudioStreamerImpl::Destroy: done -- now cleaning up

After that point, the sound never plays, or I get a sine wave. However, everything works if I restart the engine with a one-second delay after receiving the notification.

        else if type == .ended {
            print("Interruption ended")
            guard let optionsValue =
                    info[AVAudioSessionInterruptionOptionKey] as? UInt else {
                return
            }

            let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
            if options.contains(.shouldResume) {
                print("Session should resume")

                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.resumeEngine()
                }
            }
        }

I cannot understand what may be causing this issue. One second is an arbitrary delay I tried and is not a real solution.

If I pause/resume the engine using exactly the same code when app goes into foreground or background, everything works.

Any suggestions would be appreciated.

0

There are 0 best solutions below