-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagger_parallel.go
99 lines (83 loc) · 2.54 KB
/
tagger_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
94
95
96
97
98
99
package acousticid
import (
"path/filepath"
"os"
"strings"
)
var mp3FileCount = 0
var fingerPrintInputChan = make(chan FingerprintInput, 100)
var acousticIdInputChan = make(chan Acousticidinput, 100)
var id3TagInputChan = make(chan ID3TagInput, 100)
var processedChan = make(chan bool, 100)
func TagDirParallel(start_dir string) {
//Setup fingerprint worker
for w := 0; w < 3 ; w ++ {
go FingerprintWorker(w)
}
//Setup acousticid worker
for w := 0; w < 3 ; w ++ {
go AcousticidWorker(w)
}
//Setup ID3 worker
for w := 0; w < 3 ; w ++ {
go ID3Worker(w)
}
//Count the number of files to tag:
filepath.Walk(start_dir, countMp3Files)
log.Info("Need to tag %d mp3 files..", mp3FileCount)
//Send the files to be fingerprinted
filepath.Walk(start_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") {
log.Info("Sending file " + file + " for fingerprinting" )
fingerPrintInputChan <- FingerprintInput{file, info}
}
return nil
}
func FingerprintWorker(id int) {
log.Info("Initializing FingerprintWorker...")
for fingerprintInput := range fingerPrintInputChan {
log.Info("[ Worker: %d - Fingerprinting %s ]", id, fingerprintInput.file)
acousticIdInputChan <- Acousticidinput{fingerprintInput.file, fingerprintInput.info, GetFingerprint(fingerprintInput.file)}
}
log.Info("FingerprintWorker closed!")
}
func AcousticidWorker(id int) {
for acousticidInput := range acousticIdInputChan {
log.Info("[ Worker: %d - Finding acoustic id of file with duration %s ]", id, acousticidInput.fingerprint.duration)
id3TagInputChan <- ID3TagInput{acousticidInput.file, acousticidInput.info, GetAcousticId(acousticidInput.fingerprint)}
}
}
func ID3Worker(id int) {
for id3TagInput := range id3TagInputChan {
log.Info("[ Worker: %d - Setting ID3 Tag %s ]", id, id3TagInput.file)
SetID3(id3TagInput.acousticidresponse, 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
acousticidresponse AcousticidResponse
}