-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.go
51 lines (39 loc) · 886 Bytes
/
sound.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
package main
// typedef unsigned char Uint8;
// void SineWave(void *userdata, Uint8 *stream, int len);
import "C"
import (
"io/ioutil"
"log"
"github.com/veandco/go-sdl2/mix"
)
// RunAudioHandler takes a bool channel, which turns audio on and off
func (chip *chip8) RunAudioHandler(audio <-chan bool) {
if err := mix.OpenAudio(44100, mix.DEFAULT_FORMAT, 2, 4096); err != nil {
log.Println(err)
return
}
// Load BEEP wav
data, err := ioutil.ReadFile("./assets/beep.wav")
if err != nil {
log.Println(err)
}
defer mix.CloseAudio()
activated := false
for {
shouldActivate := <-audio
if activated == shouldActivate {
continue
}
if shouldActivate {
// Load sound again, and go for it
chunk, err := mix.QuickLoadWAV(data)
if err != nil {
log.Println(err)
}
defer chunk.Free()
chunk.Play(1, 1)
}
activated = shouldActivate
}
}