4 TagLib Metadata
| (require racket-audio/taglib) | package: racket-audio |
The racket-audio/taglib module provides the high level metadata API used by the audio package. It wraps the lower level TagLib C FFI module and presents a Racket API for common tags, generic properties, audio properties, and embedded cover art.
The module can be used in two modes. The default mode is read-only and returns a snapshot of the metadata. A handle opened with #:mode 'read-write keeps the native TagLib file open and can be modified with the setter procedures documented below. Changes are written to the media file by calling tags-save!.
The name id3-tags is historical. The implementation uses TagLib, so the usable file types are the file types supported by the TagLib library available at run time.
4.1 Opening and closing tag handles
procedure
file : path-string? mode : (or/c 'read 'read-only 'read-write 'write) = 'read
In read-write mode, the native TagLib file remains open. Setter procedures may then be used to modify fields, properties, and pictures. Call tags-save! to write changes and tags-close! to close the native handle.
On Windows, the implementation retries with the wide-character TagLib open function when the normal open function does not produce a valid TagLib file.
procedure
(call-with-id3-tags file proc [#:mode mode]) → any/c
file : path-string? proc : procedure? mode : (or/c 'read 'read-only 'read-write 'write) = 'read
procedure
(tags-valid? tags) → boolean?
tags : any/c
procedure
(tags-read-write? tags) → boolean?
tags : any/c
procedure
(tags-closed? tags) → boolean?
tags : any/c
procedure
(tags-save! tags) → boolean?
tags : any/c
procedure
(tags-close! tags) → void?
tags : any/c
(call-with-id3-tags "track.flac" (lambda (tags) (when (tags-valid? tags) (tags-title! tags "New title") (tags-save! tags))) #:mode 'read-write)
4.2 Common tag fields
procedure
(tags-title tags) → string?
tags : any/c
procedure
(tags-album tags) → string?
tags : any/c
procedure
(tags-artist tags) → string?
tags : any/c
procedure
(tags-comment tags) → string?
tags : any/c
procedure
(tags-genre tags) → string?
tags : any/c
procedure
(tags-title! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-album! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-artist! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-comment! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-genre! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-year! tags value) → void?
tags : any/c value : (or/c exact-nonnegative-integer? 'clear)
procedure
(tags-track! tags value) → void?
tags : any/c value : (or/c exact-nonnegative-integer? 'clear)
4.3 Selected generic fields
procedure
(tags-composer tags) → (or/c string? (listof string?))
tags : any/c
procedure
(tags-album-artist tags) → (or/c string? (listof string?))
tags : any/c
procedure
(tags-disc-number tags) → (or/c number? #f)
tags : any/c
procedure
(tags-composer! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-album-artist! tags value) → void?
tags : any/c value : (or/c string? 'clear)
procedure
(tags-disc-number! tags value) → void?
tags : any/c value : (or/c exact-nonnegative-integer? string? 'clear)
4.4 Audio properties
procedure
(tags-length tags) → integer?
tags : any/c
procedure
(tags-sample-rate tags) → integer?
tags : any/c
procedure
(tags-bit-rate tags) → integer?
tags : any/c
procedure
(tags-channels tags) → integer?
tags : any/c
4.5 Generic properties
procedure
(tags-set-values! tags key values) → void?
tags : any/c key : (or/c symbol? string?) values : (or/c (listof string?) 'clear)
procedure
(tags-append! tags key value) → void?
tags : any/c key : (or/c symbol? string?) value : string?
(call-with-id3-tags "track.flac" (lambda (tags) (tags-set-values! tags 'composer '("Johann Sebastian Bach")) (tags-set! tags 'discnumber "1") (tags-save! tags)) #:mode 'read-write)
Generic properties may contain multiple values for a single key. The API keeps those values as lists instead of joining them into one string.
4.6 Embedded pictures
The module represents embedded artwork as an opaque picture value. The picture value is returned by tags-picture and can be inspected with the picture procedures documented below. It can also be written to another file with tags-picture! or tags-append-picture!.
procedure
(tags-picture tags) → (or/c any/c #f)
tags : any/c
procedure
(tags-picture->kind tags) → (or/c integer? #f)
tags : any/c
procedure
(tags-picture->mimetype tags) → (or/c string? #f)
tags : any/c
procedure
(tags-picture->description tags) → (or/c string? #f)
tags : any/c
procedure
(tags-picture->size tags) → (or/c integer? #f)
tags : any/c
procedure
(tags-picture->ext tags) → (or/c symbol? #f)
tags : any/c
procedure
(tags-picture->bitmap tags) → (or/c (is-a?/c bitmap%) #f)
tags : any/c
procedure
(tags-picture->file tags path) → boolean?
tags : any/c path : path-string?
procedure
(make-tags-picture mimetype kind data [ #:description description]) → id3-picture? mimetype : string? kind : integer? data : (or/c bytes? (is-a?/c bitmap%)) description : string? = ""
procedure
(make-tags-picture-from-bitmap bitmap kind [ #:mimetype mimetype #:description description]) → id3-picture? bitmap : (is-a?/c bitmap%) kind : integer? mimetype : string? = "image/png" description : string? = ""
procedure
(tags-picture! tags picture) → void?
tags : any/c picture : (or/c id3-picture? 'clear)
procedure
(tags-append-picture! tags picture) → void?
tags : any/c picture : id3-picture?
procedure
(tags-clear-picture! tags) → void?
tags : any/c
(define cover (make-tags-picture "image/jpeg" 3 (file->bytes "cover.jpg") #:description "Cover")) (call-with-id3-tags "track.flac" (lambda (tags) (tags-picture! tags cover) (tags-save! tags)) #:mode 'read-write)
4.7 Picture values
procedure
(id3-picture? v) → boolean?
v : any/c
procedure
(id3-picture-mimetype picture) → string?
picture : id3-picture?
procedure
(id3-picture-kind picture) → integer?
picture : id3-picture?
procedure
(id3-picture-size picture) → integer?
picture : id3-picture?
procedure
(id3-picture-bytes picture) → bytes?
picture : id3-picture?
procedure
(id3-picture-description picture) → string?
picture : id3-picture?
4.8 Converting to a hash
procedure
(tags->hash tags) → hash?
tags : any/c
The hash is intended as a convenient snapshot for application code. Generic property values are not expanded into the hash; use tags-ref for those values.
4.9 Copying tags and pictures
The encoder pipeline uses this module for metadata transfer. For FLAC output, audio-encode first writes the audio stream and then opens the resulting file with id3-tags in read-write mode to copy tags and pictures through TagLib. For Opus output, comments and pictures are supplied to libopusenc before encoding starts, because OpusTags are written at the start of the Ogg Opus stream.
Applications that need explicit metadata editing should use the read-write API directly, as in the examples above.
4.10 Example
(define tags (id3-tags "track.flac")) (cond [(not (tags-valid? tags)) (printf "No readable tags\n")] [else (printf "Title: ~a\n" (tags-title tags)) (printf "Artist: ~a\n" (tags-artist tags)) (printf "Album: ~a\n" (tags-album tags)) (printf "Length: ~a seconds\n" (tags-length tags)) (when (tags-picture tags) (define ext (or (tags-picture->ext tags) 'bin)) (tags-picture->file tags (format "cover.~a" ext)))])
4.11 Implementation notes
This chapter documents the public "taglib.rkt" layer. The native TagLib calls are delegated to "taglib-ffi.rkt", but callers normally should not use that lower level module directly.
A read-only tag handle is a Racket-side snapshot. A read-write tag handle keeps the native TagLib file open until tags-close! is called. Setter procedures update both the native file and the Racket-side cache; the changes are persisted only after tags-save! succeeds.
The implementation normalizes generic property names by lower-casing TagLib property keys and converting them to symbols. Values remain lists of strings because TagLib properties may contain multiple values for one key.