Differenze tra le versioni di "Eseguire un file WAV con le funzioni esterne del API di LibAo"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
'''Libao''' è una libreria multi-piattaforma che permette ai programmi di inviare dati audio PCM ai dispositivi audio nativi su una vasta gamma di piattaforme.
 
'''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 libreria attualmente alla versione: "''libao.so.4.0.0''" .
+
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''. Nell'esempio si supporrà che il file wav abbia una frequenza di campionamento a 44100 hertz, una risoluzione a 16bit e due canali:
 
Di seguito un possibile codice per eseguire un file audio WAV con le risorse della libreria ''libao''. Nell'esempio si supporrà che il file wav abbia una frequenza di campionamento a 44100 hertz, una risoluzione a 16bit e due canali:
Riga 11: Riga 13:
 
   matrix As Pointer          <FONT color=gray>' ''input channel location/ordering''</font>
 
   matrix As Pointer          <FONT color=gray>' ''input channel location/ordering''</font>
 
  '''End''' Struct
 
  '''End''' Struct
 +
 
 +
 +
Library "libao:4.0.0"
 +
 +
Private Const AO_FMT_LITTLE As Integer = 1
 +
 +
<FONT color=gray>' ''void ao_initialize(void)''
 +
' ''Library setup''</font>
 +
'''Private''' Extern ao_initialize()
 +
 +
<FONT color=gray>' ''int ao_default_driver_id(void)''
 +
' ''Driver information''</font>
 +
Private Extern ao_default_driver_id() As Integer
 
   
 
   
  '''Private''' ao_sf As Struct Ao_sample_format
+
  <FONT color=gray>' ''ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)''
 +
' ''Driver information''</font>
 +
Private Extern ao_open_live(driver_id As Integer, fmt As Ao_sample_format, option As Pointer) As Pointer
 
   
 
   
 +
<FONT color=gray>' ''int ao_play(ao_device *device, char *output_samples, uint_32 num_bytes)''
 +
' ''Write samples to the device. Channels are interleaved. 1 indicates success.''</font>
 +
Private Extern ao_play(device As Pointer, output_samples As Byte[], num_bytes As Integer) As Integer
 +
 
 +
<FONT color=gray>' ''void ao_shutdown(void)''
 +
' ''Library teardown''</font>
 +
Private Extern ao_shutdown()
 
   
 
   
Library "libao:4.0.0"
 
 
   
 
   
  '''Private''' Const AO_FMT_LITTLE As Integer = 1
+
  Library "/usr/lib/x86_64-linux-gnu/ao/plugins-4/libalsa"
 
   
 
   
  <FONT color=gray>' ''void ao_initialize(void)''</font>
+
  <FONT color=gray>' ''int ao_plugin_test()''
  <FONT color=gray>' ''library setup''</font>
+
  ' ''Determine if parameters are requires for this particular plugin.''</font>
  '''Private''' Extern ao_initialize()
+
  Private Extern ao_plugin_test() As Integer
 
   
 
   
  <FONT color=gray>' ''int ao_default_driver_id(void)''</font>
+
  <FONT color=gray>' ''int ao_plugin_device_init(ao_device *device)''
  <FONT color=gray>' ''driver information''</font>
+
  ' Initialize internal data structures.''</font>
  '''Private''' Extern ao_default_driver_id() As Integer
+
  Private Extern ao_plugin_device_init(ao_dev As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)''</font>
+
  <FONT color=gray>' ''int ao_plugin_open(ao_device *device, ao_sample_format *format)''
  <FONT color=gray>' ''driver information''</font>
+
  ' ''Prepare the audio device for playback.''</font>
  '''Private''' Extern ao_open_live(driver_id As Integer, fmt As Ao_sample_format, option As Pointer) As Pointer
+
  Private Extern ao_plugin_open(ao_dev As Pointer, fmt As Ao_sample_format) As Integer
 
   
 
   
  <FONT color=gray>' ''int ao_play(ao_device *device, char *output_samples, uint_32 num_bytes)''</font>
+
  <FONT color=gray>' ''int ao_plugin_play(ao_device *device, const char *output_samples, uint_32 num_bytes)''
  <FONT color=gray>' ''Write samples to the device. Channels are interleaved. 1 indicates success.''</font>
+
  ' ''Play num_bytes of audio data.''</font>
  '''Private''' Extern ao_play(device As Pointer, output_samples As Byte[], num_bytes As Integer) As Integer
+
  Private Extern ao_plugin_play(ao_dev As Pointer, output_samples As Pointer, num_bytes As Integer) As Integer
 
   
 
   
  <FONT color=gray>' ''int ao_close(ao_device *device)''</font>
+
  <FONT color=gray>' ''int ao_plugin_close(ao_device *device)''
  <FONT color=gray>' ''Chiude il dispositivo audio''</font>
+
  ' ''Close the audio device.''</font>
  '''Private''' Extern ao_close(device As Pointer) As Integer
+
  Private Extern ao_plugin_close(ao_dev As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''void ao_shutdown(void)''</font>
+
  <FONT color=gray>' ''void ao_plugin_device_clear(ao_device *device)''
  <FONT color=gray>' ''library teardown''</font>
+
  ' ''Free the internal data structures.''</font>
  '''Private''' Extern ao_shutdown()
+
  Private Extern ao_plugin_device_clear(ao_dev As Pointer)
 
   
 
   
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
  Dim device As Pointer
+
  Dim device As Pointer
  Dim default_driver, i, err As Integer
+
  Dim ao_sf As New Ao_sample_format
  Dim buffer As Byte[]
+
  Dim default_driver, i, err, opz As Integer
  Dim fileWAV As String
+
  Dim buffer As Byte[]
  Dim fl As File
+
  Dim fileWAV As String
 +
  Dim fl As File
 
   
 
   
 +
  fileWAV = "''/percorso/del/file.wav''"
 
   
 
   
    fileWAV = "''/percorso/del/file.wav''"
+
  fl = Open fileWAV For Read
 
    fl = Open fileWAV For Read
 
 
    
 
    
 
   
 
   
 
  <FONT color=gray>' ''Inizializza la libreria 'libao':''</font>
 
  <FONT color=gray>' ''Inizializza la libreria 'libao':''</font>
    ao_initialize()
+
  ao_initialize()
 
      
 
      
 
  <FONT color=gray>' ''Imposta il driver audio come predefinito:''</font>
 
  <FONT color=gray>' ''Imposta il driver audio come predefinito:''</font>
    default_driver = ao_default_driver_id()
+
  default_driver = ao_default_driver_id()
 
   
 
   
 
  <FONT color=gray>' ''Imposta le caratteristiche del file wav caricato:''</font>
 
  <FONT color=gray>' ''Imposta le caratteristiche del file wav caricato:''</font>
    With ao_sf
+
  With ao_sf
      .bits = 16
+
    .bits = 16
      .channels = 2
+
    .channels = 2
      .rate = 44100
+
    .rate = 44100
      .byte_format = AO_FMT_LITTLE
+
    .byte_format = AO_FMT_LITTLE
    End With   
+
  End With   
 
    
 
    
 
  <FONT color=gray>' ''Apre il driver:''</font>
 
  <FONT color=gray>' ''Apre il driver:''</font>
    device = ao_open_live(default_driver, VarPtr(ao_sf), Null)
+
  device = ao_open_live(default_driver, ao_sf, Null)
    If IsNull(device) Then Error.Raise("Errore nell'apertura del dispositivo audio !")
+
  If IsNull(device) Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 +
 +
  opz = ao_plugin_test()
 +
  If opz = 0 Then Error.Raise("Il driver audio ha bisogno che vengano impostate alcune opzioni !")
 
      
 
      
    buffer = New Byte[Stat(fileWAV).Size]
+
  ao_plugin_device_init(device)
 +
 
 +
  ao_plugin_open(device, ao_sf)
 +
 +
  buffer = New Byte[Lof(fl)]
 
      
 
      
 
  <FONT color=gray>' ''Ciclo per l'elaborazione dell'audio:''</font>
 
  <FONT color=gray>' ''Ciclo per l'elaborazione dell'audio:''</font>
    For i = 1 To (buffer.Count) / 1024
+
  For i = 1 To (buffer.Count) / 1024
 
   
 
   
 
  <FONT color=gray>' ''Carica i dati audio nel vettore di tipo "Byte[]":''</font>
 
  <FONT color=gray>' ''Carica i dati audio nel vettore di tipo "Byte[]":''</font>
      buffer.Read(fl, 0, 1024)
+
    buffer.Read(fl, 0, 1024)
 
   
 
   
 
  <FONT color=gray>' ''Esegue i dati audio:''</font>
 
  <FONT color=gray>' ''Esegue i dati audio:''</font>
      err = ao_play(device, buffer, 1024)
+
    err = ao_plugin_play(device, buffer.Data, 1024)
      If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
+
    If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
 
        
 
        
 
     Next
 
     Next
Riga 92: Riga 122:
 
    
 
    
 
  <FONT color=gray>' ''Va in chiusura:''</font>
 
  <FONT color=gray>' ''Va in chiusura:''</font>
    fl.Close
+
  fl.Close
    buffer.Clear()
+
  ao_plugin_close(device)
    ao_close(device)
+
  ao_plugin_device_clear(device)
    ao_shutdown()
+
  ao_shutdown()
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 17:01, 5 mag 2015

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. Nell'esempio si supporrà che il file wav abbia una frequenza di campionamento a 44100 hertz, una risoluzione a 16bit e due canali:

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 Byte[], num_bytes As Integer) As Integer
 
' 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, opz 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:
  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 !")

  opz = ao_plugin_test()
  If opz = 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)]
   
' 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