mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-17 17:04:22 +00:00
Determine score from consistent relative timing.
This commit is contained in:
parent
2d13181fa8
commit
8a891ec3cd
1 changed files with 21 additions and 79 deletions
100
shazam/shazam.go
100
shazam/shazam.go
|
|
@ -3,7 +3,6 @@ package shazam
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"song-recognition/models"
|
|
||||||
"song-recognition/utils"
|
"song-recognition/utils"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
@ -17,6 +16,7 @@ type Match struct {
|
||||||
Score float64
|
Score float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindMatches processes the audio samples and finds matches in the database
|
||||||
func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int) ([]Match, error) {
|
func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int) ([]Match, error) {
|
||||||
logger := utils.GetLogger()
|
logger := utils.GetLogger()
|
||||||
|
|
||||||
|
|
@ -27,10 +27,9 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int)
|
||||||
|
|
||||||
peaks := ExtractPeaks(spectrogram, audioDuration)
|
peaks := ExtractPeaks(spectrogram, audioDuration)
|
||||||
fingerprints := Fingerprint(peaks, utils.GenerateUniqueID())
|
fingerprints := Fingerprint(peaks, utils.GenerateUniqueID())
|
||||||
fmt.Println("peaks len: ", len(peaks))
|
|
||||||
|
|
||||||
addresses := make([]uint32, 0, len(fingerprints))
|
addresses := make([]uint32, 0, len(fingerprints))
|
||||||
for address, _ := range fingerprints {
|
for address := range fingerprints {
|
||||||
addresses = append(addresses, address)
|
addresses = append(addresses, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,34 +44,17 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
matches := map[uint32]map[uint32]models.Couple{}
|
matches := map[uint32][][2]uint32{} // songID -> [(sampleTime, dbTime)]
|
||||||
timestamps := map[uint32]uint32{}
|
timestamps := map[uint32]uint32{}
|
||||||
|
|
||||||
for address, couples := range m {
|
for address, couples := range m {
|
||||||
for _, couple := range couples {
|
for _, couple := range couples {
|
||||||
|
matches[couple.SongID] = append(matches[couple.SongID], [2]uint32{fingerprints[address].AnchorTimeMs, couple.AnchorTimeMs})
|
||||||
if _, ok := matches[couple.SongID]; !ok {
|
timestamps[couple.SongID] = couple.AnchorTimeMs
|
||||||
matches[couple.SongID] = map[uint32]models.Couple{}
|
|
||||||
timestamps[couple.SongID] = couple.AnchorTimeMs
|
|
||||||
}
|
|
||||||
|
|
||||||
matches[couple.SongID][address] = couple
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scores := map[uint32]float64{}
|
scores := analyzeRelativeTiming(matches)
|
||||||
for songID, couples := range matches {
|
|
||||||
song, songExists, err := db.GetSongByID(songID)
|
|
||||||
if err != nil || !songExists {
|
|
||||||
// log error
|
|
||||||
fmt.Println("Continuing")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Printf("Song: %v, Scores:\n", song.Title)
|
|
||||||
|
|
||||||
scores[songID] = matchScore(fingerprints, couples)
|
|
||||||
fmt.Println("------------------------------------")
|
|
||||||
}
|
|
||||||
|
|
||||||
var matchList []Match
|
var matchList []Match
|
||||||
for songID, points := range scores {
|
for songID, points := range scores {
|
||||||
|
|
@ -87,7 +69,6 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Song: %v, Score: %v\n", song.Title, points)
|
fmt.Printf("Song: %v, Score: %v\n", song.Title, points)
|
||||||
fmt.Println("====================================")
|
|
||||||
match := Match{songID, song.Title, song.Artist, song.YouTubeID, timestamps[songID], points}
|
match := Match{songID, song.Title, song.Artist, song.YouTubeID, timestamps[songID], points}
|
||||||
matchList = append(matchList, match)
|
matchList = append(matchList, match)
|
||||||
}
|
}
|
||||||
|
|
@ -100,60 +81,21 @@ func FindMatches(audioSamples []float64, audioDuration float64, sampleRate int)
|
||||||
return matchList, nil
|
return matchList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchScore computes a match score between the two transformed audio samples (into a list of Key + TableValue)
|
// AnalyzeRelativeTiming checks for consistent relative timing and returns a score
|
||||||
func matchScore(sample, match map[uint32]models.Couple) float64 {
|
func analyzeRelativeTiming(matches map[uint32][][2]uint32) map[uint32]float64 {
|
||||||
// Will hold a list of points (time in the sample sound file, time in the matched database sound file)
|
scores := make(map[uint32]float64)
|
||||||
points := [2][]float64{}
|
for songID, times := range matches {
|
||||||
matches := 0.0
|
count := 0
|
||||||
for k, sampleValue := range sample {
|
for i := 0; i < len(times); i++ {
|
||||||
if matchValue, ok := match[k]; ok {
|
for j := i + 1; j < len(times); j++ {
|
||||||
points[0] = append(points[0], float64(sampleValue.AnchorTimeMs))
|
sampleDiff := math.Abs(float64(times[i][0] - times[j][0]))
|
||||||
points[1] = append(points[1], float64(matchValue.AnchorTimeMs))
|
dbDiff := math.Abs(float64(times[i][1] - times[j][1]))
|
||||||
matches++
|
if math.Abs(sampleDiff-dbDiff) < 100 { // Allow some tolerance
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
scores[songID] = float64(count)
|
||||||
}
|
}
|
||||||
corr := correlation(points[0], points[1])
|
return scores
|
||||||
fmt.Printf("Score (%v * %v * %v): %v\n", corr, corr, matches, corr*corr*matches)
|
|
||||||
return corr * corr * matches
|
|
||||||
}
|
|
||||||
|
|
||||||
// Correlation computes the correlation between 2 series of points
|
|
||||||
// the length used is x's
|
|
||||||
func correlation(x []float64, y []float64) float64 {
|
|
||||||
n := len(x)
|
|
||||||
meanX, meanY := Avg(x[:n]), Avg(y[:n])
|
|
||||||
|
|
||||||
sXY := 0.0
|
|
||||||
sX := 0.0
|
|
||||||
sY := 0.0
|
|
||||||
|
|
||||||
for i, xp := range x {
|
|
||||||
dx := xp - meanX
|
|
||||||
dy := y[i] - meanY
|
|
||||||
|
|
||||||
sX += dx * dx
|
|
||||||
sY += dy * dy
|
|
||||||
|
|
||||||
sXY += dx * dy
|
|
||||||
}
|
|
||||||
|
|
||||||
if sX == 0 || sY == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return sXY / (math.Sqrt(sX) * math.Sqrt(sY))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Avg computes the average of the given array
|
|
||||||
func Avg(arr []float64) float64 {
|
|
||||||
if len(arr) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
sum := 0.0
|
|
||||||
for _, v := range arr {
|
|
||||||
sum += v
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum / float64(len(arr))
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue