Creating ShazamKit signatures from audio files

I was trying to generate a signature from an audio file on-disk. Loading the files work, however generating signatures from them don't seem to work.

Here's the code I'm using to generate the signature:

func signature(from asset: AVAudioFile) -> SHSignature? {
    let format = asset.processingFormat
    let frameCount = AVAudioFrameCount(asset.length)
     
    guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCount) else { return nil }
    let generator = SHSignatureGenerator()
     
    do {
        try asset.read(into: buffer)
       
        let sampleRate = asset.processingFormat.sampleRate
       
        try generator.append(buffer, at: AVAudioTime(sampleTime: .zero, atRate: sampleRate))
       
        return generator.signature()
    } catch {
        print(error)
        return nil
    }
}

There are no errors being thrown in the code, but when I try to use the signatures in an SHSession, the audio is not being recognised. However, using the Shazam library works.

I have tried searching ways to do this, however the only examples I've seen are for generating signatures from live audio input. Is there something I'm missing from the code above?

Answered by Frameworks Engineer in 679057022

This could be caused by the way the audio file is converted into PCM buffers. There's an example code snippet posted on this thread on how to convert AVAudioFile into AVAudioPCMBuffer.

Accepted Answer

This could be caused by the way the audio file is converted into PCM buffers. There's an example code snippet posted on this thread on how to convert AVAudioFile into AVAudioPCMBuffer.

// Initialise Model in your View controller

  private var viewModel = ContentViewModel()

//Add In Click Event or just call in viewDidLoad() viewModel.startMatching()

We have an API update for 2022 that should simplify this, please try https://developer.apple.com/documentation/shazamkit/shsignaturegenerator/3991425-generatesignature.

There is also a Shazam CLI shipping on macOS that can perform the task.

Thanks

Creating ShazamKit signatures from audio files
 
 
Q