Differenze tra le versioni di "Estrarre informazioni da un file OggVorbis con le funzioni esterne del API di Vorbisfile"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
La libreria di '''VorbisFile''' consente di estrarre informazioni di carattere generale da un file audio OggVorbis.
 
La libreria di '''VorbisFile''' consente di estrarre informazioni di carattere generale da un file audio OggVorbis.
 
  
 
Per poter utilizzare le risorse del API di ''VorbisFile'', bisognerà richiamare la libreria attualmente alla versione: ''libvorbisfile.so.3.3.4''
 
Per poter utilizzare le risorse del API di ''VorbisFile'', bisognerà richiamare la libreria attualmente alla versione: ''libvorbisfile.so.3.3.4''
Riga 6: Riga 5:
  
 
Vediamo di seguito un esempio di codice.
 
Vediamo di seguito un esempio di codice.
 
+
Library "libvorbisfile:3.3.4"
Poiché l'uso della libreria esterna ''libvorbisfile'' prevede il richiamo diretto ed indiretto di alcune Strutture, ed al fine di poter gestire dette Strutture esterne in modo assolutamente sicuro, ci serviremo di un'apposita libreria esterna scritta in C, che realizzeremo ''ad hoc'' e che richiameremo all'interno del codice Gambas.
+
La libreria ''ad hoc'' per la [[Gestire_con_sicurezza_le_Strutture_esterne|''sicura'' gestione delle Strutture]] di ''Vorbis'' sarà la seguente:
+
Public Struct vorbis_comment
#include <vorbis/vorbisfile.h>
+
  user_comments As Pointer
  #include "vorbis/codec.h"
+
  comment_lengths As Pointer
 +
  comments As Integer
 +
  vendor As Pointer
 +
End Struct
 +
 +
Public Struct ogg_sync_state
 +
  data As Pointer
 +
  storage As Integer
 +
  fill As Integer
 +
  returned As Integer
 +
  unisynced As Integer
 +
  headerbytes As Integer
 +
  bodybytes As Integer
 +
End Struct
 +
 +
Public Struct ogg_stream_state
 +
  body_data As Pointer
 +
  body_storage As Long
 +
  body_fill As Long
 +
  body_returned As Long
 +
  lacing_vals As Pointer
 +
  granule_vals As Pointer
 +
  lacing_storage As Long
 +
  lacing_fill As Long
 +
  lacing_packet As Long
 +
  lacing_returned As Long
 +
  header[282] As Byte
 +
  header_fill As Integer
 +
  e_o_s As Integer
 +
  b_o_s As Integer
 +
  serialno As Long
 +
  pageno As Long
 +
  packetno As Long
 +
  granulepos As Long
 +
  End Struct
 
   
 
   
 +
Public Struct vorbis_dsp_state
 +
  analysisp As Integer
 +
  vi As Pointer
 +
  pcm As Pointer
 +
  pcmret As Pointer
 +
  pcm_storage As Integer
 +
  pcm_current As Integer
 +
  pcm_returned As Integer
 +
  preextrapolate As Integer
 +
  eofflag As Integer
 +
  lW As Long
 +
  W As Long
 +
  nW As Long
 +
  centerW As Long
 +
  granulepos As Long
 +
  sequence As Long
 +
  glue_bits As Long
 +
  time_bits As Long
 +
  floor_bits As Long
 +
  res_bits As Long
 +
End Struct
 
   
 
   
  int Dim_OggVorbis_File() {
+
  Public Struct oggpack_buffer
   return sizeof(OggVorbis_File);
+
   endbyte As Long
  }
+
  endbit As Integer
 +
  buffer As Pointer
 +
  ptr As Pointer
 +
  storage As Long
 +
  End Struct
 
   
 
   
  long Lungh_Header(OggVorbis_File *ov, int i) {
+
  Public Struct vorbis_block
    return (ov->dataoffsets[i]-ov->offsets[i]);
+
  pcm As Pointer
  }
+
  opb As Struct Oggpack_buffer
 +
  lW As Long
 +
  W As Long
 +
  nW As Long
 +
  pcmend As Integer
 +
  mode As Integer
 +
  eofflag As Integer
 +
  granulepos As Long
 +
  sequence As Long
 +
  vd As Pointer
 +
  localstore As Pointer
 +
  localtop As Long
 +
  localalloc As Long
 +
  totaluse As Long
 +
  reap As Pointer
 +
  glue_bits As Long
 +
  time_bits As Long
 +
  floor_bits As Long
 +
  res_bits As Long
 +
  internal As Pointer
 +
End Struct
 +
 +
Public Struct ov_callbacks
 +
  fread As Pointer
 +
  _ov_header_fseek_wrap As Pointer
 +
  void As Pointer
 +
  ftell As Pointer
 +
End Struct
 +
 +
Public Struct OggVorbis_File
 +
  datasource As Pointer
 +
  seekable As Integer
 +
  offset As Long
 +
  end_ As Long
 +
  oy As Struct Ogg_sync_state
 +
  links As Integer
 +
  offsets As Pointer
 +
  dataoffsets As Pointer
 +
  serialnos As Pointer
 +
  pcmlengths As Pointer
 +
  vi As Pointer
 +
  vc As Pointer
 +
  pcm_offset As Long
 +
  ready_state As Integer
 +
  current_serialno As Long
 +
  current_link As Integer
 +
  bittrack As Float
 +
  samptrack As Float
 +
  os As Struct Ogg_stream_state
 +
  vd As Struct Vorbis_dsp_state
 +
  vb As Struct Vorbis_block
 +
  callbacks As Struct Ov_callbacks
 +
  End Struct
 
   
 
   
char * Obt_Comm(vorbis_comment * p) {
 
  return p->vendor;
 
}
 
Tale codice in linguaggio C in questo esempio verrà posto nella cartella Dati dell'applicativo Gambas, ed andrà poi trasformato in una libreria condivisa .so , che - come già accennato - sarà richiamata nel codice Gambas.
 
 
 
Il codice Gambas dell'applicativo sarà invece il seguente:
 
 
  Public Struct vorbis_info
 
  Public Struct vorbis_info
 
   version As Integer
 
   version As Integer
Riga 37: Riga 140:
 
   bitrate_window As Long
 
   bitrate_window As Long
 
  End Struct
 
  End Struct
+
 
+
  <FONT color=gray>' ''int ov_fopen(const char *path,OggVorbis_File *vf)''
Library "libvorbisfile:3.3.4"
+
  ' ''Opens and initialize an OggVorbis_File structure.''</font>
+
  Private Extern ov_fopen(path As String, vf As OggVorbis_File) As Integer
  <FONT color=gray>' ''int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes)''
 
  ' ''Initializes an OggVorbis_File structure and prepare a bitstream for playback.''</font>
 
  Private Extern ov_open(fP As Pointer, vf As Pointer, initial As String, ibytes As Long) As Integer
 
 
   
 
   
 
  <FONT color=gray>' ''long ov_seekable(OggVorbis_File *vf)''
 
  <FONT color=gray>' ''long ov_seekable(OggVorbis_File *vf)''
Riga 56: Riga 156:
 
  ' ''Returns the total pcm samples of the physical bitstream or a specified logical bitstream.''</font>
 
  ' ''Returns the total pcm samples of the physical bitstream or a specified logical bitstream.''</font>
 
  Private Extern ov_pcm_total(vf As Pointer, i As Integer) As Long
 
  Private Extern ov_pcm_total(vf As Pointer, i As Integer) As Long
 
<FONT color=gray>' ''double ov_time_total(OggVorbis_File *vf,int i)''
 
' ''Returns the total time in seconds of the physical bitstream or a specified logical bitstream.''</font>
 
Private Extern ov_time_total(vf As Pointer, i As Integer) As Float
 
 
   
 
   
 
  <FONT color=gray>' ''vorbis_info *ov_info(OggVorbis_File *vf,int link)''
 
  <FONT color=gray>' ''vorbis_info *ov_info(OggVorbis_File *vf,int link)''
Riga 76: Riga 172:
 
  ' ''Returns the total (compressed) bytes of the physical bitstream or a specified logical bitstream.''</font>
 
  ' ''Returns the total (compressed) bytes of the physical bitstream or a specified logical bitstream.''</font>
 
  Private Extern ov_raw_total(vf As Pointer, i As Integer) As Long
 
  Private Extern ov_raw_total(vf As Pointer, i As Integer) As Long
 +
 
 +
<FONT color=gray>' ''double ov_time_total(OggVorbis_File *vf,int i)''
 +
' ''Returns the total time in seconds of the physical bitstream or a specified logical bitstream.''</font>
 +
Private Extern ov_time_total(vf As Pointer, i As Integer) As Float
 
   
 
   
 
  <FONT color=gray>' ''vorbis_comment *ov_comment(OggVorbis_File *vf,int link)''
 
  <FONT color=gray>' ''vorbis_comment *ov_comment(OggVorbis_File *vf,int link)''
 
  ' ''Returns a pointer to the vorbis_comment struct for the specified bitstream.''</font>
 
  ' ''Returns a pointer to the vorbis_comment struct for the specified bitstream.''</font>
 
  Private Extern ov_comment(OggVorbis_File As Pointer, lnk As Integer) As Pointer
 
  Private Extern ov_comment(OggVorbis_File As Pointer, lnk As Integer) As Pointer
 
 
 
Library "libc:6"
 
 
<FONT color=gray>' ''FILE *fopen(const char *path, const char *mode)''
 
' ''Apre il file path associandolo ad uno stream.''</font>
 
Private Extern fopen(path As String, mode As String) As Pointer
 
 
<FONT color=gray>' ''int fclose(FILE *stream)''
 
' ''Chiude il file associato a stream.''</font>
 
Private Extern fclose(stmP As Pointer) As Integer
 
 
 
 
Library "/tmp/libadhoc"
 
 
Private Extern Dim_OggVorbis_File() As Integer
 
Private Extern Lungh_Header(OggVorbis_File As Pointer, n As Integer) As Long
 
Private Extern Obt_Comm(p As Pointer) As Pointer
 
 
   
 
   
 
   
 
   
Riga 105: Riga 185:
 
   
 
   
 
   Dim percorsoFileOGG As String
 
   Dim percorsoFileOGG As String
 +
  Dim ovf As New OggVorbis_File
 
   Dim err, i As Integer
 
   Dim err, i As Integer
   Dim ov, fin, commP, vend As Pointer
+
   Dim vi As Vorbis_info
   Dim vi As New Vorbis_info
+
   Dim vc As Vorbis_comment
+
   
 
+
  percorsoFileOGG = "''/percorso/del/file.ogg''"
    Shell "gcc -o /tmp/libadhoc.so " & Application.path &/ "libadhoc.c -shared -fPIC" Wait
+
 
   
+
  err = ov_fopen(percorsoOGG, ovf)
    percorsoFileOGG = "''/percorso/del/file.ogg''"
+
  If err < 0 Then Error.Raise("Impossibile aprire il file: " & percorsoFileOGG & " !")
 
    fin = fopen(percorsoFileOGG, "rb")
 
 
      
 
      
    ov = Alloc(Dim_OggVorbis_File())
+
  Print "File audio: "; percorsoFileOGG
+
  Print "\n==Caratteristiche del file audio==\n"
    err = ov_open(fin, ov, Null, -1)
+
 
    If err < 0 Then Error.Raise("Impossibile aprire il file: " & percorsoFileOGG & " !")
+
  If ov_seekable(ov) Then
 +
    Print "Il flusso di dati in ingresso è contenuto in "; ov_streams(ov); " sezioni logiche di dati"
 +
    Print "Totale campioni di dati: "; ov_pcm_total(ov, -1)
 +
  Else
 +
    Print "Il file non ha consentito la ricerca."
 +
    Print "Informazioni sul primo flusso di dati logico:\n"
 +
  Endif
 
      
 
      
    Print "File audio: "; percorsoFileOGG
+
  For i = 0 To ov_streams(ov) - 1
    Print "\n==Caratteristiche del file audio==\n"
+
    vi = ov_info(ov, i)
+
    Print "\n\tInformazioni sulla sezione "; i + 1; " di dati logici:"
    If ov_seekable(ov) Then
+
    Print "\t\tFrequenza di campionamento: Hz "; vi.rate
      Print "Il flusso di dati in ingresso è contenuto in "; ov_streams(ov); " sezioni logiche di dati"
+
    Print "\t\tCanali di uscita: "; vi.channels
      Print "Totale campioni di dati: "; ov_pcm_total(ov, -1)
+
    Print "\t\tBitrate: "; ov_bitrate(ov, i) \ 1000; " kbps"
      Print "Durata totale di esecuzione dei dati: "; Fix(ov_time_total(ov, -1)); " secondi"
+
    Print "\t\tNumero seriale: "; ov_serialnumber(ov, i)
    Else
+
    Print "\t\tLunghezza dell'header: "; Lungh_Header(ov, i); " byte"
      Print "Il file non ha consentito la ricerca."
+
    Print "\t\tLunghezza file compresso: "; ov_raw_total(ov, i); " byte"
      Print "Informazioni sul primo flusso di dati logico:\n"
+
    Print "\t\tDurata dell'esecuzione: "; CStr(Date(0, 0, 0, 0, 0, 0, ov_time_total(ovf, i) * 1000))
    Endif
+
    vc = ov_comment(ovf, -1)
 +
    Print "\t\tCodificato con "; String@(vc.vendor)
 +
  Next
 
      
 
      
    For i = 0 To ov_streams(ov) - 1
 
     
 
      vi = ov_info(ov, i)
 
      Print "\n\tInformazioni sulla sezione "; i + 1; " di dati logici:"
 
      Print "\t\tFrequenza di campionamento: Hz "; vi.rate
 
      Print "\t\tCanali di uscita: "; vi.channels
 
      Print "\t\tBitrate: "; ov_bitrate(ov, i) \ 1000; " kbps"
 
      Print "\t\tNumero seriale: "; ov_serialnumber(ov, i)
 
      Print "\t\tLunghezza dell'header: "; Lungh_Header(ov, i); " byte"
 
      Print "\t\tLunghezza file compresso: "; ov_raw_total(ov, i); " byte"
 
      Print "\t\tDurata dell'esecuzione: "; Fix(ov_time_total(ov, i)); " secondi"
 
      commP = ov_comment(ov, -1)
 
      vend = Obt_Comm(commP)
 
      Print "\t\tCodificato con "; String@(vend)
 
    Next
 
 
<FONT color=gray>' ''Va in chiusura:''</font>
 
    fclose(fin)
 
    Free(ov)
 
 
 
  '''End'''
 
  '''End'''
 +
  
  

Versione delle 20:23, 8 lug 2017

La libreria di VorbisFile consente di estrarre informazioni di carattere generale da un file audio OggVorbis.

Per poter utilizzare le risorse del API di VorbisFile, bisognerà richiamare la libreria attualmente alla versione: libvorbisfile.so.3.3.4


Vediamo di seguito un esempio di codice.

Library "libvorbisfile:3.3.4"

Public Struct vorbis_comment
  user_comments As Pointer
  comment_lengths As Pointer
  comments As Integer
  vendor As Pointer
End Struct

Public Struct ogg_sync_state
  data As Pointer
  storage As Integer
  fill As Integer
  returned As Integer
  unisynced As Integer
  headerbytes As Integer
  bodybytes As Integer
End Struct

Public Struct ogg_stream_state
  body_data As Pointer
  body_storage As Long
  body_fill As Long
  body_returned As Long
  lacing_vals As Pointer
  granule_vals As Pointer
  lacing_storage As Long
  lacing_fill As Long
  lacing_packet As Long
  lacing_returned As Long
  header[282] As Byte
  header_fill As Integer
  e_o_s As Integer
  b_o_s As Integer
  serialno As Long
  pageno As Long
  packetno As Long
  granulepos As Long
End Struct

Public Struct vorbis_dsp_state
  analysisp As Integer
  vi As Pointer
  pcm As Pointer
  pcmret As Pointer
  pcm_storage As Integer
  pcm_current As Integer
  pcm_returned As Integer
  preextrapolate As Integer
  eofflag As Integer
  lW As Long
  W As Long
  nW As Long
  centerW As Long
  granulepos As Long
  sequence As Long
  glue_bits As Long
  time_bits As Long
  floor_bits As Long
  res_bits As Long
End Struct

Public Struct oggpack_buffer
  endbyte As Long
  endbit As Integer
  buffer As Pointer
  ptr As Pointer
  storage As Long
End Struct

Public Struct vorbis_block
  pcm As Pointer
  opb As Struct Oggpack_buffer
  lW As Long
  W As Long
  nW As Long
  pcmend As Integer
  mode As Integer
  eofflag As Integer
  granulepos As Long
  sequence As Long
  vd As Pointer
  localstore As Pointer
  localtop As Long
  localalloc As Long
  totaluse As Long
  reap As Pointer
  glue_bits As Long
  time_bits As Long
  floor_bits As Long
  res_bits As Long
  internal As Pointer
End Struct

Public Struct ov_callbacks
  fread As Pointer
  _ov_header_fseek_wrap As Pointer
  void As Pointer
  ftell As Pointer
End Struct

Public Struct OggVorbis_File
  datasource As Pointer
  seekable As Integer
  offset As Long
  end_ As Long
  oy As Struct Ogg_sync_state
  links As Integer
  offsets As Pointer
  dataoffsets As Pointer
  serialnos As Pointer
  pcmlengths As Pointer
  vi As Pointer
  vc As Pointer
  pcm_offset As Long
  ready_state As Integer
  current_serialno As Long
  current_link As Integer
  bittrack As Float
  samptrack As Float
  os As Struct Ogg_stream_state
  vd As Struct Vorbis_dsp_state
  vb As Struct Vorbis_block
  callbacks As Struct Ov_callbacks
End Struct

Public Struct vorbis_info
  version As Integer
  channels As Integer
  rate As Long
  bitrate_upper As Long
  bitrate_nominal As Long
  bitrate_lower As Long
  bitrate_window As Long
End Struct
 
' int ov_fopen(const char *path,OggVorbis_File *vf)
' Opens and initialize an OggVorbis_File structure.
Private Extern ov_fopen(path As String, vf As OggVorbis_File) As Integer

' long ov_seekable(OggVorbis_File *vf)
' This indicates whether or not the bitstream is seekable.
Private Extern ov_seekable(vf As Pointer) As Long

' long ov_streams(OggVorbis_File *vf)
' Returns the number of logical bitstreams within our physical bitstream.
Private Extern ov_streams(vf As Pointer) As Long

' ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i)
' Returns the total pcm samples of the physical bitstream or a specified logical bitstream.
Private Extern ov_pcm_total(vf As Pointer, i As Integer) As Long

' vorbis_info *ov_info(OggVorbis_File *vf,int link)
' Returns the vorbis_info struct for the specified bitstream.
Private Extern ov_info(vf As Pointer, linkI As Integer) As Vorbis_info

' long ov_bitrate(OggVorbis_File *vf,int i)
' Returns the average bitrate for the specified logical bitstream.
Private Extern ov_bitrate(vf As Pointer, i As Integer) As Long

' long ov_serialnumber(OggVorbis_File *vf,int i)
' Returns the serialnumber of the specified logical bitstream link number within the overall physical bitstream.
Private Extern ov_serialnumber(vf As Pointer, i As Integer) As Long

' ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i)
' Returns the total (compressed) bytes of the physical bitstream or a specified logical bitstream.
Private Extern ov_raw_total(vf As Pointer, i As Integer) As Long
 
' double ov_time_total(OggVorbis_File *vf,int i)
' Returns the total time in seconds of the physical bitstream or a specified logical bitstream.
Private Extern ov_time_total(vf As Pointer, i As Integer) As Float

' vorbis_comment *ov_comment(OggVorbis_File *vf,int link)
' Returns a pointer to the vorbis_comment struct for the specified bitstream.
Private Extern ov_comment(OggVorbis_File As Pointer, lnk As Integer) As Pointer


Public Sub Main()

 Dim percorsoFileOGG As String
 Dim ovf As New OggVorbis_File
 Dim err, i As Integer
 Dim vi As Vorbis_info
 Dim vc As Vorbis_comment
    
  percorsoFileOGG = "/percorso/del/file.ogg"
  
  err = ov_fopen(percorsoOGG, ovf)
  If err < 0 Then Error.Raise("Impossibile aprire il file: " & percorsoFileOGG & " !")
   
  Print "File audio: "; percorsoFileOGG
  Print "\n==Caratteristiche del file audio==\n"
  
  If ov_seekable(ov) Then
    Print "Il flusso di dati in ingresso è contenuto in "; ov_streams(ov); " sezioni logiche di dati"
    Print "Totale campioni di dati: "; ov_pcm_total(ov, -1)
  Else
    Print "Il file non ha consentito la ricerca."
    Print "Informazioni sul primo flusso di dati logico:\n"
  Endif
   
  For i = 0 To ov_streams(ov) - 1
    vi = ov_info(ov, i)
    Print "\n\tInformazioni sulla sezione "; i + 1; " di dati logici:"
    Print "\t\tFrequenza di campionamento: Hz "; vi.rate
    Print "\t\tCanali di uscita: "; vi.channels
    Print "\t\tBitrate: "; ov_bitrate(ov, i) \ 1000; " kbps"
    Print "\t\tNumero seriale: "; ov_serialnumber(ov, i)
    Print "\t\tLunghezza dell'header: "; Lungh_Header(ov, i); " byte"
    Print "\t\tLunghezza file compresso: "; ov_raw_total(ov, i); " byte"
    Print "\t\tDurata dell'esecuzione: "; CStr(Date(0, 0, 0, 0, 0, 0, ov_time_total(ovf, i) * 1000))
    vc = ov_comment(ovf, -1)
    Print "\t\tCodificato con "; String@(vc.vendor)
  Next
   
End



Riferimenti

[1] Vorbisfile Documentation