Generare un'onda sinusoidale con il componente gb.openal

Da Gambas-it.org - Wikipedia.
Versione del 28 mar 2018 alle 15:46 di Vuott (Discussione | contributi) (Creata pagina con "Utilizzando le risorse della Classe " ''Alc'' " del Componente ''gb.openal'' di Gambas, è possibile eseguire un'onda sinusoidale, i cui dati saranno realizzati mediante appso...")

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

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 AMPLITUD As Byte = 127
Private Const FRECUENCIA As Short = 880
Private Const MUESTREO As Integer = 44100
Private Const DURACION 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 datos 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(datos)
  
  Formato_audio = 4352
  
' I dati audio sono caricati nel buffer audio:
  Al.BufferData(buffer[0], Formato_audio, datos.Data, datos.Count, MUESTREO)
 
' 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 DURACION
  
' 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 (DURACION * 2 * MUESTREO) - 1
    bb.Push(CByte(128 + AMPLITUD * Sin(CFloat(i / MUESTREO * FRECUENCIA * (2 * Pi)))))
  Next
  
End