-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (102 loc) · 3.33 KB
/
main.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
114
115
116
117
118
119
120
121
122
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/Try431/MIDI-part-splitter/midi"
)
// enabling line numbers in logging
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
binaryName := os.Args[0]
fileFlagPtr := flag.String("f", "", "Name of .mid file you wish to parse\n(e.g., '"+binaryName+" -f midi_file.mid')")
dirFlagPtr := flag.String("d", "", "Directory containing .mid files you wish to parse - will recursively search subdirectories\n(e.g., '"+binaryName+" -d ./dir/to/search/')")
instFlagPtr := flag.Int("inst", 65, "Instrument number for emphasized track - see README for instrument list\n(e.g., '"+binaryName+" -f midi_file.mid -inst 22) ")
volFlagPtr := flag.Int("vol", 40, "Volume of de-emphasized voice tracks - must be between 0 and 100\n(e.g., '"+binaryName+" -f midi_file.mid -vol 30)")
outFlagPtr := flag.String("o", "./"+midi.MIDIOutputDirectory+"/mp3s", "Directory where mp3 files will be stored\n(e.g., '"+binaryName+" -f midi_file.mid -o ./dir/to/store/mp3s)")
quietFlagPtr := flag.Bool("quiet", true, "Whether or not to silence standard output when running (will still allow stderr)")
listFlagPtr := flag.String("l", "", "List of comma-separated MIDI files to be parsed")
flag.Parse()
if !(isFlagPassed("f") || isFlagPassed("d") || isFlagPassed("l")) {
flag.Usage()
os.Exit(1)
}
if isFlagPassed("quiet") {
midi.SilenceOutput = bool(*quietFlagPtr)
}
if isFlagPassed("vol") {
midi.NonEmphasizedTrackVolume = uint8(*volFlagPtr)
}
if isFlagPassed("o") {
midi.MP3OutputDirectory = *outFlagPtr
}
if isFlagPassed("inst") {
midi.EmphasizedInstrumentNum = uint8(*instFlagPtr)
}
var filePaths []string
if isFlagPassed("f") {
filePath := filepath.Clean(*fileFlagPtr)
if !isMIDIFile(filePath) {
log.Fatal("File " + filePath + " not accepted - only MIDI files allowed")
}
filePaths = append(filePaths, filePath)
}
if isFlagPassed("d") {
files := grabFilesInDir(*dirFlagPtr)
filePaths = append(filePaths, files...)
}
if isFlagPassed("l") {
files := strings.Split(*listFlagPtr, ",")
for _, f := range files {
filePath := filepath.Clean(f)
if !isMIDIFile(filePath) {
log.Fatal("File " + filePath + " not accepted - only MIDI files allowed")
}
filePaths = append(filePaths, filePath)
}
}
fmt.Println("Starting split & conversion process...")
var wg sync.WaitGroup
wg.Add(len(filePaths))
for i := 0; i < len(filePaths); i++ {
fPath := filePaths[i]
midi.SplitParts(&wg, fPath)
}
wg.Wait()
fmt.Println("All done! 😄 Enjoy your MP3 files!")
}
// Determines if a flag was passed in
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func isMIDIFile(path string) bool {
extension := filepath.Ext(path)
return (extension == ".mid" || extension == ".midi")
}
// Walks through directory recursively and grabs all MIDI files
func grabFilesInDir(dirPath string) []string {
var files []string
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err == nil && isMIDIFile(info.Name()) {
filePath := filepath.Clean(path)
files = append(files, filePath)
}
return nil
})
if err != nil {
log.Fatalf("Failed to walk through dirPath %v with error: %v", dirPath, err)
}
return files
}