From 854b94fca6a8a00735a81fee06d6e84d33a3910a Mon Sep 17 00:00:00 2001 From: Chigozirim Igweamaka Date: Sat, 8 Mar 2025 05:13:28 +0100 Subject: [PATCH] Implement filterMatches to remove matches with insufficient target zones --- shazam/shazam.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/shazam/shazam.go b/shazam/shazam.go index a3e5f68..09371cf 100644 --- a/shazam/shazam.go +++ b/shazam/shazam.go @@ -49,17 +49,26 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int) matches := map[uint32][][2]uint32{} // songID -> [(sampleTime, dbTime)] timestamps := map[uint32][]uint32{} + targetZones := map[uint32]map[uint32]int{} // songID -> timestamp -> count for address, couples := range m { for _, couple := range couples { matches[couple.SongID] = append(matches[couple.SongID], [2]uint32{fingerprints[address].AnchorTimeMs, couple.AnchorTimeMs}) timestamps[couple.SongID] = append(timestamps[couple.SongID], couple.AnchorTimeMs) + + if _, ok := targetZones[couple.SongID]; !ok { + targetZones[couple.SongID] = make(map[uint32]int) + } + targetZones[couple.SongID][couple.AnchorTimeMs]++ } } + // matches = filterMatches(10, matches, targetZones) + scores := analyzeRelativeTiming(matches) var matchList []Match + for songID, points := range scores { song, songExists, err := db.GetSongByID(songID) if !songExists { @@ -86,6 +95,33 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int) return matchList, time.Since(startTime), nil } +// filterMatches filters out matches that don't have enough +// target zones to meet the specified threshold +func filterMatches( + threshold int, + matches map[uint32][][2]uint32, + targetZones map[uint32]map[uint32]int) map[uint32][][2]uint32 { + + // Filter out non target zones. + // When a target zone has less than `targetZoneSize` anchor times, it is not considered a target zone. + for songID, anchorTimes := range targetZones { + for anchorTime, count := range anchorTimes { + if count < targetZoneSize { + delete(targetZones[songID], anchorTime) + } + } + } + + filteredMatches := map[uint32][][2]uint32{} + for songID, zones := range targetZones { + if len(zones) >= threshold { + filteredMatches[songID] = matches[songID] + } + } + + return filteredMatches +} + // AnalyzeRelativeTiming checks for consistent relative timing and returns a score func analyzeRelativeTiming(matches map[uint32][][2]uint32) map[uint32]float64 { scores := make(map[uint32]float64)