-
Notifications
You must be signed in to change notification settings - Fork 8
/
gochroma.go
88 lines (78 loc) · 2.2 KB
/
gochroma.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package gochroma provides a high-level API to the acoustic
// fingerprinting library chromaprint.
package gochroma
import (
"github.com/go-fingerprint/fingerprint"
"github.com/go-fingerprint/gochroma/chromaprint"
"io"
)
const (
seconds = 10
minmaxseconds = 120
)
// Available algorithms for fingerprinting.
const (
Algorithm1 = chromaprint.CHROMAPRINT_ALGORITHM_TEST1
Algorithm2 = chromaprint.CHROMAPRINT_ALGORITHM_TEST2
Algorithm3 = chromaprint.CHROMAPRINT_ALGORITHM_TEST3
Algorithm4 = chromaprint.CHROMAPRINT_ALGORITHM_TEST4
AlgorithmDefault = chromaprint.CHROMAPRINT_ALGORITHM_DEFAULT
)
// A Printer is a fingerprint.Calculator backed by libchromaprint.
type Printer struct {
context *chromaprint.ChromaprintContext
}
// New creates new Printer. Returned Printer must be closed
// with Close(). Note that if libchromaprint is compiled with FFTW,
// New should be called only from one goroutine at a time.
func New(algorithm int) (p *Printer) {
return &Printer{chromaprint.NewChromaprint(algorithm)}
}
// Close existing Printer.
func (p *Printer) Close() {
p.context.Free()
}
// Fingerprint implements fingerprint.Calculator interface.
func (p *Printer) Fingerprint(i fingerprint.RawInfo) (fprint string, err error) {
if err = p.prepare(i); err != nil {
return
}
fprint, err = p.context.GetFingerprint()
return
}
// RawFingerprint implements fingerprint.Calculator interface.
func (p *Printer) RawFingerprint(i fingerprint.RawInfo) (fprint []int32, err error) {
if err = p.prepare(i); err != nil {
return
}
fprint, err = p.context.GetRawFingerprint()
return
}
func (p *Printer) prepare(i fingerprint.RawInfo) error {
if i.MaxSeconds < minmaxseconds {
i.MaxSeconds = minmaxseconds
}
ctx := p.context
rate, channels := i.Rate, i.Channels
if err := ctx.Start(int(rate), int(channels)); err != nil {
return err
}
numbytes := 2 * seconds * rate * channels
buf := make([]byte, numbytes)
for total := uint(0); total <= i.MaxSeconds; total += seconds {
read, err := i.Src.Read(buf)
if err != nil && err != io.EOF {
return err
}
if read == 0 {
break
}
if err := ctx.Feed(buf[:read]); err != nil {
return err
}
}
if err := ctx.Finish(); err != nil {
return err
}
return nil
}