-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchromaprintbuilder.go
113 lines (97 loc) · 2.86 KB
/
chromaprintbuilder.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package chromaprint
import (
"os"
"path/filepath"
"runtime"
)
var (
fpcalcFileName = "fpcalc"
windowsExtension = ".exe"
)
// Create a chromaprint builder
func NewBuilder() ChromaprintBuilder {
return &builder{
filePath: "",
sampleRateInHz: -1,
channels: -1,
maxFingerPrintLength: 0,
chunkSizeInSeconds: -1,
overlap: false,
algorithm: 2,
}
}
// ChromaprintBuilder defines the fields we want to set on this builder, you could add/remove
// fields here.
type ChromaprintBuilder interface {
// Builds the Chromaprint object
Build() (*Chromaprint, error)
// Use this to specify the path to the Chromaprint CLI file.
WithPathToChromaprint(filePath string) ChromaprintBuilder
// Set the sample rate of the input audio
WithSampleRate(rateInHz int) ChromaprintBuilder
// Set the number of channels in the input audio
WithChannels(numberOfChannels int) ChromaprintBuilder
// Restrict the duration of the process input audio (default is unrestricted).
WithMaxFingerPrintLength(lengthInSeconds int) ChromaprintBuilder
// Split the input audio into chunks of this duration
WithChunksSize(chunkSizeInSeconds int) ChromaprintBuilder
// Set the algorithm method (default 2)
WithAlgorithm(algorithm int) ChromaprintBuilder
// Overlap the chunks slightly to make sure audio on the edges is fingerprinted
WithOverlap(overlap bool) ChromaprintBuilder
}
type builder struct {
filePath string
sampleRateInHz int
channels int
maxFingerPrintLength int
chunkSizeInSeconds int
overlap bool
algorithm int
}
func (b *builder) WithPathToChromaprint(filePath string) ChromaprintBuilder {
b.filePath = filePath
return b
}
func (b *builder) WithSampleRate(rateInHz int) ChromaprintBuilder {
b.sampleRateInHz = rateInHz
return b
}
func (b *builder) WithChannels(numberOfChannels int) ChromaprintBuilder {
b.channels = numberOfChannels
return b
}
func (b *builder) WithMaxFingerPrintLength(length int) ChromaprintBuilder {
b.maxFingerPrintLength = length
return b
}
func (b *builder) WithChunksSize(chunkSizeInSeconds int) ChromaprintBuilder {
b.chunkSizeInSeconds = chunkSizeInSeconds
return b
}
func (b *builder) WithAlgorithm(algorithm int) ChromaprintBuilder {
b.algorithm = algorithm
return b
}
func (b *builder) WithOverlap(overlap bool) ChromaprintBuilder {
b.overlap = overlap
return b
}
func (b *builder) Build() (*Chromaprint, error) {
if b.filePath == "" {
// on mac and linux file default name is "fpcalc"
// and on windows it is"fpcalc.exe"
chromaprintFilePath := fpcalcFileName
if runtime.GOOS == "windows" {
chromaprintFilePath = chromaprintFilePath + windowsExtension
}
folder, err := os.Getwd()
if err != nil {
return nil, err
}
b.filePath = filepath.Join(folder, chromaprintFilePath)
}
return &Chromaprint{
options: *b,
}, nil
}