-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel.go
93 lines (76 loc) · 1.99 KB
/
parallel.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
package acoustid
import (
"os"
"path/filepath"
"runtime"
"strings"
)
var mp3FileCount = 0
var (
fingerPrintInputChan = make(chan FingerprintInput, 100)
acousticIdInputChan = make(chan Acousticidinput, 100)
id3TagInputChan = make(chan ID3TagInput, 100)
processedChan = make(chan bool, 100)
)
var numCores = 0
func init() {
numCores = runtime.NumCPU()
}
func TagDirParallel(dir string) {
for i := 0; i < numCores; i++ {
go FingerprintWorker()
go AcousticidWorker()
go ID3Worker()
}
//Count the number of files to tag:
filepath.Walk(dir, countMp3Files)
//Send the files to be fingerprinted
filepath.Walk(dir, tagFileParallel)
//Ensure all files have been processed
for i := 0; i < mp3FileCount; i++ {
<-processedChan
}
}
func countMp3Files(file string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && strings.HasSuffix(info.Name(), ".mp3") {
mp3FileCount++
}
return nil
}
func tagFileParallel(file string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && strings.HasSuffix(info.Name(), ".mp3") {
fingerPrintInputChan <- FingerprintInput{file, info}
}
return nil
}
func FingerprintWorker() {
for fingerprintInput := range fingerPrintInputChan {
acousticIdInputChan <- Acousticidinput{fingerprintInput.file, fingerprintInput.info, NewFingerprint(fingerprintInput.file)}
}
}
func AcousticidWorker() {
for acousticidInput := range acousticIdInputChan {
id3TagInputChan <- ID3TagInput{acousticidInput.file, acousticidInput.info, MakeAcoustIDRequest(acousticidInput.fingerprint)}
}
}
func ID3Worker() {
for id3TagInput := range id3TagInputChan {
SetID3(id3TagInput.resp, id3TagInput.file, id3TagInput.info)
processedChan <- true
}
}
//Structs to pass around the channels
type FingerprintInput struct {
file string
info os.FileInfo
}
type Acousticidinput struct {
file string
info os.FileInfo
fingerprint Fingerprint
}
type ID3TagInput struct {
file string
info os.FileInfo
resp AcoustIDResponse
}