Ottenere informazioni su uno specifico campione audio mediante le funzioni esterne del API di PulseAudio

Da Gambas-it.org - Wikipedia.

Alcune funzioni esterne del API di PulseAudio consentono di ottenere alcune informazioni relative ad un campione audio grezzo, del quale siano state passate le tre caratteristiche principali (frequenza di campionamento, risoluzione in bit, numero canali).

E' necessario in questo caso avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "libpulse.so.0.24.2 ".

Mostriamo un esempio pratico:

Library "libpulse:0.24.2"

Private Enum PA_SAMPLE_U8, PA_SAMPLE_ALAW, PA_SAMPLE_ULAW, PA_SAMPLE_S16LE,
             PA_SAMPLE_S16BE, PA_SAMPLE_FLOAT32LE, PA_SAMPLE_FLOAT32BE,
             PA_SAMPLE_S32LE, PA_SAMPLE_S32BE, PA_SAMPLE_S24LE, PA_SAMPLE_S24BE,
             PA_SAMPLE_S24_32LE, PA_SAMPLE_S24_32BE

Public Struct pa_sample_spec
  formato As Integer
  rate As Integer
  channels As Byte
End Struct

' const char *pa_sample_format_to_string(pa_sample_format_t f)
' Return a descriptive string for the specified sample format.
Private Extern pa_sample_format_to_string(f As Integer) As String

' size_t pa_bytes_per_second(const pa_sample_spec *spec)
' Return the amount of bytes playback of a second of audio with the specified sample type takes.
Private Extern pa_bytes_per_second(spec As Pa_sample_spec) As Long

' size_t pa_frame_size(const pa_sample_spec *spec)
' Return the size of a frame with the specific sample type.
Private Extern pa_frame_size(spec As Pa_sample_spec) As Long

' size_t pa_sample_size(const pa_sample_spec *spec)
' Return the size of a sample with the specific sample type.
Private Extern pa_sample_size(spec As Pa_sample_spec) As Long

' pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec)
' Calculate the time the specified bytes take to play with the specified sample type.
Private Extern pa_bytes_to_usec(length As Long, spec As Pa_sample_spec) As Long

' size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec)
' Calculates the number of bytes that are required for the specified time.
Private Extern pa_usec_to_bytes(t As Long, spec As Pa_sample_spec) As Long


Public Sub Main()
 
 Dim pa_ssp As New Pa_sample_spec
 Dim byte_rate As Long
 
' Impostiamo le caratteristiche del campione audio da analizzare:
 With pa_ssp
   .formato = PA_SAMPLE_S16LE
   .rate = 44100
   .channels = 2
 End With
  
 Print "Valori riferiti ad 1 campione audio\n"
 Print "Formato del campione audio:              "; pa_sample_format_to_string(pa_ssp.formato)
 byte_rate = pa_bytes_per_second(pa_ssp)
 Print "Quantità di byte eseguiti in un secondo: "; byte_rate
 Print "Dimensione di un frame (block align):    "; pa_frame_size(pa_ssp); " byte"
 Print "Dimensione di un singolo campione:       "; pa_sample_size(pa_ssp); " byte"
 Print "Microsecondi per eseguire 1 'byte rate': "; pa_bytes_to_usec(byte_rate, pa_ssp)
 Print "Quantità di byte per eseguire 1 secondo: "; pa_usec_to_bytes(1000000, pa_ssp)
  
End