Skip to content
Newer
Older
100644 52 lines (37 sloc) 1.18 KB
f42a463 @tsion Initial commit. Converted an example from the libao website to Clay. …
authored Jan 6, 2011
1 import ao.*;
2 import libc.(sin);
3
4 main() {
5 /* -- Initialize -- */
6
7 printlnTo(stderr, "clay libao bindings example program");
8
9 ao_initialize();
10
11 /* -- Setup for default driver -- */
12
13 var defaultDriver = ao_default_driver_id();
14
15 var format = Struct_ao_sample_format(16, 44100, 2, AO_FMT_LITTLE, null(CChar));
16
17 /* -- Open driver -- */
18 var device = ao_open_live(defaultDriver, &format, null(Struct_ao_option) /* no options */);
19 if(null?(device)) {
20 printlnTo(stderr, "Error opening device.\n");
21 return 1;
22 }
23
24 /* -- Play some stuff -- */
25 var size = UInt(format.bits/8 * format.channels * format.rate);
26 var buf = allocateRawMemory(Byte, size);
27
28 alias PI = 3.14159265358979323846;
29 alias freq = 440.0;
30
31 for(i in range(format.rate)) {
32 var sample = Int(0.75 * 32768.0 *
33 sin(2 * PI * freq * (Float(i)/format.rate)));
34
35 /* Put the same stuff in left and right channel */
36 var left = Byte(bitwiseAnd(sample, 0xFF));
37 var right = Byte(bitwiseAnd(shiftRight(sample, 8), 0xFF));
38 buf[4*i] = left;
39 buf[4*i+2] = left;
40 buf[4*i+1] = right;
41 buf[4*i+3] = right;
42 }
43 ao_play(device, Pointer[CChar](buf), size);
44
45 /* -- Close and shutdown -- */
46 ao_close(device);
47
48 ao_shutdown();
49
50 return 0;
51 }
Something went wrong with that request. Please try again.