aiff audio file reader

db4
Joe Groff 2010-01-19 12:25:54 -08:00
parent 9c95b884dd
commit 0d57d0deb2
3 changed files with 116 additions and 30 deletions

View File

@ -0,0 +1,81 @@
USING: accessors alien alien.c-types alien.data audio
audio.chunked-file classes.struct combinators
combinators.short-circuit endian io io.binary
io.encodings.binary io.files kernel locals math sequences ;
IN: audio.aiff
CONSTANT: FORM-MAGIC "FORM"
CONSTANT: AIFF-MAGIC "AIFF"
CONSTANT: COMM-MAGIC "COMM"
CONSTANT: SSND-MAGIC "SSND"
STRUCT: aiff-chunk-header
{ id char[4] }
{ size char[4] } ;
STRUCT: form-chunk
{ header aiff-chunk-header }
{ form-type char[4] } ;
STRUCT: common-chunk
{ header aiff-chunk-header }
{ num-channels uchar[2] }
{ num-sample-frames uchar[4] }
{ sample-size uchar[2] }
{ sample-rate uchar[10] } ;
STRUCT: sound-data-chunk
{ header aiff-chunk-header }
{ offset uchar[4] }
{ block-size uchar[4] }
{ waveform-data uchar[0] } ;
! cheesy long-double>integer converter that assumes the long double is a positive integer
: sample-rate>integer ( byte[10] -- sample-rate )
[ 2 tail-slice be> ]
[ 2 head-slice be> 16383 - 63 - ] bi shift ;
: read-form-chunk ( -- byte-array/f )
form-chunk heap-size ensured-read* ;
: verify-aiff ( chunk -- )
{
[ FORM-MAGIC id= ]
[ form-chunk memory>struct form-type>> 4 memory>byte-array AIFF-MAGIC id= ]
} 1&&
[ invalid-audio-file ] unless ;
:: read-aiff-chunks ( -- comm ssnd )
f :> comm! f :> ssnd!
[ { [ comm ssnd and not ] [ read-chunk ] } 0&& dup ]
[ {
{
[ dup COMM-MAGIC common-chunk check-chunk ]
[ common-chunk memory>struct comm! ]
}
{
[ dup SSND-MAGIC sound-data-chunk check-chunk ]
[ sound-data-chunk memory>struct ssnd! ]
}
[ drop ]
} cond ] while drop
comm ssnd 2dup and [ invalid-audio-file ] unless ;
: (read-aiff) ( -- audio )
read-aiff-chunks
[
[ num-channels>> 2 memory>byte-array be> ]
[ sample-size>> 2 memory>byte-array be> ]
[ sample-rate>> sample-rate>integer ] tri
] [
[ header>> size>> 4 memory>byte-array be> 8 - dup ]
[ waveform-data>> >c-ptr ] bi swap memory>byte-array
] bi*
<audio> convert-data-endian ;
: read-aiff ( filename -- audio )
big-endian [
binary [
read-form-chunk verify-aiff (read-aiff)
] with-file-reader
] with-endianness ;

View File

@ -0,0 +1,27 @@
USING: accessors alien.c-types combinators endian io kernel
math sequences ;
IN: audio.chunked-file
ERROR: invalid-audio-file ;
: ensured-read ( count -- output/f )
[ read ] keep over length = [ drop f ] unless ;
: ensured-read* ( count -- output )
ensured-read [ invalid-audio-file ] unless* ;
: read-chunk ( -- byte-array/f )
4 ensured-read [ 4 ensured-read* dup endian> ensured-read* 3append ] [ f ] if* ;
: id= ( chunk id -- ? )
[ 4 head ] dip sequence= ; inline
: convert-data-endian ( audio -- audio )
dup sample-bits>> {
{ 16 [ [ 2 seq>native-endianness ] change-data ] }
{ 32 [ [ 4 seq>native-endianness ] change-data ] }
[ drop ]
} case ;
: check-chunk ( chunk id class -- ? )
heap-size [ id= ] [ [ length ] dip >= ] bi-curry* bi and ;

View File

@ -1,7 +1,8 @@
USING: alien.c-types alien.syntax audio combinators endian
combinators.short-circuit io io.binary io.encodings.binary
io.files io.streams.byte-array kernel locals math
sequences alien alien.data classes.struct accessors ;
sequences alien alien.data classes.struct accessors
audio.chunked-file ;
IN: audio.wav
CONSTANT: RIFF-MAGIC "RIFF"
@ -30,24 +31,9 @@ STRUCT: wav-data-chunk
{ header riff-chunk-header }
{ body uchar[0] } ;
ERROR: invalid-audio-file ;
: ensured-read ( count -- output/f )
[ read ] keep over length = [ drop f ] unless ;
: ensured-read* ( count -- output )
ensured-read [ invalid-audio-file ] unless* ;
: read-chunk ( -- byte-array/f )
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 -- ? )
[ 4 head ] dip sequence= ; inline
: check-chunk ( chunk id class -- ? )
heap-size [ id= ] [ [ length ] dip >= ] bi-curry* bi and ;
:: read-wav-chunks ( -- fmt data )
f :> fmt! f :> data!
[ { [ fmt data and not ] [ read-chunk ] } 0&& dup ]
@ -64,15 +50,6 @@ ERROR: invalid-audio-file ;
} 1&&
[ invalid-audio-file ] unless ;
: convert-data-endian ( audio -- audio )
little-endian [
dup sample-bits>> {
{ 16 [ [ 2 seq>native-endianness ] change-data ] }
{ 32 [ [ 4 seq>native-endianness ] change-data ] }
[ drop ]
} case
] with-endianness ;
: (read-wav) ( -- audio )
read-wav-chunks
[
@ -83,10 +60,11 @@ ERROR: invalid-audio-file ;
[ header>> size>> 4 memory>byte-array le> dup ]
[ body>> >c-ptr ] bi swap memory>byte-array
] bi*
<audio>
convert-data-endian ;
<audio> convert-data-endian ;
: read-wav ( filename -- audio )
binary [
read-riff-chunk verify-wav (read-wav)
] with-file-reader ;
little-endian [
binary [
read-riff-chunk verify-wav (read-wav)
] with-file-reader
] with-endianness ;