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