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

Da Gambas-it.org - Wikipedia.

Le risorse del API di id3tag consentono anche di ottenere informazioni (Tag) dai file audio formato mp3 richiamando l'attuale versione della libreria: libid3tag.so.0.3.0

Poiché l'uso della libreria esterna libid3tag prevede il richiamo diretto ed indiretto di numerose 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 all'interno del codice Gambas e che da esso richiameremo.

Il codice Gambas per la lettura di alcuni (in questo esempio) tag dei file .mp3 potrà essere il seguente:

Library "libid3tag:0.3.0"

Public Struct audio_file_tags
  title As String
  artist As String
  album As String
  anno As String
  genere As String
  comment As String
  track As String
  composer As String
  orig_artist As String
  copyright As String
  url As String
  encoded_b As String
End Struct

Private aftag As Struct Audio_file_tags

Private genre_table As String[] = ["Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
"Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
"Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip",
"Gospel", "Noise", "Alt. Rock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock",
"Ethnic", "Gothic", "Darkwave", "Techno-Indust.", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock",
"Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
"New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka",
"Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing", "Fusion", "Bebob",
"Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progress. Rock", "Psychadel. Rock",
"Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
"Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club",
"Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock",
"Drum Solo", "A capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror",
"Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal", "Black Metal",
"Crossover", "Contemporary Christian", "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop", "Synthpop"]

Private Const ID3_FIELD_TYPE_STRINGLIST As Byte = 6
Private Enum ID3_FILE_MODE_READONLY = 0, ID3_FILE_MODE_READWRITE

' id3_file *id3_file_open(char const *, enum id3_file_mode)
Private Extern id3_file_open(fl As String, id3_file_mode As Byte) As Pointer

' id3_tag *id3_file_tag(struct id3_file const *)
Private Extern id3_file_tag(id3_file As Pointer) As Pointer

' id3_frame *id3_tag_findframe(struct id3_tag const *, char const *, unsigned int)
Private Extern id3_tag_findframe(id3_tag As Pointer, id$ As String, ui As Integer) As Pointer

' id3_ucs4_t const *id3_field_getstrings(union id3_field const *, unsigned int)
Private Extern id3_field_getstrings(unid3_field As Pointer, ui As Integer) As Pointer

' id3_utf8_t *id3_ucs4_utf8duplicate(id3_ucs4_t const *)
Private Extern id3_ucs4_utf8duplicate(id3_ucs4_t As Pointer) As Pointer

' union id3_field *id3_frame_field(struct id3_frame const *, unsigned int)
Private Extern id3_frame_field(strFrame As Pointer, ui As Integer) As Pointer

' unsigned int id3_field_getnstrings(union id3_field const *)
Private Extern id3_field_getnstrings(unid3_field As Pointer) As Integer

' id3_latin1_t *id3_ucs4_latin1duplicate(id3_ucs4_t const *)
Private Extern id3_ucs4_latin1duplicate(id3_ucs4_t As Pointer) As Pointer

' int id3_file_close(struct id3_file *)
Private Extern id3_file_close(id3_file As Pointer) As Integer


Library "/tmp/libtag3"   ' Richiama la libreria ad hoc da noi realizzata per la gestione sicura delle Strutture di id3tag

Private Extern obt_frame_nfields(fr As Pointer) As Integer
Private Extern obt_frame_type(fr As Pointer) As Integer
Private Extern obt_frame_fields(fr As Pointer) As Pointer


Public Sub Main()

 Dim id3_file, id3_tag As Pointer
 Dim titolo, album, anno As Pointer
 Dim artista As String
 Dim genus, tracce, nomeFl As String
 Dim dur As Integer

' Verifica se già è presente la libreria ad hoc per la gestione sicura delle "Strutture" utilizzate di "id3tag", altrimenti la va a creare:
 If Exist("/tmp/libtag3.so") = False Then creaId3()
 
 id3_file = id3_file_open("/percorso/del/file.mp3", ID3_FILE_MODE_READONLY)
 If id3_file == 0 Then Error.Raise("Impossibile aprire il file !")
 
 id3_tag = id3_file_tag(id3_file)
 If id3_tag == 0 Then Error.Raise("Impossibile ottenere i tag del file mp3 !")
 
 With aftag
   artista = obtArtista(id3_tag)
   Print "Artista = "; artista
   titolo = obtTitolo(id3_tag)
   Print "Titolo = "; String@(titolo)
   anno = obtAnno(id3_tag)
   Print "Anno = "; String@(anno)
   album = obtAlbum(id3_tag)
   Print "Album = "; String@(album)
   genus = String@(obtGenere(id3_tag))
   If IsDigit(genus) Then genus = genre_table[Val(genus)]
   Print "Genere = "; genus
   tracce = obtTracce(id3_tag)
   Print "Tracce = "; tracce
   nomeFl = obtOrigFilename(id3_tag)
   Print "Nome file originale = "; nomeFl
   dur = obtLen(id3_tag)
   Print "Durata = "; Time(0, 0, 0, dur)

 End With

 id3_file_close(id3_file)

End


Private Function obtArtista(id3_tag As Pointer) As String
 
 Dim nomeArtistaP As Pointer
 
 nomeArtistaP = obtFrameText(id3_tag, "TPE1")
 If nomeartistaP == 0 Then nomeartistaP = obtFrameText(id3_tag, "TPE2")
 If nomeartistaP == 0 Then nomeartistaP = obtFrameText(id3_tag, "TPE3")
 If nomeartistaP == 0 Then nomeartistaP = obtFrameText(id3_tag, "TPE4")
 If nomeartistaP == 0 Then nomeartistaP = obtFrameText(id3_tag, "TCOM")

 Return String@(nomeArtistaP)
 
End

Private Function obtTitolo(id3_tag As Pointer) As Pointer
 
 Dim titoloP As Pointer
 
 titoloP = obtFrameText(id3_tag, "TIT2")
 If titoloP == 0 Then Error.Raise("Errore !")
 Return titoloP
 
End

Private Function obtAlbum(id3_tag As Pointer) As Pointer
 
 Dim albumP As Pointer
 
 albumP = obtFrameText(id3_tag, "TALB")
 If albumP == 0 Then Error.Raise("Errore !")
 Return albumP
 
End

Private Function obtAnno(id3_tag As Pointer) As Pointer
 
 Dim annoP As Pointer
 
 annoP = obtFrameText(id3_tag, "TYER")
 If annoP == 0 Then annoP = obtFrameText(id3_tag, "TDRC")
 Return annoP
 
End

Private Function obtGenere(id3_tag As Pointer) As Pointer
 
 Dim gnreP As Pointer
 
 gnreP = obtFrameText(id3_tag, "TCON")
 If gnreP == 0 Then Error.Raise("Errore !")
 Return gnreP
 
End

Private Function obtTracce(id3_tag As Pointer) As String
 
 Dim tracceP As Pointer
 
 tracceP = obtFrameText(id3_tag, "TRCK")
 Return String@(tracceP)

End

Private Function obtOrigFilename(id3_tag As Pointer) As String
 
 Dim nomP As Pointer
 
 nomP = obtFrameText(id3_tag, "TOFN")
 Return String@(nomP)
   
End

Private Function obtLen(id3_tag As Pointer) As Integer
 
 Dim durI As Integer
 
 durI = obtDurata(id3_tag, "TLEN")
 Return durI
   
End


Private Function obtFrameText(tag As Pointer, id As String) As Pointer
 
 Dim frame, field1 As Pointer
 Dim ucs4, utf8 As Pointer
 
 frame = id3_tag_findframe(tag, id, 0)
 If frame == 0 Then Return 0
   
 If obt_frame_nfields(frame) <> 2 Then Return 0
   
 If obt_frame_type(frame) <> ID3_FIELD_TYPE_STRINGLIST Then Return 0
   
 field1 = obt_frame_fields(frame)
   
 ucs4 = id3_field_getstrings(field1, 0)
 If ucs4 == 0 Then Return Null
   
 utf8 = id3_ucs4_utf8duplicate(ucs4)

 Return utf8

End


Private Function obtDurata(tag As Pointer, id As String) As Integer
 
 Dim frame, field, latin1 As Pointer
 Dim nstrings, tempI As Integer
 Dim tempu$ As String
 
 frame = id3_tag_findframe(tag, id, 0)
 If frame == 0 Then Return 0
   
 field = id3_frame_field(frame, 1)
   
 nstrings = id3_field_getnstrings(field)
 If nstrings <= 0 Then Return -1
   
 latin1 = id3_ucs4_latin1duplicate(id3_field_getstrings(field, 0))
 If latin1 == 0 Then Return -1
   
' Il frame 'lengh' contiene la durata dei dati audio espressa in millisecondi e rappresentata in una stringa numerica:
 tempu$ = String@(latin1)
 tempI = Val(tempu$)
       
 If tempI > 0 Then
   Return tempI
 Else
   Return -1
 Endif

End


' Crea l'apposita libreria per la gestione sicura delle Strutture di "id3tag":
Private Procedure creaId3()
  
 File.Save("/tmp/libtag3.c", "#include <id3tag.h>" &
                             "\n\n" &
                             "/* Legge nei campi delle Strutture utilizzate: */\n" &
                             "int obt_frame_nfields(struct id3_frame *p) {\n" &
                             "   return p->nfields;\n}" &
                             "\n\n" &
                             "int obt_frame_type(struct id3_frame *p) {\n" &
                             "   return p->fields[1].type;\n}" &
                             "\n\n" &
                             "union id3_field * obt_frame_fields(struct id3_frame *p) {\n" &
                             "   return &p->fields[1];\n}")

 Shell "gcc -o /tmp/libtag3.so /tmp/libtag3.c -shared -fPIC" Wait
 
End


Riferimenti