Skip to content

Commit 881bbb6

Browse files
neurlangYour Name
authored and
Your Name
committed
add flac support
1 parent 6d431ae commit 881bbb6

File tree

5 files changed

+102
-15
lines changed

5 files changed

+102
-15
lines changed

cmd/tomel/main.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"localhost/gomel/mel" // make sure this is the correct import path for your `libmel` package
6+
"github.com/neurlang/gomel/mel"
7+
"strings"
78
)
89

910
func main() {
@@ -27,12 +28,32 @@ func main() {
2728
m.Window = 1024
2829
m.Resolut = 8192
2930

30-
// Generate the mel spectrogram and save it as a PNG file
31-
inputFile := filename + ".wav"
32-
outputFile := filename + ".png"
33-
err := m.ToMelWav(inputFile, outputFile)
34-
if err != nil {
35-
fmt.Printf("Error generating mel spectrogram: %v\n", err)
36-
os.Exit(1)
31+
if strings.HasSuffix(filename, ".flac") {
32+
// Generate the mel spectrogram and save it as a PNG file
33+
inputFile := filename
34+
outputFile := filename + ".png"
35+
err := m.ToMelFlac(inputFile, outputFile)
36+
if err != nil {
37+
fmt.Printf("Error generating mel spectrogram: %v\n", err)
38+
os.Exit(1)
39+
}
40+
} else if strings.HasSuffix(filename, ".wav") {
41+
// Generate the mel spectrogram and save it as a PNG file
42+
inputFile := filename
43+
outputFile := filename + ".png"
44+
err := m.ToMelWav(inputFile, outputFile)
45+
if err != nil {
46+
fmt.Printf("Error generating mel spectrogram: %v\n", err)
47+
os.Exit(1)
48+
}
49+
} else {
50+
// Generate the mel spectrogram and save it as a PNG file
51+
inputFile := filename + ".wav"
52+
outputFile := filename + ".png"
53+
err := m.ToMelWav(inputFile, outputFile)
54+
if err != nil {
55+
fmt.Printf("Error generating mel spectrogram: %v\n", err)
56+
os.Exit(1)
57+
}
3758
}
3859
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module localhost/gomel
1+
module github.com/neurlang/gomel
22

33
go 1.13
44

@@ -8,6 +8,7 @@ require (
88
)
99

1010
require (
11+
github.com/mewkiz/flac v1.0.7
1112
github.com/mjibson/go-dsp v0.0.0-20180508042940-11479a337f12 // indirect
1213
github.com/pkg/errors v0.9.1 // indirect
1314
github.com/r9y9/go-dsp v0.0.0-20140323125827-6ccdfeca7723 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5
1010
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
1111
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
1212
github.com/hajimehoshi/oto v0.7.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos=
13+
github.com/icza/bitio v1.0.0 h1:squ/m1SHyFeCA6+6Gyol1AxV9nmPPlJFT8c2vKdj3U8=
1314
github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
1415
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
1516
github.com/jfreymuth/oggvorbis v1.0.1/go.mod h1:NqS+K+UXKje0FUYUPosyQ+XTVvjmVjps1aEZH1sumIk=
1617
github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0=
1718
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
1819
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
20+
github.com/mewkiz/flac v1.0.7 h1:uIXEjnuXqdRaZttmSFM5v5Ukp4U6orrZsnYGGR3yow8=
1921
github.com/mewkiz/flac v1.0.7/go.mod h1:yU74UH277dBUpqxPouHSQIar3G1X/QIclVbFahSd1pU=
22+
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2 h1:EyTNMdePWaoWsRSGQnXiSoQu0r6RS1eA557AwJhlzHU=
2023
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8+r9zAwpeHJzqRVVMIYnpzD/clwWxyA=
2124
github.com/mjibson/go-dsp v0.0.0-20180508042940-11479a337f12 h1:dd7vnTDfjtwCETZDrRe+GPYNLA1jBtbZeyfyE8eZCyk=
2225
github.com/mjibson/go-dsp v0.0.0-20180508042940-11479a337f12/go.mod h1:i/KKcxEWEO8Yyl11DYafRPKOPVYTrhxiTRigjtEEXZU=

mel/impl.go

+30
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package libmel
33
import "image"
44
import "image/png"
55
import "os"
6+
import "io"
67
import "image/color"
78
import "github.com/faiface/beep/wav"
9+
import "github.com/mewkiz/flac"
810
import "math"
911

1012
func dumpimage(name string, buf [][2]float64, mels int, reverse bool) error {
@@ -87,7 +89,35 @@ func loadwav(name string) (out []float64) {
8789
out = append(out, samples[i][0])
8890
}
8991
}
92+
return
93+
}
94+
95+
func loadflac(name string) (out []float64) {
96+
// Open love.flac for audio streaming without parsing metadata.
97+
stream, err := flac.Open(name)
98+
if err != nil {
99+
println(err.Error())
100+
return nil
101+
}
102+
defer stream.Close()
90103

104+
for {
105+
// Parse one frame of audio samples at the time, each frame containing one
106+
// subframe per audio channel.
107+
frame, err := stream.ParseNext()
108+
if err != nil {
109+
if err == io.EOF {
110+
break
111+
}
112+
}
113+
114+
for _, subframe := range frame.Subframes {
115+
for _, sample := range subframe.Samples {
116+
out = append(out, float64(sample)/(256*256))
117+
}
118+
//break
119+
}
120+
}
91121
return
92122
}
93123

mel/mel.go

+38-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ func NewMel() *Mel {
3131

3232
var ErrFileNotLoaded = errors.New("wavNotLoaded")
3333

34-
// ToMel generates a mel spectrogram from an input audio file and saves it as a PNG image.
35-
func (m *Mel) ToMelWav(inputFile, outputFile string) error {
34+
// ToMel generates a mel spectrogram from a wave buffer and returns the mel buffer.
35+
func (m *Mel) ToMel(buf []float64) ([][2]float64, error) {
3636

37-
var buf = loadwav(inputFile)
38-
if len(buf) == 0 {
39-
return ErrFileNotLoaded
40-
}
4137

4238
buf = pad(buf, m.Window)
4339

@@ -67,6 +63,42 @@ func (m *Mel) ToMelWav(inputFile, outputFile string) error {
6763
ospectrum = domel(m.Resolut/2, m.NumMels, ospectrum, m.MelFmin, m.MelFmax)
6864

6965
spectral_normalize(ospectrum)
66+
67+
return ospectrum, nil
68+
69+
}
70+
71+
// ToMel generates a mel spectrogram from an input FLAC audio file and saves it as a PNG image.
72+
func (m *Mel) ToMelFlac(inputFile, outputFile string) error {
73+
74+
var buf = loadflac(inputFile)
75+
if len(buf) == 0 {
76+
return ErrFileNotLoaded
77+
}
78+
79+
ospectrum, err := m.ToMel(buf)
80+
if err != nil {
81+
return err
82+
}
83+
84+
dumpimage(outputFile, ospectrum, m.NumMels, m.YReverse)
85+
86+
return nil
87+
}
88+
89+
// ToMel generates a mel spectrogram from an input WAV audio file and saves it as a PNG image.
90+
func (m *Mel) ToMelWav(inputFile, outputFile string) error {
91+
92+
var buf = loadwav(inputFile)
93+
if len(buf) == 0 {
94+
return ErrFileNotLoaded
95+
}
96+
97+
ospectrum, err := m.ToMel(buf)
98+
if err != nil {
99+
return err
100+
}
101+
70102
dumpimage(outputFile, ospectrum, m.NumMels, m.YReverse)
71103

72104
return nil

0 commit comments

Comments
 (0)