forked from timshannon/go-openal
-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.go
152 lines (119 loc) · 2.79 KB
/
example.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/binary"
"fmt"
"github.com/vova616/go-openal/openal"
"math"
"os"
"time"
)
type Format struct {
FormatTag int16
Channels int16
Samples int32
AvgBytes int32
BlockAlign int16
BitsPerSample int16
}
type Format2 struct {
Format
SizeOfExtension int16
}
type Format3 struct {
Format2
ValidBitsPerSample int16
ChannelMask int32
SubFormat [16]byte
}
func ReadWavFile(path string) (*Format, []byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, nil, err
}
defer f.Close()
var buff [4]byte
f.Read(buff[:4])
if string(buff[:4]) != "RIFF" {
return nil, nil, fmt.Errorf("Not a WAV file.\n")
}
var size int32
binary.Read(f, binary.LittleEndian, &size)
f.Read(buff[:4])
if string(buff[:4]) != "WAVE" {
return nil, nil, fmt.Errorf("Not a WAV file.\n")
}
f.Read(buff[:4])
if string(buff[:4]) != "fmt " {
return nil, nil, fmt.Errorf("Not a WAV file.\n")
}
binary.Read(f, binary.LittleEndian, &size)
var format Format
switch size {
case 16:
binary.Read(f, binary.LittleEndian, &format)
case 18:
var f2 Format2
binary.Read(f, binary.LittleEndian, &f2)
format = f2.Format
case 40:
var f3 Format3
binary.Read(f, binary.LittleEndian, &f3)
format = f3.Format
}
fmt.Println(format)
f.Read(buff[:4])
if string(buff[:4]) != "data" {
return nil, nil, fmt.Errorf("Not supported WAV file.\n")
}
binary.Read(f, binary.LittleEndian, &size)
wavData := make([]byte, size)
n, e := f.Read(wavData)
if e != nil {
return nil, nil, fmt.Errorf("Cannot read WAV data.\n")
}
if int32(n) != size {
return nil, nil, fmt.Errorf("WAV data size doesnt match.\n")
}
return &format, wavData, nil
}
func Period(freq int, samples int) float64 {
return float64(freq) * 2 * math.Pi * (1 / float64(samples))
}
func TimeToData(t time.Duration, samples int, channels int) int {
return int((float64(samples)/(1/t.Seconds()))+0.5) * channels
}
func main() {
device := openal.OpenDevice("")
context := device.CreateContext()
context.Activate()
//listener := new(openal.Listener)
//listener.
source := openal.NewSource()
source.SetPitch(1)
source.SetGain(1)
source.SetPosition(0, 0, 0)
source.SetVelocity(0, 0, 0)
source.SetLooping(false)
buffer := openal.NewBuffer()
format, data, err := ReadWavFile("welcome.wav")
if err != nil {
panic(err)
}
switch format.Channels {
case 1:
buffer.SetData(openal.FormatMono16, data[:len(data)], int32(format.Samples))
case 2:
buffer.SetData(openal.FormatStereo16, data[:len(data)], int32(format.Samples))
}
source.SetBuffer(buffer)
source.Play()
for source.State() == openal.Playing {
//loop long enough to let the wave file finish
}
fmt.Println(source.State())
source.Pause()
source.Stop()
return
context.Destroy()
time.Sleep(time.Second)
}