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

Da Gambas-it.org - Wikipedia.
 
(9 versioni intermedie di uno stesso utente non sono mostrate)
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''" .
+
E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "''libao:4.1.1'' ".
  
Di seguito un possibile codice per generare con le risorse della libreria ''libao'' un'onda sinusoidale:
+
 
  '''Public''' Struct ao_sample_format
+
Per generare ed eseguire un'onda sinusoidale con le risorse del API di ''Libao'', sono possibili almeno due modalità.
 +
 
 +
===1<SUP>a</sup> 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            <FONT color=gray>' ''bits per sample''</font>
 
   bits As Integer            <FONT color=gray>' ''bits per sample''</font>
 
   rate As Integer            <FONT color=gray>' ''samples per second (in a single channel)''</font>
 
   rate As Integer            <FONT color=gray>' ''samples per second (in a single channel)''</font>
Riga 10: Riga 16:
 
   byte_format As Integer      <FONT color=gray>' ''Byte ordering in sample, see constants below''</font>
 
   byte_format As Integer      <FONT color=gray>' ''Byte ordering in sample, see constants below''</font>
 
   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
 
   
 
   
  '''Private''' ao_sf As Struct Ao_sample_format
+
  Private Const AO_FMT_LITTLE As Integer = 1
 
   
 
   
 +
<FONT color=gray>' ''void ao_initialize(void)''
 +
' ''Library setup.''</font>
 +
Private Extern ao_initialize()
 
   
 
   
  Library "libao:4.0.0"
+
  <FONT color=gray>' ''int ao_default_driver_id(void)''
 +
' ''Driver information.''</font>
 +
Private Extern ao_default_driver_id() As Integer
 
   
 
   
  '''Private''' Const AO_FMT_LITTLE As Integer = 1
+
  <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>' ''void ao_initialize(void)''</font>
+
  <FONT color=gray>' ''int ao_play(ao_device *device, char *output_samples, uint_32 num_bytes)''
  <FONT color=gray>' ''library setup''</font>
+
  ' ''Write samples to the device. Channels are interleaved. 1 indicates success.''</font>
  '''Private''' Extern ao_initialize()
+
  Private Extern ao_play(device As Pointer, output_samples As Pointer, num_bytes As Integer) As Integer
 
   
 
   
  <FONT color=gray>' ''int ao_default_driver_id(void)''</font>
+
  <FONT color=gray>' ''int ao_close(ao_device *device)''
  <FONT color=gray>' ''driver information''</font>
+
' ''Closes audio device.''</font>
  '''Private''' Extern ao_default_driver_id() As Integer
+
Private Extern ao_close(device As Pointer) As Integer
 +
 
 +
  <FONT color=gray>' ''void ao_shutdown(void)''
 +
' ''Library teardown.''</font>
 +
  Private Extern ao_shutdown()
 
   
 
   
<FONT color=gray>' ''ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)''</font>
+
 
<FONT color=gray>' ''driver information''</font>
+
  Public Sub Main()
  '''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)''</font>
+
  Dim device As Pointer
  <FONT color=gray>' ''Write samples to the device. Channels are interleaved. 1 indicates success.''</font>
+
  Dim ao_sf As New Ao_sample_format
'''Private''' Extern ao_play(device As Pointer, output_samples As Byte[], num_bytes As Integer) As Integer
+
  Dim default_driver, buf_size, sample, i, err As Integer
 +
  Dim freq As Single = 440.0
 +
  Dim buffer As Byte[]
 +
   
 +
  <FONT color=gray>' ''Inizializza la libreria 'libao':''</font>
 +
  ao_initialize()
 +
   
 +
  <FONT color=gray>' ''Imposta il driver audio come predefinito:''</font>
 +
  default_driver = ao_default_driver_id()
 
   
 
   
  <FONT color=gray>' ''int ao_close(ao_device *device)''</font>
+
  <FONT color=gray>' ''Imposta le caratteristiche audio in uscita:''</font>
<FONT color=gray>' ''Chiude il dispositivo audio''</font>
+
  With ao_sf
'''Private''' Extern ao_close(device As Pointer) As Integer
+
    .bits = 16
 +
    .rate = 44100
 +
    .channels = 2
 +
    .byte_format = AO_FMT_LITTLE
 +
  End With
 
   
 
   
  <FONT color=gray>' ''void ao_shutdown(void)''</font>
+
  <FONT color=gray>' ''Apre il driver:''</font>
<FONT color=gray>' ''library teardown''</font>
+
  device = ao_open_live(default_driver, ao_sf, 0)
'''Private''' Extern ao_shutdown()
+
  If device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 
   
 
   
 +
<FONT color=gray>' ''Elabora il suono:''</font>
 +
  buf_size = ao_sf.bits / 8 * ao_sf.channels * ao_sf.rate
 
   
 
   
  '''Public''' Sub Main()
+
  buffer = New Byte[buf_size]
 +
   
 +
  For i = 0 To ao_sf.rate - 1
 +
  <FONT color=gray>' ''Genera l'onda sinusoidale:''</font>
 +
    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>
 +
    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 !")
 
   
 
   
   Dim device As Pointer
+
<FONT color=gray>' ''Va in chiusura:''</font>
   Dim default_driver, buf_size, sample, i, err As Integer
+
  ao_close(device)
   Dim buffer As Byte[]
+
  ao_shutdown()
   Dim freq As Single = 440.0
+
 +
End
 +
 
 +
 
 +
 
 +
===2<SUP>a</sup> 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            <FONT Color=gray>' ''bits per sample''</font>
 +
   rate As Integer             <FONT Color=gray>' ''samples per second (in a single channel)''</font>
 +
   channels As Integer        <FONT Color=gray>' ''number of audio channels''</font>
 +
  byte_format As Integer      <FONT Color=gray>' ''Byte ordering in sample, see constants below''</font>
 +
   matrix As Pointer          <FONT Color=gray>' ''input channel location/ordering''</font>
 +
End Struct
 +
 
 +
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)''
 +
' ''Get information about a particular driver.''</font>
 +
Private Extern ao_default_driver_id() As Integer
 +
 +
<FONT Color=gray>' ''ao_device * ao_open_live(int driver_id, ao_sample_format *format, ao_option *option)''
 +
' ''Open a live playback audio device for output.''</font>
 +
Private Extern ao_open_live(driver_id As Integer, fmt As Ao_sample_format, option As Pointer) As Pointer
 
    
 
    
 +
<FONT Color=gray>' ''void ao_shutdown(void)
 +
' ''Unloads all of the plugins and deallocates any internal data structures the library has created.''</font>
 +
Private Extern ao_shutdown()
 +
 
   
 
   
  <FONT color=gray>' ''Inizializza la libreria 'libao':''</font>
+
Library "/usr/lib/x86_64-linux-gnu/ao/plugins-4/libalsa"
    ao_initialize()
+
   
+
<FONT Color=gray>' ''int ao_plugin_test()''
  <FONT color=gray>' ''Imposta il driver audio come predefinito:''</font>
+
' ''Determine if parameters are requires for this particular plugin.''</font>
     default_driver = ao_default_driver_id()
+
Private Extern ao_plugin_test() As Integer
 +
 +
<FONT Color=gray>' ''int ao_plugin_device_init(ao_device *device)''
 +
' ''Initialize internal data structures.''</font>
 +
Private Extern ao_plugin_device_init(ao_dev As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int ao_plugin_open(ao_device *device, ao_sample_format *format)''
 +
' Prepare the audio device for playback.''</font>
 +
Private Extern ao_plugin_open(ao_dev As Pointer, fmt As Ao_sample_format) As Integer
 +
 +
<FONT Color=gray>' ''int ao_plugin_play(ao_device *device, const char *output_samples, uint_32 num_bytes)''
 +
' Play num_bytes of audio data.''</font>
 +
Private Extern ao_plugin_play(ao_dev As Pointer, output_samples As Pointer, num_bytes As Integer) As Integer
 +
 +
<FONT Color=gray>' ''int ao_plugin_close(ao_device *device)''
 +
' ''Close the audio device.''</font>
 +
Private Extern ao_plugin_close(ao_dev As Pointer) As Integer
 +
 +
  <FONT Color=gray>' ''void ao_plugin_device_clear(ao_device *device)''
 +
' ''Free the internal data structures.''</font>
 +
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[]
 +
 
 +
<FONT Color=gray>' ''Inizializza la libreria 'libao':''</font>
 +
  ao_initialize()
 +
 
 +
  <FONT Color=gray>' ''Imposta il driver audio come predefinito:''</font>
 +
  default_driver = ao_default_driver_id()
 +
 +
<FONT Color=gray>' ''Imposta le caratteristiche audio in uscita:''</font>
 +
  With ao_sf
 +
    .bits = 16
 +
    .channels = 2
 +
     .rate = 44100
 +
    .byte_format = AO_FMT_LITTLE
 +
  End With
 +
 +
<FONT Color=gray>' ''Apre il driver:''</font>
 +
  device = ao_open_live(default_driver, ao_sf, 0)
 +
  If device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 
   
 
   
    With ao_sf
+
  err = ao_plugin_test()
      .bits = 16
+
  If err = 0 Then Error.Raise("Il driver audio ha bisogno che vengano impostate alcune opzioni !")
      .channels = 2
+
 
      .rate = 44100
+
  ao_plugin_device_init(device)
      .byte_format = AO_FMT_LITTLE
+
 
    End With 
+
  ao_plugin_open(device, ao_sf)
 
    
 
    
  <FONT color=gray>' ''Apre il driver:''</font>
+
  <FONT Color=gray>' ''Elabora il suono:''</font>
    device = ao_open_live(default_driver, VarPtr(ao_sf), Null)
+
  buf_size = ao_sf.bits / 8 * ao_sf.channels * ao_sf.rate
    If IsNull(device) Then Error.Raise("Errore nell'apertura del dispositivo audio !")
 
   
 
<FONT color=gray>' ''Elabora il suono:''</font>
 
    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, buf_size)
 
    If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
 
 
      
 
      
 +
  err = ao_plugin_play(device, buffer.Data, buf_size)
 +
  If err < 1 Then Error.Raise("Errore nell'esecuzione dei dati audio !")
 +
 
 +
<FONT Color=gray>' ''Va in chiusura:''</font>
 +
  ao_plugin_close(device)
 +
  ao_plugin_device_clear(device)
 +
  ao_shutdown()
 
    
 
    
  <FONT color=gray>' ''Va in chiusura:''</font>
+
  End
    buffer.Clear()
 
    ao_close(device)
 
    ao_shutdown()
 
 
'''End'''
 
  
  
  
 
=Riferimenti=
 
=Riferimenti=
[1] [http://www.xiph.org/ao/doc/ L'API di LibAo]
+
* http://www.xiph.org/ao/doc
 +
* http://manual.freeshell.org/libao-1.0.0

Versione attuale delle 18:43, 13 gen 2024

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
   .rate = 44100
   .channels = 2
   .byte_format = AO_FMT_LITTLE
 End With

' Apre il driver:
 device = ao_open_live(default_driver, ao_sf, 0)
 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, 0)
 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