 M: vorbis-stream generator-audio-format
     [ info>> channels>> ] [ drop 16 ] [ info>> rate>> ] tri ;
 M: vorbis-stream generate-audio
-    dup decode-audio
-    [ [ drop ] 2dip ]
-    [ drop [ buffer-data-from-stream drop ] [ sync-pages ] [ decode-audio ] tri ] if* ;
+    [ buffer>> ] [ 0 decode-audio ] bi ;

        ] [ drop ] if t
    ] [ drop f ] if ;

: decode-audio ( vorbis-stream -- short-array/f length/f )
    dup get-pending-decoded-audio dup 0 > [
        [ make-pcm-buffer dup byte-length ] [ read-samples ] 3bi
    ] [
        2drop dup queue-audio [ decode-audio ] [ drop f f ] if
    ] if ;
PRIVATE>

: <vorbis-stream> ( stream -- vorbis-stream )
    [
        vorbis-stream new-disposable
            swap >>stream
            ogg-packet malloc-struct |free >>packet
            ogg-sync-state malloc-struct |free >>sync-state
            ogg-page malloc-struct |free >>page

    { block vorbis-block }
    { comment vorbis-comment }
    { temp-state ogg-stream-state }
    { #vorbis-headers integer initial: 0 } ;

CONSTANT: stream-buffer-size 4096

