github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/exp/audio/al/al.go (about)

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