al.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. //go:build darwin || linux || windows
  5. // Package al provides OpenAL Soft bindings for Go.
  6. //
  7. // Calls are not safe for concurrent use.
  8. //
  9. // More information about OpenAL Soft is available at
  10. // http://www.openal.org/documentation/openal-1.1-specification.pdf.
  11. //
  12. // In order to use this package on Linux desktop distros,
  13. // you will need OpenAL library as an external dependency.
  14. // On Ubuntu 14.04 'Trusty', you may have to install this library
  15. // by running the command below.
  16. //
  17. // sudo apt-get install libopenal-dev
  18. //
  19. // When compiled for Android, this package uses OpenAL Soft. Please add its
  20. // license file to the open source notices of your application.
  21. // OpenAL Soft's license file could be found at
  22. // http://repo.or.cz/w/openal-soft.git/blob/HEAD:/COPYING.
  23. package al
  24. // Capability represents OpenAL extension capabilities.
  25. type Capability int32
  26. // Enable enables a capability.
  27. func Enable(c Capability) {
  28. alEnable(int32(c))
  29. }
  30. // Disable disables a capability.
  31. func Disable(c Capability) {
  32. alDisable(int32(c))
  33. }
  34. // Enabled reports whether the specified capability is enabled.
  35. func Enabled(c Capability) bool {
  36. return alIsEnabled(int32(c))
  37. }
  38. // Vector represents an vector in a Cartesian coordinate system.
  39. type Vector [3]float32
  40. // Orientation represents the angular position of an object in a
  41. // right-handed Cartesian coordinate system.
  42. // A cross product between the forward and up vector returns a vector
  43. // that points to the right.
  44. type Orientation struct {
  45. // Forward vector is the direction that the object is looking at.
  46. Forward Vector
  47. // Up vector represents the rotation of the object.
  48. Up Vector
  49. }
  50. func orientationFromSlice(v []float32) Orientation {
  51. return Orientation{
  52. Forward: Vector{v[0], v[1], v[2]},
  53. Up: Vector{v[3], v[4], v[5]},
  54. }
  55. }
  56. func (v Orientation) slice() []float32 {
  57. return []float32{v.Forward[0], v.Forward[1], v.Forward[2], v.Up[0], v.Up[1], v.Up[2]}
  58. }
  59. // Geti returns the int32 value of the given parameter.
  60. func Geti(param int) int32 {
  61. return alGetInteger(param)
  62. }
  63. // Getiv returns the int32 vector value of the given parameter.
  64. func Getiv(param int, v []int32) {
  65. alGetIntegerv(param, v)
  66. }
  67. // Getf returns the float32 value of the given parameter.
  68. func Getf(param int) float32 {
  69. return alGetFloat(param)
  70. }
  71. // Getfv returns the float32 vector value of the given parameter.
  72. func Getfv(param int, v []float32) {
  73. alGetFloatv(param, v[:])
  74. }
  75. // Getb returns the bool value of the given parameter.
  76. func Getb(param int) bool {
  77. return alGetBoolean(param)
  78. }
  79. // Getbv returns the bool vector value of the given parameter.
  80. func Getbv(param int, v []bool) {
  81. alGetBooleanv(param, v)
  82. }
  83. // GetString returns the string value of the given parameter.
  84. func GetString(param int) string {
  85. return alGetString(param)
  86. }
  87. // DistanceModel returns the distance model.
  88. func DistanceModel() int32 {
  89. return Geti(paramDistanceModel)
  90. }
  91. // SetDistanceModel sets the distance model.
  92. func SetDistanceModel(v int32) {
  93. alDistanceModel(v)
  94. }
  95. // DopplerFactor returns the doppler factor.
  96. func DopplerFactor() float32 {
  97. return Getf(paramDopplerFactor)
  98. }
  99. // SetDopplerFactor sets the doppler factor.
  100. func SetDopplerFactor(v float32) {
  101. alDopplerFactor(v)
  102. }
  103. // DopplerVelocity returns the doppler velocity.
  104. func DopplerVelocity() float32 {
  105. return Getf(paramDopplerVelocity)
  106. }
  107. // SetDopplerVelocity sets the doppler velocity.
  108. func SetDopplerVelocity(v float32) {
  109. alDopplerVelocity(v)
  110. }
  111. // SpeedOfSound is the speed of sound in meters per second (m/s).
  112. func SpeedOfSound() float32 {
  113. return Getf(paramSpeedOfSound)
  114. }
  115. // SetSpeedOfSound sets the speed of sound, its unit should be meters per second (m/s).
  116. func SetSpeedOfSound(v float32) {
  117. alSpeedOfSound(v)
  118. }
  119. // Vendor returns the vendor.
  120. func Vendor() string {
  121. return GetString(paramVendor)
  122. }
  123. // Version returns the version string.
  124. func Version() string {
  125. return GetString(paramVersion)
  126. }
  127. // Renderer returns the renderer information.
  128. func Renderer() string {
  129. return GetString(paramRenderer)
  130. }
  131. // Extensions returns the enabled extensions.
  132. func Extensions() string {
  133. return GetString(paramExtensions)
  134. }
  135. // Error returns the most recently generated error.
  136. func Error() int32 {
  137. return alGetError()
  138. }
  139. // Source represents an individual sound source in 3D-space.
  140. // They take PCM data, apply modifications and then submit them to
  141. // be mixed according to their spatial location.
  142. type Source uint32
  143. // GenSources generates n new sources. These sources should be deleted
  144. // once they are not in use.
  145. func GenSources(n int) []Source {
  146. return alGenSources(n)
  147. }
  148. // PlaySources plays the sources.
  149. func PlaySources(source ...Source) {
  150. alSourcePlayv(source)
  151. }
  152. // PauseSources pauses the sources.
  153. func PauseSources(source ...Source) {
  154. alSourcePausev(source)
  155. }
  156. // StopSources stops the sources.
  157. func StopSources(source ...Source) {
  158. alSourceStopv(source)
  159. }
  160. // RewindSources rewinds the sources to their beginning positions.
  161. func RewindSources(source ...Source) {
  162. alSourceRewindv(source)
  163. }
  164. // DeleteSources deletes the sources.
  165. func DeleteSources(source ...Source) {
  166. alDeleteSources(source)
  167. }
  168. // Gain returns the source gain.
  169. func (s Source) Gain() float32 {
  170. return s.Getf(paramGain)
  171. }
  172. // SetGain sets the source gain.
  173. func (s Source) SetGain(v float32) {
  174. s.Setf(paramGain, v)
  175. }
  176. // MinGain returns the source's minimum gain setting.
  177. func (s Source) MinGain() float32 {
  178. return s.Getf(paramMinGain)
  179. }
  180. // SetMinGain sets the source's minimum gain setting.
  181. func (s Source) SetMinGain(v float32) {
  182. s.Setf(paramMinGain, v)
  183. }
  184. // MaxGain returns the source's maximum gain setting.
  185. func (s Source) MaxGain() float32 {
  186. return s.Getf(paramMaxGain)
  187. }
  188. // SetMaxGain sets the source's maximum gain setting.
  189. func (s Source) SetMaxGain(v float32) {
  190. s.Setf(paramMaxGain, v)
  191. }
  192. // Position returns the position of the source.
  193. func (s Source) Position() Vector {
  194. v := Vector{}
  195. s.Getfv(paramPosition, v[:])
  196. return v
  197. }
  198. // SetPosition sets the position of the source.
  199. func (s Source) SetPosition(v Vector) {
  200. s.Setfv(paramPosition, v[:])
  201. }
  202. // Velocity returns the source's velocity.
  203. func (s Source) Velocity() Vector {
  204. v := Vector{}
  205. s.Getfv(paramVelocity, v[:])
  206. return v
  207. }
  208. // SetVelocity sets the source's velocity.
  209. func (s Source) SetVelocity(v Vector) {
  210. s.Setfv(paramVelocity, v[:])
  211. }
  212. // Orientation returns the orientation of the source.
  213. func (s Source) Orientation() Orientation {
  214. v := make([]float32, 6)
  215. s.Getfv(paramOrientation, v)
  216. return orientationFromSlice(v)
  217. }
  218. // SetOrientation sets the orientation of the source.
  219. func (s Source) SetOrientation(o Orientation) {
  220. s.Setfv(paramOrientation, o.slice())
  221. }
  222. // State returns the playing state of the source.
  223. func (s Source) State() int32 {
  224. return s.Geti(paramSourceState)
  225. }
  226. // BuffersQueued returns the number of the queued buffers.
  227. func (s Source) BuffersQueued() int32 {
  228. return s.Geti(paramBuffersQueued)
  229. }
  230. // BuffersProcessed returns the number of the processed buffers.
  231. func (s Source) BuffersProcessed() int32 {
  232. return s.Geti(paramBuffersProcessed)
  233. }
  234. // OffsetSeconds returns the current playback position of the source in seconds.
  235. func (s Source) OffsetSeconds() int32 {
  236. return s.Geti(paramSecOffset)
  237. }
  238. // OffsetSample returns the sample offset of the current playback position.
  239. func (s Source) OffsetSample() int32 {
  240. return s.Geti(paramSampleOffset)
  241. }
  242. // OffsetByte returns the byte offset of the current playback position.
  243. func (s Source) OffsetByte() int32 {
  244. return s.Geti(paramByteOffset)
  245. }
  246. // Geti returns the int32 value of the given parameter.
  247. func (s Source) Geti(param int) int32 {
  248. return alGetSourcei(s, param)
  249. }
  250. // Getf returns the float32 value of the given parameter.
  251. func (s Source) Getf(param int) float32 {
  252. return alGetSourcef(s, param)
  253. }
  254. // Getfv returns the float32 vector value of the given parameter.
  255. func (s Source) Getfv(param int, v []float32) {
  256. alGetSourcefv(s, param, v)
  257. }
  258. // Seti sets an int32 value for the given parameter.
  259. func (s Source) Seti(param int, v int32) {
  260. alSourcei(s, param, v)
  261. }
  262. // Setf sets a float32 value for the given parameter.
  263. func (s Source) Setf(param int, v float32) {
  264. alSourcef(s, param, v)
  265. }
  266. // Setfv sets a float32 vector value for the given parameter.
  267. func (s Source) Setfv(param int, v []float32) {
  268. alSourcefv(s, param, v)
  269. }
  270. // QueueBuffers adds the buffers to the buffer queue.
  271. func (s Source) QueueBuffers(buffer ...Buffer) {
  272. alSourceQueueBuffers(s, buffer)
  273. }
  274. // UnqueueBuffers removes the specified buffers from the buffer queue.
  275. func (s Source) UnqueueBuffers(buffer ...Buffer) {
  276. alSourceUnqueueBuffers(s, buffer)
  277. }
  278. // ListenerGain returns the total gain applied to the final mix.
  279. func ListenerGain() float32 {
  280. return GetListenerf(paramGain)
  281. }
  282. // ListenerPosition returns the position of the listener.
  283. func ListenerPosition() Vector {
  284. v := Vector{}
  285. GetListenerfv(paramPosition, v[:])
  286. return v
  287. }
  288. // ListenerVelocity returns the velocity of the listener.
  289. func ListenerVelocity() Vector {
  290. v := Vector{}
  291. GetListenerfv(paramVelocity, v[:])
  292. return v
  293. }
  294. // ListenerOrientation returns the orientation of the listener.
  295. func ListenerOrientation() Orientation {
  296. v := make([]float32, 6)
  297. GetListenerfv(paramOrientation, v)
  298. return orientationFromSlice(v)
  299. }
  300. // SetListenerGain sets the total gain that will be applied to the final mix.
  301. func SetListenerGain(v float32) {
  302. SetListenerf(paramGain, v)
  303. }
  304. // SetListenerPosition sets the position of the listener.
  305. func SetListenerPosition(v Vector) {
  306. SetListenerfv(paramPosition, v[:])
  307. }
  308. // SetListenerVelocity sets the velocity of the listener.
  309. func SetListenerVelocity(v Vector) {
  310. SetListenerfv(paramVelocity, v[:])
  311. }
  312. // SetListenerOrientation sets the orientation of the listener.
  313. func SetListenerOrientation(v Orientation) {
  314. SetListenerfv(paramOrientation, v.slice())
  315. }
  316. // GetListenerf returns the float32 value of the listener parameter.
  317. func GetListenerf(param int) float32 {
  318. return alGetListenerf(param)
  319. }
  320. // GetListenerfv returns the float32 vector value of the listener parameter.
  321. func GetListenerfv(param int, v []float32) {
  322. alGetListenerfv(param, v)
  323. }
  324. // SetListenerf sets the float32 value for the listener parameter.
  325. func SetListenerf(param int, v float32) {
  326. alListenerf(param, v)
  327. }
  328. // SetListenerfv sets the float32 vector value of the listener parameter.
  329. func SetListenerfv(param int, v []float32) {
  330. alListenerfv(param, v)
  331. }
  332. // A buffer represents a chunk of PCM audio data that could be buffered to an audio
  333. // source. A single buffer could be shared between multiple sources.
  334. type Buffer uint32
  335. // GenBuffers generates n new buffers. The generated buffers should be deleted
  336. // once they are no longer in use.
  337. func GenBuffers(n int) []Buffer {
  338. return alGenBuffers(n)
  339. }
  340. // DeleteBuffers deletes the buffers.
  341. func DeleteBuffers(buffer ...Buffer) {
  342. alDeleteBuffers(buffer)
  343. }
  344. // Geti returns the int32 value of the given parameter.
  345. func (b Buffer) Geti(param int) int32 {
  346. return b.Geti(param)
  347. }
  348. // Frequency returns the frequency of the buffer data in Hertz (Hz).
  349. func (b Buffer) Frequency() int32 {
  350. return b.Geti(paramFreq)
  351. }
  352. // Bits return the number of bits used to represent a sample.
  353. func (b Buffer) Bits() int32 {
  354. return b.Geti(paramBits)
  355. }
  356. // Channels return the number of the audio channels.
  357. func (b Buffer) Channels() int32 {
  358. return b.Geti(paramChannels)
  359. }
  360. // Size returns the size of the data.
  361. func (b Buffer) Size() int32 {
  362. return b.Geti(paramSize)
  363. }
  364. // BufferData buffers PCM data to the current buffer.
  365. func (b Buffer) BufferData(format uint32, data []byte, freq int32) {
  366. alBufferData(b, format, data, freq)
  367. }
  368. // Valid reports whether the buffer exists and is valid.
  369. func (b Buffer) Valid() bool {
  370. return alIsBuffer(b)
  371. }