forked from gen2brain/raylib-go
/
raudio.go
336 lines (285 loc) · 8.64 KB
/
raudio.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// +build !noaudio
package rl
/*
//#include "external/stb_vorbis.c"
#include "raylib.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "reflect"
// cptr returns C pointer
func (w *Wave) cptr() *C.Wave {
return (*C.Wave)(unsafe.Pointer(w))
}
func (s *Sound) cptr() *C.Sound {
return (*C.Sound)(unsafe.Pointer(s))
}
// cptr returns C pointer
func (a *AudioStream) cptr() *C.AudioStream {
return (*C.AudioStream)(unsafe.Pointer(a))
}
// InitAudioDevice - Initialize audio device and context
func InitAudioDevice() {
C.InitAudioDevice()
}
// CloseAudioDevice - Close the audio device and context
func CloseAudioDevice() {
C.CloseAudioDevice()
}
// IsAudioDeviceReady - Check if audio device has been initialized successfully
func IsAudioDeviceReady() bool {
ret := C.IsAudioDeviceReady()
v := bool(ret)
return v
}
// SetMasterVolume - Set master volume (listener)
func SetMasterVolume(volume float32) {
cvolume := (C.float)(volume)
C.SetMasterVolume(cvolume)
}
// LoadWave - Load wave data from file into RAM
func LoadWave(fileName string) Wave {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ret := C.LoadWave(cfileName)
v := newWaveFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadSound - Load sound to memory
func LoadSound(fileName string) Sound {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ret := C.LoadSound(cfileName)
v := newSoundFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadSoundFromWave - Load sound to memory from wave data
func LoadSoundFromWave(wave Wave) Sound {
cwave := wave.cptr()
ret := C.LoadSoundFromWave(*cwave)
v := newSoundFromPointer(unsafe.Pointer(&ret))
return v
}
// UpdateSound - Update sound buffer with new data
func UpdateSound(sound Sound, data []byte, samplesCount int32) {
csound := sound.cptr()
cdata := unsafe.Pointer(&data[0])
csamplesCount := (C.int)(samplesCount)
C.UpdateSound(*csound, cdata, csamplesCount)
}
// UnloadWave - Unload wave data
func UnloadWave(wave Wave) {
cwave := wave.cptr()
C.UnloadWave(*cwave)
}
// UnloadSound - Unload sound
func UnloadSound(sound Sound) {
csound := sound.cptr()
C.UnloadSound(*csound)
}
// ExportWave - Export wave data to file
func ExportWave(wave Wave, fileName string) {
cwave := wave.cptr()
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
C.ExportWave(*cwave, cfileName)
}
// PlaySound - Play a sound
func PlaySound(sound Sound) {
csound := sound.cptr()
C.PlaySound(*csound)
}
// PauseSound - Pause a sound
func PauseSound(sound Sound) {
csound := sound.cptr()
C.PauseSound(*csound)
}
// ResumeSound - Resume a paused sound
func ResumeSound(sound Sound) {
csound := sound.cptr()
C.ResumeSound(*csound)
}
// StopSound - Stop playing a sound
func StopSound(sound Sound) {
csound := sound.cptr()
C.StopSound(*csound)
}
// IsSoundPlaying - Check if a sound is currently playing
func IsSoundPlaying(sound Sound) bool {
csound := sound.cptr()
ret := C.IsSoundPlaying(*csound)
v := bool(ret)
return v
}
// SetSoundVolume - Set volume for a sound (1.0 is max level)
func SetSoundVolume(sound Sound, volume float32) {
csound := sound.cptr()
cvolume := (C.float)(volume)
C.SetSoundVolume(*csound, cvolume)
}
// SetSoundPitch - Set pitch for a sound (1.0 is base level)
func SetSoundPitch(sound Sound, pitch float32) {
csound := sound.cptr()
cpitch := (C.float)(pitch)
C.SetSoundPitch(*csound, cpitch)
}
// WaveFormat - Convert wave data to desired format
func WaveFormat(wave Wave, sampleRate int32, sampleSize int32, channels int32) {
cwave := wave.cptr()
csampleRate := (C.int)(sampleRate)
csampleSize := (C.int)(sampleSize)
cchannels := (C.int)(channels)
C.WaveFormat(cwave, csampleRate, csampleSize, cchannels)
}
// WaveCopy - Copy a wave to a new wave
func WaveCopy(wave Wave) Wave {
cwave := wave.cptr()
ret := C.WaveCopy(*cwave)
v := newWaveFromPointer(unsafe.Pointer(&ret))
return v
}
// WaveCrop - Crop a wave to defined samples range
func WaveCrop(wave Wave, initSample int32, finalSample int32) {
cwave := wave.cptr()
cinitSample := (C.int)(initSample)
cfinalSample := (C.int)(finalSample)
C.WaveCrop(cwave, cinitSample, cfinalSample)
}
// GetWaveData - Get samples data from wave as a floats array
func GetWaveData(wave Wave) []float32 {
var data []float32
cwave := wave.cptr()
ret := C.GetWaveData(*cwave)
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&data)))
sliceHeader.Cap = int(wave.SampleCount)
sliceHeader.Len = int(wave.SampleCount)
sliceHeader.Data = uintptr(unsafe.Pointer(ret))
return data
}
// LoadMusicStream - Load music stream from file
func LoadMusicStream(fileName string) Music {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ret := C.LoadMusicStream(cfileName)
v := *(*Music)(unsafe.Pointer(&ret))
return v
}
// UnloadMusicStream - Unload music stream
func UnloadMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.UnloadMusicStream(cmusic)
}
// PlayMusicStream - Start music playing
func PlayMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.PlayMusicStream(cmusic)
}
// UpdateMusicStream - Updates buffers for music streaming
func UpdateMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.UpdateMusicStream(cmusic)
}
// StopMusicStream - Stop music playing
func StopMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.StopMusicStream(cmusic)
}
// PauseMusicStream - Pause music playing
func PauseMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.PauseMusicStream(cmusic)
}
// ResumeMusicStream - Resume playing paused music
func ResumeMusicStream(music Music) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
C.ResumeMusicStream(cmusic)
}
// IsMusicPlaying - Check if music is playing
func IsMusicPlaying(music Music) bool {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
ret := C.IsMusicPlaying(cmusic)
v := bool(ret)
return v
}
// SetMusicVolume - Set volume for music (1.0 is max level)
func SetMusicVolume(music Music, volume float32) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
cvolume := (C.float)(volume)
C.SetMusicVolume(cmusic, cvolume)
}
// SetMusicPitch - Set pitch for a music (1.0 is base level)
func SetMusicPitch(music Music, pitch float32) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
cpitch := (C.float)(pitch)
C.SetMusicPitch(cmusic, cpitch)
}
// SetMusicLoopCount - Set music loop count (loop repeats)
// NOTE: If set to -1, means infinite loop
func SetMusicLoopCount(music Music, count int32) {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
ccount := (C.int)(count)
C.SetMusicLoopCount(cmusic, ccount)
}
// GetMusicTimeLength - Get music time length (in seconds)
func GetMusicTimeLength(music Music) float32 {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
ret := C.GetMusicTimeLength(cmusic)
v := (float32)(ret)
return v
}
// GetMusicTimePlayed - Get current music time played (in seconds)
func GetMusicTimePlayed(music Music) float32 {
cmusic := *(*C.Music)(unsafe.Pointer(&music))
ret := C.GetMusicTimePlayed(cmusic)
v := (float32)(ret)
return v
}
// InitAudioStream - Init audio stream (to stream raw audio pcm data)
func InitAudioStream(sampleRate uint32, sampleSize uint32, channels uint32) AudioStream {
csampleRate := (C.uint)(sampleRate)
csampleSize := (C.uint)(sampleSize)
cchannels := (C.uint)(channels)
ret := C.InitAudioStream(csampleRate, csampleSize, cchannels)
v := newAudioStreamFromPointer(unsafe.Pointer(&ret))
return v
}
// UpdateAudioStream - Update audio stream buffers with data
func UpdateAudioStream(stream AudioStream, data []float32, samplesCount int32) {
cstream := stream.cptr()
cdata := unsafe.Pointer(&data[0])
csamplesCount := (C.int)(samplesCount)
C.UpdateAudioStream(*cstream, cdata, csamplesCount)
}
// CloseAudioStream - Close audio stream and free memory
func CloseAudioStream(stream AudioStream) {
cstream := stream.cptr()
C.CloseAudioStream(*cstream)
}
// IsAudioStreamProcessed - Check if any audio stream buffers requires refill
func IsAudioStreamProcessed(stream AudioStream) bool {
cstream := stream.cptr()
ret := C.IsAudioStreamProcessed(*cstream)
v := bool(ret)
return v
}
// PlayAudioStream - Play audio stream
func PlayAudioStream(stream AudioStream) {
cstream := stream.cptr()
C.PlayAudioStream(*cstream)
}
// PauseAudioStream - Pause audio stream
func PauseAudioStream(stream AudioStream) {
cstream := stream.cptr()
C.PauseAudioStream(*cstream)
}
// ResumeAudioStream - Resume audio stream
func ResumeAudioStream(stream AudioStream) {
cstream := stream.cptr()
C.ResumeAudioStream(*cstream)
}
// StopAudioStream - Stop audio stream
func StopAudioStream(stream AudioStream) {
cstream := stream.cptr()
C.StopAudioStream(*cstream)
}