]> gitweb.factorcode.org Git - factor.git/commitdiff
write synchsafe numbers to sequences
authorDoug Coleman <erg@jobim.local>
Thu, 9 Apr 2009 23:23:05 +0000 (18:23 -0500)
committerDoug Coleman <erg@jobim.local>
Thu, 9 Apr 2009 23:23:05 +0000 (18:23 -0500)
extra/id3/id3-tests.factor
extra/id3/id3.factor

index a8f35e582cef10ae7a98adf6b2549f7c7f06c70b..9bb755807771054a1aaf8cda2a74ec6abd8f058d 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2009 Tim Wawrzynczak
 ! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test id3 combinators ;
+USING: tools.test id3 combinators grouping id3.private
+sequences math ;
 IN: id3.tests
 
 : id3-params ( id3 -- title artist album year comment genre )
@@ -40,3 +41,6 @@ IN: id3.tests
    "Big Band"
 ] [ "vocab:id3/tests/blah3.mp3" mp3>id3 id3-params ] unit-test
 
+
+[ t ]
+[ 10000 [ synchsafe>seq seq>synchsafe ] map [ < ] monotonic? ] unit-test
index 8a235d305d1d5a7ec9e577ae4d1b7ecdff123825..a742a1f08d3c66616aa49d52ab5481db6a2d64e7 100644 (file)
@@ -6,7 +6,7 @@ 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 ;
+io.directories.search literals math.functions ;
 IN: id3
 
 <PRIVATE
@@ -100,9 +100,13 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
         } cleave
     ] output>array sift ;
 
-: >28bitword ( seq -- int )
+: seq>synchsafe ( seq -- n )
     0 [ [ 7 shift ] dip bitor ] reduce ; inline
 
+: synchsafe>seq ( n -- seq )
+    dup 1+ log2 1+ 7 / ceiling
+    [ [ -7 shift ] keep HEX: 7f bitand  ] replicate nip reverse ; inline
+
 : filter-text-data ( data -- filtered )
     [ printable? ] filter ; inline
 
@@ -121,7 +125,7 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
     [ <frame> ] dip
     {
         [ 4 head-slice decode-text >>tag ]
-        [ [ 4 8 ] dip subseq >28bitword >>size ]
+        [ [ 4 8 ] dip subseq seq>synchsafe >>size ]
         [ [ 8 10 ] dip subseq >byte-array >>flags ]
         [ read-frame-data decode-text >>data ]
     } cleave ; inline
@@ -144,7 +148,7 @@ CONSTANT: id3v1+-offset $[ 128 227 + ]
     {
         [ [ 3 5 ] dip <slice> >array >>version ]
         [ [ 5 ] dip nth >>flags ]
-        [ [ 6 10 ] dip <slice> >28bitword >>size ]
+        [ [ 6 10 ] dip <slice> seq>synchsafe >>size ]
     } cleave ; inline
 
 : merge-frames ( id3 assoc -- id3 )