TagLib [1.5] [Include + Libary]

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
Kai
Beiträge: 264
Registriert: 29.09.2005 20:58
Computerausstattung: iMac 2019
Wohnort: Hannover
Kontaktdaten:

TagLib [1.5] [Include + Libary]

Beitrag von Kai »

Habe für mein aktuelles Projekt eine Libary gesucht womit ich MP3 Tags einlesen kann, dabei fand ich diese.
Funktioniert bei mir bisher wunderbar, deshalb dachte ich mir poste ich meine Ergebnisse hier gleich mal für alle.
Glaube im Englischen Forum gabs das eh schon, aber was solls.

Download

Include

Code: Alles auswählen

EnableExplicit

; Current Version: 1.5 (http://developer.kde.org/~wheeler/taglib.html)
; LastEdit:        Kai Gartenschläger, 07.06.2009
; PureBasic:       4.31 x86

ImportC "tag_c.lib"
  taglib_audioproperties_bitrate(audioProperties.l) As "_taglib_audioproperties_bitrate"
  ; Returns the bitrate of the file in kb/s.
  
  taglib_audioproperties_channels(audioProperties.l) As "_taglib_audioproperties_channels"
  ; Returns the number of channels in the audio stream.
  
  taglib_audioproperties_length(audioProperties.l) As "_taglib_audioproperties_length"
  ; Returns the length of the file in seconds.
  
  taglib_audioproperties_samplerate(audioProperties.l) As "_taglib_audioproperties_samplerate"
  ; Returns the sample rate of the file in Hz.
  
  taglib_file_audioproperties(file.l) As "_taglib_file_audioproperties"
  ; Returns a pointer To the the audio properties associated With this file.  This will be freed automatically when the file is freed.
  
  taglib_file_free(file.l) As "_taglib_file_free"
  ; Frees And closes the file.
  
  taglib_file_is_valid(file.l) As "_taglib_file_is_valid"
  ; Unknown
  
  taglib_file_new(filename.s) As "_taglib_file_new"
  ; Creates a TagLib file based on \a filename.  TagLib will try To guess the file type.
  ; \returns NULL If the file type cannot be determined Or the file cannot be opened.
    
  taglib_file_new_type(filename.s,type.l) As "_taglib_file_new_type"
  ; Creates a TagLib file based on \a filename.  Rather than attempting To guess the type, it will use the one specified by \a type.
  
  taglib_file_save(file.l) As "_taglib_file_save"
  ; Saves the \a file To disk.
  
  taglib_file_tag(file.l) As "_taglib_file_tag"
  ; Returns a pointer To the tag associated With this file.  This will be freed automatically when the file is freed.
  
  taglib_id3v2_set_default_text_encoding(encoding.l) As "_taglib_id3v2_set_default_text_encoding" 
  ; This sets the Default encoding For ID3v2 frames that are written To tags.
  
  taglib_set_string_management_enabled(management.b) As "_taglib_set_string_management_enabled"
  ; TagLib can keep track of strings that are created when outputting tag values And clear them using taglib_tag_clear_strings().
  ; This is enabled by Default.
  ; However If you wish To do more fine grained management of strings, you can do so by setting \a management To FALSE.
  
  taglib_set_strings_unicode(unicode.b) As "_taglib_set_strings_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.
  
  taglib_tag_title(tag.l) As "_taglib_tag_title"
  ; Sets the tag's title.
  ; By Default this string should be UTF8 encoded.
  
  taglib_tag_artist(tag.l) As "_taglib_tag_artist"
  ; Sets the tag's artist.
  ; By Default this string should be UTF8 encoded.
  
  taglib_tag_album(tag.l) As "_taglib_tag_album"
  ; Sets the tag's album.
  ; By Default this string should be UTF8 encoded.
  
  taglib_tag_comment(tag.l) As "_taglib_tag_comment"
  ; Sets the tag's comment.
  ; By Default this string should be UTF8 encoded.
  
  taglib_tag_genre(tag.l) As "_taglib_tag_genre"
  ; Sets the tag's genre.
  ; By Default this string should be UTF8 encoded.
  
  taglib_tag_year(tag.l) As "_taglib_tag_year"
  ; Sets the tag's year. 0 indicates that this field should be cleared.
  
  taglib_tag_track(tag.l) As "_taglib_tag_track"
  ; Sets the tag's track number.  0 indicates that this field should be cleared.
  
  taglib_tag_set_title(tag.l,title.s) As "_taglib_tag_set_title"
  ; Returns a string With this tag's title.
  ; By Default this string should be UTF8 encoded And its memory should be freed using taglib_tag_free_strings().
  
  taglib_tag_set_artist(tag.l,artist.s) As "_taglib_tag_set_artist"
  ; Returns a string With this tag's artist.
  ; By Default this string should be UTF8 encoded And its memory should be freed using taglib_tag_free_strings().
  
  taglib_tag_set_album(tag.l,album.s) As "_taglib_tag_set_album"
  ; Returns a string With this tag's album name.
  ; By Default this string should be UTF8 encoded And its memory should be freed using taglib_tag_free_strings().
  
  taglib_tag_set_comment(tag.l,comment.s) As "_taglib_tag_set_comment"
  ; Returns a string With this tag's comment.
  ; By Default this string should be UTF8 encoded And its memory should be freed using taglib_tag_free_strings().
  
  taglib_tag_set_genre(tag.l,genre.s) As "_taglib_tag_set_genre"
  ; Returns a string With this tag's genre.
  ; By Default this string should be UTF8 encoded And its memory should be freed using taglib_tag_free_strings().
  
  taglib_tag_set_year(tag.l,year.l) As "_taglib_tag_set_year"
  ; Returns the tag's year or 0 if year is not set.
  
  taglib_tag_set_track(tag.l,track.l) As "_taglib_tag_set_track"
  ; Returns the tag's track number or 0 if track number is not set.
  
  taglib_tag_free_strings() As "_taglib_tag_free_strings"
  ; Frees all of the strings that have been created by the tag.
EndImport

Enumeration 
  #TagLib_File_MPEG
  #TagLib_File_OggVorbis
  #TagLib_File_FLAC
  #TagLib_File_MPC
  #TagLib_File_OggFlac
  #TagLib_File_WavPack
  #TagLib_File_Speex
  #TagLib_File_TrueAudio
  #TagLib_File_Type
EndEnumeration

Enumeration 
  #TagLib_ID3v2_Latin1
  #TagLib_ID3v2_UTF16
  #TagLib_ID3v2_UTF16BE
  #TagLib_ID3v2_UTF8
  #TagLib_ID3v2_Encoding
EndEnumeration

DisableExplicit
Beispiel

Code: Alles auswählen

EnableExplicit

; LastEdit:        Kai Gartenschläger, 07.06.2009
; PureBasic:       4.31 x86

XIncludeFile #PB_Compiler_Home + "Include\taglib.pbi"

Procedure.s MyMusicDirectory()
  Protected sPath.s = Space(#MAX_PATH)
  SHGetSpecialFolderPath_(#Null, @sPath, #CSIDL_MYMUSIC, 0)
  PathAddBackslash_(@sPath)
  ProcedureReturn sPath
EndProcedure

Procedure.s TimeString(Time, Sep$ = ":")
 If Time > 0
  Protected sTimeString.s
  Protected iHours.i, iMinutes.i, iSeconds.i
  
  Time / 1000: iSeconds = Time % 60
  Time / 60:   iMinutes = Time % 60
  Time / 60:   iHours   = Time % 60
    
  sTimeString + RSet(Str(iHours), 2, "0") + Sep$ + RSet(Str(iMinutes), 2, "0") + Sep$ + RSet(Str(iSeconds), 2, "0")
  
  ProcedureReturn sTimeString
 Else
  ProcedureReturn "00:00:00"
 EndIf
EndProcedure

Structure _Tag
  title.s
  artist.s
  album.s
  year.i
  comment.s
  track.i
  genre.s
  bitrate.i
  samplerate.i
  channels.i
  length.i
EndStructure
Global Tag._Tag

Procedure.i TagLib_ReadTag(File$)
  Protected iResult.i
  
  If Trim(File$) = "" Or FileSize(File$) < 1
    iResult = 0
  Else
    Protected iFile.i
    
    iFile = taglib_file_new(File$)
    If iFile
      Protected *Tag, *Proberties
      
      *Tag = taglib_file_tag(iFile)
      If *Tag
        Tag\title   = Trim(PeekS(taglib_tag_title(*Tag) ,-1, #PB_UTF8))
        Tag\artist  = Trim(PeekS(taglib_tag_artist(*Tag) ,-1, #PB_UTF8))
        Tag\album   = Trim(PeekS(taglib_tag_album(*Tag) ,-1, #PB_UTF8))
        Tag\year    = taglib_tag_year(*Tag)
        Tag\comment = Trim(PeekS(taglib_tag_comment(*Tag), -1, #PB_UTF8))
        Tag\track   = taglib_tag_track(*Tag)
        Tag\genre   = Trim(PeekS(taglib_tag_genre(*Tag) ,-1, #PB_UTF8))
      EndIf
      
      *Proberties = taglib_file_audioproperties(iFile)
      If *Proberties
        Tag\bitrate    = taglib_audioproperties_bitrate(*Proberties)
        Tag\samplerate = taglib_audioproperties_samplerate(*Proberties)
        Tag\channels   = taglib_audioproperties_channels(*Proberties)
        Tag\length     = taglib_audioproperties_length(*Proberties)
      EndIf
      
      taglib_tag_free_strings()
      taglib_file_free(iFile)
      
      iResult = 1
    EndIf
  EndIf
  
  ProcedureReturn iResult
EndProcedure

;Test
Define.s sFolder = PathRequester("Ordnerwahl", MyMusicDirectory())
If sFolder
  If ExamineDirectory(0, sFolder, "*.*")
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Debug "########" + DirectoryEntryName(0) + "########"
        If TagLib_ReadTag(sFolder + DirectoryEntryName(0)) = 1
          If Tag\title
            Debug "Title: " + Tag\title
          EndIf
          If Tag\artist
            Debug "Artist: " + Tag\artist
          EndIf
          If Tag\album
            Debug "Album: " + Tag\album
          EndIf
          If Tag\year
            Debug "Jahr: " + Str(Tag\year)
          EndIf
          If Tag\comment
            Debug "Kommentar: "  + Tag\comment
          EndIf
          If Tag\track
            Debug "Track: " + Str(Tag\track)
          EndIf
          If Tag\genre
            Debug "Genre: " + Tag\genre
          EndIf
          If Tag\bitrate
            Debug "BitRate: " + Str(Tag\bitrate)
          EndIf
          If Tag\samplerate
            Debug "SampleRate: " + Str(Tag\samplerate)
          EndIf
          If Tag\channels
            Debug "Channels: " + Str(Tag\channels)
          EndIf
          If Tag\length
            Debug "Length: " + TimeString(Tag\length * 1000)
          EndIf
        Else
          Debug "Open Failed"
        EndIf
      EndIf
    Wend
    FinishDirectory(0)
  EndIf
EndIf
Zuletzt geändert von Kai am 07.04.2010 15:43, insgesamt 1-mal geändert.
Meine Homepage - Aktuelle Projekte - Launcher
Letzte PB-Version - Window 7 x86
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Beitrag von ts-soft »

:allright:

Leider haste im Download die "tag.dll" vergessen, ohne gehts nicht :wink:
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
Kai
Beiträge: 264
Registriert: 29.09.2005 20:58
Computerausstattung: iMac 2019
Wohnort: Hannover
Kontaktdaten:

Beitrag von Kai »

ts-soft hat geschrieben::allright:

Leider haste im Download die "tag.dll" vergessen, ohne gehts nicht :wink:
Peinlich aber wahr :oops:

Behoben :mrgreen:
Meine Homepage - Aktuelle Projekte - Launcher
Letzte PB-Version - Window 7 x86
Flohmarkt
Beiträge: 3
Registriert: 03.08.2009 22:45

Beitrag von Flohmarkt »

Funktioniert wirklich klasse. Leider bin ich totaler Anfänger, würde das ganze aber noch gerne um folgende Funktionen erweitern:
- Cover auslesen (AttachedPictureFrame)
- selbstdefinierte Tags wie z.B mit MP3Tag oder Foobar möglich

Kann mir jemand einen Hinweis geben, wie das ganze eingebaut werden kann?

Danke!

Floh
Benutzeravatar
Kai
Beiträge: 264
Registriert: 29.09.2005 20:58
Computerausstattung: iMac 2019
Wohnort: Hannover
Kontaktdaten:

Beitrag von Kai »

Flohmarkt hat geschrieben:Funktioniert wirklich klasse. Leider bin ich totaler Anfänger, würde das ganze aber noch gerne um folgende Funktionen erweitern:
- Cover auslesen (AttachedPictureFrame)
- selbstdefinierte Tags wie z.B mit MP3Tag oder Foobar möglich

Kann mir jemand einen Hinweis geben, wie das ganze eingebaut werden kann?

Danke!

Floh
Wenn ich das richtig vertanden habe, mit PureBasic garnicht, da TagLib eigentlich OOP ist und lediglich die DLL Funktionen aus PureBasic nutzbar sind.
Fals ich stuß erzähle, bitte jemand klarstellen, hab mit OOP echt nix am hut, wüßte jedenfalls nicht wie ich das einbinden könnte.

Alternative die das auf jeden fall kann, ist Audiogenie.
Ebenfalls eine Freeware Libary, sogar auf deutsch.
Einziger nachteil der mich davon abgehalten hat sie zu benutzen, die Libary blendet eine Meldung ein, solange man nicht freiwilig etwas spendet um einen Freischaltcode zu erhalten.

Hier nochn direkter Link zur Taglib Online Doku, falls sich wer schlaulesen möchte: http://developer.kde.org/~wheeler/taglib/api/
Meine Homepage - Aktuelle Projekte - Launcher
Letzte PB-Version - Window 7 x86
Andesdaf
Moderator
Beiträge: 2671
Registriert: 15.06.2008 18:22
Wohnort: Dresden

Beitrag von Andesdaf »

schöne Sache :allright: danke dafür.
Win11 x64 | PB 6.20
Flohmarkt
Beiträge: 3
Registriert: 03.08.2009 22:45

Beitrag von Flohmarkt »

Kann mir bitte jemand ein kleines Code Beispiel geben, wie ich eine Funktion der Audiogenie2.dll aus Purebasic aufrufen kann.
Ich (Anfänger) bin am verzweifeln vor lauter Pointer und Peeks.....oder denk ich viel zu kompliziert?

Danke für die Mühen!
Benutzeravatar
Kai
Beiträge: 264
Registriert: 29.09.2005 20:58
Computerausstattung: iMac 2019
Wohnort: Hannover
Kontaktdaten:

Beitrag von Kai »

Flohmarkt hat geschrieben:Kann mir bitte jemand ein kleines Code Beispiel geben, wie ich eine Funktion der Audiogenie2.dll aus Purebasic aufrufen kann.
Ich (Anfänger) bin am verzweifeln vor lauter Pointer und Peeks.....oder denk ich viel zu kompliziert?

Danke für die Mühen!

Code: Alles auswählen

EnableExplicit

Global iLibary.i

Procedure PrintXY(X, Y, String$)
  ConsoleLocate(X, Y)
  String$ = ReplaceString(String$, "ü", Chr(129))
  String$ = ReplaceString(String$, "ö", Chr(148))
  String$ = ReplaceString(String$, "ä", Chr(132))
  String$ = ReplaceString(String$, "Ü", Chr(154))
  String$ = ReplaceString(String$, "Ö", Chr(153))
  String$ = ReplaceString(String$, "Ä", Chr(142))
  Print(String$)
EndProcedure

iLibary = OpenLibrary(#PB_Any, "AudioGenie2.dll")
If iLibary
  Global sFile.s, *File, iFormat.i, iValidAudio.i
  
  sFile = OpenFileRequester("Datei Öffnen", GetCurrentDirectory(), "MP3|*.mp3|Alle Dateien|*.*", 0)
  If sFile
    
    ; Convert Filestring to Unicode
    *File = AllocateMemory( StringByteLength(sFile, #PB_Unicode) )
    PokeS(*File, sFile, -1, #PB_Unicode)
    
    ; Audio Format
    iFormat = CallFunction(iLibary, "AUDIOAnalyzeFile", *File) : FreeMemory(*File)
    ; Valid File
    iValidAudio = CallFunction(iLibary, "AUDIOFormatIsValid")
    
    ; Display Console
    OpenConsole()
    EnableGraphicalConsole(1)
    
    PrintXY(2, 2, "Format:")
    Select iFormat
      Case 0 : PrintXY(10, 2, "Unbekannt")
      Case 1 : PrintXY(10, 2, "MP3")
      Case 2 : PrintXY(10, 2, "WMA")
      Case 3 : PrintXY(10, 2, "MONKEY")
      Case 4 : PrintXY(10, 2, "FLAC")
      Case 5 : PrintXY(10, 2, "WAV")
      Case 6 : PrintXY(10, 2, "OGG VORBIS")
      Case 7 : PrintXY(10, 2, "MPP")
      Case 8 : PrintXY(10, 2, "AAC")
      Case 9 : PrintXY(10, 2, "MP4/M4A")
    EndSelect    
    
    If iValidAudio = -1
      PrintXY(2, 3, "Gültiges Audioformat")
    Else
      PrintXY(2, 3, "Ungültiges Audioformat")
    EndIf
    
    If iFormat > 0 And iValidAudio = -1
      ; Read Default Tags
      PrintXY(2, 5,  "Title:     " + PeekS(CallFunction(iLibary, "GetAUDIOTitle"), -1, #PB_Unicode))
      PrintXY(2, 6,  "Artist:    " + PeekS(CallFunction(iLibary, "GetAUDIOArtist"), -1, #PB_Unicode))
      PrintXY(2, 7,  "Album:     " + PeekS(CallFunction(iLibary, "GetAUDIOAlbum"), -1, #PB_Unicode))
      PrintXY(2, 8,  "Genre:     " + PeekS(CallFunction(iLibary, "GetAUDIOGenre"), -1, #PB_Unicode))
      PrintXY(2, 9,  "Nummer:    " + PeekS(CallFunction(iLibary, "GetAUDIOTrack"), -1, #PB_Unicode))
      PrintXY(2, 10, "Jahr:      " + PeekS(CallFunction(iLibary, "GetAUDIOYear"), -1, #PB_Unicode))
      PrintXY(2, 11, "Kommentar: " + PeekS(CallFunction(iLibary, "GetAUDIOComment"), -1, #PB_Unicode))
      
      ; Read many tags..
      If iFormat = 1 ; MP3
      EndIf
      If iFormat = 2 ; WMA
      EndIf
    EndIf
    
    Input()
    CloseLibrary(iLibary)
    End
  EndIf
Else
  MessageRequester("Fehler", "AudioGenie2.dll konnte nicht initialisert werden")
EndIf
Für Cover usw schau bitte selber in der Doku nach, ist ja deutsch und sollte eigentlich recht einfach sein.
Meine Homepage - Aktuelle Projekte - Launcher
Letzte PB-Version - Window 7 x86
Flohmarkt
Beiträge: 3
Registriert: 03.08.2009 22:45

Beitrag von Flohmarkt »

SUPER! Bin immer wieder baff, wie schnell und toll und selbstlos hier einem in diesem Forum geholfen wird. Werde sicher noch mehr Fragen stellen im Laufe meines Lebens, aber jetzt sollte ich schon mal weiter kommen.
DarkDragon
Beiträge: 6291
Registriert: 29.08.2004 08:37
Computerausstattung: Hoffentlich bald keine mehr
Kontaktdaten:

Beitrag von DarkDragon »

Hallo,

Also ich hab da ja mal ein paar Fragen dazu:

1. Unter welchen Umständen darf man das in seinem Projekt verwenden? Die TagLib hat ja doch eine sehr eigenartige Lizenzform (2 miteinander vermischt). Und deinen Wrapper und das Beispiel würde ich gerne teilweise übernehmen für mein kleines Projekt.
2. Bei den ..._tag_set_... Befehlen steht:
; Returns a string with this tag's comment.
; By default this string should be UTF8 encoded and its memory should be freed using taglib_tag_free_strings().
Das verhakt sich doch dann mit PureBasic, denn PureBasic gibt das ja auch am Ende des Programms frei (Auch AllocateMemory() Speicherbereiche).
3. Darf ich dir meine Erweiterungen/Verbesserungen mal schicken, vielleicht möchtest du sie übernehmen wenn sie dir gefallen.
Angenommen es gäbe einen Algorithmus mit imaginärer Laufzeit O(i * n), dann gilt O((i * n)^2) = O(-1 * n^2) d.h. wenn man diesen Algorithmus verschachtelt ist er fertig, bevor er angefangen hat.
Antworten