1 //------------------------------------------------------------------------------
2 //  saudio.d
3 //  Test sokol-audio D bindings
4 //------------------------------------------------------------------------------
5 module examples.saudio;
6 
7 private:
8 
9 import sg = sokol.gfx;
10 import app = sokol.app;
11 import log = sokol.log;
12 import sglue = sokol.glue;
13 import saudio = sokol.audio;
14 
15 extern (C):
16 @safe:
17 
18 enum NUM_SAMPLES = 32;
19 
20 struct State
21 {
22     sg.PassAction pass_action = {
23         colors: [
24             {
25                 load_action: sg.LoadAction.Clear, clear_value: {
26                     r: 1.0, g: 0.5, b: 0.0, a: 1.0
27                 }
28             },
29         ]
30     };
31     int even_odd;
32     size_t sample_pos;
33     float[NUM_SAMPLES] samples;
34 }
35 
36 static State state;
37 
38 void init()
39 {
40     sg.Desc gfx = {
41         environment: sglue.environment,
42         logger: {func: &log.slog_func}
43     };
44     sg.setup(gfx);
45     saudio.Desc audio = {logger: {func: &log.slog_func}};
46     saudio.setup(audio);
47 }
48 
49 void frame()
50 {
51     immutable num_frames = saudio.expect();
52     foreach (_; 0 .. num_frames)
53     {
54         state.even_odd += 1;
55         state.sample_pos += 1;
56         if (state.sample_pos == NUM_SAMPLES)
57         {
58             state.sample_pos = 0;
59             saudio.push(&state.samples[0], NUM_SAMPLES);
60         }
61         state.samples[state.sample_pos] = (0 != (state.even_odd & 0x20)) ? 0.1 : -0.1;
62     }
63     sg.Pass pass = {action: state.pass_action, swapchain: sglue.swapchain()};
64     sg.beginPass(pass);
65     sg.endPass();
66     sg.commit();
67 }
68 
69 void cleanup()
70 {
71     saudio.shutdown();
72     sg.shutdown();
73 }
74 
75 void main()
76 {
77     app.Desc runner = {
78         window_title: "saudio.d",
79         init_cb: &init,
80         frame_cb: &frame,
81         cleanup_cb: &cleanup,
82         width: 640,
83         height: 480,
84         icon: {sokol_default: true},
85         logger: {func: &log.func}
86     };
87     app.run(runner);
88 }