github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/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
     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 returns true if 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  func geti(param int) int32 {
    71  	return alGetInteger(param)
    72  }
    73  
    74  func getf(param int) float32 {
    75  	return alGetFloat(param)
    76  }
    77  
    78  func getString(param int) string {
    79  	return alGetString(param)
    80  }
    81  
    82  // DistanceModel returns the distance model.
    83  func DistanceModel() int32 {
    84  	return geti(paramDistanceModel)
    85  }
    86  
    87  // SetDistanceModel sets the distance model.
    88  func SetDistanceModel(v int32) {
    89  	alDistanceModel(v)
    90  }
    91  
    92  // DopplerFactor returns the doppler factor.
    93  func DopplerFactor() float32 {
    94  	return getf(paramDopplerFactor)
    95  }
    96  
    97  // SetDopplerFactor sets the doppler factor.
    98  func SetDopplerFactor(v float32) {
    99  	alDopplerFactor(v)
   100  }
   101  
   102  // DopplerVelocity returns the doppler velocity.
   103  func DopplerVelocity() float32 {
   104  	return getf(paramDopplerVelocity)
   105  }
   106  
   107  // SetDopplerVelocity sets the doppler velocity.
   108  func SetDopplerVelocity(v float32) {
   109  	alDopplerVelocity(v)
   110  }
   111  
   112  // SpeedOfSound is the speed of sound in meters per second (m/s).
   113  func SpeedOfSound() float32 {
   114  	return getf(paramSpeedOfSound)
   115  }
   116  
   117  // SetSpeedOfSound sets the speed of sound, its unit should be meters per second (m/s).
   118  func SetSpeedOfSound(v float32) {
   119  	alSpeedOfSound(v)
   120  }
   121  
   122  // Vendor returns the vendor.
   123  func Vendor() string {
   124  	return getString(paramVendor)
   125  }
   126  
   127  // Version returns the version string.
   128  func Version() string {
   129  	return getString(paramVersion)
   130  }
   131  
   132  // Renderer returns the renderer information.
   133  func Renderer() string {
   134  	return getString(paramRenderer)
   135  }
   136  
   137  // Extensions returns the enabled extensions.
   138  func Extensions() string {
   139  	return getString(paramExtensions)
   140  }
   141  
   142  // Error returns the most recently generated error.
   143  func Error() int32 {
   144  	return alGetError()
   145  }
   146  
   147  // Source represents an individual sound source in 3D-space.
   148  // They take PCM data, apply modifications and then submit them to
   149  // be mixed according to their spatial location.
   150  type Source uint32
   151  
   152  // GenSources generates n new sources. These sources should be deleted
   153  // once they are not in use.
   154  func GenSources(n int) []Source {
   155  	return alGenSources(n)
   156  }
   157  
   158  // PlaySources plays the sources.
   159  func PlaySources(source ...Source) {
   160  	alSourcePlayv(source)
   161  }
   162  
   163  // PauseSources pauses the sources.
   164  func PauseSources(source ...Source) {
   165  	alSourcePausev(source)
   166  }
   167  
   168  // StopSources stops the sources.
   169  func StopSources(source ...Source) {
   170  	alSourceStopv(source)
   171  }
   172  
   173  // RewindSources rewinds the sources to their beginning positions.
   174  func RewindSources(source ...Source) {
   175  	alSourceRewindv(source)
   176  }
   177  
   178  // DeleteSources deletes the sources.
   179  func DeleteSources(source ...Source) {
   180  	alDeleteSources(source)
   181  }
   182  
   183  // Gain returns the source gain.
   184  func (s Source) Gain() float32 {
   185  	return getSourcef(s, paramGain)
   186  }
   187  
   188  // SetGain sets the source gain.
   189  func (s Source) SetGain(v float32) {
   190  	setSourcef(s, paramGain, v)
   191  }
   192  
   193  // MinGain returns the source's minimum gain setting.
   194  func (s Source) MinGain() float32 {
   195  	return getSourcef(s, paramMinGain)
   196  }
   197  
   198  // SetMinGain sets the source's minimum gain setting.
   199  func (s Source) SetMinGain(v float32) {
   200  	setSourcef(s, paramMinGain, v)
   201  }
   202  
   203  // MaxGain returns the source's maximum gain setting.
   204  func (s Source) MaxGain() float32 {
   205  	return getSourcef(s, paramMaxGain)
   206  }
   207  
   208  // SetMaxGain sets the source's maximum gain setting.
   209  func (s Source) SetMaxGain(v float32) {
   210  	setSourcef(s, paramMaxGain, v)
   211  }
   212  
   213  // Position returns the position of the source.
   214  func (s Source) Position() Vector {
   215  	v := Vector{}
   216  	getSourcefv(s, paramPosition, v[:])
   217  	return v
   218  }
   219  
   220  // SetPosition sets the position of the source.
   221  func (s Source) SetPosition(v Vector) {
   222  	setSourcefv(s, paramPosition, v[:])
   223  }
   224  
   225  // Velocity returns the source's velocity.
   226  func (s Source) Velocity() Vector {
   227  	v := Vector{}
   228  	getSourcefv(s, paramVelocity, v[:])
   229  	return v
   230  }
   231  
   232  // SetVelocity sets the source's velocity.
   233  func (s Source) SetVelocity(v Vector) {
   234  	setSourcefv(s, paramVelocity, v[:])
   235  }
   236  
   237  // Orientation returns the orientation of the source.
   238  func (s Source) Orientation() Orientation {
   239  	v := make([]float32, 6)
   240  	getSourcefv(s, paramOrientation, v)
   241  	return orientationFromSlice(v)
   242  }
   243  
   244  // SetOrientation sets the orientation of the source.
   245  func (s Source) SetOrientation(o Orientation) {
   246  	setSourcefv(s, paramOrientation, o.slice())
   247  }
   248  
   249  // State returns the playing state of the source.
   250  func (s Source) State() int32 {
   251  	return getSourcei(s, paramSourceState)
   252  }
   253  
   254  // BuffersQueued returns the number of the queued buffers.
   255  func (s Source) BuffersQueued() int32 {
   256  	return getSourcei(s, paramBuffersQueued)
   257  }
   258  
   259  // BuffersProcessed returns the number of the processed buffers.
   260  func (s Source) BuffersProcessed() int32 {
   261  	return getSourcei(s, paramBuffersProcessed)
   262  }
   263  
   264  // OffsetSeconds returns the current playback position of the source in seconds.
   265  func (s Source) OffsetSeconds() int32 {
   266  	return getSourcei(s, paramSecOffset)
   267  }
   268  
   269  // OffsetSample returns the sample offset of the current playback position.
   270  func (s Source) OffsetSample() int32 {
   271  	return getSourcei(s, paramSampleOffset)
   272  }
   273  
   274  // OffsetByte returns the byte offset of the current playback position.
   275  func (s Source) OffsetByte() int32 {
   276  	return getSourcei(s, paramByteOffset)
   277  }
   278  
   279  func getSourcei(s Source, param int) int32 {
   280  	return alGetSourcei(s, param)
   281  }
   282  
   283  func getSourcef(s Source, param int) float32 {
   284  	return alGetSourcef(s, param)
   285  }
   286  
   287  func getSourcefv(s Source, param int, v []float32) {
   288  	alGetSourcefv(s, param, v)
   289  }
   290  
   291  func setSourcei(s Source, param int, v int32) {
   292  	alSourcei(s, param, v)
   293  }
   294  
   295  func setSourcef(s Source, param int, v float32) {
   296  	alSourcef(s, param, v)
   297  }
   298  
   299  func setSourcefv(s Source, param int, v []float32) {
   300  	alSourcefv(s, param, v)
   301  }
   302  
   303  // QueueBuffers adds the buffers to the buffer queue.
   304  func (s Source) QueueBuffers(buffer ...Buffer) {
   305  	alSourceQueueBuffers(s, buffer)
   306  }
   307  
   308  // UnqueueBuffers removes the specified buffers from the buffer queue.
   309  func (s Source) UnqueueBuffers(buffer ...Buffer) {
   310  	alSourceUnqueueBuffers(s, buffer)
   311  }
   312  
   313  // ListenerGain returns the total gain applied to the final mix.
   314  func ListenerGain() float32 {
   315  	return getListenerf(paramGain)
   316  }
   317  
   318  // ListenerPosition returns the position of the listener.
   319  func ListenerPosition() Vector {
   320  	v := Vector{}
   321  	getListenerfv(paramPosition, v[:])
   322  	return v
   323  }
   324  
   325  // ListenerVelocity returns the velocity of the listener.
   326  func ListenerVelocity() Vector {
   327  	v := Vector{}
   328  	getListenerfv(paramVelocity, v[:])
   329  	return v
   330  }
   331  
   332  // ListenerOrientation returns the orientation of the listener.
   333  func ListenerOrientation() Orientation {
   334  	v := make([]float32, 6)
   335  	getListenerfv(paramOrientation, v)
   336  	return orientationFromSlice(v)
   337  }
   338  
   339  // SetListenerGain sets the total gain that will be applied to the final mix.
   340  func SetListenerGain(v float32) {
   341  	setListenerf(paramGain, v)
   342  }
   343  
   344  // SetListenerPosition sets the position of the listener.
   345  func SetListenerPosition(v Vector) {
   346  	setListenerfv(paramPosition, v[:])
   347  }
   348  
   349  // SetListenerVelocity sets the velocity of the listener.
   350  func SetListenerVelocity(v Vector) {
   351  	setListenerfv(paramVelocity, v[:])
   352  }
   353  
   354  // SetListenerOrientation sets the orientation of the listener.
   355  func SetListenerOrientation(v Orientation) {
   356  	setListenerfv(paramOrientation, v.slice())
   357  }
   358  
   359  func getListenerf(param int) float32 {
   360  	return alGetListenerf(param)
   361  }
   362  
   363  func getListenerfv(param int, v []float32) {
   364  	alGetListenerfv(param, v)
   365  }
   366  
   367  func setListenerf(param int, v float32) {
   368  	alListenerf(param, v)
   369  }
   370  
   371  func setListenerfv(param int, v []float32) {
   372  	alListenerfv(param, v)
   373  }
   374  
   375  // A buffer represents a chunk of PCM audio data that could be buffered to an audio
   376  // source. A single buffer could be shared between multiple sources.
   377  type Buffer uint32
   378  
   379  // GenBuffers generates n new buffers. The generated buffers should be deleted
   380  // once they are no longer in use.
   381  func GenBuffers(n int) []Buffer {
   382  	return alGenBuffers(n)
   383  }
   384  
   385  // DeleteBuffers deletes the buffers.
   386  func DeleteBuffers(buffer ...Buffer) {
   387  	alDeleteBuffers(buffer)
   388  }
   389  
   390  func getBufferi(b Buffer, param int) int32 {
   391  	return alGetBufferi(b, param)
   392  }
   393  
   394  // Frequency returns the frequency of the buffer data in Hertz (Hz).
   395  func (b Buffer) Frequency() int32 {
   396  	return getBufferi(b, paramFreq)
   397  }
   398  
   399  // Bits return the number of bits used to represent a sample.
   400  func (b Buffer) Bits() int32 {
   401  	return getBufferi(b, paramBits)
   402  }
   403  
   404  // Channels return the number of the audio channels.
   405  func (b Buffer) Channels() int32 {
   406  	return getBufferi(b, paramChannels)
   407  }
   408  
   409  // Size returns the size of the data.
   410  func (b Buffer) Size() int32 {
   411  	return getBufferi(b, paramSize)
   412  }
   413  
   414  // BufferData buffers PCM data to the current buffer.
   415  func (b Buffer) BufferData(format uint32, data []byte, freq int32) {
   416  	alBufferData(b, format, data, freq)
   417  }
   418  
   419  // Valid returns true if the buffer exists and is valid.
   420  func (b Buffer) Valid() bool {
   421  	return alIsBuffer(b)
   422  }