Steve Ayerhart 2020-04-30 17:39:46 -05:00
parent 440365c072
commit c91134d0ba
No known key found for this signature in database
GPG Key ID: 5BFD39C5359E967D
2 changed files with 57 additions and 51 deletions

View File

@ -2,11 +2,12 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: math io.encodings.binary kernel io io.files locals endian bit-arrays math.intervals combinators combinators.extras math.order sequences namespaces accessors byte-arrays math.bitwise ; USING: math io.encodings.binary kernel io io.files locals endian bit-arrays math.intervals combinators combinators.extras math.order sequences namespaces accessors byte-arrays math.bitwise ;
USING: prettyprint ; USING: prettyprint ;
USING: flac.bitstream flac.metadata.private flac.format ; USING: flac.bitstream flac.metadata flac.format ;
IN: flac.decoder IN: flac.decoder
CONSTANT: sync-code 16382 CONSTANT: sync-code 16382
ERROR: sync-code-error ; ERROR: sync-code-error ;
ERROR: invalid-channel-assignment ; ERROR: invalid-channel-assignment ;
ERROR: reserved-block-size ; ERROR: reserved-block-size ;
@ -14,9 +15,6 @@ ERROR: invalid-sample-rate ;
ERROR: reserved-subframe-type ; ERROR: reserved-subframe-type ;
ERROR: invalid-subframe-sync ; ERROR: invalid-subframe-sync ;
: read-flac-magic ( -- ? )
32 flac-read-uint FLAC-MAGIC = ;
: 0xxxxxxx? ( n -- ? ) 0x80 mask? not ; : 0xxxxxxx? ( n -- ? ) 0x80 mask? not ;
: 110xxxxx? ( n -- ? ) [ 0xc0 mask? ] [ 0x20 mask? not ] bi and ; : 110xxxxx? ( n -- ? ) [ 0xc0 mask? ] [ 0x20 mask? not ] bi and ;
: 1110xxxx? ( n -- ? ) [ 0xe0 mask? ] [ 0x10 mask? not ] bi and ; : 1110xxxx? ( n -- ? ) [ 0xe0 mask? ] [ 0x10 mask? not ] bi and ;
@ -44,6 +42,14 @@ ERROR: invalid-subframe-sync ;
! 6 shift 6 bitstream read-bit bitor ! 6 shift 6 bitstream read-bit bitor
! ] each ; ! ] each ;
: read-utf8-uint ( -- n )
8 flac-read-uint dup
[ 0b11000000 <= ]
[
8 flac-read-uint drop
1 shift 0xff bitand
] while ;
! : read-utf8-uint ( -- n ) ! : read-utf8-uint ( -- n )
! 1 read dup ! 1 read dup
! be> 0xxxxxxx? ! be> 0xxxxxxx?
@ -192,7 +198,19 @@ ERROR: invalid-subframe-sync ;
! 4 <iota> [ drop read-frame ] map ! 4 <iota> [ drop read-frame ] map
! ] with-file-reader ; ! ] with-file-reader ;
: read-flac-frame-header ( -- frame-header )
14 flac-read-uint drop
1 flac-read-uint drop
1 flac-read-uint <flac-frame-number-type>
4 flac-read-uint decode-block-size
4 flac-read-uint decode-sample-rate
4 flac-read-uint decode-channels
3 flac-read-uint decode-bits-per-sample
1 flac-read-uint drop
read-utf8-uint
flac-frame-header boa ;
: read-flac-file ( filename -- flac-stream ) : read-flac-file ( filename -- flac-stream )
[ [
read-flac-magic [ not-a-flac-file ] unless read-flac-metadata
] with-flac-stream-reader ; ] with-flac-stream-reader ;

View File

@ -16,31 +16,25 @@ ERROR: cuesheet-index-reserved-must-be-zero ;
24 flac-read-uint 24 flac-read-uint
metadata-block-header boa ; metadata-block-header boa ;
:: (decode-stream-info) ( bs -- stream-info ) : read-metadata-block-stream-info ( -- stream-info )
16 bs bitstreams:read 16 flac-read-uint
16 bs bitstreams:read 16 flac-read-uint
24 bs bitstreams:read 24 flac-read-uint
24 bs bitstreams:read 24 flac-read-uint
20 bs bitstreams:read 20 flac-read-uint
3 bs bitstreams:read 1 + 3 flac-read-uint 1 +
5 bs bitstreams:read 1 + 5 flac-read-uint 1 +
36 bs bitstreams:read 36 flac-read-uint
128 bs bitstreams:read 16 >be bytes>hex-string 128 flac-read-uint 16 >be bytes>hex-string
stream-info boa ; stream-info boa ;
: decode-stream-info ( byte-array -- stream-info ) : read-metadata-block-seek-table ( length -- seek-table )
bitstreams:<msb0-bit-reader> (decode-stream-info) ; 18 / <iota> [
drop 64 flac-read-uint 64 flac-read-uint 16 flac-read-uint seek-point boa
] map seek-table boa ;
: decode-seek-table ( byte-array -- seek-table ) : read-metadata-block-vorbis-comment ( length -- vorbis-comment )
dup dup [ 8 * flac-read-uint ] dip >be
binary
[
length 18 / <iota>
[ drop 8 io:read be> 8 io:read be> 2 io:read be> seek-point boa ] map
] with-byte-reader
seek-table boa ;
: decode-vorbis-comment ( byte-array -- comments )
binary binary
[ [
4 io:read le> io:read utf8 decode 4 io:read le> io:read utf8 decode
@ -69,13 +63,14 @@ ERROR: cuesheet-index-reserved-must-be-zero ;
: encode-padding ( padding -- byte-array ) : encode-padding ( padding -- byte-array )
length>> <byte-array> ; length>> <byte-array> ;
: decode-padding ( byte-array -- padding ) : read-metadata-block-padding ( length -- padding )
length flac-padding boa ; dup 8 * flac-read-uint drop flac-padding boa ;
: decode-application ( byte-array -- application ) : read-metadata-block-application ( length -- application )
drop application new ; 8 * flac-read-uint drop application new ;
: decode-cuesheet ( byte-array -- cuesheet ) : read-metadata-block-cuesheet ( length -- cuesheet )
dup [ 8 * flac-read-uint ] dip >be
binary binary
[ [
128 io:read ascii decode 128 io:read ascii decode
@ -98,7 +93,8 @@ ERROR: cuesheet-index-reserved-must-be-zero ;
] map ] map
] with-byte-reader cuesheet boa ; ] with-byte-reader cuesheet boa ;
: decode-picture ( byte-array -- picture ) : read-metadata-block-picture ( length -- picture )
dup [ 8 * flac-read-uint ] dip >be
binary binary
[ [
4 io:read be> <picture-type> 4 io:read be> <picture-type>
@ -111,32 +107,24 @@ ERROR: cuesheet-index-reserved-must-be-zero ;
4 io:read be> io:read 4 io:read be> io:read
] with-byte-reader picture boa ; ] with-byte-reader picture boa ;
: decode-metadata-block ( metadata byte-array type -- metadata ) : read-metadata-block ( metadata length type -- metadata )
[ [
{ {
{ metadata-stream-info [ decode-stream-info >>stream-info ] } { metadata-stream-info [ drop read-metadata-block-stream-info >>stream-info ] }
{ metadata-padding [ decode-padding >>padding ] } { metadata-padding [ read-metadata-block-padding >>padding ] }
{ metadata-application [ decode-application >>application ] } { metadata-application [ read-metadata-block-application >>application ] }
{ metadata-seek-table [ decode-seek-table >>seek-table ] } { metadata-seek-table [ read-metadata-block-seek-table >>seek-table ] }
{ metadata-vorbis-comment [ decode-vorbis-comment >>vorbis-comment ] } { metadata-vorbis-comment [ read-metadata-block-vorbis-comment >>vorbis-comment ] }
{ metadata-cuesheet [ decode-cuesheet >>cuesheet ] } { metadata-cuesheet [ read-metadata-block-cuesheet >>cuesheet ] }
{ metadata-picture [ decode-picture >>picture ] } { metadata-picture [ read-metadata-block-picture >>picture ] }
} case } case
] with-big-endian ; ] with-big-endian ;
: read-flac-metadata ( -- metadata ) : read-flac-metadata ( -- metadata )
32 flac-read-uint FLAC-MAGIC = [ not-a-flac-file ] unless 32 flac-read-uint FLAC-MAGIC = [ not-a-flac-file ] unless
metadata new metadata new
"HI" .
[ [
"HI" .
read-metadata-block-header read-metadata-block-header
[ length>> io:read ] [ type>> ] [ last?>> not ] tri [ length>> ] [ type>> ] [ last?>> not ] tri
[ decode-metadata-block ] dip [ read-metadata-block ] dip
] loop ; ] loop ;
! metadata new
! [
! read-metadata-block-header
! [ length>> io:read ] [ type>> ] [ last?>> not ] tri
! [ decode-metadata-block ] dip
! ] loop ;