-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.go
More file actions
39 lines (29 loc) · 724 Bytes
/
audio.go
File metadata and controls
39 lines (29 loc) · 724 Bytes
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
package chimer
import (
"fmt"
"os"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
)
func LoadSound(path string) (*beep.Buffer, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open sound: %w", err)
}
streamer, format, err := mp3.Decode(f)
if err != nil {
_ = f.Close()
return nil, fmt.Errorf("failed to decode %s: %w", path, err)
}
defer func(streamer beep.StreamSeekCloser) {
_ = streamer.Close()
}(streamer)
buffer := beep.NewBuffer(format)
buffer.Append(streamer)
return buffer, nil
}
type AudioCache = Cache[string, *beep.Buffer]
type SoundLoader struct{}
func (a SoundLoader) Get(path string) (*beep.Buffer, error) {
return LoadSound(path)
}