Estrarre informazioni e TAG da file audio con le funzioni esterne del API di LibTagC

Da Gambas-it.org - Wikipedia.

LibTagC è una libreria per estrarre informazioni generali ed i Tag dei file di vari formati audio.

La documentazione di LibTagC può essere trovata nel codice sorgente: "/usr/include/taglib/tag_c.h" .

Per poter fruire delle risorse di LibTagC è necessario utilizzare nelle applicazioni Gambas la libreria: libtag_c.so.0.0.0

Riportiamo di seguito un semplice esempio, con il quale è possibile estrarre i TAG ed informazioni generali da un file mp3:

Library "libtag_c:0.0.0"

Private Enum TagLib_File_MPEG = 0,
              TagLib_File_OggVorbis,
              TagLib_File_FLAC,
              TagLib_File_MPC,
              TagLib_File_OggFlac,
              TagLib_File_WavPack,
              TagLib_File_Speex,
              TagLib_File_TrueAudio,
              TagLib_File_MP4,
              TagLib_File_ASF

' void taglib_set_strings_unicode(BOOL unicode)
' By default all strings coming into or out of TagLib's C API are in UTF8.
' However, it may be desirable For TagLib To operate On Latin1(ISO - 8859 - 1) strings In which Case this should be set To FALSE.
Private Extern taglib_set_strings_unicode(unicode As Boolean)

' TagLib_File *taglib_file_new_type(const char *filename, TagLib_File_Type type)
' Creates a TagLib file based on \a filename.
Private Extern taglib_file_new(filename As String, type As Integer) As Pointer

' TagLib_Tag *taglib_file_tag(const TagLib_File *file)
' Returns a pointer to the tag associated with this file.
Private Extern taglib_file_tag(Tl_file As Pointer) As Pointer

' const TagLib_AudioProperties *taglib_file_audioproperties(const TagLib_File *file)
' Returns a pointer to the the audio properties associated with this file.
Private Extern taglib_file_audioproperties(Tl_file As Pointer) As Pointer

' char *taglib_tag_title(const TagLib_Tag *tag)
' Returns a string with this tag's title.
Private Extern taglib_tag_title(tag As Pointer) As String

' char *taglib_tag_artist(const TagLib_Tag *tag)
' Returns a string with this tag's artist.
Private Extern taglib_tag_artist(tag As Pointer) As String

' char *taglib_tag_album(const TagLib_Tag *tag)
' Returns a string with this tag's album name.
Private Extern taglib_tag_album(tag As Pointer) As String

' char *taglib_tag_comment(const TagLib_Tag *tag)
' Returns a string with this tag's comment.
Private Extern taglib_tag_comment(tag As Pointer) As String

' char *taglib_tag_genre(const TagLib_Tag *tag)
' Returns a string with this tag's genre.
Private Extern taglib_tag_genre(tag As Pointer) As String

' unsigned int taglib_tag_year(const TagLib_Tag *tag)
' Returns the tag's year or 0 if year is not set.
Private Extern taglib_tag_year(tag As Pointer) As Integer

' unsigned int taglib_tag_track(const TagLib_Tag *tag)
' Returns the tag's track number or 0 if track number is not set.
Private Extern taglib_tag_track(tag As Pointer) As Integer

' int taglib_audioproperties_length(const TagLib_AudioProperties *audioProperties)
' Returns the length of the file in seconds.
Private Extern taglib_audioproperties_length(audioProperties As Pointer) As Integer

' int taglib_audioproperties_bitrate(const TagLib_AudioProperties *audioProperties)
' Returns the bitrate of the file in kb/s.
Private Extern taglib_audioproperties_bitrate(audioProperties As Pointer) As Integer

' int taglib_audioproperties_samplerate(const TagLib_AudioProperties *audioProperties)
' Returns the sample rate of the file in Hz.
Private Extern taglib_audioproperties_samplerate(audioProperties As Pointer) As Integer

' int taglib_audioproperties_channels(const TagLib_AudioProperties *audioProperties)
' Returns the number of channels in the audio stream.
Private Extern taglib_audioproperties_channels(audioProperties As Pointer) As Integer

' void taglib_tag_free_strings(void)
' Frees all of the strings that have been created by the tag.
Private Extern taglib_tag_free_strings()

' void taglib_file_free(TagLib_File *file)
' Frees and closes the file.
Private Extern taglib_file_free(Tl_file As Pointer)


Public Sub Main()

 Dim i, secondi, minuti As Integer
 Dim TagLib_File, Taglib_tag, TagLib_AudioProperties As Pointer
 Dim percorsoFile As String

 taglib_set_strings_unicode(False)
   
 percorsoFile = "/percorso/del/file.mp3"
   
 TagLib_File = taglib_file_new(percorsoFile, TagLib_File_MPEG)
 If IsNull(TagLib_File) Then Error.Raise("Impossibile caricare il file !")

 Taglib_tag = taglib_file_tag(TagLib_File)
 If IsNull(Taglib_tag) Then Error.Raise("Errore alla funzione 'taglib_file_tag()' !")
       
 TagLib_AudioProperties = taglib_file_audioproperties(TagLib_File)
 If IsNull(TagLib_AudioProperties) Then Error.Raise("Errore alla funzione 'taglib_file_audioproperties()' !")
    
' Mostra i Tag del file audio:
 Print "=== T A G ===\n"
 Print "Titolo: ", taglib_tag_title(Taglib_tag)
 Print "Artista: ", taglib_tag_artist(Taglib_tag)
 Print "Album: ", taglib_tag_album(Taglib_tag)
 Print "Anno: ", taglib_tag_year(Taglib_tag)
 Print "Commento: ", taglib_tag_comment(Taglib_tag)
 Print "Tracce: ", taglib_tag_track(Taglib_tag)
 Print "Genere: ", taglib_tag_genre(Taglib_tag)
     
' Mostra le proprietà del file audio:
 Print "\n\n== PROPRIETA' del FILE AUDIO ==\n"
 Print "Frequenza di campionamento: Hz "; taglib_audioproperties_samplerate(TagLib_AudioProperties)
 Print "bitrate: "; taglib_audioproperties_bitrate(TagLib_AudioProperties); " kb/s"
 Print "Canali di uscita: "; taglib_audioproperties_channels(TagLib_AudioProperties)
 secondi = taglib_audioproperties_length(TagLib_AudioProperties) % 60
 minuti = (taglib_audioproperties_length(TagLib_AudioProperties) - secondi) / 60
 Print "Durata: "; minuti; "' "; secondi; Chr(34)
     
' Va in chiusura:
 taglib_tag_free_strings()
 taglib_file_free(TagLib_File)

End


Riferimenti

[1] https://taglib.org/