-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathaudio.go
57 lines (47 loc) · 939 Bytes
/
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
package main
import (
"fmt"
"github.com/scottferg/Go-SDL/sdl"
sdl_audio "github.com/scottferg/Go-SDL/sdl/audio"
"log"
"sync"
)
var (
SampleSize = 2048
as sdl_audio.AudioSpec
)
type Audio struct {
samples []int16
sampleIndex int
mutex sync.Mutex
}
func NewAudio() *Audio {
as = sdl_audio.AudioSpec{
Freq: 44100,
Format: sdl_audio.AUDIO_S16SYS,
Channels: 1,
Out_Silence: 0,
Samples: uint16(SampleSize),
Out_Size: 0,
}
if sdl_audio.OpenAudio(&as, nil) < 0 {
log.Fatal(sdl.GetError())
}
sdl_audio.PauseAudio(false)
return &Audio{
samples: make([]int16, SampleSize),
}
}
func (a *Audio) AppendSample(s int16) {
a.samples[a.sampleIndex] = s
a.sampleIndex++
if a.sampleIndex == SampleSize {
sdl_audio.SendAudio_int16(a.samples)
a.sampleIndex = 0
}
}
func (a *Audio) Close() {
fmt.Println("Closing!")
sdl_audio.PauseAudio(true)
sdl_audio.CloseAudio()
}