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

Da Gambas-it.org - Wikipedia.

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

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

Per poter fruire delle risorse di Libtag_c, è necessario installare nel sistema ed utilizzare nelle applicazioni Gambas la libreria condivisa: "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 audio:

Library "libtag_c:0.0.0"

Public Struct TagLib_File
  dummy As Integer
End Struct

Public Struct TagLib_Tag
  dummy As Integer
End Struct

Public Struct TagLib_AudioProperties
  dummy As Integer
End Struct
 
' 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)
' Creates a TagLib file based on \a filename.
Private Extern taglib_file_new(filename As String) As TagLib_File

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

' char *taglib_tag_title(const TagLib_Tag *tag)
' Returns a string with this tag's title.
Private Extern taglib_tag_title(tag As TagLib_Tag) 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 TagLib_Tag) 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 TagLib_Tag) 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 TagLib_Tag) As Integer

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

' 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 TagLib_Tag) As Integer

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

' 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(_file As TagLib_File) As TagLib_AudioProperties

' int taglib_audioproperties_length(const TagLib_AudioProperties *audioProperties)
' Returns the length of the file in seconds.
Private Extern taglib_audioproperties_length(audioProperties As TagLib_AudioProperties) 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 TagLib_AudioProperties) 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 TagLib_AudioProperties) 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 TagLib_AudioProperties) 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(_file As TagLib_File)


Public Sub Main()

 Dim fileaudio As String
 Dim fl As TagLib_File
 Dim autag As TagLib_Tag
 Dim auprop As TagLib_AudioProperties
 Dim secondi, minuti As Integer
 
 fileaudio = "/percorso/file/audio"
  
 taglib_set_strings_unicode(False)
 
 fl = taglib_file_new(fileaudio)
 If IsNull(fl) Then Error.Raise("Impossibile caricare il file audio: " & fileaudio)
  
 autag = taglib_file_tag(fl)
 If IsNull(autag) Then Error.Raise("Impossibile ottenere i tag dal file audio: " & fileaudio)
   
' Mostra i Tag del file audio:
 Print "=== T A G ===\n"
 Print "Titolo:   "; taglib_tag_title(autag)
 Print "Artista:  "; taglib_tag_artist(autag)
 Print "Album:    "; taglib_tag_album(autag)
 Print "Anno:     "; taglib_tag_year(autag)
 Print "Commento: "; taglib_tag_comment(autag)
 Print "Traccia:  "; taglib_tag_track(autag)
 Print "Genere:   "; taglib_tag_genre(autag)
     
' Mostra le proprietà del file audio:
 Print "\n\n== PROPRIETA' del FILE AUDIO ==\n"
 auprop = taglib_file_audioproperties(fl)
 If IsNull(auprop) Then Error.Raise("Impossibile ottenere le proprietà del file audio: " & fileaudio)
 secondi = taglib_audioproperties_length(auprop) Mod 60
 minuti = (taglib_audioproperties_length(auprop) - secondi) / 60
 Print "Bitrate:    "; taglib_audioproperties_bitrate(auprop)
 Print "Frequenza:  "; taglib_audioproperties_samplerate(auprop); " hertz"
 Print "Canali:     "; taglib_audioproperties_channels(auprop)
 Print "Durata:     "; Time(0, minuti, secondi, 0)
     
' Va in chiusura:
 taglib_tag_free_strings()
 taglib_file_free(fl)

End


Riferimenti