Differenze tra le versioni di "Generare un'onda sinusoidale con le funzioni esterne delle 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 seguente libreria dinamica: "''libao.so.4.0.0''"
+
Per generare ed eseguire un'onda sinusoidale con le risorse del API di ''Libao'', sono possibili almeno due modalità.
  
  
Di seguito un possibile codice per generare con le risorse della libreria ''libao'' un'onda sinusoidale:
+
 
 +
===1<SUP>a</sup> modalità===
 +
La prima modalità prevede l'uso nell'applicativo Gambas della sola principale libreria dinamica condivisa: "''libao.so.4.0.0''"
 +
 
 +
Di seguito un possibile codice:
 
  Public Struct ao_sample_format
 
  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>
Riga 95: Riga 99:
 
   ao_shutdown()
 
   ao_shutdown()
 
   
 
   
 +
'''End'''
 +
 +
 +
 +
===2<SUP>a</sup> modalità===
 +
La seconda modalità prevede l'uso sia della libreria "''libao.so.4.0.0''" che della libreria "''libalsa.so''".
 +
 +
 +
Di seguito un possibile codice:
 +
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
 +
 
 +
 +
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)''
 +
' ''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()
 +
 +
 +
Library "/usr/lib/x86_64-linux-gnu/ao/plugins-4/libalsa"
 +
 +
<FONT Color=gray>' ''int ao_plugin_test()''
 +
' ''Determine if parameters are requires for this particular plugin.''</font>
 +
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, 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)
 +
 
 +
<FONT Color=gray>' ''Elabora il suono:''</font>
 +
  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
 +
 +
<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_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()
 +
 
 
  '''End'''
 
  '''End'''
  
Riga 101: Riga 231:
  
 
=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 delle 19:59, 6 gen 2016

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 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.0.0"

Di seguito un possibile codice:

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


2a modalità

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


Di seguito un possibile codice:

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)
' 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 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)
  
' 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