53 lines
1.6 KiB
Factor
53 lines
1.6 KiB
Factor
|
USING: kernel namespaces accessors sequences destructors io.files io.encodings.binary math math.functions math.bitwise ;
|
||
|
FROM: flac.format => FLAC-MAGIC not-a-flac-stream ;
|
||
|
QUALIFIED: bitstreams
|
||
|
QUALIFIED: io
|
||
|
|
||
|
IN: flac.stream
|
||
|
|
||
|
SYMBOL: flac-input-stream
|
||
|
|
||
|
CONSTANT: default-bitreader-capacity 65536
|
||
|
|
||
|
TUPLE: flac-stream-reader stream bitstream ;
|
||
|
|
||
|
M: flac-stream-reader dispose stream>> dispose ;
|
||
|
|
||
|
: <flac-stream-reader> ( stream -- flac-stream-reader )
|
||
|
B{ } bitstreams:<msb0-bit-reader> flac-stream-reader boa ;
|
||
|
|
||
|
: flac-read ( n -- m )
|
||
|
[ dup flac-input-stream get bitstream>> bitstreams:enough-bits? not ]
|
||
|
[
|
||
|
flac-input-stream get
|
||
|
[ stream>> default-bitreader-capacity swap io:stream-read ] [ bitstream>> ] bi
|
||
|
dup bytes>> swap [ prepend ] dip swap >>bytes drop
|
||
|
] while flac-input-stream get bitstream>> bitstreams:read ;
|
||
|
|
||
|
: >32int ( x -- x' )
|
||
|
32 >signed ;
|
||
|
|
||
|
: flac-read-int32 ( n -- m )
|
||
|
flac-read >32int ;
|
||
|
|
||
|
: flac-read-rice-signed-int ( param -- n )
|
||
|
[ 0 [ 1 flac-read 0 = ] [ 1 + ] do while ] dip
|
||
|
[ shift ] keep flac-read bitor
|
||
|
[ -1 shift ] [ 1 bitand -1 * ] bi ^ ;
|
||
|
|
||
|
: flac-read-coded-number ( -- n )
|
||
|
8 flac-read
|
||
|
[ dup 0b11000000 >= ] [ 8 flac-read drop 2^ 0xff bitand ] while ;
|
||
|
|
||
|
: (with-flac-stream-reader) ( stream quot -- )
|
||
|
flac-input-stream swap with-variable ; inline
|
||
|
|
||
|
: with-flac-stream-reader ( stream quot -- )
|
||
|
[ <flac-stream-reader> ] dip [ (with-flac-stream-reader) ] curry with-disposal ; inline
|
||
|
|
||
|
: with-flac-file-reader ( path quot -- )
|
||
|
[ binary <file-reader> ] dip with-flac-stream-reader ; inline
|
||
|
|
||
|
: read/assert-flac-magic ( -- )
|
||
|
32 flac-read FLAC-MAGIC = [ not-a-flac-stream ] unless ;
|