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 generare ed eseguire un'onda sinusoidale con le risorse del API di ''Libao'', sono possibili almeno due modalità.
+
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à.
  
 
===1<SUP>a</sup> modalità===
 
===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''"
+
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"
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 16: Riga 17:
 
   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
 
  Private Const AO_FMT_LITTLE As Integer = 1
Riga 104: Riga 102:
  
 
===2<SUP>a</sup> modalità===
 
===2<SUP>a</sup> modalità===
La seconda modalità prevede l'uso sia della libreria "''libao.so.4.0.0''" che della libreria "''libalsa.so''".
+
La seconda modalità prevede l'uso sia della libreria "''libao.so.4.1.1''" che della libreria "''libalsa.so''".
 
+
Library "libao:4.1.1"
 
+
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 116: Riga 113:
 
  End Struct
 
  End Struct
 
    
 
    
 
Library "libao:4.0.0"
 
 
 
  Private Const AO_FMT_LITTLE As Integer = 1
 
  Private Const AO_FMT_LITTLE As Integer = 1
 
   
 
   

Versione delle 12:06, 18 ago 2018

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