<commit_before>USING: alien.c-types alien.syntax audio combinators
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 ;
IN: audio.wav

CONSTANT: RIFF-MAGIC "RIFF"
CONSTANT: WAVE-MAGIC "WAVE"
CONSTANT: FMT-MAGIC  "fmt "
CONSTANT: DATA-MAGIC "data"

STRUCT: riff-chunk-header
    { id char[4] }
    { size char[4] } ;

STRUCT: riff-chunk
    { header riff-chunk-header }
    { format char[4] } ;

STRUCT: wav-fmt-chunk
    { header riff-chunk-header }
    { audio-format uchar[2] }
    { num-channels uchar[2] }
    { sample-rate uchar[4] }
    { byte-rate uchar[4] }
    { block-align uchar[2] }
    { bits-per-sample uchar[2] } ;

STRUCT: wav-data-chunk
    { header riff-chunk-header }
    { body uchar[0] } ;

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 )
    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 ]
    [ {
        { [ dup FMT-MAGIC  wav-fmt-chunk  check-chunk ] [ wav-fmt-chunk  memory>struct fmt!  ] }
        { [ dup DATA-MAGIC wav-data-chunk check-chunk ] [ wav-data-chunk memory>struct data! ] }
    } cond ] while drop
    fmt data 2dup and [ invalid-wav-file ] unless ;

: verify-wav ( chunk -- )
    {
        [ RIFF-MAGIC id= ]
        [ riff-chunk memory>struct format>> 4 memory>byte-array WAVE-MAGIC id= ]
    } 1&&
    [ invalid-wav-file ] unless ;

: (read-wav) ( -- audio )
    read-wav-chunks
    [
        [ num-channels>>    2 memory>byte-array le> ]
        [ bits-per-sample>> 2 memory>byte-array le> ]
        [ sample-rate>>     4 memory>byte-array le> ] tri
    ] [
        [ header>> size>> 4 memory>byte-array le> dup ]
        [ body>> >c-ptr ] bi swap memory>byte-array
    ] bi* <audio> ;

: read-wav ( filename -- audio )
    binary [
        read-riff-chunk verify-wav (read-wav)
    ] with-file-reader ;<commit_msg>make audio.wav endian-neutral<commit_after>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 ;
IN: audio.wav

CONSTANT: RIFF-MAGIC "RIFF"
CONSTANT: WAVE-MAGIC "WAVE"
CONSTANT: FMT-MAGIC  "fmt "
CONSTANT: DATA-MAGIC "data"

STRUCT: riff-chunk-header
    { id char[4] }
    { size char[4] } ;

STRUCT: riff-chunk
    { header riff-chunk-header }
    { format char[4] } ;

STRUCT: wav-fmt-chunk
    { header riff-chunk-header }
    { audio-format uchar[2] }
    { num-channels uchar[2] }
    { sample-rate uchar[4] }
    { byte-rate uchar[4] }
    { block-align uchar[2] }
    { bits-per-sample uchar[2] } ;

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 ]
    [ {
        { [ dup FMT-MAGIC  wav-fmt-chunk  check-chunk ] [ wav-fmt-chunk  memory>struct fmt!  ] }
        { [ dup DATA-MAGIC wav-data-chunk check-chunk ] [ wav-data-chunk memory>struct data! ] }
    } cond ] while drop
    fmt data 2dup and [ invalid-audio-file ] unless ;

: verify-wav ( chunk -- )
    {
        [ RIFF-MAGIC id= ]
        [ riff-chunk memory>struct format>> 4 memory>byte-array WAVE-MAGIC id= ]
    } 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
    [
        [ num-channels>>    2 memory>byte-array le> ]
        [ bits-per-sample>> 2 memory>byte-array le> ]
        [ sample-rate>>     4 memory>byte-array le> ] tri
    ] [
        [ header>> size>> 4 memory>byte-array le> dup ]
        [ body>> >c-ptr ] bi swap memory>byte-array
    ] bi*
    <audio>
    convert-data-endian ;

: read-wav ( filename -- audio )
    binary [
        read-riff-chunk verify-wav (read-wav)
    ] with-file-reader ;
{'fraction_non_alphanumeric': 0.15138755980861243, 'fraction_numerical': 0.009569377990430622, 'mean_word_length': 2.288860918816866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4792596', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1895, 'n_words': 439}