-
Notifications
You must be signed in to change notification settings - Fork 0
/
sox.go
163 lines (149 loc) · 3.6 KB
/
sox.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sox
import (
"bytes"
"errors"
"fmt"
"log"
"os/exec"
"strconv"
"strings"
)
// Sox parent group for all functions in sox-go
type Sox struct {
}
// NewSox Create a new instance for sox
func NewSox() Sox {
return Sox{}
}
//File - return output file define
type File struct {
FilePath string
Duration float32
}
//Trim - is func for crop the file sound -
func (s Sox) Trim(file string, outputFile string, start float32, duration float32) (*File, error) {
cmd := exec.Command("sox", file, outputFile, "trim", fmt.Sprintf("%.2f", start), fmt.Sprintf("%.2f", duration))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
return &File{
FilePath: outputFile,
}, nil
}
//Repeat - is func for crop the file sound -
func (s Sox) Repeat(file string, outputFile string, repeat int) (*File, error) {
cmd := exec.Command("sox", file, outputFile, "repeat", fmt.Sprintf("%d", repeat))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
log.Printf("in all caps: %q\n", out.String())
return &File{
FilePath: outputFile,
}, nil
}
//Volume - is func for change volume
func (s Sox) Volume(file string, outputFile string, volume float32) (*File, error) {
cmd := exec.Command("sox", "-v", fmt.Sprintf("%f", volume), file, outputFile)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
log.Printf("in all caps: %q\n", out.String())
return &File{
FilePath: outputFile,
}, nil
}
func (s Sox) Info(file string) (*File, error) {
cmdDuration := exec.Command("sox", "--info", "-D", file)
var out bytes.Buffer
cmdDuration.Stdout = &out
err := cmdDuration.Run()
if err != nil {
return nil, err
}
duration, err := strconv.ParseFloat(strings.ReplaceAll(out.String(), "\n", ""), 32)
if err != nil {
return nil, err
}
return &File{
FilePath: file,
Duration: float32(duration),
}, nil
}
func (s Sox) Join(files []string, outputFile string, mix bool) (*File, error) {
commandCobine := "-M"
if mix {
commandCobine = "-m"
}
params := []string{commandCobine}
for _, file := range files {
params = append(params, file)
}
params = append(params, outputFile)
cmd := exec.Command("sox", params...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
return &File{
FilePath: outputFile,
}, nil
}
func (s Sox) Combine(files []string, outputFile string) (*File, error) {
params := []string{}
for _, file := range files {
params = append(params, file)
}
params = append(params, outputFile)
cmd := exec.Command("sox", params...)
log.Printf("ref %s", cmd.String())
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
return &File{
FilePath: outputFile,
}, nil
}
func (s Sox) Fade(file string, outputFile string, start float32, stop float32, typeFade string) (*File, error) {
if typeFade != "in" && typeFade != "out" {
return nil, errors.New("invalid type")
}
params := []string{}
params = append(params, file)
params = append(params, outputFile)
params = append(params, "fade")
switch typeFade {
case "in":
params = append(params, fmt.Sprintf("%f", start))
params = append(params, fmt.Sprintf("%f", stop))
params = append(params, "0")
case "out":
params = append(params, "0")
params = append(params, fmt.Sprintf("%f", stop))
params = append(params, fmt.Sprintf("%f", start))
}
cmd := exec.Command("sox", params...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
log.Printf("ref %s", cmd.String())
if err != nil {
return nil, err
}
return &File{
FilePath: outputFile,
}, nil
}