am trying to identify songs using shazamkit. every time it runs, this is the output:
2023-04-26 04:39:28.947059-0400 shazoom[38003:3098314] [AQ] AudioQueueObject.cpp:2364 Error (-4) getting reporterIDs
Processing chunk 1 from 0.0 to 60.0: 4316113920
Chunk saved at: file:///Users/youssefhemimy/Downloads/beyoncee.mp3
No match found
2023-04-26 04:39:29.483659-0400 shazoom[38003:3098339] [AQ] AudioQueueObject.cpp:2364 Error (-4) getting reporterIDs
Processing chunk 2 from 60.0 to 120.0: 4317271680
Chunk saved at: file:///Users/youssefhemimy/Downloads/beyoncee.mp3
No match found
2023-04-26 04:39:29.965718-0400 shazoom[38003:3098343] [AQ] AudioQueueObject.cpp:2364 Error (-4) getting reporterIDs
Processing chunk 3 from 120.0 to 180.0: 4317324608
Chunk saved at: file:///Users/youssefhemimy/Downloads/beyoncee.mp3
No match found
Note: all songs I have used have been identified by shazam the app. I have tried to use different songs, re-exported them from logic pro x but no luck. It also takes less than 2 seconds to generate this output which is way faster than the app.
I have tried to use:
var minimumQuerySignatureDuration: TimeInterval { get }
but no luck.
Below is the full code:
import Foundation
import AVFoundation
import ShazamKit
class ShazamAnalyzer: NSObject, SHSessionDelegate {
let session = SHSession()
func analyzeAudioFile(url: URL) {
let asset = AVAsset(url: url)
let assetDuration = asset.duration.seconds
let chunkDuration = 30.0
let numberOfChunks = Int(ceil(assetDuration / chunkDuration))
session.delegate = self
for i in 0..<numberOfChunks {
let startTime = Double(i) * chunkDuration
let start = CMTime(seconds: startTime, preferredTimescale: asset.duration.timescale)
let end = CMTime(seconds: min(startTime + chunkDuration, assetDuration), preferredTimescale: asset.duration.timescale)
let range = CMTimeRange(start: start, end: end)
if let signature = generateSignature(from: asset, for: range) {
print("Processing chunk \(i + 1) from \(start.seconds) to \(end.seconds): \(signature.hash)")
print("Chunk saved at: \(url.absoluteString)")
session.match(signature)
} else {
print("Error generating signature for chunk \(i + 1)")
}
}
}
func generateSignature(from asset: AVAsset, for timeRange: CMTimeRange) -> SHSignature? {
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 1)
let signatureGenerator = SHSignatureGenerator()
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)!
exportSession.timeRange = timeRange
let reader = try? AVAssetReader(asset: exportSession.asset)
let audioOutput = AVAssetReaderAudioMixOutput(audioTracks: exportSession.asset.tracks(withMediaType: .audio), audioSettings: nil)
audioOutput.alwaysCopiesSampleData = false
reader?.add(audioOutput)
reader?.startReading()
while let sampleBuffer = audioOutput.copyNextSampleBuffer() {
if let blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer) {
let length = CMBlockBufferGetDataLength(blockBuffer)
var data = Data(count: length)
_ = data.withUnsafeMutableBytes {(bytes: UnsafeMutableRawBufferPointer) in
CMBlockBufferCopyDataBytes(blockBuffer, atOffset: 0, dataLength: length, destination: bytes.baseAddress!)
}
let audioBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat!, frameCapacity: AVAudioFrameCount(length) / audioFormat!.streamDescription.pointee.mBytesPerFrame)!
guard let channelData = audioBuffer.floatChannelData else {
print("Error: Channel data is nil")
return nil
}
let channels = UnsafeBufferPointer(start: channelData, count: Int(audioFormat!.channelCount))
let destinationBuffer = UnsafeMutableBufferPointer(start: channels[0], count: length)
_ = data.copyBytes(to: destinationBuffer)
do {
try signatureGenerator.append(audioBuffer, at: nil)
} catch {
print(
"Error appending buffer to signature generator: \(error)")
return nil
}
}
}
return signatureGenerator.signature()
}
func session(_ session: SHSession, didFind match: SHMatch) {
if let matchedItem = match.mediaItems.first {
print("Match found: \(matchedItem.title ?? "Unknown Title") by \(matchedItem.artist ?? "Unknown Artist")")
} else {
print("Match found, but unable to retrieve media item details.")
}
}
func session(_ session: SHSession, didNotFindMatchFor signature: SHSignature, error: Error?) {
print("No match found")
}
}
let filePath = "/Users/youssefhemimy/Downloads/beyoncee.mp3" // Replace this with the actual path to your audio file
if let fileURL = URL(string: "file://" + filePath) {
let analyzer = ShazamAnalyzer()
analyzer.analyzeAudioFile(url: fileURL)
} else {
print("Invalid file path")
}