forked from mdlayher/waveform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveform_example_test.go
More file actions
55 lines (50 loc) · 1.32 KB
/
waveform_example_test.go
File metadata and controls
55 lines (50 loc) · 1.32 KB
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
package waveform
import (
"bytes"
"fmt"
"image/color"
"image/png"
"os"
)
// ExampleGenerate provides example usage of Generate, using a media file from the filesystem.
// Generate is typically used for one-time, direct creation of an image.Image from
// an input audio stream.
func ExampleGenerate() {
// Generate accepts io.Reader, so we will use a media file in the filesystem
file, err := os.Open("./test/tone16bit.flac")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("open:", file.Name())
defer file.Close()
// Directly generate waveform image from audio file, applying any number
// of options functions along the way
img, err := Generate(file,
// Solid white background
BGColorFunction(SolidColor(color.White)),
// Striped red, green, and blue foreground
FGColorFunction(StripeColor(
color.RGBA{255, 0, 0, 255},
color.RGBA{0, 255, 0, 255},
color.RGBA{0, 0, 255, 255},
)),
// Scaled 10x horizontally, 2x vertically
Scale(10, 2),
)
if err != nil {
fmt.Println(err)
return
}
// Encode image as PNG into buffer
buf := bytes.NewBuffer(nil)
if err := png.Encode(buf, img); err != nil {
fmt.Println(err)
return
}
fmt.Printf("encoded: %d bytes\nresolution: %s", buf.Len(), img.Bounds().Max)
// Output:
// open: ./test/tone16bit.flac
// encoded: 344 bytes
// resolution: (50,256)
}