plug some holes in wav parser

db4
Joe Groff 2009-04-29 08:22:35 -05:00
parent 570315d8c5
commit 2741b3739d
1 changed files with 12 additions and 8 deletions

View File

@ -1,6 +1,6 @@
USING: alien.c-types alien.syntax audio combinators USING: alien.c-types alien.syntax audio combinators
combinators.short-circuit io io.binary io.encodings.binary combinators.short-circuit io io.binary io.encodings.binary
io.files io.streams.memory kernel locals sequences ; io.files io.streams.byte-array kernel locals sequences ;
IN: audio.wav IN: audio.wav
CONSTANT: RIFF-MAGIC "RIFF" CONSTANT: RIFF-MAGIC "RIFF"
@ -16,7 +16,6 @@ C-STRUCT: riff-chunk-header
C-STRUCT: riff-chunk C-STRUCT: riff-chunk
{ "riff-chunk-header" "header" } { "riff-chunk-header" "header" }
{ "char[4]" "format" } { "char[4]" "format" }
{ "uchar[0]" "body" }
; ;
C-STRUCT: wav-fmt-chunk C-STRUCT: wav-fmt-chunk
@ -34,8 +33,17 @@ C-STRUCT: wav-data-chunk
{ "uchar[0]" "body" } { "uchar[0]" "body" }
; ;
ERROR: invalid-wav-file ;
: ensured-read ( count -- output/f )
[ read ] keep over length = [ drop f ] unless ;
: ensured-read* ( count -- output )
ensured-read [ invalid-wav-file ] unless* ;
: read-chunk ( -- byte-array/f ) : read-chunk ( -- byte-array/f )
4 read [ 4 read le> [ <uint> ] [ read ] bi 3append ] [ f ] if* ; 4 ensured-read [ 4 ensured-read* dup le> ensured-read* 3append ] [ f ] if* ;
: read-riff-chunk ( -- byte-array/f )
"riff-chunk" heap-size ensured-read* ;
: id= ( chunk id -- ? ) : id= ( chunk id -- ? )
[ 4 memory>byte-array ] dip sequence= ; [ 4 memory>byte-array ] dip sequence= ;
@ -49,8 +57,6 @@ C-STRUCT: wav-data-chunk
} cond ] while drop } cond ] while drop
fmt data ; fmt data ;
ERROR: invalid-wav-file ;
: verify-wav ( chunk -- ) : verify-wav ( chunk -- )
{ [ RIFF-MAGIC id= ] [ riff-chunk-format WAVE-MAGIC id= ] } 1&& { [ RIFF-MAGIC id= ] [ riff-chunk-format WAVE-MAGIC id= ] } 1&&
[ invalid-wav-file ] unless ; [ invalid-wav-file ] unless ;
@ -68,7 +74,5 @@ ERROR: invalid-wav-file ;
: read-wav ( filename -- audio ) : read-wav ( filename -- audio )
binary [ binary [
read-chunk read-riff-chunk verify-wav (read-wav)
[ verify-wav ]
[ riff-chunk-body <memory-stream> [ (read-wav) ] with-input-stream* ] bi
] with-file-reader ; ] with-file-reader ;