! Copyright (C) 2008 Tim Wawrzynczak
! See http://factorcode.org/license.txt for BSD license.
-USING: help.markup help.syntax sequences kernel ;
+USING: help.markup help.syntax sequences kernel accessors ;
IN: id3
HELP: file-id3-tags
{ $values
{ "path" "a path string" }
{ "object/f" "a tuple storing ID3 metadata or f" } }
-{ $description "Return a tuple containing the ID3 information parsed out of the MP3 file, or " { $link f } " if no metadata is present." } ;
+ { $description "Return a tuple containing the ID3 information parsed out of the MP3 file, or " { $link f } " if no metadata is present. Currently, the parser supports the following tags: "
+ $nl { $link title>> }
+ $nl { $link artist>> }
+ $nl { $link album>> }
+ $nl { $link year>> }
+ $nl { $link genre>> }
+ $nl { $link comment>> } } ;
ARTICLE: "id3" "ID3 tags"
"The " { $vocab-link "id3" } " vocabulary contains words for parsing " { $emphasis "ID3" } " tags, which are textual fields storing an MP3's title, artist, and other metadata." $nl
! Copyright (C) 2009 Tim Wawrzynczak
! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test id3 ;
+USING: tools.test id3 id3.private ;
IN: id3.tests
-[ T{ mp3v2-file
- { header T{ header f t 0 502 } }
- { frames
- {
- T{ frame
- { frame-id "COMM" }
- { flags B{ 0 0 } }
- { size 19 }
- { data "eng, AG# 08E1C12E" }
- }
- T{ frame
- { frame-id "TIT2" }
- { flags B{ 0 0 } }
- { size 15 }
- { data "Stormy Weather" }
- }
- T{ frame
- { frame-id "TRCK" }
- { flags B{ 0 0 } }
- { size 3 }
- { data "32" }
- }
- T{ frame
- { frame-id "TCON" }
- { flags B{ 0 0 } }
- { size 5 }
- { data "(96)" }
- }
- T{ frame
- { frame-id "TALB" }
- { flags B{ 0 0 } }
- { size 28 }
- { data "Night and Day Frank Sinatra" }
- }
- T{ frame
- { frame-id "PRIV" }
- { flags B{ 0 0 } }
- { size 39 }
- { data "WM/MediaClassPrimaryID�}`�#��K�H�*(D" }
- }
- T{ frame
- { frame-id "PRIV" }
- { flags B{ 0 0 } }
- { size 41 }
- { data "WM/MediaClassSecondaryID" }
- }
- T{ frame
- { frame-id "TPE1" }
- { flags B{ 0 0 } }
- { size 14 }
- { data "Frank Sinatra" }
- }
- }
- }
-}
-] [ "resource:extra/id3/tests/blah3.mp3" file-id3-tags ] unit-test
-
[
- T{ mp3v2-file
- { header
- T{ header { version t } { flags 0 } { size 1405 } }
+ T{ id3-info
+ { title "BLAH" }
+ { artist "ARTIST" }
+ { album "ALBUM" }
+ { year "2009" }
+ { comment "COMMENT" }
+ { genre "Bluegrass" }
}
- { frames
- {
- T{ frame
- { frame-id "TIT2" }
- { flags B{ 0 0 } }
- { size 22 }
- { data "Anthem of the Trinity" }
- }
- T{ frame
- { frame-id "TPE1" }
- { flags B{ 0 0 } }
- { size 12 }
- { data "Terry Riley" }
- }
- T{ frame
- { frame-id "TALB" }
- { flags B{ 0 0 } }
- { size 11 }
- { data "Shri Camel" }
- }
- T{ frame
- { frame-id "TCON" }
- { flags B{ 0 0 } }
- { size 10 }
- { data "Classical" }
- }
- T{ frame
- { frame-id "UFID" }
- { flags B{ 0 0 } }
- { size 23 }
- { data "http://musicbrainz.org" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 23 }
- { data "MusicBrainz Artist Id" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 22 }
- { data "musicbrainz_artistid" }
- }
- T{ frame
- { frame-id "TRCK" }
- { flags B{ 0 0 } }
- { size 2 }
- { data "1" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 22 }
- { data "MusicBrainz Album Id" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 21 }
- { data "musicbrainz_albumid" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 29 }
- { data "MusicBrainz Album Artist Id" }
- }
- T{ frame
- { frame-id "TXXX" }
- { flags B{ 0 0 } }
- { size 27 }
- { data "musicbrainz_albumartistid" }
- }
- T{ frame
- { frame-id "TPOS" }
- { flags B{ 0 0 } }
- { size 2 }
- { data "1" }
- }
- T{ frame
- { frame-id "TSOP" }
- { flags B{ 0 0 } }
- { size 1 }
- }
- T{ frame
- { frame-id "TMED" }
- { flags B{ 0 0 } }
- { size 4 }
- { data "DIG" }
- }
- }
+] [ "resource:extra/id3/tests/blah.mp3" file-id3-tags ] unit-test
+
+[
+ T{ id3-info
+ { title "Anthem of the Trinity" }
+ { artist "Terry Riley" }
+ { album "Shri Camel" }
+ { genre "Classical" }
}
-}
] [ "resource:extra/id3/tests/blah2.mp3" file-id3-tags ] unit-test
[
- T{ mp3v1-file
- { title
- "BLAH"
- }
- { artist
- "ARTIST"
- }
- { album
- "ALBUM"
- }
- { year "2009" }
- { comment
- "COMMENT"
- }
- { genre 89 }
- }
-] [ "resource:extra/id3/tests/blah.mp3" file-id3-tags ] unit-test
+ T{ id3-info
+ { title "Stormy Weather" }
+ { artist "Frank Sinatra" }
+ { album "Night and Day Frank Sinatra" }
+ { comment "eng, AG# 08E1C12E" }
+ { genre "Big Band" }
+ }
+] [ "resource:extra/id3/tests/blah3.mp3" file-id3-tags ] unit-test
! Copyright (C) 2009 Tim Wawrzynczak
! See http://factorcode.org/license.txt for BSD license.
-USING: sequences io io.encodings.binary io.files io.pathnames strings kernel math io.mmap io.mmap.uchar accessors syntax combinators math.ranges unicode.categories byte-arrays prettyprint io.encodings.string io.encodings.ascii ;
+USING: sequences io io.encodings.binary io.files io.pathnames strings kernel math io.mmap io.mmap.uchar accessors syntax combinators math.ranges unicode.categories byte-arrays io.encodings.string io.encodings.utf8 assocs math.parser ;
IN: id3
+<PRIVATE
+
+! genres
+CONSTANT: genres
+ H{
+ { 0 "Blues" }
+ { 1 "Classic Rock" }
+ { 2 "Country" }
+ { 3 "Dance" }
+ { 4 "Disco" }
+ { 5 "Funk" }
+ { 6 "Grunge" }
+ { 7 "Hip-Hop" }
+ { 8 "Jazz" }
+ { 9 "Metal" }
+ { 10 "New Age" }
+ { 11 "Oldies" }
+ { 12 "Other" }
+ { 13 "Pop" }
+ { 14 "R&B" }
+ { 15 "Rap" }
+ { 16 "Reggae" }
+ { 17 "Rock" }
+ { 18 "Techno" }
+ { 19 "Industrial" }
+ { 20 "Alternative" }
+ { 21 "Ska" }
+ { 22 "Death Metal" }
+ { 23 "Pranks" }
+ { 24 "Soundtrack" }
+ { 25 "Euro-Techno" }
+ { 26 "Ambient" }
+ { 27 "Trip-Hop" }
+ { 28 "Vocal" }
+ { 29 "Jazz+Funk" }
+ { 30 "Fusion" }
+ { 31 "Trance" }
+ { 32 "Classical" }
+ { 33 "Instrumental" }
+ { 34 "Acid" }
+ { 35 "House" }
+ { 36 "Game" }
+ { 37 "Sound Clip" }
+ { 38 "Gospel" }
+ { 39 "Noise" }
+ { 40 "AlternRock" }
+ { 41 "Bass" }
+ { 42 "Soul" }
+ { 43 "Punk" }
+ { 44 "Space" }
+ { 45 "Meditative" }
+ { 46 "Instrumental Pop" }
+ { 47 "Instrumental Rock" }
+ { 48 "Ethnic" }
+ { 49 "Gothic" }
+ { 50 "Darkwave" }
+ { 51 "Techno-Industrial" }
+ { 52 "Electronic" }
+ { 53 "Pop-Folk" }
+ { 54 "Eurodance" }
+ { 55 "Dream" }
+ { 56 "Southern Rock" }
+ { 57 "Comedy" }
+ { 58 "Cult" }
+ { 59 "Gangsta" }
+ { 60 "Top 40" }
+ { 61 "Christian Rap" }
+ { 62 "Pop/Funk" }
+ { 63 "Jungle" }
+ { 64 "Native American" }
+ { 65 "Cabaret" }
+ { 66 "New Wave" }
+ { 67 "Psychedelic" }
+ { 68 "Rave" }
+ { 69 "Showtunes" }
+ { 70 "Trailer" }
+ { 71 "Lo-Fi" }
+ { 72 "Tribal" }
+ { 73 "Acid Punk" }
+ { 74 "Acid Jazz" }
+ { 75 "Polka" }
+ { 76 "Retro" }
+ { 77 "Musical" }
+ { 78 "Rock & Roll" }
+ { 79 "Hard Rock" }
+ { 80 "Folk" }
+ { 81 "Folk-Rock" }
+ { 82 "National Folk" }
+ { 83 "Swing" }
+ { 84 "Fast Fusion" }
+ { 85 "Bebop" }
+ { 86 "Latin" }
+ { 87 "Revival" }
+ { 88 "Celtic" }
+ { 89 "Bluegrass" }
+ { 90 "Avantgarde" }
+ { 91 "Gothic Rock" }
+ { 92 "Progressive Rock" }
+ { 93 "Psychedelic Rock" }
+ { 94 "Symphonic Rock" }
+ { 95 "Slow Rock" }
+ { 96 "Big Band" }
+ { 97 "Chorus" }
+ { 98 "Easy Listening" }
+ { 99 "Acoustic" }
+ { 100 "Humour" }
+ { 101 "Speech" }
+ { 102 "Chanson" }
+ { 103 "Opera" }
+ { 104 "Chamber Music" }
+ { 105 "Sonata" }
+ { 106 "Symphony" }
+ { 107 "Booty Bass" }
+ { 108 "Primus" }
+ { 109 "Porn Groove" }
+ { 110 "Satire" }
+ { 111 "Slow Jam" }
+ { 112 "Club" }
+ { 113 "Tango" }
+ { 114 "Samba" }
+ { 115 "Folklore" }
+ { 116 "Ballad" }
+ { 117 "Power Ballad" }
+ { 118 "Rhythmic Soul" }
+ { 119 "Freestyle" }
+ { 120 "Duet" }
+ { 121 "Punk Rock" }
+ { 122 "Drum Solo" }
+ { 123 "A capella" }
+ { 124 "Euro-House" }
+ { 125 "Dance Hall" }
+} ! end genre hashtable
+
! tuples
TUPLE: header version flags size ;
TUPLE: frame frame-id flags size data ;
-TUPLE: mp3v2-file header frames ;
+TUPLE: id3v2-info header frames ;
-TUPLE: mp3v1-file title artist album year comment genre ;
+TUPLE: id3-info title artist album year comment genre ;
-: <mp3v1-file> ( -- object ) mp3v1-file new ;
+: <id3-info> ( -- object ) id3-info new ;
-: <mp3v2-file> ( header frames -- object ) mp3v2-file boa ;
+: <id3v2-info> ( header frames -- object ) id3v2-info boa ;
: <header> ( -- object ) header new ;
: <frame> ( -- object ) frame new ;
-<PRIVATE
-
! utility words
: id3v2? ( mmap -- ? )
: (read-frame) ( mmap -- frame )
[ <frame> ] dip
{
- [ read-frame-id ascii decode >>frame-id ]
+ [ read-frame-id utf8 decode >>frame-id ]
[ read-frame-flags >byte-array >>flags ]
[ read-frame-size >28bitword >>size ]
- [ read-frame-data ascii decode >>data ]
+ [ read-frame-data utf8 decode >>data ]
} cleave ;
: read-frame ( mmap -- frame/f )
: drop-header ( mmap -- seq1 seq2 )
dup 10 tail-slice swap ;
-: read-v2-tag-data ( seq -- mp3v2-file )
- drop-header read-v2-header swap read-frames <mp3v2-file> ;
+: parse-frames ( id3v2-info -- id3-info )
+ [ <id3-info> ] dip frames>>
+ {
+ [ [ frame-id>> "TIT2" = ] find nip [ data>> >>title ] when* ]
+ [ [ frame-id>> "TALB" = ] find nip [ data>> >>album ] when* ]
+ [ [ frame-id>> "TPE1" = ] find nip [ data>> >>artist ] when* ]
+ [ [ frame-id>> "TCON" = ] find nip [ data>> [ [ digit? ] filter string>number ] keep swap [ genres at nip ] when*
+ >>genre ] when* ]
+ [ [ frame-id>> "COMM" = ] find nip [ data>> >>comment ] when* ]
+ [ [ frame-id>> "TYER" = ] find nip [ data>> >>year ] when* ]
+ } cleave ;
+: read-v2-tag-data ( seq -- id3-info )
+ drop-header read-v2-header swap read-frames <id3v2-info> parse-frames ;
+
! v1 information
: skip-to-v1-data ( seq -- seq )
[ 124 ] dip nth ;
: (read-v1-tag-data) ( seq -- mp3-file )
- [ <mp3v1-file> ] dip
+ [ <id3-info> ] dip
{
- [ read-title ascii decode filter-text-data >>title ]
- [ read-artist ascii decode filter-text-data >>artist ]
- [ read-album ascii decode filter-text-data >>album ]
- [ read-year ascii decode filter-text-data >>year ]
- [ read-comment ascii decode filter-text-data >>comment ]
- [ read-genre >fixnum >>genre ]
+ [ read-title utf8 decode filter-text-data >>title ]
+ [ read-artist utf8 decode filter-text-data >>artist ]
+ [ read-album utf8 decode filter-text-data >>album ]
+ [ read-year utf8 decode filter-text-data >>year ]
+ [ read-comment utf8 decode filter-text-data >>comment ]
+ [ read-genre >fixnum genres at >>genre ]
} cleave ;
: read-v1-tag-data ( seq -- mp3-file )
PRIVATE>
-! main stuff
+! public interface
: file-id3-tags ( path -- object/f )
[
{
- { [ dup id3v2? ] [ read-v2-tag-data ] } ! ( ? -- mp3v2-file )
- { [ dup id3v1? ] [ read-v1-tag-data ] } ! ( ? -- mp3v1-file )
+ { [ dup id3v2? ] [ read-v2-tag-data ] } ! ( ? -- id3v2 )
+ { [ dup id3v1? ] [ read-v1-tag-data ] } ! ( ? -- id3-info )
[ drop f ] ! ( mmap -- f )
} cond
] with-mapped-uchar-file ;