Steve Ayerhart 2020-05-03 21:07:58 -05:00
parent 1b67ebac16
commit 3dd8221447
No known key found for this signature in database
GPG Key ID: 5BFD39C5359E967D
3 changed files with 53 additions and 33 deletions

View File

@ -30,6 +30,10 @@ M: flac-stream-reader dispose stream>> dispose ;
dup bytes>> swap [ prepend ] dip swap >>bytes drop dup bytes>> swap [ prepend ] dip swap >>bytes drop
] while flac-input-stream get bitstream>> bitstreams:read ; ] while flac-input-stream get bitstream>> bitstreams:read ;
: flac-read-sint ( n -- n )
! TODO: this isn't rightt
dup flac-read-uint dup . dup 1 - neg shift swap shift ;
: with-flac-stream-reader* ( flac-bitstream quot -- ) : with-flac-stream-reader* ( flac-bitstream quot -- )
flac-input-stream swap with-variable ; inline flac-input-stream swap with-variable ; inline

View File

@ -6,23 +6,6 @@ USING: flac.bitstream flac.metadata flac.format ;
IN: flac.decoder IN: flac.decoder
CONSTANT: sync-code 16382
ERROR: sync-code-error ;
ERROR: invalid-channel-assignment ;
ERROR: reserved-block-size ;
ERROR: invalid-sample-rate ;
ERROR: reserved-subframe-type ;
ERROR: invalid-subframe-sync ;
: 0xxxxxxx? ( n -- ? ) 0x80 mask? not ;
: 110xxxxx? ( n -- ? ) [ 0xc0 mask? ] [ 0x20 mask? not ] bi and ;
: 1110xxxx? ( n -- ? ) [ 0xe0 mask? ] [ 0x10 mask? not ] bi and ;
: 11110xxx? ( n -- ? ) [ 0xf0 mask? ] [ 0x08 mask? not ] bi and ;
: 111110xx? ( n -- ? ) [ 0xf8 mask? ] [ 0x04 mask? not ] bi and ;
: 1111110x? ( n -- ? ) [ 0xfc mask? ] [ 0x02 mask? not ] bi and ;
: 11111110? ( n -- ? ) [ 0xfe mask? ] [ 0x01 mask? not ] bi and ;
: read-utf8-uint ( -- n ) : read-utf8-uint ( -- n )
0 [ 1 flac-read-uint 1 = ] [ 1 + ] while 0 [ 1 flac-read-uint 1 = ] [ 1 + ] while
dup [ 7 swap - flac-read-uint ] dip dup [ 7 swap - flac-read-uint ] dip
@ -86,10 +69,27 @@ ERROR: invalid-subframe-sync ;
: read-flac-subframe-constant ( frame-header subframe-header -- constant-subframe ) : read-flac-subframe-constant ( frame-header subframe-header -- constant-subframe )
drop bits-per-sample>> flac-read-uint flac-subframe-constant boa ; drop bits-per-sample>> flac-read-uint flac-subframe-constant boa ;
: read-flac-subframe-fixed ( frame-header subframe-header -- fixed-subframe ) ! : read-flac-residuals ( block-size - seq )
2drop flac-subframe-fixed new ; ! 2 flac-read-uint
! dup
! {
! { [ 0 1 between? ] [ <flac-entropy-coding-method> ] }
! [ drop reserved-residual-coding ]
! } cond-case
! 4 flac-read-uint
! ;
: decode-flac-subframe-type ( n -- order type ) : read-flac-residuals ( block-size -- seq )
;
: read-flac-subframe-fixed ( frame-header subframe-header -- fixed-subframe )
[ [ blocksize>> ] [ bits-per-sample>> ] bi ] dip
pre-order>> swap <repetition> [ flac-read-sint ] map ! warm up samples
[ read-flac-residuals ] dip
! swap read-flac-redisduals
flac-subframe-fixed new swap >>warmup swap drop ;
: decode-flac-subframe-type ( n -- type order )
dup dup
{ {
{ [ 0 = ] [ drop f 0 ] } { [ 0 = ] [ drop f 0 ] }
@ -103,7 +103,7 @@ ERROR: invalid-subframe-sync ;
: read-flac-subframe-header ( -- subframe-header ) : read-flac-subframe-header ( -- subframe-header )
1 flac-read-uint 1 = [ invalid-subframe-sync ] when 1 flac-read-uint 1 = [ invalid-subframe-sync ] when
6 flac-read-uint decode-flac-subframe-type 6 flac-read-uint decode-flac-subframe-type
1 flac-read-uint ! TODO: handle wasted bits 1 flac-read-uint
flac-subframe-header boa ; flac-subframe-header boa ;
: read-flac-subframe ( frame-header -- subframe ) : read-flac-subframe ( frame-header -- subframe )
@ -142,12 +142,21 @@ ERROR: invalid-subframe-sync ;
8 flac-read-uint 8 flac-read-uint
flac-frame-header boa ; flac-frame-header boa ;
: read-flac-frame-footer ( -- frame-footer )
16 flac-read-uint flac-frame-footer boa ;
: read-flac-frame ( -- frame ) : read-flac-frame ( -- frame )
read-flac-frame-header read-flac-frame-header dup
read-flac-subframes ; read-flac-subframes
read-flac-frame-footer
flac-frame boa ;
: read-flac-file ( filename -- flac-stream ) : read-flac-file ( filename -- flac-stream )
[ [
read-flac-metadata drop read-flac-metadata drop
read-flac-frame drop
read-flac-frame drop
read-flac-frame drop
read-flac-frame read-flac-frame
] with-flac-file-reader ; ] with-flac-file-reader ;

View File

@ -5,6 +5,7 @@ USING: alien.syntax math byte-arrays sequences kernel strings arrays assocs ;
IN: flac.format IN: flac.format
CONSTANT: FLAC-MAGIC 0x664c6143 ! fLaC CONSTANT: FLAC-MAGIC 0x664c6143 ! fLaC
CONSTANT: sync-code 0b11111111111110
CONSTANT: MIN-BLOCK-SIZE 16 CONSTANT: MIN-BLOCK-SIZE 16
CONSTANT: MAX-BLOCK-SIZE 65535 CONSTANT: MAX-BLOCK-SIZE 65535
@ -20,6 +21,12 @@ CONSTANT: MAX-FIXED-ORDER 4
CONSTANT: MAX-RICE-PARTITION-ORDER 15 CONSTANT: MAX-RICE-PARTITION-ORDER 15
ERROR: not-a-flac-file ; ERROR: not-a-flac-file ;
ERROR: sync-code-error ;
ERROR: invalid-channel-assignment ;
ERROR: reserved-block-size ;
ERROR: invalid-sample-rate ;
ERROR: reserved-subframe-type ;
ERROR: invalid-subframe-sync ;
ENUM: flac-frame-number-type ENUM: flac-frame-number-type
frame-number-type-frame frame-number-type-frame
@ -69,14 +76,6 @@ ENUM: flac-entropy-coding-method-type
entropy-coding-partioned-rice entropy-coding-partioned-rice
entropy-coding-partioned-rice2 ; entropy-coding-partioned-rice2 ;
TUPLE: flac-subframe-header
{ subframe-type maybe{ subframe-type-constant
subframe-type-verbatim
subframe-type-fixed
subframe-type-lpc } }
{ order maybe{ integer } }
{ wasted-bits integer } ;
TUPLE: flac-entropy-coding-method-partioned-rice-contents TUPLE: flac-entropy-coding-method-partioned-rice-contents
{ parameters integer } { parameters integer }
{ raw-bits integer } { raw-bits integer }
@ -98,8 +97,8 @@ TUPLE: flac-subframe-verbatim
{ data byte-array } ; { data byte-array } ;
TUPLE: flac-subframe-fixed TUPLE: flac-subframe-fixed
{ entropy-coding-method flac-entropy-coding-method }
{ warmup sequence } { warmup sequence }
{ entropy-coding-method flac-entropy-coding-method }
residual ; residual ;
TUPLE: flac-subframe-lpc TUPLE: flac-subframe-lpc
@ -111,8 +110,16 @@ TUPLE: flac-subframe-lpc
{ warmup integer } { warmup integer }
residual ; residual ;
TUPLE: flac-subframe-header
{ subframe-type maybe{ subframe-type-constant
subframe-type-verbatim
subframe-type-fixed
subframe-type-lpc } }
{ pre-order maybe{ integer } }
{ wasted-bits integer } ;
TUPLE: flac-subframe TUPLE: flac-subframe
{ subframe-header flac-subframe-header } { header flac-subframe-header }
{ data maybe{ flac-subframe-constant { data maybe{ flac-subframe-constant
flac-subframe-verbatim flac-subframe-verbatim
flac-subframe-fixed flac-subframe-fixed