1 ! Copyright (C) 2007 Adam Wendt.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
4 USING: alien.c-types io kernel libc mad mad.api math namespaces openal prettyprint sequences tools.interpreter vars ;
\r
7 VARS: openal-buffer ;
\r
9 : get-format ( pcm -- format )
\r
10 mad_pcm-channels 2 =
\r
11 [ AL_FORMAT_STEREO16 ] [ AL_FORMAT_MONO16 ] if ;
\r
13 : no-error? ( -- ? )
\r
14 alGetError dup . flush AL_NO_ERROR = ;
\r
16 : round ( sample -- rounded )
\r
17 1 MAD_F_FRACBITS 16 - shift + ;
\r
19 : clip ( sample -- clipped ) MAD_F_ONE 1- min MAD_F_ONE neg max ;
\r
21 : quantize ( sample -- quantized )
\r
22 MAD_F_FRACBITS 1+ 16 - neg shift ;
\r
24 : scale-sample ( sample -- scaled )
\r
25 round clip quantize ;
\r
27 : get-needed-size ( pcm -- size )
\r
28 [ mad_pcm-channels ] keep mad_pcm-length 2 * * ;
\r
30 : make-data ( pcm -- )
\r
31 [ mad_pcm-channels ] keep ! channels pcm
\r
32 [ mad_pcm-length ] keep swap ! channels pcm length
\r
33 [ ! channels pcm counter
\r
34 [ mad_pcm-sample-right ] 2keep ! channels right pcm counter
\r
35 [ mad_pcm-sample-left ] 2keep ! channels right left pcm counter
\r
36 drop -rot scale-sample , pick ! channels pcm right channels
\r
37 2 = [ scale-sample , ] [ drop ] if ! channels pcm right
\r
40 : array>alien ( alien array -- ) dup length [ pick set-int-nth ] 2each drop ;
\r
42 : fill-data ( pcm alien -- )
\r
43 swap [ make-data ] { } make array>alien ;
\r
45 : get-data ( pcm -- size alien )
\r
46 [ get-needed-size ] keep over
\r
47 malloc [ fill-data ] keep ;
\r
49 : output-openal ( pcm -- ? )
\r
50 openal-buffer> swap ! buffer pcm
\r
51 [ get-format ] keep ! buffer format pcm
\r
52 [ get-data ] keep ! buffer format size alien pcm
\r
53 mad_pcm-samplerate ! buffer format size alien samplerate
\r
54 swapd alBufferData no-error?
\r
57 : play-mp3 ( filename -- )
\r
58 gen-buffer >openal-buffer [ output-openal ] >output-callback-var decode-mp3 ;
\r