Estrarre informazioni da un file MP3 con le funzioni esterne del API di libmpg123

Da Gambas-it.org - Wikipedia.

Con le funzzioni della libreria libmpg123 è possibile ottenere alcune informazioni generali relative ad un file MP3.

E' necessario avere installata nel sistema e richiamare in Gambas la libreria condivisa: "libmpg123.so.0.48.1 ".


Mostriamo un possibile codice:

Library "libmpg123:0.48.1"

Public Struct mpg123_frameinfo
  version As Integer
  layer As Integer
  rate As Long
  mode As Integer
  mode_ext As Integer
  famesize As Integer
  flags As Integer
  emphasis As Integer
  bitrate As Integer
  abr_rate As Integer
  vbr As Integer
End Struct

Private Const MPG123_OK As Integer = 0

' int mpg123_init (void)
' Function to initialise the mpg123 library.
Private Extern mpg123_init() As Integer

' const char* mpg123_plain_strerror (int errcode)
' Return a string describing that error errcode means.
Private Extern mpg123_plain_strerror(errcode As Integer) As String

' mpg123_handle * mpg123_new (const char *decoder, int *error)
' Create a handle with optional choice of decoder (named by a string)
Private Extern mpg123_new(decoder As String, perror As Pointer) As Pointer

' int mpg123_open (mpg123_handle * mh, const char * path)
' Open and prepare to decode the specified file by filesystem path. This does not open HTTP urls.
Private Extern mpg123_open(mh As Pointer, pathFile As String) As Integer

' const char* mpg123_strerror (mpg123_handle * mh)
' Give string describing what error has occured in the context of handle mh.
Private Extern mpg123_strerror(mh As Pointer) As String

' int mpg123_getformat (mpg123_handle * mh, long * rate, int * channels, int * encoding)
' Get the current output format written to the addresses given.
Private Extern mpg123_getformat(mh As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer

' int mpg123_info (mpg123_handle * mh, struct mpg123_frameinfo * mi)
' Get frame information about the MPEG audio bitstream and store it in a mpg123_frameinfo structure.
Private Extern mpg123_info(mh As Pointer, mi As Mpg123_frameinfo) As Integer

' off_t mpg123_length (mpg123_handle * mh)
' Return, if possible, the full (expected) length of current track in samples.
Private Extern mpg123_length(mh As Pointer) As Long

' int mpg123_close (mpg123_handle * mh)
' Closes the source, if libmpg123 opened it
Private Extern mpg123_close(mh As Pointer) As Integer

' void mpg123_delete (mpg123_handle * mh)
' Delete handle, mh is either a valid mpg123 handle or NULL
Private Extern mpg123_delete(mh As Pointer)

' void mpg123_exit (void)
' Function to close down the mpg123 library.
Private Extern mpg123_exit()


Public Sub Main()
 
 Dim filemp3 As String
 Dim err, channels As Integer
 Dim frequenza As Long
 Dim inf As New Mpg123_frameinfo
 Dim mp As Pointer
 
' Inizializza la liberia di "mpg123":
 err = mpg123_init()
 If err <> MPG123_OK Then Error.Raise("Configurazione di base errata ! " & mpg123_plain_strerror(err))
  
 mp = mpg123_new(Null, 0)
  
 filemp3 = "/percorso/del/file.mp3"

' Apre il file mpeg in lettura:
 err = mpg123_open(mp, filemp3)
 If err <> MPG123_OK Then Error.Raise("Attenzione ! Problemi con 'mpg123': " & mpg123_strerror(mp))
 
 mpg123_getformat(mp, VarPtr(frequenza), VarPtr(channels), 0)
   
 mpg123_info(mp, inf)
 Print "File audio: "; filemp3
 Print "Frequenza:  "; frequenza; " hertz"
 Print "Canali:     "; channels
 Print "Bitrate:    "; inf.bitrate
 Print "MPEG:       "; inf.mode
 Print "Layer:      "; inf.layer
 Print "Durata:     "; Time(0, 0, 0, (mpg123_length(mp) / frequenza) * 1000)
  
   
' Libera la memoria precedentemente occupata:
 mpg123_close(mp)
 mpg123_delete(mp)
 mpg123_exit()
  
End


Riferimenti