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

Da Gambas-it.org - Wikipedia.
(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...')
 
 
(8 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
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.
 
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''" .
+
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:
 
Mostriamo di seguito un semplice esempio per eseguire in modo ordinario un file WAV:
  '''Private''' Const AL_FORMAT_MONO8 As Short = 4352
+
  Library "libopenal:1.23.1"
'''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
 
 
   
 
   
   
+
  Private Const AL_BUFFER As Short = 4105
  Library "libopenal:1.14.0"
+
  Private Const AL_FALSE As Boolean = False
 +
Private Const AL_PLAYING As Short = 4114
 +
Private Const AL_SOURCE_STATE As Short = 4112
 
   
 
   
 
  <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 ALUT_APIENTRY 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"
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  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
 
 
 
   
 
   
 +
  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 = "<FONT color=darkgreen>''/percorso/del/file.wav''</font>"
 +
 
 
   device = alcOpenDevice("OpenAL Soft")
 
   device = alcOpenDevice("OpenAL Soft")
 
   
 
   
Riga 107: Riga 100:
 
      
 
      
 
   alGenSources(1, VarPtr(alSource))
 
   alGenSources(1, VarPtr(alSource))
 
 
<FONT color=gray>' '''''''''''''''''''''''''''''''''''''''''''''''
 
 
' ''Carica un file audio:''</font>
 
    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 !")
 
 
<FONT color=gray>' ''Legge il tipo di formato wave:''</font>
 
    If CStr(buf[20]) & CStr(buf[21]) <> "10" Then Error.Raise("Non è un file PCM !")
 
 
<FONT color=gray>' ''Acquisisce il numero di canali (legge 2 byte):''</font>
 
    channels = buf[23] * CInt(2 ^ 8)
 
    channels = channels Or buf[22]
 
 
<FONT color=gray>' ''Acquisisce la frequenza di campionamento (legge 4 byte):''</font>
 
    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]
 
 
<FONT color=gray>' ''Acquisisce la risoluzione di campionamento (legge 2 byte):''</font>
 
    bits = buf[35] * CInt(2 ^ 8)
 
    bits = bits Or buf[34]
 
 
<FONT color=gray>' ''La quantità di canali e la risoluzione in bit del file WAV vengono convertiti in formato OpenAL:''</font>
 
    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)
 
 
<FONT color=gray>' ''Quindi viene verificato il chunk "data", che contiene i dati campioni decodificati:''</font>
 
    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]
 
 
 
<FONT color=gray>' '''''''''''''''''''''''''''''''''''''''''''''''</font>
 
 
   
 
   
 
   alLoop = AL_FALSE
 
   alLoop = AL_FALSE
Riga 186: Riga 113:
 
   alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))
 
   alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))
 
   
 
   
 +
  tm = Now
 
   While source_state = AL_PLAYING
 
   While source_state = AL_PLAYING
 
     alGetSourcei(alSource, AL_SOURCE_STATE, VarPtr(source_state))
 
     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
 
   Wend
 
   
 
   
 
 
 
  <FONT color=gray>' ''Va in chiusura: libera la memoria e rilascia il contesto ed il dispositivo "OpenAl":''</font>
 
  <FONT color=gray>' ''Va in chiusura: libera la memoria e rilascia il contesto ed il dispositivo "OpenAl":''</font>
 
     alDeleteSources(1, VarPtr(alSource))
 
     alDeleteSources(1, VarPtr(alSource))
   
 
 
  <FONT color=gray>' ''Elimina il buffer generato:''</font>
 
  <FONT color=gray>' ''Elimina il buffer generato:''</font>
 
     alDeleteBuffers(1, VarPtr(alBuffer))
 
     alDeleteBuffers(1, VarPtr(alBuffer))
   
 
 
  <FONT color=gray>' ''Rilascia i contesti generati:''</font>
 
  <FONT color=gray>' ''Rilascia i contesti generati:''</font>
 
     alcDestroyContext(context)
 
     alcDestroyContext(context)
   
 
 
  <FONT color=gray>' ''Chiude in fine il dispositivo "OpenAl":''</font>
 
  <FONT color=gray>' ''Chiude in fine il dispositivo "OpenAl":''</font>
 
     alcCloseDevice(device)
 
     alcCloseDevice(device)
 
   
 
   
  '''End'''
+
  End

Versione attuale delle 17:16, 13 gen 2024

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