| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Go Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 // +build darwin | |
| 6 | |
| 7 // Package al provides OpenAL Soft bindings for Go. | |
| 8 // | |
| 9 // More information about OpenAL Soft is available at | |
| 10 // http://www.openal.org/documentation/openal-1.1-specification.pdf. | |
| 11 package al | |
| 12 | |
| 13 /* | |
| 14 // TODO(jbd,crawshaw): Add android, linux and windows support. | |
| 15 | |
| 16 #cgo darwin CFLAGS: -DGOOS_darwin | |
| 17 #cgo darwin LDFLAGS: -framework OpenAL | |
| 18 | |
| 19 #ifdef GOOS_darwin | |
| 20 #include <stdlib.h> | |
| 21 #include <OpenAL/al.h> | |
| 22 #endif | |
| 23 | |
| 24 */ | |
| 25 import "C" | |
| 26 import "unsafe" | |
| 27 | |
| 28 // Enable enables a capability. | |
| 29 func Enable(capability int32) { | |
| 30 C.alEnable(C.ALenum(capability)) | |
| 31 } | |
| 32 | |
| 33 // Disable disables a capability. | |
| 34 func Disable(capability int32) { | |
| 35 C.alDisable(C.ALenum(capability)) | |
| 36 } | |
| 37 | |
| 38 // Enabled returns true if the specified capability is enabled. | |
| 39 func Enabled(capability int32) bool { | |
| 40 return C.alIsEnabled(C.ALenum(capability)) == 1 | |
| 41 } | |
| 42 | |
| 43 // Vector represents an vector in a Cartesian coordinate system. | |
| 44 type Vector [3]float32 | |
| 45 | |
| 46 // Orientation represents the angular position of an object in a | |
| 47 // right-handed Cartesian coordinate system. | |
| 48 // A cross product between the forward and up vector returns a vector | |
| 49 // that points to the right. | |
| 50 type Orientation struct { | |
| 51 // Forward vector is the direction that the object is looking at. | |
| 52 Forward Vector | |
| 53 // Up vector represents the rotation of the object. | |
| 54 Up Vector | |
| 55 } | |
| 56 | |
| 57 func orientationFromSlice(v []float32) Orientation { | |
| 58 return Orientation{ | |
| 59 Forward: Vector{v[0], v[1], v[2]}, | |
| 60 Up: Vector{v[3], v[4], v[5]}, | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 func (v Orientation) slice() []float32 { | |
| 65 return []float32{v.Forward[0], v.Forward[1], v.Forward[2], v.Up[0], v.Up
[1], v.Up[2]} | |
| 66 } | |
| 67 | |
| 68 func geti(param int) int32 { | |
| 69 return int32(C.alGetInteger(C.ALenum(param))) | |
| 70 } | |
| 71 | |
| 72 func getf(param int) float32 { | |
| 73 return float32(C.alGetFloat(C.ALenum(param))) | |
| 74 } | |
| 75 | |
| 76 func getString(param int) string { | |
| 77 v := C.alGetString(C.ALenum(param)) | |
| 78 return C.GoString((*C.char)(v)) | |
| 79 } | |
| 80 | |
| 81 // DistanceModel returns the distance model. | |
| 82 func DistanceModel() int32 { | |
| 83 return geti(paramDistanceModel) | |
| 84 } | |
| 85 | |
| 86 // SetDistanceModel sets the distance model. | |
| 87 func SetDistanceModel(v int32) { | |
| 88 C.alDistanceModel(C.ALenum(v)) | |
| 89 } | |
| 90 | |
| 91 // DopplerFactor returns the doppler factor. | |
| 92 func DopplerFactor() float32 { | |
| 93 return getf(paramDopplerFactor) | |
| 94 } | |
| 95 | |
| 96 // SetDopplerFactor sets the doppler factor. | |
| 97 func SetDopplerFactor(v float32) { | |
| 98 C.alDopplerFactor(C.ALfloat(v)) | |
| 99 } | |
| 100 | |
| 101 // DopplerVelocity returns the doppler velocity. | |
| 102 func DopplerVelocity() float32 { | |
| 103 return getf(paramDopplerVelocity) | |
| 104 } | |
| 105 | |
| 106 // SetDopplerVelocity sets the doppler velocity. | |
| 107 func SetDopplerVelocity(v float32) { | |
| 108 C.alDopplerVelocity(C.ALfloat(v)) | |
| 109 } | |
| 110 | |
| 111 // SpeedOfSound is the speed of sound in meters per second (m/s). | |
| 112 func SpeedOfSound() float32 { | |
| 113 return getf(paramSpeedOfSound) | |
| 114 } | |
| 115 | |
| 116 // SetSpeedOfSound sets the speed of sound, its unit should be meters per second
(m/s). | |
| 117 func SetSpeedOfSound(v float32) { | |
| 118 C.alSpeedOfSound(C.ALfloat(v)) | |
| 119 } | |
| 120 | |
| 121 // Vendor returns the vendor. | |
| 122 func Vendor() string { | |
| 123 return getString(paramVendor) | |
| 124 } | |
| 125 | |
| 126 // Version returns the version string. | |
| 127 func Version() string { | |
| 128 return getString(paramVersion) | |
| 129 } | |
| 130 | |
| 131 // Renderer returns the renderer information. | |
| 132 func Renderer() string { | |
| 133 return getString(paramRenderer) | |
| 134 } | |
| 135 | |
| 136 // Extensions returns the enabled extensions. | |
| 137 func Extensions() string { | |
| 138 return getString(paramExtensions) | |
| 139 } | |
| 140 | |
| 141 // Error returns the most recently generated error. | |
| 142 func Error() int32 { | |
| 143 return int32(C.alGetError()) | |
| 144 } | |
| 145 | |
| 146 // Source represents an individual sound source in 3D-space. | |
| 147 // They take PCM data, apply modifications and then submit them to | |
| 148 // be mixed according to their spatial location. | |
| 149 type Source uint32 | |
| 150 | |
| 151 // GenSources generates n new sources. These sources should be deleted | |
| 152 // once they are not in use. | |
| 153 func GenSources(n int) []Source { | |
| 154 s := make([]Source, n) | |
| 155 C.alGenSources(C.ALsizei(n), (*C.ALuint)(unsafe.Pointer(&s[0]))) | |
| 156 return s | |
| 157 } | |
| 158 | |
| 159 // PlaySources plays the sources. | |
| 160 func PlaySources(source ...Source) { | |
| 161 C.alSourcePlayv(C.ALsizei(len(source)), (*C.ALuint)(unsafe.Pointer(&sour
ce[0]))) | |
| 162 } | |
| 163 | |
| 164 // PauseSources pauses the sources. | |
| 165 func PauseSources(source ...Source) { | |
| 166 C.alSourcePausev(C.ALsizei(len(source)), (*C.ALuint)(unsafe.Pointer(&sou
rce[0]))) | |
| 167 } | |
| 168 | |
| 169 // StopSources stops the sources. | |
| 170 func StopSources(source ...Source) { | |
| 171 C.alSourceStopv(C.ALsizei(len(source)), (*C.ALuint)(unsafe.Pointer(&sour
ce[0]))) | |
| 172 } | |
| 173 | |
| 174 // RewindSources rewinds the sources to their beginning positions. | |
| 175 func RewindSources(source ...Source) { | |
| 176 C.alSourceRewindv(C.ALsizei(len(source)), (*C.ALuint)(unsafe.Pointer(&so
urce[0]))) | |
| 177 } | |
| 178 | |
| 179 // DeleteSources deletes the sources. | |
| 180 func DeleteSources(source ...Source) { | |
| 181 C.alDeleteSources(C.ALsizei(len(source)), (*C.ALuint)(unsafe.Pointer(&so
urce[0]))) | |
| 182 } | |
| 183 | |
| 184 // Gain returns the source gain. | |
| 185 func (s Source) Gain() float32 { | |
| 186 return getSourcef(s, paramGain) | |
| 187 } | |
| 188 | |
| 189 // SetGain sets the source gain. | |
| 190 func (s Source) SetGain(v float32) { | |
| 191 setSourcef(s, paramGain, v) | |
| 192 } | |
| 193 | |
| 194 // MinGain returns the source's minimum gain setting. | |
| 195 func (s Source) MinGain() float32 { | |
| 196 return getSourcef(s, paramMinGain) | |
| 197 } | |
| 198 | |
| 199 // SetMinGain sets the source's minimum gain setting. | |
| 200 func (s Source) SetMinGain(v float32) { | |
| 201 setSourcef(s, paramMinGain, v) | |
| 202 } | |
| 203 | |
| 204 // MaxGain returns the source's maximum gain setting. | |
| 205 func (s Source) MaxGain() float32 { | |
| 206 return getSourcef(s, paramMaxGain) | |
| 207 } | |
| 208 | |
| 209 // SetMaxGain sets the source's maximum gain setting. | |
| 210 func (s Source) SetMaxGain(v float32) { | |
| 211 setSourcef(s, paramMaxGain, v) | |
| 212 } | |
| 213 | |
| 214 // Position returns the position of the source. | |
| 215 func (s Source) Position() Vector { | |
| 216 v := Vector{} | |
| 217 getSourcefv(s, paramPosition, v[:]) | |
| 218 return v | |
| 219 } | |
| 220 | |
| 221 // SetPosition sets the position of the source. | |
| 222 func (s Source) SetPosition(v Vector) { | |
| 223 setSourcefv(s, paramPosition, v[:]) | |
| 224 } | |
| 225 | |
| 226 // Velocity returns the source's velocity. | |
| 227 func (s Source) Velocity() Vector { | |
| 228 v := Vector{} | |
| 229 getSourcefv(s, paramVelocity, v[:]) | |
| 230 return v | |
| 231 } | |
| 232 | |
| 233 // SetVelocity sets the source's velocity. | |
| 234 func (s Source) SetVelocity(v Vector) { | |
| 235 setSourcefv(s, paramVelocity, v[:]) | |
| 236 } | |
| 237 | |
| 238 // Orientation returns the orientation of the source. | |
| 239 func (s Source) Orientation() Orientation { | |
| 240 v := make([]float32, 6) | |
| 241 getSourcefv(s, paramOrientation, v) | |
| 242 return orientationFromSlice(v) | |
| 243 } | |
| 244 | |
| 245 // SetOrientation sets the orientation of the source. | |
| 246 func (s Source) SetOrientation(o Orientation) { | |
| 247 setSourcefv(s, paramOrientation, o.slice()) | |
| 248 } | |
| 249 | |
| 250 // State returns the playing state of the source. | |
| 251 func (s Source) State() int32 { | |
| 252 return getSourcei(s, paramSourceState) | |
| 253 } | |
| 254 | |
| 255 // BuffersQueued returns the number of the queued buffers. | |
| 256 func (s Source) BuffersQueued() int32 { | |
| 257 return getSourcei(s, paramBuffersQueued) | |
| 258 } | |
| 259 | |
| 260 // BuffersProcessed returns the number of the processed buffers. | |
| 261 func (s Source) BuffersProcessed() int32 { | |
| 262 return getSourcei(s, paramBuffersProcessed) | |
| 263 } | |
| 264 | |
| 265 // OffsetSeconds returns the current playback position of the source in seconds. | |
| 266 func (s Source) OffsetSeconds() int32 { | |
| 267 return getSourcei(s, paramSecOffset) | |
| 268 } | |
| 269 | |
| 270 // OffsetSample returns the sample offset of the current playback position. | |
| 271 func (s Source) OffsetSample() int32 { | |
| 272 return getSourcei(s, paramSampleOffset) | |
| 273 } | |
| 274 | |
| 275 // OffsetByte returns the byte offset of the current playback position. | |
| 276 func (s Source) OffsetByte() int32 { | |
| 277 return getSourcei(s, paramByteOffset) | |
| 278 } | |
| 279 | |
| 280 func getSourcei(s Source, param int) int32 { | |
| 281 var v C.ALint | |
| 282 C.alGetSourcei(C.ALuint(s), C.ALenum(param), &v) | |
| 283 return int32(v) | |
| 284 } | |
| 285 | |
| 286 func getSourcef(s Source, param int) float32 { | |
| 287 var v C.ALfloat | |
| 288 C.alGetSourcef(C.ALuint(s), C.ALenum(param), &v) | |
| 289 return float32(v) | |
| 290 } | |
| 291 | |
| 292 func getSourcefv(s Source, param int, v []float32) { | |
| 293 C.alGetSourcefv(C.ALuint(s), C.ALenum(param), (*C.ALfloat)(unsafe.Pointe
r(&v[0]))) | |
| 294 } | |
| 295 | |
| 296 func setSourcei(s Source, param int, v int32) { | |
| 297 C.alSourcei(C.ALuint(s), C.ALenum(param), C.ALint(v)) | |
| 298 } | |
| 299 | |
| 300 func setSourcef(s Source, param int, v float32) { | |
| 301 C.alSourcef(C.ALuint(s), C.ALenum(param), C.ALfloat(v)) | |
| 302 } | |
| 303 | |
| 304 func setSourcefv(s Source, param int, v []float32) { | |
| 305 C.alSourcefv(C.ALuint(s), C.ALenum(param), (*C.ALfloat)(unsafe.Pointer(&
v[0]))) | |
| 306 } | |
| 307 | |
| 308 // QueueBuffers adds the buffers to the buffer queue. | |
| 309 func (s Source) QueueBuffers(buffers []Buffer) { | |
| 310 C.alSourceQueueBuffers(C.ALuint(s), C.ALsizei(len(buffers)), (*C.ALuint)
(unsafe.Pointer(&buffers[0]))) | |
| 311 } | |
| 312 | |
| 313 // UnqueueBuffers removes the specified buffers from the buffer queue. | |
| 314 func (s Source) UnqueueBuffers(buffers []Buffer) { | |
| 315 C.alSourceUnqueueBuffers(C.ALuint(s), C.ALsizei(len(buffers)), (*C.ALuin
t)(unsafe.Pointer(&buffers[0]))) | |
| 316 } | |
| 317 | |
| 318 // ListenerGain returns the total gain applied to the final mix. | |
| 319 func ListenerGain() float32 { | |
| 320 return getListenerf(paramGain) | |
| 321 } | |
| 322 | |
| 323 // ListenerPosition returns the position of the listener. | |
| 324 func ListenerPosition() Vector { | |
| 325 v := Vector{} | |
| 326 getListenerfv(paramPosition, v[:]) | |
| 327 return v | |
| 328 } | |
| 329 | |
| 330 // ListenerVelocity returns the velocity of the listener. | |
| 331 func ListenerVelocity() Vector { | |
| 332 v := Vector{} | |
| 333 getListenerfv(paramVelocity, v[:]) | |
| 334 return v | |
| 335 } | |
| 336 | |
| 337 // ListenerOrientation returns the orientation of the listener. | |
| 338 func ListenerOrientation() Orientation { | |
| 339 v := make([]float32, 6) | |
| 340 getListenerfv(paramOrientation, v) | |
| 341 return orientationFromSlice(v) | |
| 342 } | |
| 343 | |
| 344 // SetListenerGain sets the total gain that will be applied to the final mix. | |
| 345 func SetListenerGain(v float32) { | |
| 346 setListenerf(paramGain, v) | |
| 347 } | |
| 348 | |
| 349 // SetListenerPosition sets the position of the listener. | |
| 350 func SetListenerPosition(v Vector) { | |
| 351 setListenerfv(paramPosition, v[:]) | |
| 352 } | |
| 353 | |
| 354 // SetListenerVelocity sets the velocity of the listener. | |
| 355 func SetListenerVelocity(v Vector) { | |
| 356 setListenerfv(paramVelocity, v[:]) | |
| 357 } | |
| 358 | |
| 359 // SetListenerOrientation sets the orientation of the listener. | |
| 360 func SetListenerOrientation(v Orientation) { | |
| 361 setListenerfv(paramOrientation, v.slice()) | |
| 362 } | |
| 363 | |
| 364 func getListenerf(param int) float32 { | |
| 365 var v C.ALfloat | |
| 366 C.alGetListenerf(C.ALenum(param), &v) | |
| 367 return float32(v) | |
| 368 } | |
| 369 | |
| 370 func getListenerfv(param int, v []float32) { | |
| 371 C.alGetListenerfv(C.ALenum(param), (*C.ALfloat)(unsafe.Pointer(&v[0]))) | |
| 372 } | |
| 373 | |
| 374 func setListenerf(param int, v float32) { | |
| 375 C.alListenerf(C.ALenum(param), C.ALfloat(v)) | |
| 376 } | |
| 377 | |
| 378 func setListenerfv(param int, v []float32) { | |
| 379 C.alListenerfv(C.ALenum(param), (*C.ALfloat)(unsafe.Pointer(&v[0]))) | |
| 380 } | |
| 381 | |
| 382 // A buffer represents a chunk of PCM audio data that could be buffered to an au
dio | |
| 383 // source. A single buffer could be shared between multiple sources. | |
| 384 type Buffer uint32 | |
| 385 | |
| 386 // GenBuffers generates n new buffers. The generated buffers should be deleted | |
| 387 // once they are no longer in use. | |
| 388 func GenBuffers(n int) []Buffer { | |
| 389 s := make([]Buffer, n) | |
| 390 C.alGenBuffers(C.ALsizei(n), (*C.ALuint)(unsafe.Pointer(&s[0]))) | |
| 391 return s | |
| 392 } | |
| 393 | |
| 394 // DeleteBuffers deletes the buffers. | |
| 395 func DeleteBuffers(buffers []Buffer) { | |
| 396 C.alDeleteBuffers(C.ALsizei(len(buffers)), (*C.ALuint)(unsafe.Pointer(&b
uffers[0]))) | |
| 397 } | |
| 398 | |
| 399 func getBufferi(b Buffer, param int) int32 { | |
| 400 var v C.ALint | |
| 401 C.alGetBufferi(C.ALuint(b), C.ALenum(param), &v) | |
| 402 return int32(v) | |
| 403 } | |
| 404 | |
| 405 // Frequency returns the frequency of the buffer data in Hertz (Hz). | |
| 406 func (b Buffer) Frequency() int32 { | |
| 407 return getBufferi(b, paramFreq) | |
| 408 } | |
| 409 | |
| 410 // Bits return the number of bits used to represent a sample. | |
| 411 func (b Buffer) Bits() int32 { | |
| 412 return getBufferi(b, paramBits) | |
| 413 } | |
| 414 | |
| 415 // Channels return the number of the audio channels. | |
| 416 func (b Buffer) Channels() int32 { | |
| 417 return getBufferi(b, paramChannels) | |
| 418 } | |
| 419 | |
| 420 // Size returns the size of the data. | |
| 421 func (b Buffer) Size() int32 { | |
| 422 return getBufferi(b, paramSize) | |
| 423 } | |
| 424 | |
| 425 // BufferData buffers PCM data to the current buffer. | |
| 426 func (b Buffer) BufferData(format uint32, data []byte, freq int32) { | |
| 427 C.alBufferData(C.ALuint(b), C.ALenum(format), unsafe.Pointer(&data[0]),
C.ALsizei(len(data)), C.ALsizei(freq)) | |
| 428 } | |
| 429 | |
| 430 // Valid returns true if the buffer exists and is valid. | |
| 431 func (b Buffer) Valid() bool { | |
| 432 return C.alIsBuffer(C.ALuint(b)) == 1 | |
| 433 } | |
| OLD | NEW |