-
Notifications
You must be signed in to change notification settings - Fork 4
/
audio.go
51 lines (42 loc) · 1012 Bytes
/
audio.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
package waveform
import (
"bytes"
"log"
"math"
"os"
"os/exec"
"strconv"
"strings"
)
const (
PIXEL_PER_SECOND float64 = 1000 / 30.0
)
func generateRawFile(sourcePath string, tempFilePath string) {
if _, err := os.Stat(sourcePath); os.IsNotExist(err) {
log.Fatalf("Source file does not exists: %s", sourcePath)
return
}
cmd := exec.Command("sox", sourcePath, "-t", "raw", "-r", "44100", "-c", "1", "-e", "signed-integer", "-L", tempFilePath)
var error bytes.Buffer
cmd.Stderr = &error
err := cmd.Run()
if err != nil {
log.Fatal(err.Error(), "\n", error.String())
return
}
}
func getWidth(sourcePath string) float64 {
return math.Ceil((getDuration(sourcePath) * 1000) / PIXEL_PER_SECOND)
}
func getDuration(sourcePath string) float64 {
cmd := exec.Command("soxi", "-D", sourcePath)
output, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
result, err := strconv.ParseFloat(strings.TrimSpace(string(output[0:])), 64)
if err != nil {
log.Fatal(err)
}
return result
}