Differenze tra le versioni di "Generare un'onda sinusoidale con le funzioni esterne delle API di LibAo"

Da Gambas-it.org - Wikipedia.
Riga 45: Riga 45:
 
   
 
   
 
    
 
    
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
 
   Dim device As Pointer
 
   Dim device As Pointer
Riga 54: Riga 54:
 
      
 
      
 
  <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 audio in uscita:''</font>
 
  <FONT color=gray>' ''Imposta le caratteristiche audio in uscita:''</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, 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 device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 
   
 
   
 
  <FONT color=gray>' ''Elabora il suono:''</font>
 
  <FONT color=gray>' ''Elabora il suono:''</font>
  buf_size = ao_sf.bits / 8 * ao_sf.channels * ao_sf.rate
+
  buf_size = ao_sf.bits / 8 * ao_sf.channels * ao_sf.rate
 
   
 
   
  buffer = New Byte[buf_size]
+
  buffer = New Byte[buf_size]
 
      
 
      
  For i = 0 To ao_sf.rate - 1
+
  For i = 0 To ao_sf.rate - 1
 
 
  <FONT color=gray>' ''Genera l'onda sinusoidale:''</font>
 
  <FONT color=gray>' ''Genera l'onda sinusoidale:''</font>
    sample = CInt(0.75 * 32768.0 * Sin(2 * Pi * freq * CSingle(i / ao_sf.rate)))
+
    sample = CInt(0.75 * 32768.0 * Sin(2 * Pi * freq * CSingle(i / ao_sf.rate)))
 
 
  <FONT color=gray>' ''Imposta il medesimo suono nei canali destro e sinistro:''</font>
 
  <FONT color=gray>' ''Imposta il medesimo suono nei canali destro e sinistro:''</font>
    buffer[4 * i] = sample And &FF
+
    buffer[4 * i] = sample And &FF
    buffer[4 * i + 2] = sample And &FF
+
    buffer[4 * i + 2] = sample And &FF
    buffer[4 * i + 1] = Shr(sample, 8) And &FF
+
    buffer[4 * i + 1] = Shr(sample, 8) And &FF
    buffer[4 * i + 3] = Shr(sample, 8) And &FF
+
    buffer[4 * i + 3] = Shr(sample, 8) And &FF
   
+
  Next
  Next
 
 
      
 
      
  err = ao_play(device, buffer.Data, buf_size)
+
  err = ao_play(device, buffer.Data, buf_size)
  If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
+
  If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
   
+
 
 
 
  <FONT color=gray>' ''Va in chiusura:''</font>
 
  <FONT color=gray>' ''Va in chiusura:''</font>
  ao_close(device)
+
  ao_close(device)
  ao_shutdown()
+
  ao_shutdown()
 
   
 
   
  '''End'''
+
  End
  
  
Riga 159: Riga 155:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
Dim device As Pointer
+
  Dim device As Pointer
Dim ao_sf As New Ao_sample_format
+
  Dim ao_sf As New Ao_sample_format
Dim default_driver, buf_size, sample, i, err As Integer
+
  Dim default_driver, buf_size, sample, i, err As Integer
Dim freq As Single = 440.0
+
  Dim freq As Single = 440.0
Dim buffer As Byte[]
+
  Dim buffer As Byte[]
 
+
 
 
  <FONT Color=gray>' ''Inizializza la libreria 'libao':''</font>
 
  <FONT Color=gray>' ''Inizializza la libreria 'libao':''</font>
 
   ao_initialize()
 
   ao_initialize()
Riga 183: Riga 179:
 
  <FONT Color=gray>' ''Apre il driver:''</font>
 
  <FONT Color=gray>' ''Apre il driver:''</font>
 
   device = ao_open_live(default_driver, 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 device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 
   
 
   
 
   err = ao_plugin_test()
 
   err = ao_plugin_test()
Riga 213: Riga 209:
 
   If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
 
   If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
 
    
 
    
 
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
 
   ao_plugin_close(device)
 
   ao_plugin_close(device)
Riga 219: Riga 214:
 
   ao_shutdown()
 
   ao_shutdown()
 
    
 
    
  '''End'''
+
  End
 
 
  
  

Versione delle 09:45, 31 ott 2023

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

E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "libao:4.1.1 ".


Per generare ed eseguire un'onda sinusoidale con le risorse del API di Libao, sono possibili almeno due modalità.

1a modalità

La prima modalità prevede l'uso nell'applicativo Gambas della sola principale libreria dinamica condivisa: "libao.so.4.1.1 ":

Library "libao:4.1.1"

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

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 device == 0 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


2a modalità

La seconda modalità prevede l'uso sia della libreria "libao.so.4.1.1 " che della libreria "libalsa.so ".

Library "libao:4.1.1"

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
 
Private Const AO_FMT_LITTLE As Integer = 1

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

' int ao_default_driver_id(void)
' Get information about a particular driver.
Private Extern ao_default_driver_id() As Integer

' ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)
' Open a live playback audio device for output.
Private Extern ao_open_live(driver_id As Integer, fmt As Ao_sample_format, option As Pointer) As Pointer
 
' void ao_shutdown(void)
' Unloads all of the plugins and deallocates any internal data structures the library has created.
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, 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 device == 0 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)
  
' 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_plugin_play(device, buffer.Data, buf_size)
 If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
  
' Va in chiusura:
 ao_plugin_close(device)
 ao_plugin_device_clear(device)
 ao_shutdown()
 
End


Riferimenti