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

Da Gambas-it.org - Wikipedia.
 
(12 versioni intermedie di uno stesso utente non sono mostrate)
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.
  
 +
E' necessario avere installata nel sistema e richiamare nel programma Gambas la libreria condivisa: "''libvorbisfile.so.3.3.8'' ".
  
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 pratico:
 
+
Library "libvorbisfile:3.3.8"
 
+
Vediamo di seguito un esempio di codice.
+
Public Struct vorbis_comment
 
+
  user_comments As Pointer
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.
+
  comment_lengths As Pointer
La libreria ''ad hoc'' per la [[Gestire_con_sicurezza_le_Strutture_esterne|''sicura'' gestione delle Strutture]] di ''Vorbis'' sarà la seguente:
+
  comments As Integer
  #include <vorbis/vorbisfile.h>
+
  vendor As Pointer
  #include "vorbis/codec.h"
+
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
 
   
 
   
  int Dim_OggVorbis_File() {
+
  Public Struct ov_callbacks
   return sizeof(OggVorbis_File);
+
   fread As Pointer
  }
+
  _ov_header_fseek_wrap As Pointer
 +
  void As Pointer
 +
  ftell As Pointer
 +
  End Struct
 
   
 
   
  long Lungh_Header(OggVorbis_File *ov, int i) {
+
  Public Struct OggVorbis_File
    return (ov->dataoffsets[i]-ov->offsets[i]);
+
  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 139:
 
   bitrate_window As Long
 
   bitrate_window As Long
 
  End Struct
 
  End Struct
 +
 
 +
<FONT color=gray>' ''int ov_fopen(const char *path,OggVorbis_File *vf)''
 +
' ''Opens and initialize an OggVorbis_File structure.''</font>
 +
Private Extern ov_fopen(path As String, vf As OggVorbis_File) As Integer
 
   
 
   
 +
<FONT color=gray>' ''vorbis_info *ov_info(OggVorbis_File *vf,int link)''
 +
' ''Returns the vorbis_info struct for the specified bitstream.''</font>
 +
Private Extern ov_info(vf As OggVorbis_File, olink As Integer) As Vorbis_info
 
   
 
   
  Library "libvorbisfile:3.3.4"
+
  <FONT color=gray>' ''ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i)''
 +
' ''Returns the total (compressed) bytes.''</font>
 +
Private Extern ov_raw_total(vf As OggVorbis_File, i As Integer) As Long
 
   
 
   
  ' int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes)
+
  <FONT color=gray>' ''ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i)''
  ' Initializes an OggVorbis_File structure and prepare a bitstream for playback.  
+
  ' ''Returns the total pcm samples.''</font>
  Private Extern ov_open(fP As Pointer, vf As Pointer, initial As String, ibytes As Long) As Integer
+
  Private Extern ov_pcm_total(vf As OggVorbis_File, i As Integer) As Long
 
   
 
   
  ' long ov_seekable(OggVorbis_File *vf)
+
  <FONT color=gray>' ''double ov_time_total(OggVorbis_File *vf,int i)''
' This indicates whether or not the bitstream is seekable.
+
  ' ''Returns the total time in seconds of the physical bitstream or a specified logical bitstream.''</font>
Private Extern ov_seekable(vf As Pointer) As Long
+
  Private Extern ov_time_total(vf As OggVorbis_File, i As Integer) As Float
 
' 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
 
 
' 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_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
 
 
  <FONT color=gray>' ''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.''</font>
 
  Private Extern ov_raw_total(vf As Pointer, i As Integer) As Long
 
 
   
 
   
 
  <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(vf As OggVorbis_File, olink As Integer) As Vorbis_comment
 
   
 
   
 +
<FONT color=gray>' ''long ov_serialnumber(OggVorbis_File *vf,int i)''
 +
' ''Returns the serialnumber of the specified logical bitstream link number within the overall physical bitstream.''</font>
 +
Private Extern ov_serialnumber(vf As OggVorbis_File, i As Integer) As Long
 
   
 
   
 +
<FONT color=gray>' ''int ov_clear(OggVorbis_File *vf)''
 +
' ''Clears the decoder's buffers.''</font>
 +
Private Extern ov_clear(vf As OggVorbis_File) As Integer
 +
 
 
   
 
   
  Library "libc:6"
+
  Public Sub Main()
 
<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
 
 
 
'''Public''' Sub Main()
 
 
  Dim percorsoFileOGG As String
 
  Dim err, i As Integer
 
  Dim ov, fin, commP, vend As Pointer
 
  Dim vi As New Vorbis_info
 
 
   
 
   
 +
  Dim ogg As String
 +
  Dim ovf As New OggVorbis_File
 +
  Dim info As Vorbis_info
 +
  Dim comm As Vorbis_comment
 +
  Dim av As Integer
 
    
 
    
    Shell "gcc -o /tmp/libadhoc.so " & Application.path &/ "libadhoc.c -shared -fPIC" Wait
+
  ogg = "<FONT color=darkgreen>''/percorso/del/file.ogg''</font>"
   
 
    percorsoFileOGG = "''/percorso/del/file.ogg''"
 
 
   
 
   
    fin = fopen(percorsoFileOGG, "rb")
+
  if ov_fopen(ogg, ovf) < 0 Then Error.Raise("Impossibile aprire il file: " & ogg & " !")
   
 
    ov = Alloc(Dim_OggVorbis_File())
 
 
   
 
   
    err = ov_open(fin, ov, Null, -1)
+
  Print "\nFile audio ogg:          "; ogg
     If err < 0 Then Error.Raise("Impossibile aprire il file: " & percorsoFileOGG & " !")
+
  With info = ov_info(ovf, 0)
   
+
     Print "\nNumero canali:          "; .channels
    Print "File audio: "; percorsoFileOGG
+
    Print "Frequenza campinamento:  "; .rate; " hertz"
    Print "\n==Caratteristiche del file audio==\n"
+
    Print "Bitrate nominale:       "; .bitrate_nominal \ 1000; " Kbps"
 +
  End With
 +
  Print "Dati compressi:          "; ov_raw_total(ovf, -1); " byte"
 +
  Print "Dati pcm:                "; ov_pcm_total(ovf, -1); " byte"
 +
  Print "Dimensione totale:      "; Len(File.Load(ogg)); " byte"
 +
  Print "Numero seriale:         "; ov_serialnumber(ovf, -1)
 +
  Print "\nDurata:                  "; Time(0, 0, 0, ov_time_total(ovf, -1) * 1000)
 
   
 
   
     If ov_seekable(ov) Then
+
  With comm = ov_comment(ovf, 0)
       Print "Il flusso di dati in ingresso è contenuto in "; ov_streams(ov); " sezioni logiche di dati"
+
     If .comments > 0 Then
       Print "Totale campioni di dati: "; ov_pcm_total(ov, -1)
+
       Print "\nVendor:                  "; String@(.vendor)
      Print "Durata totale di esecuzione dei dati: "; Fix(ov_time_total(ov, -1)); " secondi"
+
       Print "\nCommenti:"
    Else
+
      For av = 0 To .comments - 1
      Print "Il file non ha consentito la ricerca."
+
        Print String@(Pointer@(comm.user_comments + (8 * av)))
       Print "Informazioni sul primo flusso di dati logico:\n"
+
       Next
 
     Endif
 
     Endif
   
+
  End With
    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>
+
  ov_clear(ovf)  
    fclose(fin)
 
    Free(ov)
 
 
   
 
   
  '''End'''
+
  End
  
  
  
 
=Riferimenti=
 
=Riferimenti=
[1] [http://www.xiph.org/vorbis/doc/vorbisfile/index.html Vorbisfile Documentation]
+
[1] http://www.xiph.org/vorbis/doc/vorbisfile/index.html

Versione attuale delle 05:54, 14 gen 2024

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

E' necessario avere installata nel sistema e richiamare nel programma Gambas la libreria condivisa: "libvorbisfile.so.3.3.8 ".

Vediamo di seguito un esempio pratico:

Library "libvorbisfile:3.3.8"

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

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

' ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i)
' Returns the total (compressed) bytes.
Private Extern ov_raw_total(vf As OggVorbis_File, i As Integer) As Long

' ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i)
' Returns the total pcm samples.
Private Extern ov_pcm_total(vf As OggVorbis_File, 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 OggVorbis_File, 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(vf As OggVorbis_File, olink As Integer) As Vorbis_comment

' 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 OggVorbis_File, i As Integer) As Long

' int ov_clear(OggVorbis_File *vf)
' Clears the decoder's buffers.
Private Extern ov_clear(vf As OggVorbis_File) As Integer
 

Public Sub Main()

 Dim ogg As String
 Dim ovf As New OggVorbis_File
 Dim info As Vorbis_info
 Dim comm As Vorbis_comment
 Dim av As Integer
 
 ogg = "/percorso/del/file.ogg"

 if ov_fopen(ogg, ovf) < 0 Then Error.Raise("Impossibile aprire il file: " & ogg & " !")

 Print "\nFile audio ogg:          "; ogg
 With info = ov_info(ovf, 0)
   Print "\nNumero canali:           "; .channels
   Print "Frequenza campinamento:  "; .rate; " hertz"
   Print "Bitrate nominale:        "; .bitrate_nominal \ 1000; " Kbps"
 End With
 Print "Dati compressi:          "; ov_raw_total(ovf, -1); " byte"
 Print "Dati pcm:                "; ov_pcm_total(ovf, -1); " byte"
 Print "Dimensione totale:       "; Len(File.Load(ogg)); " byte"
 Print "Numero seriale:          "; ov_serialnumber(ovf, -1)
 Print "\nDurata:                  "; Time(0, 0, 0, ov_time_total(ovf, -1) * 1000)

 With comm = ov_comment(ovf, 0)
   If .comments > 0 Then
     Print "\nVendor:                  "; String@(.vendor)
     Print "\nCommenti:"
     For av = 0 To .comments - 1
       Print String@(Pointer@(comm.user_comments + (8 * av)))
     Next
   Endif
 End With

 ov_clear(ovf) 

End


Riferimenti

[1] http://www.xiph.org/vorbis/doc/vorbisfile/index.html