Debian Codegraph @

examples/simple_spatialization.c

raw symbols SCIP
1 /*
2 Demonstrates how to do basic spatialization via the high level API.
3
4 You can position and orientate sounds to create a simple spatialization effect. This example shows
5 how to do this.
6
7 In addition to positioning sounds, there is the concept of a listener. This can also be positioned
8 and orientated to help with spatialization.
9
10 This example only covers the basics to get your started. See the documentation for more detailed
11 information on the available features.
12
13 To use this example, pass in the path of a sound as the first argument. The sound will be
14 positioned in front of the listener, while the listener rotates on the the spot to create an
15 orbiting effect. Terminate the program with Ctrl+C.
16 */
17 #include "../miniaudio.c"
18
19 #include <stdio.h>
20 #include <math.h> /* For sinf() and cosf() */
21
22 /* Silence warning about unreachable code for MSVC. */
23 #ifdef _MSC_VER
24 #pragma warning(push)
25 #pragma warning(disable: 4702)
26 #endif
27
28 int main(int argc, char** argv)
29 {
30 ma_result result;
31 ma_engine engine;
32 ma_sound sound;
33 float listenerAngle = 0;
34
35 if (argc < 2) {
36 printf("No input file.\n");
37 return -1;
38 }
39
40 result = ma_engine_init(NULL, &engine);
41 if (result != MA_SUCCESS) {
42 printf("Failed to initialize engine.\n");
43 return -1;
44 }
45
46 result = ma_sound_init_from_file(&engine, argv[1], 0, NULL, NULL, &sound);
47 if (result != MA_SUCCESS) {
48 printf("Failed to load sound: %s\n", argv[1]);
49 ma_engine_uninit(&engine);
50 return -1;
51 }
52
53 /* This sets the position of the sound. miniaudio follows the same coordinate system as OpenGL, where -Z is forward. */
54 ma_sound_set_position(&sound, 0, 0, -1);
55
56 /*
57 This sets the position of the listener. The second parameter is the listener index. If you have only a single listener, which is
58 most likely, just use 0. The position defaults to (0,0,0).
59 */
60 ma_engine_listener_set_position(&engine, 0, 0, 0, 0);
61
62
63 /* Sounds are stopped by default. We'll start it once initial parameters have been setup. */
64 ma_sound_start(&sound);
65
66
67 /* Rotate the listener on the spot to create an orbiting effect. */
68 for (;;) {
69 listenerAngle += 0.01f;
70 ma_engine_listener_set_direction(&engine, 0, (float)sin(listenerAngle), 0, (float)cos(listenerAngle));
71
72 ma_sleep(1);
73 }
74
75
76 /* Won't actually get here, but do this to tear down. */
77 ma_sound_uninit(&sound);
78 ma_engine_uninit(&engine);
79
80 return 0;
81 }
82
83 #ifdef _MSC_VER
84 #pragma warning(pop)
85 #endif