Differenze tra le versioni di "Eseguire un file WAVE mediante le funzioni di OpenAL Utility Toolkit"

Da Gambas-it.org - Wikipedia.
 
(4 versioni intermedie di uno stesso utente non sono mostrate)
Riga 2: Riga 2:
 
<BR>ALUT, eliminando i dettagli per ottenere un'applicazione audio funzionante, consente agli sviluppatori di imparare ''OpenAL'' facilmente.
 
<BR>ALUT, eliminando i dettagli per ottenere un'applicazione audio funzionante, consente agli sviluppatori di imparare ''OpenAL'' facilmente.
  
La libreria condivisa contenente le funzioni esterne dell'API di OpenAlut, e che dovrà essere richiamata in Gambas, è attualmente la seguente:
+
E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "''libalut.so.0.1.0'' ".
libalut.so.0.1.0
 
 
 
  
 
Vediamo di seguito un semplice esempio per eseguire un file audio WAVE:
 
Vediamo di seguito un semplice esempio per eseguire un file audio WAVE:
'''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
 
 
 
 
  Library "libalut:0.1.0"
 
  Library "libalut:0.1.0"
 
   
 
   
 +
Private Const ALUT_ERROR_NO_ERROR As Byte = 0  <FONT color=gray>' ''Indica che non v'è alcun errore''</font>
 +
Private Const AL_BUFFER As Short = 4105        <FONT color=gray>' ''Indica  al buffer di fornire campioni sonori''</font>
 +
Private Const AL_SOURCE_STATE As Short = 4112  <FONT color=gray>' ''Lo stato della sorgente sonora (AL_STOPPED, AL_PLAYING, ...)''</font>
 +
Private Const AL_PLAYING As Short = 4114        <FONT color=gray>' ''Lo stato della sorgente sonora quando è in esecuzione del file audio''</font>
 
   
 
   
 
  <FONT color=gray>' ''ALboolean alutInit (int *argcp, char **argv)  --> alutInit - initialize the ALUT library and create a default current context.''
 
  <FONT color=gray>' ''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''</font>
 
  ' ''alutInit returns AL_TRUE on success or AL_FALSE on failure''</font>
  '''Private''' Extern alutInit(argcp As Integer, argv As Pointer) As Boolean
+
  Private Extern alutInit(argcp As Integer, argv As Pointer) As Boolean
 
   
 
   
 
  <FONT color=gray>' ''ALenum alutGetError (void)  --> alutGetError - return and clear the current error state''
 
  <FONT color=gray>' ''ALenum alutGetError (void)  --> alutGetError - return and clear the current error state''
 
  ' ''alutGetError returns the contents of the global error state''</font>
 
  ' ''alutGetError returns the contents of the global error state''</font>
  '''Private''' Extern alGetError() As Integer
+
  Private Extern alGetError() As Integer
 
   
 
   
 
  <FONT color=gray>' ''ALuint alutCreateBufferFromFile (const char *filename)  --> tries to guess the sound data format by looking at the filename and/or the file contents
 
  <FONT color=gray>' ''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.''</font>
 
  ' ''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.''</font>
  '''Private''' Extern alutCreateBufferFromFile(fileWAV As String) As Pointer
+
  Private Extern alutCreateBufferFromFile(fileWAV As String) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''void alGenSources(Alsizei n, ALuint *sources)''</font>
 
  <FONT color=gray>' ''void alGenSources(Alsizei n, ALuint *sources)''</font>
  '''Private''' Extern alGenSources(n As Integer, sources As Pointer)
+
  Private Extern alGenSources(n As Integer, sources As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alSourcei(ALuint source, ALenum param, ALint value) --> This function sets an integer property of a source''</font>
 
  <FONT color=gray>' ''void alSourcei(ALuint source, ALenum param, ALint value) --> This function sets an integer property of a source''</font>
  '''Private''' Extern alSourcei(source As Integer, param As Integer, value As Integer)
+
  Private Extern alSourcei(source As Integer, param As Integer, value As Integer)
 
   
 
   
 
  <FONT color=gray>' ''void alSourcePlay(ALuint source)  --> This function plays a source. The playing source will have its state changed To AL_PLAYING.''
 
  <FONT color=gray>' ''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,''
 
  ' ''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. ''</font>
 
  ' ''the source will progress To the AL_STOPPED state. ''</font>
  '''Private''' Extern alSourcePlay(sourcI As Integer)
+
  Private Extern alSourcePlay(sourcI As Integer)
 
   
 
   
 
  <FONT color=gray>' ''void alGetSourcei(ALuint source, ALenum pname, ALint * value)  --> This function retrieves an integer property of a source''</font>
 
  <FONT color=gray>' ''void alGetSourcei(ALuint source, ALenum pname, ALint * value)  --> This function retrieves an integer property of a source''</font>
  '''Private''' Extern alGetSourcei(sourcI As Integer, pname As Integer, value As Pointer)
+
  Private Extern alGetSourcei(sourcI As Integer, pname As Integer, value As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''' void alDeleteSources(ALsizei n, ALuint * sources)  --> This function deletes one or more sources''</font>
 
  <FONT color=gray>' ''' void alDeleteSources(ALsizei n, ALuint * sources)  --> This function deletes one or more sources''</font>
  '''Private''' Extern alDeleteSources(n As Integer, sources As Pointer)
+
  Private Extern alDeleteSources(n As Integer, sources As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''' void alDeleteBuffers(ALsizei n, ALuint * buffers)  --> his function deletes one or more buffers, freeing the resources used by the buffer''</font>
 
  <FONT color=gray>' ''' void alDeleteBuffers(ALsizei n, ALuint * buffers)  --> his function deletes one or more buffers, freeing the resources used by the buffer''</font>
  '''Private''' Extern alDeleteBuffers(n As Integer, buffers As Pointer)
+
  Private Extern alDeleteBuffers(n As Integer, buffers As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''ALboolean alutExit (void)  --> When the application has finished playing audio, it should shut down ALUT using aluExit.''
 
  <FONT color=gray>' ''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.''
 
  ' ''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''</font>
 
  ' ''alutExit returns AL_TRUE on success or AL_FALSE on failure''</font>
  '''Private''' Extern alutExit()
+
  Private Extern alutExit()
 
 
   
 
   
'''Public''' Sub Form_Open()
 
 
   
 
   
  Dim buffer, source, state, err As Integer
+
  Public Sub Main()
Dim veritas As Boolean
 
 
   
 
   
 +
  Dim buffer, source, state, err As Integer
 +
  Dim ver As Boolean
 +
  Dim tm As Date
 
   
 
   
 
  <FONT color=gray>' ''Inizializza l'applicazione:''</font>
 
  <FONT color=gray>' ''Inizializza l'applicazione:''</font>
  veritas = alutInit(0, Null)
+
  ver = alutInit(0, Null)
  If veritas = False Then Error.Raise("Rilevato errore nell'inizializzazione dell'applicazione !")
+
  If ver = False Then Error.Raise("Rilevato errore nell'inizializzazione dell'applicazione !")
 
 
      
 
      
 
  <FONT color=gray>' ''Intercetta eventuali errori:''</font>
 
  <FONT color=gray>' ''Intercetta eventuali errori:''</font>
  err = alGetError()
+
  err = alGetError()
  If err > ALUT_ERROR_NO_ERROR Then Error.Raise("Intercettato errore n. " & err & " <FONT color=gray>''(vedere file: alut.h)''</font>")
+
  If err > ALUT_ERROR_NO_ERROR Then Error.Raise("Intercettato errore n. " & err & " <FONT color=gray>''(vedere file: alut.h)''</font>")
 
 
   
 
   
 
  <FONT color=gray>' ''Carica un file WAVE nella variabile "buffer":''</font>
 
  <FONT color=gray>' ''Carica un file WAVE nella variabile "buffer":''</font>
  buffer = alutCreateBufferFromFile("''percorso_del_file.wav''")
+
  buffer = alutCreateBufferFromFile("<FONT color=gray>''/percorso/del/file.wav''</font>")
+
 
 
  <FONT color=gray>' ''Crea una sorgente sonora:''</font>
 
  <FONT color=gray>' ''Crea una sorgente sonora:''</font>
  alGenSources(1, VarPtr(source))
+
  alGenSources(1, VarPtr(source))
 
   
 
   
 
  <FONT color=gray>' ''Viene utilizzata la variabile "buffer" per riempire la sorgente:''</font>
 
  <FONT color=gray>' ''Viene utilizzata la variabile "buffer" per riempire la sorgente:''</font>
  alSourcei(source, AL_BUFFER, buffer)
+
  alSourcei(source, AL_BUFFER, buffer)
 
   
 
   
  alSourcePlay(source)
+
  alSourcePlay(source)
 
 
 
    
 
    
 +
  tm = Now
 +
 
  <FONT color=gray>' ''Attende che l'esecuzione sia terminata:''</font>
 
  <FONT color=gray>' ''Attende che l'esecuzione sia terminata:''</font>
  Do
+
  Do
    alGetSourcei(source, AL_SOURCE_STATE, VarPtr(state))
+
    alGetSourcei(source, AL_SOURCE_STATE, VarPtr(state))
  Loop While state = AL_PLAYING
+
    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
 
    
 
    
 
  <FONT color=gray>' ''Elimina sorgenti audio e buffer:''</font>
 
  <FONT color=gray>' ''Elimina sorgenti audio e buffer:''</font>
  alDeleteSources(1, VarPtr(source))
+
  alDeleteSources(1, VarPtr(source))
  alDeleteBuffers(1, VarPtr(buffer))
+
  alDeleteBuffers(1, VarPtr(buffer))
 
 
  <FONT color=gray>' ''Esce:''</font>
 
  <FONT color=gray>' ''Esce:''</font>
  alutExit()
+
  alutExit()
 
   
 
   
  '''End'''
+
  End
  
  
  
 
=Riferimenti=
 
=Riferimenti=
Il sito dell'[http://connect.creativelabs.com/openal/Documentation/The%20OpenAL%20Utility%20Toolkit.htm API di OpenAlut]
+
* http://distro.ibiblio.org/rootlinux/rootlinux-ports/more/freealut/freealut-1.1.0/doc/alut.html
 +
* https://www.openal.org/

Versione attuale delle 17:24, 13 gen 2024

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