-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathaudio.go
99 lines (81 loc) · 1.76 KB
/
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
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
92
93
94
95
96
97
98
99
package pacman
import (
"io"
"io/ioutil"
"github.com/hajimehoshi/ebiten/audio"
"github.com/skatiyar/pacman/assets"
)
type Audio struct {
ctx *audio.Context
players *AudioPlayers
}
const (
// This sample rate doesn't match with wav/vorbis's sample rate,
// but decoders adjust them.
sampleRate = 48000
)
func NewAudio() (*Audio, error) {
audioCtx, err := audio.NewContext(sampleRate)
if err != nil {
return nil, err
}
sounds, err := assets.LoadSounds(audioCtx)
if err != nil {
return nil, err
}
beginning, err := newAudioPlayer(audioCtx, sounds.Beginning)
if err != nil {
return nil, err
}
chomp, err := newAudioPlayer(audioCtx, sounds.Chomp)
if err != nil {
return nil, err
}
death, err := newAudioPlayer(audioCtx, sounds.Death)
if err != nil {
return nil, err
}
eatFlask, err := newAudioPlayer(audioCtx, sounds.EatFlask)
if err != nil {
return nil, err
}
eatGhost, err := newAudioPlayer(audioCtx, sounds.EatGhost)
if err != nil {
return nil, err
}
extraPac, err := newAudioPlayer(audioCtx, sounds.ExtraPac)
if err != nil {
return nil, err
}
return &Audio{
ctx: audioCtx,
players: &AudioPlayers{
Beginning: beginning,
Chomp: chomp,
Death: death,
EatFlask: eatFlask,
EatGhost: eatGhost,
ExtraPac: extraPac,
},
}, nil
}
type AudioPlayers struct {
Beginning *audio.Player
Chomp *audio.Player
Death *audio.Player
EatFlask *audio.Player
EatGhost *audio.Player
ExtraPac *audio.Player
}
func newAudioPlayer(ctx *audio.Context, src io.ReadCloser) (*audio.Player, error) {
buffer, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
player, err := audio.NewPlayerFromBytes(ctx, buffer)
if err != nil {
return nil, err
}
player.SetVolume(0.3)
return player, nil
}