flac overhaul with tests
parent
e5f4bd1186
commit
0992da86d0
|
@ -0,0 +1,175 @@
|
|||
USING: tools.test io.encodings.binary io.streams.byte-array kernel accessors ;
|
||||
USING: flac.format flac.metadata flac.metadata.private flac.stream flac.decoder ;
|
||||
|
||||
IN: flac.tests
|
||||
|
||||
CONSTANT: example-1 B{
|
||||
0x66 0x4c 0x61 0x43 0x80 0x00 0x00 0x22 0x10 0x00 0x10 0x00
|
||||
0x00 0x00 0x0f 0x00 0x00 0x0f 0x0a 0xc4 0x42 0xf0 0x00 0x00
|
||||
0x00 0x01 0x3e 0x84 0xb4 0x18 0x07 0xdc 0x69 0x03 0x07 0x58
|
||||
0x6a 0x3d 0xad 0x1a 0x2e 0x0f 0xff 0xf8 0x69 0x18 0x00 0x00
|
||||
0xbf 0x03 0x58 0xfd 0x03 0x12 0x8b 0xaa 0x9a
|
||||
}
|
||||
|
||||
CONSTANT: example-2 B{
|
||||
0x66 0x4c 0x61 0x43 0x00 0x00 0x00 0x22 0x00 0x10 0x00 0x10
|
||||
0x00 0x00 0x17 0x00 0x00 0x44 0x0a 0xc4 0x42 0xf0 0x00 0x00
|
||||
0x00 0x13 0xd5 0xb0 0x56 0x49 0x75 0xe9 0x8b 0x8d 0x8b 0x93
|
||||
0x04 0x22 0x75 0x7b 0x81 0x03 0x03 0x00 0x00 0x12 0x00 0x00
|
||||
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||
0x00 0x00 0x00 0x10 0x04 0x00 0x00 0x3a 0x20 0x00 0x00 0x00
|
||||
0x72 0x65 0x66 0x65 0x72 0x65 0x6e 0x63 0x65 0x20 0x6c 0x69
|
||||
0x62 0x46 0x4c 0x41 0x43 0x20 0x31 0x2e 0x33 0x2e 0x33 0x20
|
||||
0x32 0x30 0x31 0x39 0x30 0x38 0x30 0x34 0x01 0x00 0x00 0x00
|
||||
0x0e 0x00 0x00 0x00 0x54 0x49 0x54 0x4c 0x45 0x3d 0xd7 0xa9
|
||||
0xd7 0x9c 0xd7 0x95 0xd7 0x9d 0x81 0x00 0x00 0x06 0x00 0x00
|
||||
0x00 0x00 0x00 0x00 0xff 0xf8 0x69 0x98 0x00 0x0f 0x99 0x12
|
||||
0x08 0x67 0x01 0x62 0x3d 0x14 0x42 0x99 0x8f 0x5d 0xf7 0x0d
|
||||
0x6f 0xe0 0x0c 0x17 0xca 0xeb 0x21 0x00 0x0e 0xe7 0xa7 0x7a
|
||||
0x24 0xa1 0x59 0x0c 0x12 0x17 0xb6 0x03 0x09 0x7b 0x78 0x4f
|
||||
0xaa 0x9a 0x33 0xd2 0x85 0xe0 0x70 0xad 0x5b 0x1b 0x48 0x51
|
||||
0xb4 0x01 0x0d 0x99 0xd2 0xcd 0x1a 0x68 0xf1 0xe6 0xb8 0x10
|
||||
0xff 0xf8 0x69 0x18 0x01 0x02 0xa4 0x02 0xc3 0x82 0xc4 0x0b
|
||||
0xc1 0x4a 0x03 0xee 0x48 0xdd 0x03 0xb6 0x7c 0x13 0x30
|
||||
}
|
||||
|
||||
CONSTANT: example-3 B{
|
||||
0x66 0x4c 0x61 0x43 0x80 0x00 0x00 0x22 0x10 0x00 0x10 0x00
|
||||
0x00 0x00 0x1f 0x00 0x00 0x1f 0x07 0xd0 0x00 0x70 0x00 0x00
|
||||
0x00 0x18 0xf8 0xf9 0xe3 0x96 0xf5 0xcb 0xcf 0xc6 0xdc 0x80
|
||||
0x7f 0x99 0x77 0x90 0x6b 0x32 0xff 0xf8 0x68 0x02 0x00 0x17
|
||||
0xe9 0x44 0x00 0x4f 0x6f 0x31 0x3d 0x10 0x47 0xd2 0x27 0xcb
|
||||
0x6d 0x09 0x08 0x31 0x45 0x2b 0xdc 0x28 0x22 0x22 0x80 0x57
|
||||
0xa3
|
||||
}
|
||||
|
||||
{
|
||||
T{ metadata
|
||||
{ stream-info
|
||||
T{ stream-info
|
||||
{ min-block-size 4096 }
|
||||
{ max-block-size 4096 }
|
||||
{ min-frame-size 15 }
|
||||
{ max-frame-size 15 }
|
||||
{ sample-rate 44100 }
|
||||
{ channels 2 }
|
||||
{ bits-per-sample 16 }
|
||||
{ samples 1 }
|
||||
{ md5 "3e84b41807dc690307586a3dad1a2e0f" }
|
||||
}
|
||||
}
|
||||
{ padding f }
|
||||
{ application f }
|
||||
{ seek-table f }
|
||||
{ vorbis-comment f }
|
||||
{ cuesheet f }
|
||||
{ picture { } }
|
||||
}
|
||||
T{ flac-frame
|
||||
{ header
|
||||
T{ flac-frame-header
|
||||
{ blocking-strategy fixed-blocksize }
|
||||
{ blocksize 1 }
|
||||
{ sample-rate 44100 }
|
||||
{ channel-assignment channel-assignment-independent }
|
||||
{ bits-per-sample 16 }
|
||||
{ crc 191 }
|
||||
}
|
||||
}
|
||||
{ subframes
|
||||
{
|
||||
T{ flac-subframe
|
||||
{ header
|
||||
T{ flac-subframe-header
|
||||
{ subframe-type subframe-type-verbatim }
|
||||
{ wasted-bits 2 }
|
||||
}
|
||||
}
|
||||
{ data
|
||||
T{ flac-subframe-verbatim
|
||||
{ value { 25588 } }
|
||||
}
|
||||
}
|
||||
}
|
||||
T{ flac-subframe
|
||||
{ header
|
||||
T{ flac-subframe-header
|
||||
{ subframe-type subframe-type-verbatim }
|
||||
{ wasted-bits 4 }
|
||||
}
|
||||
}
|
||||
{ data
|
||||
T{ flac-subframe-verbatim
|
||||
{ value { 10416 } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{ footer T{ flac-frame-footer { crc 43674 } } }
|
||||
}
|
||||
} [
|
||||
example-1 binary <byte-reader> [
|
||||
read-flac-metadata dup stream-info>>
|
||||
read-flac-frame
|
||||
] with-flac-stream-reader
|
||||
] unit-test
|
||||
|
||||
{
|
||||
T{ metadata
|
||||
{ stream-info
|
||||
T{ stream-info
|
||||
{ min-block-size 16 }
|
||||
{ max-block-size 16 }
|
||||
{ min-frame-size 23 }
|
||||
{ max-frame-size 68 }
|
||||
{ sample-rate 44100 }
|
||||
{ channels 2 }
|
||||
{ bits-per-sample 16 }
|
||||
{ samples 19 }
|
||||
{ md5 "d5b0564975e98b8d8b930422757b8103" }
|
||||
}
|
||||
}
|
||||
{ padding T{ flac-padding { length 6 } } }
|
||||
{ seek-table
|
||||
T{ seek-table
|
||||
{ seek-points
|
||||
{ T{ seek-point { total-samples 16 } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
{ vorbis-comment
|
||||
T{ vorbis-comment
|
||||
{ vendor-string "reference libFLAC 1.3.3 20190804" }
|
||||
{ comments { { "TITLE" "שלום" } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
} [
|
||||
example-2 binary <byte-reader> [
|
||||
read-flac-metadata dup stream-info>>
|
||||
read-flac-frame
|
||||
] with-flac-stream-reader
|
||||
] unit-test
|
||||
|
||||
{
|
||||
T{ metadata
|
||||
{ stream-info
|
||||
T{ stream-info
|
||||
{ min-block-size 4096 }
|
||||
{ max-block-size 4096 }
|
||||
{ min-frame-size 31 }
|
||||
{ max-frame-size 31 }
|
||||
{ sample-rate 32000 }
|
||||
{ channels 1 }
|
||||
{ bits-per-sample 8 }
|
||||
{ samples 24 }
|
||||
{ md5 "f8f9e396f5cbcfc6dc807f9977906b32" }
|
||||
}
|
||||
}
|
||||
}
|
||||
} [
|
||||
example-3 binary <byte-reader> [
|
||||
read-flac-metadata
|
||||
] with-flac-stream-reader
|
||||
] unit-test
|
|
@ -0,0 +1,52 @@
|
|||
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 ;
|
Loading…
Reference in New Issue