factor-work/flac/stream/stream.factor

61 lines
2.0 KiB
Factor
Raw Normal View History

2022-08-22 23:20:30 -04:00
USING: kernel namespaces accessors sequences destructors io io.files io.encodings.binary math math.functions math.bitwise ;
2022-07-06 18:10:40 -04:00
FROM: flac.format => FLAC-MAGIC not-a-flac-stream ;
QUALIFIED: bitstreams
QUALIFIED: io
IN: flac.stream
SYMBOL: flac-input-stream
2022-08-22 23:20:30 -04:00
CONSTANT: default-bitreader-capacity 6553600
2022-07-06 18:10:40 -04:00
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 ;
2022-08-22 23:20:30 -04:00
: flac-seek ( n -- )
[ 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:seek ;
2022-08-20 22:19:05 -04:00
: flac-align-to-byte ( -- )
8 flac-input-stream get bitstream>> bitstreams:align ;
2022-07-06 18:10:40 -04:00
2022-08-20 22:19:05 -04:00
: flac-read-int ( n -- m )
dup flac-read swap >signed ;
2022-07-06 18:10:40 -04:00
: flac-read-rice-signed-int ( param -- n )
2022-08-20 22:19:05 -04:00
[ 0 [ 1 flac-read 0 = ] [ 1 + ] while ] dip
2022-07-06 18:10:40 -04:00
[ shift ] keep flac-read bitor
2022-08-20 22:19:05 -04:00
[ -1 shift ] [ 1 bitand -1 * ] bi bitxor ;
2022-07-06 18:10:40 -04:00
: 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 ;