     audio-clip [ size + ] change-next-data-offset drop ; inline
 
 :: queue-clip-buffer ( audio-clip al-buffer -- )
-    audio-clip audio-engine>> :> audio-engine
-    audio-engine buffer-size>> :> buffer-size
-    audio-clip audio>> :> audio
-    audio-clip next-data-offset>> :> next-data-offset
-    audio size>> next-data-offset - :> remaining-audio
-
-    {
-        { [ remaining-audio 0 <= ] [
-            audio-clip loop?>> [
-                audio-clip 0 >>next-data-offset
-                al-buffer queue-clip-buffer
-            ] when
-        ] }
-        { [ remaining-audio buffer-size < ] [
-            audio-clip loop?>> [
-                audio data>>
-                [ next-data-offset swap <displaced-alien> remaining-audio <direct-uchar-array> ]
-                [ buffer-size remaining-audio - <direct-uchar-array> ] bi append :> data
-                audio-clip al-buffer audio data buffer-size (queue-clip-buffer)
-
-                audio-clip [ audio size>> mod ] change-next-data-offset drop
-            ] [
-                next-data-offset audio data>> <displaced-alien> :> data
-                audio-clip al-buffer audio data remaining-audio (queue-clip-buffer)
-            ] if
-        ] }
-        [
-            next-data-offset audio data>> <displaced-alien> :> data
-            audio-clip al-buffer audio data buffer-size (queue-clip-buffer)
-        ]
-    } cond ;
+    audio-clip al-source>> :> al-source
+    audio-clip generator>> :> generator
+    audio-clip buffer-size>> :> buffer-size
+    generator buffer-size generate-audio :> data
+
+    al-buffer audio-clip openal-format data size audio-clip sample-rate>> alBufferData
+    al-source 1 al-buffer c:<uint> alSourceQueueBuffers ;
 
 : update-listener ( audio-engine -- )
     listener>> {


TUPLE: audio-clip < disposable
    { audio-engine audio-engine }
    { audio audio }
    source
    { loop? boolean }
    { al-source integer }
    { al-buffers uint-array }
    { next-data-offset integer } ;

ERROR: audio-device-not-found device-name ;
ERROR: audio-context-not-available device-name ;

:: <audio-engine> ( device-name voice-count buffer-size buffer-count -- engine )
    [
        device-name alcOpenDevice :> al-device
        al-device [ device-name audio-device-not-found ] unless

            voice-count >>voice-count
            al-device >>al-device
            al-context >>al-context
    ] with-destructors ;

: <standard-audio-engine> ( -- engine )
    f 16 <audio-engine> ;

<PRIVATE

