124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
package Hibiki
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"git.gammaspectra.live/S.O.N.G/Hibiki/panako"
|
|
"git.gammaspectra.live/S.O.N.G/Hibiki/utilities/specializedstore"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio/filter"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format/guess"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func runTest(t *testing.T, wg, wgQuery *sync.WaitGroup, strategy *panako.Strategy, resourceId int64, fileName string, callback func(prints []panako.Fingerprint, queryResults []panako.QueryResult)) {
|
|
wg.Add(1)
|
|
go func() {
|
|
var source audio.Source
|
|
|
|
file, err := os.Open(fileName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
wg.Done()
|
|
return
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
decoders, err := guess.GetDecoders(file, fileName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
wg.Done()
|
|
return
|
|
}
|
|
source, err = guess.Open(file, decoders)
|
|
if err != nil {
|
|
t.Error(err)
|
|
wg.Done()
|
|
return
|
|
}
|
|
|
|
if source == nil {
|
|
t.Error(errors.New("cannot decode"))
|
|
wg.Done()
|
|
return
|
|
}
|
|
|
|
prints := strategy.SourceToFingerprints(source)
|
|
if resourceId != 0 {
|
|
strategy.StoreFingerprints(panako.ResourceId(resourceId), prints)
|
|
}
|
|
wgQuery.Add(1)
|
|
wg.Done()
|
|
wg.Wait()
|
|
strategy.QueryFingerprintsAsync(prints, func(queryResults []panako.QueryResult) {
|
|
defer wgQuery.Done()
|
|
callback(prints, queryResults)
|
|
})
|
|
}()
|
|
}
|
|
|
|
func TestHibiki(t *testing.T) {
|
|
store := specializedstore.NewMemoryStore()
|
|
|
|
panakoInstance := panako.NewDefaultPackedInstance()
|
|
|
|
strategy := panakoInstance.GetStrategy(store, filter.QualityFastest)
|
|
|
|
var wg sync.WaitGroup
|
|
var wgQuery sync.WaitGroup
|
|
|
|
runTest(t, &wg, &wgQuery, strategy, 1, "test.flac", func(prints []panako.Fingerprint, queryResults []panako.QueryResult) {
|
|
for _, result := range queryResults {
|
|
log.Printf("result 1 %#v\n", result)
|
|
}
|
|
})
|
|
|
|
runTest(t, &wg, &wgQuery, strategy, 0, "test2.flac", func(prints []panako.Fingerprint, queryResults []panako.QueryResult) {
|
|
for _, result := range queryResults {
|
|
log.Printf("result 2 %#v\n", result)
|
|
}
|
|
|
|
printBuffer := new(bytes.Buffer)
|
|
for _, p := range prints {
|
|
hash, pack := (&panako.StoreRecord{
|
|
ResourceId: 0,
|
|
Hash: uint64(p.CompactHash()),
|
|
Time: p.T1(),
|
|
Frequency: p.F1(),
|
|
}).GetCompactPackedPrint()
|
|
binary.Write(printBuffer, binary.BigEndian, hash)
|
|
binary.Write(printBuffer, binary.LittleEndian, pack)
|
|
}
|
|
log.Printf("print: %x\n", printBuffer.Bytes())
|
|
})
|
|
|
|
runTest(t, &wg, &wgQuery, strategy, 0, "test_radio.flac", func(prints []panako.Fingerprint, queryResults []panako.QueryResult) {
|
|
for _, result := range queryResults {
|
|
log.Printf("result 3 %#v\n", result)
|
|
}
|
|
})
|
|
|
|
runTest(t, &wg, &wgQuery, strategy, 0, "negative.flac", func(prints []panako.Fingerprint, queryResults []panako.QueryResult) {
|
|
for _, result := range queryResults {
|
|
log.Printf("result 4 %#v\n", result)
|
|
}
|
|
})
|
|
|
|
runTest(t, &wg, &wgQuery, strategy, 0, "test.mp3", func(prints []panako.Fingerprint, queryResults []panako.QueryResult) {
|
|
for _, result := range queryResults {
|
|
log.Printf("result 5 %#v\n", result)
|
|
}
|
|
})
|
|
|
|
wg.Wait()
|
|
wgQuery.Wait()
|
|
|
|
//store.Close()
|
|
|
|
}
|