Differenze tra le versioni di "Generare un'onda sinusoidale con il componente gb.openal"

Da Gambas-it.org - Wikipedia.
Riga 15: Riga 15:
 
   Dim err As Boolean
 
   Dim err As Boolean
 
   Dim Formato_audio As Integer
 
   Dim Formato_audio As Integer
   Dim datos As New Byte[]
+
   Dim dati As New Byte[]
 
      
 
      
 
  <FONT Color=gray>' ''Configura il dispositivo e il contesto audio con la Clsase "Alc":''</font>
 
  <FONT Color=gray>' ''Configura il dispositivo e il contesto audio con la Clsase "Alc":''</font>
Riga 29: Riga 29:
 
   buffer = Al.GenBuffers(1)
 
   buffer = Al.GenBuffers(1)
 
    
 
    
   Unda(datos)
+
   Unda(dati)
 
    
 
    
 
   Formato_audio = 4352
 
   Formato_audio = 4352
 
    
 
    
 
  <FONT Color=gray>' ''I dati audio sono caricati nel buffer audio:''</font>
 
  <FONT Color=gray>' ''I dati audio sono caricati nel buffer audio:''</font>
   Al.BufferData(buffer[0], Formato_audio, datos.Data, datos.Count, CAMPIONAMENTO)
+
   Al.BufferData(buffer[0], Formato_audio, dati.Data, dati.Count, CAMPIONAMENTO)
 
    
 
    
 
  <FONT Color=gray>' ''Connette il buffer audio al sorgente audio:''</font>
 
  <FONT Color=gray>' ''Connette il buffer audio al sorgente audio:''</font>

Versione delle 15:48, 28 mar 2018

Utilizzando le risorse della Classe " Alc " del Componente gb.openal di Gambas, è possibile eseguire un'onda sinusoidale, i cui dati saranno realizzati mediante appsota formula matematica.

Vediamo un esempio pratico:

Private Const AMPIEZZA As Byte = 127
Private Const FREQUENZA As Short = 880
Private Const CAMPIONAMENTO As Integer = 44100
Private Const DURATA As Single = 4.0


Public Sub Main()
 
 Dim disp As AlcDevice
 Dim cont As AlcContext
 Dim src, buffer As Integer[]
 Dim err As Boolean
 Dim Formato_audio As Integer
 Dim dati As New Byte[]
   
' Configura il dispositivo e il contesto audio con la Clsase "Alc":
  disp = Alc.OpenDevice(Null)
  cont = Alc.CreateContext(disp)
  
  err = cont.MakeCurrent()
  If err = False Then Error.Raise("Impossibile creare il contesto audio !")
  
  src = Al.GenSources(1)
  
' Configura il buffer audio:
  buffer = Al.GenBuffers(1)
  
  Unda(dati)
  
  Formato_audio = 4352
  
' I dati audio sono caricati nel buffer audio:
  Al.BufferData(buffer[0], Formato_audio, dati.Data, dati.Count, CAMPIONAMENTO)
 
' Connette il buffer audio al sorgente audio:
  Al.Sourcei(src[0], Al.BUFFER, buffer[0])
  
' Esegue il sorgente audio:
  Al.SourcePlay(src[0])
  
' Consente l'esecuzione per l'intera durata dell'onda sonora:
  Sleep DURATA
  
' Libera la memoria:
  Al.DeleteBuffers(buffer)
  Al.DeleteSources(src)
  Alc.DestroyContext(cont)
  Alc.CloseDevice(disp)
  
End


Private Function Unda(bb As Byte[])   ' Crea i dati dell'onda sonora
 
 Dim i As Integer
  
  For i = 0 To (DURATA * 2 * CAMPIONAMENTO) - 1
    bb.Push(CByte(128 + AMPIEZZA * Sin(CFloat(i / CAMPIONAMENTO * FREQUENZA * (2 * Pi)))))
  Next
  
End