Debian Codegraph @

examples/engine_sdl.c

raw symbols SCIP
1 /*
2 Shows how to use the high level engine API with SDL.
3
4 By default, miniaudio's engine API will initialize a device internally for audio output. You can
5 instead use the engine independently of a device. To show this off, this example will use SDL for
6 audio output instead of miniaudio.
7
8 This example will load the sound specified on the command line and rotate it around the listener's
9 head.
10 */
11 #define MA_NO_DEVICE_IO /* <-- Disables the `ma_device` API. We don't need that in this example since SDL will be doing that part for us. */
12 #include "../miniaudio.c"
13
14 #define SDL_MAIN_HANDLED
15 #include <SDL2/SDL.h> /* Change this to your include location. Might be <SDL.h>. */
16
17 #define CHANNELS 2 /* Must be stereo for this example. */
18 #define SAMPLE_RATE 48000
19
20 static ma_engine g_engine;
21 static ma_sound g_sound; /* This example will play only a single sound at once, so we only need one `ma_sound` object. */
22
23 void data_callback(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes)
24 {
25 ma_uint32 bufferSizeInFrames;
26
27 (void)pUserData;
28
29 /* Reading is just a matter of reading straight from the engine. */
30 bufferSizeInFrames = (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(ma_format_f32, ma_engine_get_channels(&g_engine));
31 ma_engine_read_pcm_frames(&g_engine, pBuffer, bufferSizeInFrames, NULL);
32 }
33
34 int main(int argc, char** argv)
35 {
36 ma_result result;
37 ma_engine_config engineConfig;
38 SDL_AudioSpec desiredSpec;
39 SDL_AudioSpec obtainedSpec;
40 SDL_AudioDeviceID deviceID;
41
42 if (argc < 2) {
43 printf("No input file.");
44 return -1;
45 }
46
47 /*
48 We'll initialize the engine first for the purpose of the example, but since the engine and SDL
49 are independent of each other you can initialize them in any order. You need only make sure the
50 channel count and sample rates are consistent between the two.
51
52 When initializing the engine it's important to make sure we don't initialize a device
53 internally because we want SDL to be dealing with that for us instead.
54 */
55 engineConfig = ma_engine_config_init();
56 engineConfig.noDevice = MA_TRUE; /* <-- Make sure this is set so that no device is created (we'll deal with that ourselves). */
57 engineConfig.channels = CHANNELS;
58 engineConfig.sampleRate = SAMPLE_RATE;
59
60 result = ma_engine_init(&engineConfig, &g_engine);
61 if (result != MA_SUCCESS) {
62 printf("Failed to initialize audio engine.");
63 return -1;
64 }
65
66 /* Now load our sound. */
67 result = ma_sound_init_from_file(&g_engine, argv[1], 0, NULL, NULL, &g_sound);
68 if (result != MA_SUCCESS) {
69 printf("Failed to initialize sound.");
70 return -1;
71 }
72
73 /* Loop the sound so we can continuously hear it. */
74 ma_sound_set_looping(&g_sound, MA_TRUE);
75
76 /*
77 The sound will not be started by default, so start it now. We won't hear anything until the SDL
78 audio device has been opened and started.
79 */
80 ma_sound_start(&g_sound);
81
82
83 /*
84 Now that we have the engine and sound we can initialize SDL. This could have also been done
85 first before the engine and sound.
86 */
87 if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
88 printf("Failed to initialize SDL sub-system.");
89 return -1;
90 }
91
92 MA_ZERO_OBJECT(&desiredSpec);
93 desiredSpec.freq = ma_engine_get_sample_rate(&g_engine);
94 desiredSpec.format = AUDIO_F32;
95 desiredSpec.channels = ma_engine_get_channels(&g_engine);
96 desiredSpec.samples = 512;
97 desiredSpec.callback = data_callback;
98 desiredSpec.userdata = NULL;
99
100 deviceID = SDL_OpenAudioDevice(NULL, 0, &desiredSpec, &obtainedSpec, SDL_AUDIO_ALLOW_ANY_CHANGE);
101 if (deviceID == 0) {
102 printf("Failed to open SDL audio device.");
103 return -1;
104 }
105
106 /* Start playback. */
107 SDL_PauseAudioDevice(deviceID, 0);
108
109 #if 1
110 {
111 /* We'll move the sound around the listener which we'll leave at the origin. */
112 float stepAngle = 0.002f;
113 float angle = 0;
114 float distance = 2;
115
116 for (;;) {
117 double x = ma_cosd(angle) - ma_sind(angle);
118 double y = ma_sind(angle) + ma_cosd(angle);
119
120 ma_sound_set_position(&g_sound, (float)x * distance, 0, (float)y * distance);
121
122 angle += stepAngle;
123 ma_sleep(1);
124 }
125 }
126 #else
127 printf("Press Enter to quit...");
128 getchar();
129 #endif
130
131 ma_sound_uninit(&g_sound);
132 ma_engine_uninit(&g_engine);
133 SDL_CloseAudioDevice(deviceID);
134 SDL_QuitSubSystem(SDL_INIT_AUDIO);
135
136 return 0;
137 }