1 ! Copyright (C) 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types combinators kernel locals math math.ranges openal sequences sequences.merged ;
6 TUPLE: buffer sample-freq 8bit? id ;
8 : <buffer> ( sample-freq 8bit? -- buffer )
11 TUPLE: mono-buffer < buffer data ;
13 : <mono-buffer> ( sample-freq 8bit? -- buffer )
16 : <8bit-mono-buffer> ( sample-freq -- buffer ) t <mono-buffer> ;
17 : <16bit-mono-buffer> ( sample-freq -- buffer ) f <mono-buffer> ;
19 TUPLE: stereo-buffer < buffer left-data right-data ;
21 : <stereo-buffer> ( sample-freq 8bit? -- buffer )
22 f f f stereo-buffer boa ;
24 : <8bit-stereo-buffer> ( sample-freq -- buffer ) t <stereo-buffer> ;
25 : <16bit-stereo-buffer> ( sample-freq -- buffer ) f <stereo-buffer> ;
27 PREDICATE: 8bit-buffer < buffer 8bit?>> ;
28 PREDICATE: 16bit-buffer < buffer 8bit?>> not ;
29 INTERSECTION: 8bit-mono-buffer 8bit-buffer mono-buffer ;
30 INTERSECTION: 16bit-mono-buffer 16bit-buffer mono-buffer ;
31 INTERSECTION: 8bit-stereo-buffer 8bit-buffer stereo-buffer ;
32 INTERSECTION: 16bit-stereo-buffer 16bit-buffer stereo-buffer ;
34 GENERIC: buffer-format ( buffer -- format )
35 M: 8bit-mono-buffer buffer-format drop AL_FORMAT_MONO8 ;
36 M: 16bit-mono-buffer buffer-format drop AL_FORMAT_MONO16 ;
37 M: 8bit-stereo-buffer buffer-format drop AL_FORMAT_STEREO8 ;
38 M: 16bit-stereo-buffer buffer-format drop AL_FORMAT_STEREO16 ;
40 : 8bit-buffer-data ( seq -- data size )
41 [ 128 * >integer 128 + ] uchar-array{ } map-as [ underlying>> ] [ length ] bi ;
43 : 16bit-buffer-data ( seq -- data size )
44 [ 32768 * >integer ] short-array{ } map-as [ underlying>> ] [ byte-length ] bi ;
46 : stereo-data ( stereo-buffer -- left right )
47 [ left-data>> ] [ right-data>> ] bi@ ;
49 : interleaved-stereo-data ( stereo-buffer -- data )
50 stereo-data <2merged> ;
52 GENERIC: buffer-data ( buffer -- data size )
53 M: 8bit-mono-buffer buffer-data data>> 8bit-buffer-data ;
54 M: 16bit-mono-buffer buffer-data data>> 16bit-buffer-data ;
55 M: 8bit-stereo-buffer buffer-data
56 interleaved-stereo-data 8bit-buffer-data ;
57 M: 16bit-stereo-buffer buffer-data
58 interleaved-stereo-data 16bit-buffer-data ;
60 : telephone-sample-freq 8000 ;
61 : half-sample-freq 22050 ;
62 : cd-sample-freq 44100 ;
63 : digital-sample-freq 48000 ;
64 : professional-sample-freq 88200 ;
66 : send-buffer ( buffer -- buffer )
68 [ gen-buffer dup [ >>id ] dip ]
71 [ sample-freq>> alBufferData ]
74 : ?send-buffer ( buffer -- buffer )
75 dup id>> [ send-buffer ] unless ;