Generare un'onda sinusoidale con le funzioni esterne delle API di LibAo

Da Gambas-it.org - Wikipedia.

Libao è una libreria multi-piattaforma che permette ai programmi di inviare dati audio PCM ai dispositivi audio nativi su una vasta gamma di piattaforme.

Per l'uso delle risorse della libreria libao bisognerà richiamare nel proprio applicativo Gambas la seguente libreria dinamica: "libao.so.4.0.0"


Di seguito un possibile codice per generare con le risorse della libreria libao un'onda sinusoidale:

Public Struct ao_sample_format
  bits As Integer             ' bits per sample
  rate As Integer             ' samples per second (in a single channel)
  channels As Integer         ' number of audio channels
  byte_format As Integer      ' Byte ordering in sample, see constants below
  matrix As Pointer           ' input channel location/ordering
End Struct
 

Library "libao:4.0.0"

Private Const AO_FMT_LITTLE As Integer = 1

' void ao_initialize(void)
' Library setup.
Private Extern ao_initialize()

' int ao_default_driver_id(void)
' Driver information.
Private Extern ao_default_driver_id() As Integer

' ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)
' Driver information.
Private Extern ao_open_live(driver_id As Integer, fmt As Ao_sample_format, option As Pointer) As Pointer

' int ao_play(ao_device *device, char *output_samples, uint_32 num_bytes)
' Write samples to the device. Channels are interleaved. 1 indicates success.
Private Extern ao_play(device As Pointer, output_samples As Pointer, num_bytes As Integer) As Integer

' int ao_close(ao_device *device)
' Closes audio device.

Private Extern ao_close(device As Pointer) As Integer

' void ao_shutdown(void)
' Library teardown.
Private Extern ao_shutdown()

  
Public Sub Main()

 Dim device As Pointer
 Dim ao_sf As New Ao_sample_format
 Dim default_driver, buf_size, sample, i, err As Integer
 Dim freq As Single = 440.0
 Dim buffer As Byte[]
   
' Inizializza la libreria 'libao':
  ao_initialize()
   
' Imposta il driver audio come predefinito:
  default_driver = ao_default_driver_id()

' Imposta le caratteristiche audio in uscita:
  With ao_sf
    .bits = 16
    .channels = 2
    .rate = 44100
    .byte_format = AO_FMT_LITTLE
  End With

' Apre il driver:
  device = ao_open_live(default_driver, ao_sf, Null)
  If IsNull(device) Then Error.Raise("Errore nell'apertura del dispositivo audio !")

' Elabora il suono:
  buf_size = ao_sf.bits / 8 * ao_sf.channels * ao_sf.rate

  buffer = New Byte[buf_size]
   
  For i = 0 To ao_sf.rate - 1

' Genera l'onda sinusoidale:
    sample = CInt(0.75 * 32768.0 * Sin(2 * Pi * freq * CSingle(i / ao_sf.rate)))

' Imposta il medesimo suono nei canali destro e sinistro:
    buffer[4 * i] = sample And &FF
    buffer[4 * i + 2] = sample And &FF
    buffer[4 * i + 1] = Shr(sample, 8) And &FF
    buffer[4 * i + 3] = Shr(sample, 8) And &FF
   
  Next
    
  err = ao_play(device, buffer.Data, buf_size)
  If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
   
 
' Va in chiusura:
  ao_close(device)
  ao_shutdown()

End



Riferimenti

[1] L'API di LibAo