Estrarre informazioni generali da un file audio con le funzioni esterne del API di Sox

Da Gambas-it.org - Wikipedia.

La libreria Sox contiene risorse per poter gestire ampiamente i file audio: riproduzione e registrazione, conversione di vari formati audio in altri formati, nonché applicazione di vari effetti.

Le risorse del API di Sox consentono anche di ottenere facilmente le informazioni basilari dai file audio, supportati dalla libreria Sox (sicuramente: AIF, FLAC, OGG, VOC e WAV), richiamando l'attuale versione della libreria: "libsox.so.3.0.0 ".

Potremo procedere con un codice simile al seguente:

Library "libsox:3.0.0"

Public Struct sox_signalinfo_t
  rate As Float         ' sox_rate_t:  samples per second, 0 if unknown (typedef double sox_rate_t)
  channels As Integer   ' number of sound channels, 0 if unknown
  precision As Integer  ' bits per sample, 0 if unknown
  length As Long        ' samples * chans in file, 0 if unknown, -1 if unspecified
  mult As Pointer
End Struct

Public Struct sox_encodinginfo_t
  encoding As Integer
  bits_per_sample As Integer
  compression As Float
  reverse_bytes As Integer
  reverse_nibbles As Integer
  reverse_bits As Integer
  opposite_endian As Integer
End Struct

Public Struct sox_oob_t
  comments As Pointer
  instr As Pointer
  loops[24] As Pointer
End Struct

Public Struct sox_format_handler
  sox_lib_version_code As Integer
  description As Pointer
  names As Pointer
  flags As Integer
  startread As Pointer
  readP As Pointer
  stopread As Pointer
  startwrite As Pointer
  writeP As Pointer
  stopwrite As Pointer
  seekP As Pointer
  write_formats As Pointer
  write_rates As Pointer
  priv_size As Long
End Struct

Public Struct sox_format_t
  filename As Pointer
  signal As Struct Sox_signalinfo_t
  encoding As Struct Sox_encodinginfo_t
  filetype As Pointer
  oob As Struct Sox_oob_t
  seecable As Integer
  mode As Byte
  olength As Long
  clips As Long
  sox_errno As Integer
  sox_errstr[256] As Byte
  fp As Pointer
  io_type As Integer
  tell_off As Long
  data_start As Long
  handler As Struct Sox_format_handler
  priv As Pointer
End Struct

Private Const SOX_SUCCESS As Integer = 0

' int sox_init(void)
' Client API: Initialize effects library. SOX_SUCCESS if successful.
Private Extern sox_init() As Integer

' sox_format_t * sox_open_read(char const *path, sox_signalinfo_t const *signal, sox_encodinginfo_t const *encoding, char const *filetype)
Private Extern sox_open_read(path As String, signalP As Pointer, encoding As Pointer, filetype As String) As Sox_format_t

' int sox_close(sox_format_t * ft)
' Closes an encoding or decoding session.
Private Extern sox_close(ft As Pointer) As Integer

' sox_quit(void)
' Close effects library and unload format handler plugins. Returns SOX_SUCCESS if successful.
Private Extern sox_quit()


Public Sub Main()

 Dim sfIn As New Sox_format_t
 Dim err As Integer
 Dim file_audio As String

 file_audio = "/percorso/del/file/audio"

 err = sox_init()
 If err <> SOX_SUCCESS Then Error.Raise("Impossibile inizializzare la libreria 'libsox' !")

 sfIn = sox_open_read(file_audio, Null, Null, Null)
 If IsNull(sfIn) Then Error.Raise("Errore !")

 With sfIn
   Print "File audio:                 "; String@(.filename)
   Print "Frequenza di campionamento: "; .signal.rate; " hertz"
   Print "Numero di canali:           "; .signal.channels
   Print "Risoluzione campionamento:  "; .signal.precision; " bit"
   Print "Quantità dati audio:        "; .signal.length * .signal.channels; " byte"
   Print "Durata:                     "; Str(Time(0, 0, 0, (((.signal.length * .signal.channels) * 8) / (.signal.rate * .signal.channels * .signal.precision)) * 1000))
 End With

 sox_close(sfIn)
 sox_quit()

End


Riferimenti