Eseguire un file WAVE mediante le funzioni della libreria libopenal

Da Gambas-it.org - Wikipedia.
Versione del 23 ott 2013 alle 18:11 di Vuott (Discussione | contributi) (Creata pagina con '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...')

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

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_FORMAT_MONO8 As Short = 4352
Private Const AL_FORMAT_STEREO8 As Short = 4353
Private Const AL_FORMAT_MONO16 As Short = 4354
Private Const AL_FORMAT_STEREO16 As Short = 4355
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 ALUT_APIENTRY 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 fl As File
 Dim device, context, data As Pointer
 Dim err, alSource, alBuffer, source_state as integer
 Dim channels, frequenza, bits, dime_dati As Integer
 Dim fileWav As String = "/percorso/del/file.wav"
 Dim j, b, formatB As Byte
 Dim buf As New Byte[]
 Dim formato As String
 Dim alLoop As Boolean 


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


' ''''''''''''''''''''''''''''''''''''''''''

' Carica un file audio:
   fl = Open fileWav For Read 
   
   For j = 0 To 45
     Read #fl, b
     buf.Add(b)
   Next

   If Chr(buf[12]) & Chr(buf[13]) & Chr(buf[14]) & Chr(buf[15]) <> "fmt " Then Error.Raise("File errato !")

' Legge il tipo di formato wave:
   If CStr(buf[20]) & CStr(buf[21]) <> "10" Then Error.Raise("Non è un file PCM !")

' Acquisisce il numero di canali (legge 2 byte):
   channels = buf[23] * CInt(2 ^ 8)
   channels = channels Or buf[22]

' Acquisisce la frequenza di campionamento (legge 4 byte):
   frequenza = buf[27] * CInt(2 ^ 24)
   frequenza = frequenza Or buf[26] * CInt(2 ^ 16)
   frequenza = frequenza Or buf[25] * CInt(2 ^ 8)
   frequenza = frequenza Or buf[24]

' Acquisisce la risoluzione di campionamento (legge 2 byte):
   bits = buf[35] * CInt(2 ^ 8)
   bits = bits Or buf[34]

' La quantità di canali e la risoluzione in bit del file WAV vengono convertiti in formato OpenAL:
   Select Case bits
     Case 8
       Select Case channels
         Case 1
           formatB = AL_FORMAT_MONO8
           formato = "Mono 8"
         Case 2
           formatB = AL_FORMAT_STEREO8
           formato = "Stereo 8"
       End Select
       
     Case 16
       Select Case channels
         Case 1
           formatB = AL_FORMAT_MONO16
           formato = "Mono 16"
         Case 2
           formatB = AL_FORMAT_STEREO16
           formato = "Stereo 16"
       End Select
   End Select

   If IsNull(formatB) Then Error.Raise("Formato incompatibile: " & channels & " " & bits)

' Quindi viene verificato il chunk "data", che contiene i dati campioni decodificati:
   If Chr(buf[36]) & Chr(buf[37]) & Chr(buf[38]) & Chr(buf[39]) <> "data" Then Error.Raise("File privo di dati !")

   dime_dati = buf[43] * CInt(2 ^ 24)
   dime_dati = dime_dati Or buf[42] * CInt(2 ^ 16)
   dime_dati = dime_dati Or buf[41] * CInt(2 ^ 8)
   dime_dati = dime_dati Or buf[40]


' ''''''''''''''''''''''''''''''''''''''''''

  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