Eseguire un file WAV con le funzioni esterne del 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 le seguenti librerie dinamiche:

  • "libao.so.4.0.0"
  • "libalsa.so"

Di seguito un possibile codice per eseguire un file audio WAV con le risorse della libreria libao:

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
  
' void ao_shutdown(void)
' Library teardown
Private Extern ao_shutdown()


Library "/usr/lib/x86_64-linux-gnu/ao/plugins-4/libalsa"

' int ao_plugin_test()
' Determine if parameters are requires for this particular plugin.
Private Extern ao_plugin_test() As Integer

' int ao_plugin_device_init(ao_device *device)
' Initialize internal data structures.
Private Extern ao_plugin_device_init(ao_dev As Pointer) As Integer

' int ao_plugin_open(ao_device *device, ao_sample_format *format)
' Prepare the audio device for playback.
Private Extern ao_plugin_open(ao_dev As Pointer, fmt As Ao_sample_format) As Integer

' int ao_plugin_play(ao_device *device, const char *output_samples, uint_32 num_bytes)
' Play num_bytes of audio data.
Private Extern ao_plugin_play(ao_dev As Pointer, output_samples As Pointer, num_bytes As Integer) As Integer

' int ao_plugin_close(ao_device *device)
' Close the audio device.
Private Extern ao_plugin_close(ao_dev As Pointer) As Integer

' void ao_plugin_device_clear(ao_device *device)
' Free the internal data structures.
Private Extern ao_plugin_device_clear(ao_dev As Pointer)


Public Sub Main()

 Dim device As Pointer
 Dim ao_sf As New Ao_sample_format
 Dim default_driver, i, err As Integer
 Dim buffer As Byte[]
 Dim fileWAV As String
 Dim fl As File

  fileWAV = "/percorso/del/file.wav"

  fl = Open fileWAV For Read
  
' Inizializza la libreria 'libao':
  ao_initialize()
   
' Imposta il driver audio come predefinito:
  default_driver = ao_default_driver_id()

' Imposta le caratteristiche del file wav caricato, leggendole da esso:
  With ao_sf
    Seek #fl, 34
    .bits = Read #fl As Short
    Seek #fl, 22
    .channels = Read #fl As Short
    .rate = Read #fl As Integer
    .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 !")

  err = ao_plugin_test()
  If err = 0 Then Error.Raise("Il driver audio ha bisogno che vengano impostate alcune opzioni !")
   
  ao_plugin_device_init(device)
  
  ao_plugin_open(device, ao_sf)

  buffer = New Byte[Lof(fl)]

' Riportiamo il puntatore interno del flusso del file a zero:
  Seek #fl, 0
   
' Ciclo per l'elaborazione dell'audio:
  For i = 1 To (buffer.Count) / 1024

' Carica i dati audio nel vettore di tipo "Byte[]":
    buffer.Read(fl, 0, 1024)

' Esegue i dati audio:
    err = ao_plugin_play(device, buffer.Data, 1024)
    If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
     
  Next

  
' Va in chiusura:
  fl.Close
  ao_plugin_close(device)
  ao_plugin_device_clear(device)
  ao_shutdown()

End


Riferimenti

[1] L'API di LibAo