matchStreamingBuffer causes an NSException

Hi,

I am using ShazamKit to detect songs from a live stream. I am using matchStreamingBuffer with a PCMBuffer. It looks like it works for the most part, but sometimes it throws an NSException.

Here's the code calling the match:

engine.mainMixerNode.installTap(onBus: 0, bufferSize: 4096, format: options.audioFormat) { buffer, time in
                    do {
                            self.session.matchStreamingBuffer(buffer, at: time)
                    }
                    catch {

                    }
            }

The exception:

Supplied audio format is not supported <CMAudioFormatDescription 0x2828a29e0 [0x20f7863a0]> { mediaType:'soun' mediaSubType:'lpcm' mediaSpecific: { ASBD: { mSampleRate: 44100.000000 mFormatID: 'lpcm' mFormatFlags: 0x29 mBytesPerPacket: 4 mFramesPerPacket: 1 mBytesPerFrame: 4 mChannelsPerFrame: 2 mBitsPerChannel: 32 } cookie: {(null)} ACL: {Stereo (L R)} FormatList Array: { Index: 0 ChannelLayoutTag: 0x650002 ASBD: { mSampleRate: 44100.000000 mFormatID: 'lpcm' mFormatFlags: 0x29 mBytesPerPacket: 4 mFramesPerPacket: 1 mBytesPerFrame: 4 mChannelsPerFrame: 2 mBitsPerChannel: 32 }} } extensions: {(null)} }

This is the stack stack:

0  CoreFoundation                 0xa248 __exceptionPreprocess
1  libobjc.A.dylib                0x17a68 objc_exception_throw
2  ShazamKit                      0x159d0 -[SHMutableSignature appendBuffer:atTime:error:]
3  ShazamKit                      0x6d7c -[SHSignatureGenerator appendBuffer:atTime:error:]
4  ShazamKit                      0x3968 -[SHSessionDriverSignatureSlot appendBuffer:atTime:error:]
5  ShazamKit                      0x10430 -[SHSignatureBuffer flow:time:]
6  ShazamKit                      0x2490 -[SHStreamingSessionDriver flow:time:]
7  ShazamKit                      0xf784 -[SHSession matchStreamingBuffer:atTime:]
8  MyApp                            0x17f69c thunk for @escaping @callee_guaranteed (@guaranteed AVAudioPCMBuffer, @guaranteed AVAudioTime) -> () (<compiler-generated>)
9  AVFAudio                       0x482ac AVAudioNodeTap::TapMessage::RealtimeMessenger_Perform()
10 AVFAudio                       0x71c4 CADeprecated::RealtimeMessenger::_PerformPendingMessages()
11 AVFAudio                       0x471e4 invocation function for block in CADeprecated::RealtimeMessenger::RealtimeMessenger(applesauce::dispatch::v1::queue)

I don't mind failing if the format is not good, but how can I avoid crashing?

The exception is Fatal Exception: NSInvalidArgumentException. It's worth noting these streams usually work fine. When I tested this for a while in the wild however, sometimes it will crash. Maybe the buffer is randomly malformed in someway.

Thank you for posting your issue. The team had a quick look at it and here's a few suggestions:

  • It seems like the audio format you're passing in has 2 channels, make sure that these are interleaved, otherwise the code may throw an exception.
  • Also make sure that during the same session the format remains unchanged, if that changes, the code may throw an exception.

I would also suggest to watch the latest ShazamKit session from WWDC23. That will highlight tips and tricks and the latest changes we made to help you with audio format conversion issues. Updating to the latest SDKs might solve most of your issues.

Let us know if you have any more questions!

Thank you.

Can you point me to the right way to catch the exception without crashing?

Thanks again.

matchStreamingBuffer causes an NSException
 
 
Q