Eseguire un file WAVE mediante le funzioni della libreria libopenal

Da Gambas-it.org - Wikipedia.

La libreria libopenal è la libreria ordinaria delle risorse di OpenAl. Mediante le funzioni di questa libreria è possibile eseguire un file WAV e gestire il suono in 3D.

Per poter fruire delle risorse esterne della libreria standard di OpenAl, bisognerà richiamare l'attuale libreria condivisa: "libopenal.so.1.23.1 ".

Mostriamo di seguito un semplice esempio per eseguire in modo ordinario un file WAV:

Library "libopenal:1.23.1"

Private Const AL_BUFFER As Short = 4105
Private Const AL_FALSE As Boolean = False
Private Const AL_PLAYING As Short = 4114
Private Const AL_SOURCE_STATE As Short = 4112

' ALCdevice *alcOpenDevice(const ALCchar *devicename)
' Initialize the OpenAL audio layer. Returns a pointer to the opened device.
Private Extern alcOpenDevice(devicename As String) As Pointer

' ALCcontext * alcCreateContext(ALCdevice *device, ALCint* attrlist)
' This function creates a context using a specified device.
Private Extern alcCreateContext(dev As Pointer, attrlist As Pointer) As Pointer

' ALCboolean alcMakeContextCurrent(ALCcontext *context)
' This function makes a specified context the current context.
Private Extern alcMakeContextCurrent(contexP As Pointer)

' ALenum alGetError(ALvoid)
' This function returns the current error state And Then clears the Error state. Returns an Alenum representing the error state.
Private Extern alGetError() As Integer

' void alGenBuffers(ALsizei n, ALuint * sources)
'  Create Buffer objects.
Private Extern alGenBuffers(n As Integer, buffers As Pointer)

' void alGenSources(Alsizei n, ALuint *sources)
' Create Source objects.
Private Extern alGenSources(n As Integer, sources As Pointer)

' void alBufferData(ALuint buffer, ALenum Format, Const ALvoid * data, ALsizei size, ALsizei freq)
' This function fills a buffer with audio data.
Private Extern alBufferData(buffer As Integer, formato As Integer, data As Pointer, size As Integer, freq As Integer)

' void alSourcei(Aluint source, ALenum param, ALint value)
' This function sets an integer Property of a source.
Private Extern alSourcei(sourcI As Integer, param As Integer, valuInt As Integer)

' void alSourcePlay(ALuint source)
' This function plays a source. The playing source will have its state changed To AL_PLAYING.
Private Extern alSourcePlay(sourcI As Integer)

' void alGetSourcei(ALuint source, ALenum pname, ALint * value)
' This function retrieves an integer property of a source.
Private Extern alGetSourcei(sourcI As Integer, PNAME As Integer, valP As Pointer)

' void alDeleteSources(ALsizei n, const ALuint * sources)
' Delete Source objects.
Private Extern alDeleteSources(n As Integer, sources As Pointer)

' void alDeleteBuffers(ALsizei n, const ALuint * buffers)
' Delete Buffer objects.
Private Extern alDeleteBuffers(n As Integer, buffers As Pointer)

' void alcDestroyContext(ALCcontext context)
' Destroy an OpenAl context.
Private Extern alcDestroyContext(contexP As Pointer)

' ALCboolean alcCloseDevice (ALCdevice *Device)
' The alcCloseDevice function allows the application (i.e. the client program) to disconnect from a device (i.e. the server).
Private Extern alcCloseDevice(dev As Pointer)

' void alutLoadWAVFile (ALbyte *fileName, ALenum *format, void **data, ALsizei *size, ALsizei *frequency)
' Load a WAV file.
Private Extern alutLoadWAVFile(a As String, bufferP As Pointer, formatoP As Pointer, dataP As Pointer, sizeP As Pointer, ver As Pointer) In "libalut:0.1.0"


Public Sub Main()

  Dim device, context, data As Pointer
  Dim err, alSource, alBuffer, source_state as integer
  Dim frequenza, dime_dati As Integer
  Dim fileWav As String
  Dim formatB As Byte
  Dim alLoop As Boolean
  Dim tm As Date
 
  fileWav = "/percorso/del/file.wav"
  
  device = alcOpenDevice("OpenAL Soft")

  If IsNull(device) = False Then
' Crea un contesto OpenAl:
    context = alcCreateContext(device, Null)
' Imposta un contesto attivo:
    alcMakeContextCurrent(context)
  Endif

  err = alGetError()
  If err <> 0 Then Error.Raise("Errore nella inizializzazione della libreria !")
   
' Genera i buffer e le sorgenti sonore:
  alGenBuffers(1, VarPtr(alBuffer))
   
  alGenSources(1, VarPtr(alSource))

  alLoop = AL_FALSE

  alutLoadWAVFile(fileWav, VarPtr(formatB), VarPtr(data), VarPtr(dime_dati), VarPtr(frequenza), VarPtr(alLoop))

  alBufferData(alBuffer, formatB, data, dime_dati, frequenza)

  alSourcei(alSource, AL_BUFFER, alBuffer)

  alSourcePlay(alSource)

  alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))

  tm = Now
  While source_state = AL_PLAYING
    alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))
    Write "\r\e[0mTempo trascorso: \e[31m" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))
    Wait 0.01
  Wend

' Va in chiusura: libera la memoria e rilascia il contesto ed il dispositivo "OpenAl":
   alDeleteSources(1, VarPtr(alSource))
' Elimina il buffer generato:
   alDeleteBuffers(1, VarPtr(alBuffer))
' Rilascia i contesti generati:
   alcDestroyContext(context)
' Chiude in fine il dispositivo "OpenAl":
   alcCloseDevice(device)

End