]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/id3/id3.factor
use radix literals
[factor.git] / extra / id3 / id3.factor
index 6acace858276fa25cec8f85a05b209a048ad46a7..f1ee2210612462fba15c6d38161648dba625b8ef 100644 (file)
@@ -1,12 +1,13 @@
 ! Copyright (C) 2009 Tim Wawrzynczak, Doug Coleman.
 ! 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
-io.encodings.string io.encodings.utf16 assocs math.parser
-combinators.short-circuit fry namespaces combinators.smart
-splitting io.encodings.ascii arrays io.files.info unicode.case
-io.directories.search literals math.functions continuations ;
+strings kernel math io.mmap accessors combinators math.ranges
+unicode.categories byte-arrays io.encodings.string
+io.encodings.utf16 assocs math.parser combinators.short-circuit
+fry namespaces combinators.smart splitting io.encodings.ascii
+arrays io.files.info unicode.case io.directories.search literals
+math.functions continuations ;
+FROM: alien.c-types => uchar ;
 IN: id3
 
 <PRIVATE
@@ -65,7 +66,7 @@ speed genre-name start-time end-time ;
 CONSTANT: id3v1-length 128
 CONSTANT: id3v1-offset 128
 CONSTANT: id3v1+-length 227
-CONSTANT: id3v1+-offset $[ 128 227 + ]
+: id3v1+-offset ( -- n ) id3v1-length id3v1+-length + ; inline
 
 : id3v1? ( seq -- ? )
     {
@@ -104,8 +105,8 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
     0 [ [ 7 shift ] dip bitor ] reduce ;
 
 : synchsafe>seq ( n -- seq )
-    dup 1+ log2 1+ 7 / ceiling
-    [ [ -7 shift ] keep HEX: 7f bitand  ] replicate nip reverse ;
+    dup 1 + log2 1 + 7 / ceiling
+    [ [ -7 shift ] keep 0x7f bitand  ] replicate nip reverse ;
 
 : filter-text-data ( data -- filtered )
     [ printable? ] filter ;
@@ -118,7 +119,7 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
 
 : decode-text ( string -- string' )
     dup 2 short head
-    { { HEX: ff HEX: fe } { HEX: fe HEX: ff } } member?
+    { { 0xff 0xfe } { 0xfe 0xff } } member?
     utf16 ascii ? decode ;
 
 : (read-frame) ( seq -- frame )
@@ -152,7 +153,7 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
     } cleave ;
 
 : merge-frames ( id3 assoc -- id3 )
-    [ dup frames>> ] dip update ;
+    [ dup frames>> ] dip assoc-union! drop ;
 
 : merge-id3v1 ( id3 -- id3 )
     dup id3v1>frames frames>assoc merge-frames ;
@@ -209,13 +210,12 @@ PRIVATE>
 
 : mp3>id3 ( path -- id3/f )
     [
-        [ <id3> ] dip
-        {
-            [ dup id3v1? [ read-v1-tags merge-id3v1 ] [ drop ] if ]
-            [ dup id3v1+? [ read-v1+-tags merge-id3v1 ] [ drop ] if ]
-            [ dup id3v2? [ read-v2-tags ] [ drop ] if ]
-        } cleave
-    ] with-mapped-uchar-file-reader ;
+        [ <id3> ] dip uchar <mapped-array>
+        [ dup id3v1? [ read-v1-tags merge-id3v1 ] [ drop ] if ]
+        [ dup id3v1+? [ read-v1+-tags merge-id3v1 ] [ drop ] if ]
+        [ dup id3v2? [ read-v2-tags ] [ drop ] if ]
+        tri
+    ] with-mapped-file-reader ;
 
 : find-id3-frame ( id3 name -- obj/f )
     swap frames>> at* [ data>> ] when ;