Differenze tra le versioni di "Eseguire un file WAVE mediante le funzioni della libreria libopenal"

Da Gambas-it.org - Wikipedia.
Riga 5: Riga 5:
  
 
Mostriamo di seguito un semplice esempio per eseguire in modo ordinario un file WAV:
 
Mostriamo di seguito un semplice esempio per eseguire in modo ordinario un file WAV:
  '''Private''' Const AL_BUFFER As Short = 4105
+
  Private Const AL_BUFFER As Short = 4105
  '''Private''' Const AL_FALSE As Boolean = False
+
  Private Const AL_FALSE As Boolean = False
  '''Private''' Const AL_PLAYING As Short = 4114
+
  Private Const AL_PLAYING As Short = 4114
  '''Private''' Const AL_SOURCE_STATE As Short = 4112
+
  Private Const AL_SOURCE_STATE As Short = 4112
 
   
 
   
 
   
 
   
Riga 15: Riga 15:
 
  <FONT color=gray>' ''ALCdevice *alcOpenDevice(const ALCchar *devicename)''
 
  <FONT color=gray>' ''ALCdevice *alcOpenDevice(const ALCchar *devicename)''
 
  ' ''Initialize the OpenAL audio layer. Returns a pointer to the opened device.''</font>
 
  ' ''Initialize the OpenAL audio layer. Returns a pointer to the opened device.''</font>
  '''Private''' Extern alcOpenDevice(devicename As String) As Pointer
+
  Private Extern alcOpenDevice(devicename As String) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''ALCcontext * alcCreateContext(ALCdevice *device, ALCint* attrlist)''
 
  <FONT color=gray>' ''ALCcontext * alcCreateContext(ALCdevice *device, ALCint* attrlist)''
 
  ' ''This function creates a context using a specified device.''</font>
 
  ' ''This function creates a context using a specified device.''</font>
  '''Private''' Extern alcCreateContext(dev As Pointer, attrlist As Pointer) As Pointer
+
  Private Extern alcCreateContext(dev As Pointer, attrlist As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''ALCboolean alcMakeContextCurrent(ALCcontext *context)''
 
  <FONT color=gray>' ''ALCboolean alcMakeContextCurrent(ALCcontext *context)''
 
  ' ''This function makes a specified context the current context.''</font>
 
  ' ''This function makes a specified context the current context.''</font>
  '''Private''' Extern alcMakeContextCurrent(contexP As Pointer)
+
  Private Extern alcMakeContextCurrent(contexP As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''ALenum alGetError(ALvoid)''
 
  <FONT color=gray>' ''ALenum alGetError(ALvoid)''
 
  ' ''This function returns the current error state And Then clears the Error state. Returns an Alenum representing the error state.''</font>
 
  ' ''This function returns the current error state And Then clears the Error state. Returns an Alenum representing the error state.''</font>
  '''Private''' Extern alGetError() As Integer
+
  Private Extern alGetError() As Integer
 
   
 
   
 
  <FONT color=gray>' ''void alGenBuffers(ALsizei n, ALuint * sources)''
 
  <FONT color=gray>' ''void alGenBuffers(ALsizei n, ALuint * sources)''
 
  ' '' Create Buffer objects.''</font>
 
  ' '' Create Buffer objects.''</font>
  '''Private''' Extern alGenBuffers(n As Integer, buffers As Pointer)
+
  Private Extern alGenBuffers(n As Integer, buffers As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alGenSources(Alsizei n, ALuint *sources)''
 
  <FONT color=gray>' ''void alGenSources(Alsizei n, ALuint *sources)''
 
  ' ''Create Source objects.''</font>
 
  ' ''Create Source objects.''</font>
  '''Private''' Extern alGenSources(n As Integer, sources As Pointer)
+
  Private Extern alGenSources(n As Integer, sources As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alBufferData(ALuint buffer, ALenum Format, Const ALvoid * data, ALsizei size, ALsizei freq)''
 
  <FONT color=gray>' ''void alBufferData(ALuint buffer, ALenum Format, Const ALvoid * data, ALsizei size, ALsizei freq)''
 
  ' ''This function fills a buffer with audio data.''</font>
 
  ' ''This function fills a buffer with audio data.''</font>
  '''Private''' Extern alBufferData(buffer As Integer, formato As Integer, data As Pointer, size As Integer, freq As Integer)
+
  Private Extern alBufferData(buffer As Integer, formato As Integer, data As Pointer, size As Integer, freq As Integer)
 
   
 
   
 
  <FONT color=gray>' ''void alSourcei(Aluint source, ALenum param, ALint value)''
 
  <FONT color=gray>' ''void alSourcei(Aluint source, ALenum param, ALint value)''
 
  ' ''This function sets an integer Property of a source.''</font>
 
  ' ''This function sets an integer Property of a source.''</font>
  '''Private''' Extern alSourcei(sourcI As Integer, param As Integer, valuInt As Integer)
+
  Private Extern alSourcei(sourcI As Integer, param As Integer, valuInt As Integer)
 
   
 
   
 
  <FONT color=gray>' ''void alSourcePlay(ALuint source)''
 
  <FONT color=gray>' ''void alSourcePlay(ALuint source)''
 
  ' ''This function plays a source. The playing source will have its state changed To AL_PLAYING.''</font>
 
  ' ''This function plays a source. The playing source will have its state changed To AL_PLAYING.''</font>
  '''Private''' Extern alSourcePlay(sourcI As Integer)
+
  Private Extern alSourcePlay(sourcI As Integer)
 
   
 
   
 
  <FONT color=gray>' ''void alGetSourcei(ALuint source, ALenum pname, ALint * value)''
 
  <FONT color=gray>' ''void alGetSourcei(ALuint source, ALenum pname, ALint * value)''
 
  ' ''This function retrieves an integer property of a source.''</font>
 
  ' ''This function retrieves an integer property of a source.''</font>
  '''Private''' Extern alGetSourcei(sourcI As Integer, PNAME As Integer, valP As Pointer)
+
  Private Extern alGetSourcei(sourcI As Integer, PNAME As Integer, valP As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alDeleteSources(ALsizei n, const ALuint * sources)''
 
  <FONT color=gray>' ''void alDeleteSources(ALsizei n, const ALuint * sources)''
 
  ' ''Delete Source objects.''</font>
 
  ' ''Delete Source objects.''</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, const ALuint * buffers)''
 
  <FONT color=gray>' ''void alDeleteBuffers(ALsizei n, const ALuint * buffers)''
 
  ' ''Delete Buffer objects.''</font>
 
  ' ''Delete Buffer objects.''</font>
  '''Private''' Extern alDeleteBuffers(n As Integer, buffers As Pointer)
+
  Private Extern alDeleteBuffers(n As Integer, buffers As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alcDestroyContext(ALCcontext context)''
 
  <FONT color=gray>' ''void alcDestroyContext(ALCcontext context)''
 
  ' ''Destroy an OpenAl context.''</font>
 
  ' ''Destroy an OpenAl context.''</font>
  '''Private''' Extern alcDestroyContext(contexP As Pointer)
+
  Private Extern alcDestroyContext(contexP As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''ALCboolean alcCloseDevice (ALCdevice *Device)
 
  <FONT color=gray>' ''ALCboolean alcCloseDevice (ALCdevice *Device)
 
  ' ''The alcCloseDevice function allows the application (i.e. the client program) to disconnect from a device (i.e. the server).''</font>
 
  ' ''The alcCloseDevice function allows the application (i.e. the client program) to disconnect from a device (i.e. the server).''</font>
  '''Private''' Extern alcCloseDevice(dev As Pointer)
+
  Private Extern alcCloseDevice(dev As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void alutLoadWAVFile (ALbyte *fileName, ALenum *format, void **data, ALsizei *size, ALsizei *frequency)''
 
  <FONT color=gray>' ''void alutLoadWAVFile (ALbyte *fileName, ALenum *format, void **data, ALsizei *size, ALsizei *frequency)''
 
  ' ''Load a WAV file.''</font>
 
  ' ''Load a WAV file.''</font>
  '''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"
+
  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"
 
   
 
   
 
   
 
   
Riga 79: Riga 79:
 
   Dim err, alSource, alBuffer, source_state as integer
 
   Dim err, alSource, alBuffer, source_state as integer
 
   Dim frequenza, dime_dati As Integer
 
   Dim frequenza, dime_dati As Integer
   Dim fileWav As String = "''/percorso/del/file.wav''"
+
   Dim fileWav As String
 
   Dim formatB As Byte
 
   Dim formatB As Byte
 
   Dim alLoop As Boolean  
 
   Dim alLoop As Boolean  
+
 
+
  fileWav = "''/percorso/del/file.wav''"
 +
 
 
   device = alcOpenDevice("OpenAL Soft")
 
   device = alcOpenDevice("OpenAL Soft")
 
   
 
   

Versione delle 09:20, 23 ago 2015

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.14.0" .


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

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


Library "libopenal:1.14.0"

' 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 
 
  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))

  While source_state = AL_PLAYING
    alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))
  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