Comunidad Gambas-es
[TUTORIAL] Extern: Uso de la libreria C TagLib, id3.module, libtag_c, TagLib - Versión para impresión

+- Comunidad Gambas-es (https://gambas-es.org)
+-- Foro: Linux (https://gambas-es.org/forum-9.html)
+--- Foro: Programación en otros lenguajes (https://gambas-es.org/forum-12.html)
+---- Foro: C/C++ (https://gambas-es.org/forum-15.html)
+---- Tema: [TUTORIAL] Extern: Uso de la libreria C TagLib, id3.module, libtag_c, TagLib (/thread-2024.html)



Extern: Uso de la libreria C TagLib, id3.module, libtag_c, TagLib - tincho - 18-08-2026

Hola a todos,

Hice una adaptacion de un ejemplo del wiki italiano de gambas [1] que muestra como usar libtag_c [2] para usarlo como una funcion que devuelve una Collection.
Al modulo lo llame id3.module

[1] https://www.gambas-it.org/wiki/index.php/Estrarre_informazioni_e_TAG_da_file_audio_con_le_funzioni_esterne_del_API_di_Libtag_c
[2] https://taglib.org/

Código:
' Gambas module file

Export
Library "libtag_c"

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

'Private Extern taglib_tag_encoder(tag As TagLib_Tag) As String
'Private Extern taglib_tag_volume_number(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 Function Info(fileaudio As String) As Collection

  Dim fl As TagLib_File
  Dim autag As TagLib_Tag
  Dim auprop As TagLib_AudioProperties
  '  Dim secondi, minuti As Integer
  '
  Dim inf As New Collection

  taglib_set_strings_unicode(False)
  taglib_set_strings_unicode(True)

  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)

  inf.Add(taglib_tag_album(autag), "album")
  inf.Add(taglib_tag_artist(autag), "artist")
  inf.Add(taglib_tag_track(autag), "track")
  inf.Add(taglib_tag_title(autag), "title")
  inf.Add(taglib_tag_year(autag), "year")
  inf.Add(taglib_tag_genre(autag), "genre")
  inf.Add(taglib_tag_comment(autag), "comment")

  inf.Add(File.Ext(fileaudio), "format")

  ' Mostra le proprietà del file audio:
  auprop = taglib_file_audioproperties(fl)

  If IsNull(auprop) Then
    inf.Add(True, "audioerror")
  Else
    inf.Add(taglib_audioproperties_length(auprop), "length")
    inf.Add(taglib_audioproperties_bitrate(auprop), "bitrate")
    inf.Add(taglib_audioproperties_samplerate(auprop), "frequency")
    inf.Add(taglib_audioproperties_channels(auprop), "channels")
  Endif
  inf.Add(taglib_tag_comment(autag), "comment")
  ' Va in chiusura:
  taglib_tag_free_strings()
  taglib_file_free(fl)

  Return inf

End
Luego se usa de esta manera para obtener los metadatos de un archivo de audio:

Código:
  Dim sFile As String ' Ruta dle archivo
  Dim oID3 As Collection

  oID3 = id3.Info(sFile)