update C-STRUCT:s in audio.wav

db4
Joe Groff 2009-09-18 16:14:02 -05:00
parent 238f600da2
commit 53752b4cfd
1 changed files with 30 additions and 34 deletions

View File

@ -1,7 +1,7 @@
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.byte-array kernel locals math io.files io.streams.byte-array kernel locals math
sequences alien.data ; sequences alien alien.data classes.struct accessors ;
IN: audio.wav IN: audio.wav
CONSTANT: RIFF-MAGIC "RIFF" CONSTANT: RIFF-MAGIC "RIFF"
@ -9,30 +9,26 @@ CONSTANT: WAVE-MAGIC "WAVE"
CONSTANT: FMT-MAGIC "fmt " CONSTANT: FMT-MAGIC "fmt "
CONSTANT: DATA-MAGIC "data" CONSTANT: DATA-MAGIC "data"
C-STRUCT: riff-chunk-header STRUCT: riff-chunk-header
{ "char[4]" "id" } { id char[4] }
{ "uchar[4]" "size" } { size char[4] } ;
;
C-STRUCT: riff-chunk STRUCT: riff-chunk
{ "riff-chunk-header" "header" } { header riff-chunk-header }
{ "char[4]" "format" } { format char[4] } ;
;
C-STRUCT: wav-fmt-chunk STRUCT: wav-fmt-chunk
{ "riff-chunk-header" "header" } { header riff-chunk-header }
{ "uchar[2]" "audio-format" } { audio-format uchar[2] }
{ "uchar[2]" "num-channels" } { num-channels uchar[2] }
{ "uchar[4]" "sample-rate" } { sample-rate uchar[4] }
{ "uchar[4]" "byte-rate" } { byte-rate uchar[4] }
{ "uchar[2]" "block-align" } { block-align uchar[2] }
{ "uchar[2]" "bits-per-sample" } { bits-per-sample uchar[2] } ;
;
C-STRUCT: wav-data-chunk STRUCT: wav-data-chunk
{ "riff-chunk-header" "header" } { header riff-chunk-header }
{ "uchar[0]" "body" } { body uchar[0] } ;
;
ERROR: invalid-wav-file ; ERROR: invalid-wav-file ;
@ -44,39 +40,39 @@ ERROR: invalid-wav-file ;
: read-chunk ( -- byte-array/f ) : read-chunk ( -- byte-array/f )
4 ensured-read [ 4 ensured-read* dup le> ensured-read* 3append ] [ f ] if* ; 4 ensured-read [ 4 ensured-read* dup le> ensured-read* 3append ] [ f ] if* ;
: read-riff-chunk ( -- byte-array/f ) : read-riff-chunk ( -- byte-array/f )
"riff-chunk" heap-size ensured-read* ; riff-chunk heap-size ensured-read* ;
: id= ( chunk id -- ? ) : id= ( chunk id -- ? )
[ 4 head ] dip sequence= ; [ 4 head ] dip sequence= ; inline
: check-chunk ( chunk id min-size -- ? ) : check-chunk ( chunk id class -- ? )
[ id= ] [ [ length ] dip >= ] bi-curry* bi and ; heap-size [ id= ] [ [ length ] dip >= ] bi-curry* bi and ;
:: read-wav-chunks ( -- fmt data ) :: read-wav-chunks ( -- fmt data )
f :> fmt! f :> data! f :> fmt! f :> data!
[ { [ fmt data and not ] [ read-chunk ] } 0&& dup ] [ { [ fmt data and not ] [ read-chunk ] } 0&& dup ]
[ { [ {
{ [ dup FMT-MAGIC "wav-fmt-chunk" heap-size check-chunk ] [ fmt! ] } { [ dup FMT-MAGIC wav-fmt-chunk check-chunk ] [ wav-fmt-chunk memory>struct fmt! ] }
{ [ dup DATA-MAGIC "wav-data-chunk" heap-size check-chunk ] [ data! ] } { [ dup DATA-MAGIC wav-data-chunk check-chunk ] [ wav-data-chunk memory>struct data! ] }
} cond ] while drop } cond ] while drop
fmt data 2dup and [ invalid-wav-file ] unless ; fmt data 2dup and [ invalid-wav-file ] unless ;
: verify-wav ( chunk -- ) : verify-wav ( chunk -- )
{ {
[ RIFF-MAGIC id= ] [ RIFF-MAGIC id= ]
[ riff-chunk-format 4 memory>byte-array WAVE-MAGIC id= ] [ riff-chunk memory>struct format>> 4 memory>byte-array WAVE-MAGIC id= ]
} 1&& } 1&&
[ invalid-wav-file ] unless ; [ invalid-wav-file ] unless ;
: (read-wav) ( -- audio ) : (read-wav) ( -- audio )
read-wav-chunks read-wav-chunks
[ [
[ wav-fmt-chunk-num-channels 2 memory>byte-array le> ] [ num-channels>> 2 memory>byte-array le> ]
[ wav-fmt-chunk-bits-per-sample 2 memory>byte-array le> ] [ bits-per-sample>> 2 memory>byte-array le> ]
[ wav-fmt-chunk-sample-rate 4 memory>byte-array le> ] tri [ sample-rate>> 4 memory>byte-array le> ] tri
] [ ] [
[ riff-chunk-header-size 4 memory>byte-array le> dup ] [ header>> size>> 4 memory>byte-array le> dup ]
[ wav-data-chunk-body ] bi swap memory>byte-array [ body>> >c-ptr ] bi swap memory>byte-array
] bi* <audio> ; ] bi* <audio> ;
: read-wav ( filename -- audio ) : read-wav ( filename -- audio )