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 USING: prettyprint ; 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 ; : ( stream -- flac-stream-reader ) B{ } bitstreams: 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 ; : flac-align-to-byte ( -- ) 8 flac-input-stream get bitstream>> bitstreams:align ; : flac-read-int ( n -- m ) dup flac-read swap >signed ; : flac-read-rice-signed-int ( param -- n ) [ 0 [ 1 flac-read 0 = ] [ 1 + ] while ] dip [ shift ] keep flac-read bitor [ -1 shift ] [ 1 bitand -1 * ] bi bitxor ; : 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 -- ) [ ] dip [ (with-flac-stream-reader) ] curry with-disposal ; inline : with-flac-file-reader ( path quot -- ) [ binary ] dip with-flac-stream-reader ; inline : read/assert-flac-magic ( -- ) 32 flac-read FLAC-MAGIC = [ not-a-flac-stream ] unless ;