Skip to content

Instantly share code, notes, and snippets.

@jazzychad
Created June 11, 2021 07:18
Show Gist options
  • Select an option

  • Save jazzychad/43e1331ece5332a4b0e709faf1d5c4a6 to your computer and use it in GitHub Desktop.

Select an option

Save jazzychad/43e1331ece5332a4b0e709faf1d5c4a6 to your computer and use it in GitHub Desktop.
ShazamKit catalog generation for an audio file
import AVFoundation
import ShazamKit
func generateSignatureAndCatalogFromAudioFile() {
// this function takes an audio file and creates a ShazamKit signature for it,
// creates a media item to associate with the signature,
// creates a custom catalog with one entry (the audio signature + media item),
// and writes it out to disk as a .shazamcatalog file
// this example is full of force-unwrapped optionals... please use proper do/try/catch blocks in real code!
// assuming you have a sound file in your bundle called soundtrack.m4a
let url = Bundle.main.url(forResource: "soundtrack", withExtension: "m4a")!
let audioFile = try? AVAudioFile(forReading: url)
// audio format ShazamKit will understand
let newFormat = AVAudioFormat(standardFormatWithSampleRate: 48000, channels: 1)
// to convert from audiofile buffer to ShazamKit buffer
let converter = AVAudioConverter(from: audioFile!.processingFormat, to: newFormat!)
// buffer for audio file to read into
let soundtrackPCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFile!.processingFormat, frameCapacity: AVAudioFrameCount(audioFile!.length))
// buffer to convert into and then use for creating ShazamKit signature
let convertedPCMBuffer = AVAudioPCMBuffer(pcmFormat: newFormat!, frameCapacity: AVAudioFrameCount(audioFile!.length))
do {
// read the audio file into the buffer
try audioFile?.read(into: soundtrackPCMBuffer!)
// convert the audio file buffer
try converter!.convert(to: convertedPCMBuffer!, from: soundtrackPCMBuffer!)
} catch {
print("read/convert into error!! \(error)")
}
let signatureGenerator = SHSignatureGenerator()
do {
// generate signature from the converted buffer
try signatureGenerator.append(convertedPCMBuffer!, at: nil)
} catch {
print("signature generation error!! \(error)")
}
let signature = signatureGenerator.signature()
// see if we got a signature and if it has a duration
print("signature: \(String(describing: signature))")
print("signature duration: \(signature.duration)")
// create a custom catalog
let catalog = SHCustomCatalog()
// create a media item to associate with the signature
let item = SHMediaItem(properties: [
.artist: "Artist",
.subtitle: "Subtitle",
.shazamID: "123",
// ...
])
// add the item referencing the signature
try? catalog.addReferenceSignature(signature, representing: [item])
// write out the catalog to a file
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("shazamcatalog")
do {
// do the thing
try catalog.write(to: tempURL)
// print the file URL of the .shazamcatalog file
print("\(tempURL)")
} catch {
print("Export error: \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment