-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
91 lines (70 loc) · 1.71 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
package main
import (
"encoding/binary"
"fmt"
"log"
"math"
"os"
"path/filepath"
"github.com/hopesea/godub"
)
func main() {
p := os.Args[1]
d := os.Args[2]
fmt.Println("Open: ", p)
fmt.Println("Dir: ", d)
aMono, err := godub.NewLoader().Load(p)
if err != nil {
log.Fatalf("%v", err)
}
aData := aMono.RawData()
aDataLen := len(aData)
aDurationSecs := aMono.Duration().Seconds()
fmt.Printf("%s\n", aMono.String())
aBandList := make([]float64, 0)
bands := 220
BAND_STEP := aDataLen / bands
fmt.Printf("aDataLen %d\n", aDataLen)
fmt.Printf("BAND_STEP %d\n", BAND_STEP)
fmt.Println("Analyzing audio..")
for i := range bands {
aBand := aData[i*BAND_STEP : (i+1)*BAND_STEP]
// fmt.Println(aBand)
aBandInfo := ABandInfo{}
for _, sample := range aBand {
absSample := math.Abs(float64(sample))
aBandInfo.max = math.Max(float64(absSample), float64(aBandInfo.max))
aBandInfo.avg = (absSample + aBandInfo.avg) / 2
}
// writeVal := aBandInfo.avg
writeVal := (aBandInfo.max - aBandInfo.avg) / 2
fmt.Println(i, writeVal)
aBandList = append(aBandList, writeVal)
}
aBandList = append(aBandList, aDurationSecs)
filename := fmt.Sprintf("%s/%s.avg%d.bin", d, filepath.Base(p), bands)
fmt.Println("Writing into: ", filename)
err = os.Remove(filename)
if err != nil {
fmt.Printf("%v\n", err)
}
fileOutput, err := os.Create(filename)
if err != nil {
log.Fatalf("%w", err)
}
defer fileOutput.Close()
var b = make([]byte, 2)
for _, ii := range aBandList {
binary.LittleEndian.PutUint16(b, uint16(ii))
_, err := fileOutput.Write(b)
if err != nil {
fmt.Println("err!", err)
}
// fmt.Printf("%x ", b)
b[0], b[1] = 0, 0
}
}
type ABandInfo struct {
max float64
avg float64
}