Eseguire un file WAVE mediante le funzioni di OpenAL Utility Toolkit

Da Gambas-it.org - Wikipedia.

L'OpenAL Utility Toolkit (ALUT) è una libreria nettamente separata da quella ordinaria di OpenAl, e che dovrebbe essere portabile tra sistemi. ALUT dovrebbe essere un sistema di funzioni adatto per la produzione di semplici programmi demo e per aiutare i nuovi sviluppatori ad avere i primi approcci con OpenAL.
ALUT, eliminando i dettagli per ottenere un'applicazione audio funzionante, consente agli sviluppatori di imparare OpenAL facilmente.

E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "libalut.so.0.1.0 ".

Vediamo di seguito un semplice esempio per eseguire un file audio WAVE:

Library "libalut:0.1.0"

Private Const ALUT_ERROR_NO_ERROR As Byte = 0   ' Indica che non v'è alcun errore
Private Const AL_BUFFER As Short = 4105         ' Indica  al buffer di fornire campioni sonori
Private Const AL_SOURCE_STATE As Short = 4112   ' Lo stato della sorgente sonora (AL_STOPPED, AL_PLAYING, ...)
Private Const AL_PLAYING As Short = 4114        ' Lo stato della sorgente sonora quando è in esecuzione del file audio

' ALboolean alutInit (int *argcp, char **argv)  --> alutInit - initialize the ALUT library and create a default current context.
' alutInit returns AL_TRUE on success or AL_FALSE on failure
Private Extern alutInit(argcp As Integer, argv As Pointer) As Boolean

' ALenum alutGetError (void)  --> alutGetError - return and clear the current error state
' alutGetError returns the contents of the global error state
Private Extern alGetError() As Integer

' ALuint alutCreateBufferFromFile (const char *filename)  --> tries to guess the sound data format by looking at the filename and/or the file contents
' and loads the sound data into an OpenAL buffer. On success, alutCreateBufferFromFile returns a handle to an OpenAL buffer containing the loaded sound. It returns AL_NONE on failure.
Private Extern alutCreateBufferFromFile(fileWAV As String) As Pointer

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

' void alSourcei(ALuint source, ALenum param, ALint value) --> This function sets an integer property of a source
Private Extern alSourcei(source As Integer, param As Integer, value As Integer)

' void alSourcePlay(ALuint source)  --> This function plays a source. The playing source will have its state changed To AL_PLAYING.
' When called On a source which Is Already playing, the source will restart at the beginning. When the attached buffer(s)are done playing,
' the source will progress To the AL_STOPPED state. 
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, value As Pointer)

' ' void alDeleteSources(ALsizei n, ALuint * sources)  --> This function deletes one or more sources
Private Extern alDeleteSources(n As Integer, sources As Pointer)

' ' void alDeleteBuffers(ALsizei n, ALuint * buffers)  --> his function deletes one or more buffers, freeing the resources used by the buffer
Private Extern alDeleteBuffers(n As Integer, buffers As Pointer)

' ALboolean alutExit (void)  --> When the application has finished playing audio, it should shut down ALUT using aluExit.
' This closes any OpenAL device/context that ALUT may have created in alutInit. After calling alutExit, you may subsequently call alutInit or alutInitWithoutContext again.
' alutExit returns AL_TRUE on success or AL_FALSE on failure
Private Extern alutExit()


Public Sub Main()

 Dim buffer, source, state, err As Integer
 Dim ver As Boolean
 Dim tm As Date

' Inizializza l'applicazione:
 ver = alutInit(0, Null)
 If ver = False Then Error.Raise("Rilevato errore nell'inizializzazione dell'applicazione !")
   
' Intercetta eventuali errori:
 err = alGetError()
 If err > ALUT_ERROR_NO_ERROR Then Error.Raise("Intercettato errore n. " & err & " (vedere file: alut.h)")

' Carica un file WAVE nella variabile "buffer":
 buffer = alutCreateBufferFromFile("/percorso/del/file.wav")
' Crea una sorgente sonora:
 alGenSources(1, VarPtr(source))

' Viene utilizzata la variabile "buffer" per riempire la sorgente:
 alSourcei(source, AL_BUFFER, buffer)

 alSourcePlay(source)
 
 tm = Now

' Attende che l'esecuzione sia terminata:
 Do
   alGetSourcei(source, AL_SOURCE_STATE, VarPtr(state))
   Write "\r\e[0mTempo trascorso: \e[31m" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))
   Wait 0.01
 Loop While state = AL_PLAYING
 
' Elimina sorgenti audio e buffer:
 alDeleteSources(1, VarPtr(source))
 alDeleteBuffers(1, VarPtr(buffer))
' Esce:
 alutExit()

End


Riferimenti