Debian Codegraph @

examples/engine_advanced.c

raw symbols SCIP
1 /*
2 This example demonstrates some of the advanced features of the high level engine API.
3
4 The following features are demonstrated:
5
6 * Initialization of the engine from a pre-initialized device.
7 * Self-managed resource managers.
8 * Multiple engines with a shared resource manager.
9 * Creation and management of `ma_sound` objects.
10
11 This example will play the sound that's passed in on the command line.
12
13 Using a shared resource manager, as we do in this example, is useful for when you want to user
14 multiple engines so that you can output to multiple playback devices simultaneoulys. An example
15 might be a local co-op multiplayer game where each player has their own headphones.
16 */
17 #include "../miniaudio.c"
18
19 #define MAX_DEVICES 2
20 #define MAX_SOUNDS 32
21
22 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
23 {
24 (void)pInput;
25
26 /*
27 Since we're managing the underlying device ourselves, we need to read from the engine directly.
28 To do this we need access to the `ma_engine` object which we passed in to the user data. One
29 advantage of this is that you could do your own audio processing in addition to the engine's
30 standard processing.
31 */
32 ma_engine_read_pcm_frames((ma_engine*)pDevice->pUserData, pOutput, frameCount, NULL);
33 }
34
35 int main(int argc, char** argv)
36 {
37 ma_result result;
38 ma_context context;
39 ma_resource_manager_config resourceManagerConfig;
40 ma_resource_manager resourceManager;
41 ma_engine engines[MAX_DEVICES];
42 ma_device devices[MAX_DEVICES];
43 ma_uint32 engineCount = 0;
44 ma_uint32 iEngine;
45 ma_device_info* pPlaybackDeviceInfos;
46 ma_uint32 playbackDeviceCount;
47 ma_uint32 iAvailableDevice;
48 ma_uint32 iChosenDevice;
49 ma_sound sounds[MAX_SOUNDS];
50 ma_uint32 soundCount;
51 ma_uint32 iSound;
52
53 if (argc < 2) {
54 printf("No input file.");
55 return -1;
56 }
57
58
59 /*
60 We are going to be initializing multiple engines. In order to save on memory usage we can use a self managed
61 resource manager so we can share a single resource manager across multiple engines.
62 */
63 resourceManagerConfig = ma_resource_manager_config_init();
64 resourceManagerConfig.decodedFormat = ma_format_f32; /* ma_format_f32 should almost always be used as that's what the engine (and most everything else) uses for mixing. */
65 resourceManagerConfig.decodedChannels = 0; /* Setting the channel count to 0 will cause sounds to use their native channel count. */
66 resourceManagerConfig.decodedSampleRate = 48000; /* Using a consistent sample rate is useful for avoiding expensive resampling in the audio thread. This will result in resampling being performed by the loading thread(s). */
67
68 result = ma_resource_manager_init(&resourceManagerConfig, &resourceManager);
69 if (result != MA_SUCCESS) {
70 printf("Failed to initialize resource manager.");
71 return -1;
72 }
73
74
75 /* We're going to want a context so we can enumerate our playback devices. */
76 result = ma_context_init(NULL, 0, NULL, &context);
77 if (result != MA_SUCCESS) {
78 printf("Failed to initialize context.");
79 return -1;
80 }
81
82 /*
83 Now that we have a context we will want to enumerate over each device so we can display them to the user and give
84 them a chance to select the output devices they want to use.
85 */
86 result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, NULL, NULL);
87 if (result != MA_SUCCESS) {
88 printf("Failed to enumerate playback devices.");
89 ma_context_uninit(&context);
90 return -1;
91 }
92
93
94 /* We have our devices, so now we want to get the user to select the devices they want to output to. */
95 engineCount = 0;
96
97 for (iChosenDevice = 0; iChosenDevice < MAX_DEVICES; iChosenDevice += 1) {
98 int c = 0;
99 for (;;) {
100 printf("Select playback device %d ([%d - %d], Q to quit):\n", iChosenDevice+1, 0, ma_min((int)playbackDeviceCount, 9));
101
102 for (iAvailableDevice = 0; iAvailableDevice < playbackDeviceCount; iAvailableDevice += 1) {
103 printf(" %d: %s\n", iAvailableDevice, pPlaybackDeviceInfos[iAvailableDevice].name);
104 }
105
106 for (;;) {
107 c = getchar();
108 if (c != '\n') {
109 break;
110 }
111 }
112
113 if (c == 'q' || c == 'Q') {
114 return 0; /* User aborted. */
115 }
116
117 if (c >= '0' && c <= '9') {
118 c -= '0';
119
120 if (c < (int)playbackDeviceCount) {
121 ma_device_config deviceConfig;
122 ma_engine_config engineConfig;
123
124 /*
125 Create the device first before the engine. We'll specify the device in the engine's config. This is optional. When a device is
126 not pre-initialized the engine will create one for you internally. The device does not need to be started here - the engine will
127 do that for us in `ma_engine_start()`. The device's format is derived from the resource manager, but can be whatever you want.
128 It's useful to keep the format consistent with the resource manager to avoid data conversions costs in the audio callback. In
129 this example we're using the resource manager's sample format and sample rate, but leaving the channel count set to the device's
130 native channels. You can use whatever format/channels/rate you like.
131 */
132 deviceConfig = ma_device_config_init(ma_device_type_playback);
133 deviceConfig.playback.pDeviceID = &pPlaybackDeviceInfos[c].id;
134 deviceConfig.playback.format = resourceManager.config.decodedFormat;
135 deviceConfig.playback.channels = 0;
136 deviceConfig.sampleRate = resourceManager.config.decodedSampleRate;
137 deviceConfig.dataCallback = data_callback;
138 deviceConfig.pUserData = &engines[engineCount];
139
140 result = ma_device_init(&context, &deviceConfig, &devices[engineCount]);
141 if (result != MA_SUCCESS) {
142 printf("Failed to initialize device for %s.\n", pPlaybackDeviceInfos[c].name);
143 return -1;
144 }
145
146 /* Now that we have the device we can initialize the engine. The device is passed into the engine's config. */
147 engineConfig = ma_engine_config_init();
148 engineConfig.pDevice = &devices[engineCount];
149 engineConfig.pResourceManager = &resourceManager;
150 engineConfig.noAutoStart = MA_TRUE; /* Don't start the engine by default - we'll do that manually below. */
151
152 result = ma_engine_init(&engineConfig, &engines[engineCount]);
153 if (result != MA_SUCCESS) {
154 printf("Failed to initialize engine for %s.\n", pPlaybackDeviceInfos[c].name);
155 ma_device_uninit(&devices[engineCount]);
156 return -1;
157 }
158
159 engineCount += 1;
160 break;
161 } else {
162 printf("Invalid device number.\n");
163 }
164 } else {
165 printf("Invalid device number.\n");
166 }
167 }
168
169 printf("Device %d: %s\n", iChosenDevice+1, pPlaybackDeviceInfos[c].name);
170 }
171
172
173 /* We should now have our engine's initialized. We can now start them. */
174 for (iEngine = 0; iEngine < engineCount; iEngine += 1) {
175 result = ma_engine_start(&engines[iEngine]);
176 if (result != MA_SUCCESS) {
177 printf("WARNING: Failed to start engine %d.\n", iEngine);
178 }
179 }
180
181
182 /*
183 At this point our engine's are running and outputting nothing but silence. To get them playing something we'll need
184 some sounds. In this example we're just using one sound per engine, but you can create as many as you like. Since
185 we're using a shared resource manager, the sound data will only be loaded once. This is how you would implement
186 multiple listeners.
187 */
188 soundCount = 0;
189 for (iEngine = 0; iEngine < engineCount; iEngine += 1) {
190 /* Just one sound per engine in this example. We're going to be loading this asynchronously. */
191 result = ma_sound_init_from_file(&engines[iEngine], argv[1], MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM, NULL, NULL, &sounds[iEngine]);
192 if (result != MA_SUCCESS) {
193 printf("WARNING: Failed to load sound \"%s\"", argv[1]);
194 break;
195 }
196
197 /*
198 The sound can be started as soon as ma_sound_init_from_file() returns, even for sounds that are initialized
199 with MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC. The sound will start playing while it's being loaded. Note that if the
200 asynchronous loading process cannot keep up with the rate at which you try reading you'll end up glitching.
201 If this is an issue, you need to not load sounds asynchronously.
202 */
203 result = ma_sound_start(&sounds[iEngine]);
204 if (result != MA_SUCCESS) {
205 printf("WARNING: Failed to start sound.");
206 }
207
208 soundCount += 1;
209 }
210
211
212 printf("Press Enter to quit...");
213 getchar();
214
215 for (;;) {
216 int c = getchar();
217 if (c == '\n') {
218 break;
219 }
220 }
221
222 /* Teardown. */
223
224 /* The application owns the `ma_sound` object which means you're responsible for uninitializing them. */
225 for (iSound = 0; iSound < soundCount; iSound += 1) {
226 ma_sound_uninit(&sounds[iSound]);
227 }
228
229 /* We can now uninitialize each engine. */
230 for (iEngine = 0; iEngine < engineCount; iEngine += 1) {
231 ma_engine_uninit(&engines[iEngine]);
232
233 /*
234 The engine has been uninitialized so now lets uninitialize the device. Do this first to ensure we don't
235 uninitialize the resource manager from under the device while the data callback is running.
236 */
237 ma_device_uninit(&devices[iEngine]);
238 }
239
240 /* The context can only be uninitialized after the devices. */
241 ma_context_uninit(&context);
242
243 /*
244 Do the resource manager last. This way we can guarantee the data callbacks of each device aren't trying to access
245 and data managed by the resource manager.
246 */
247 ma_resource_manager_uninit(&resourceManager);
248
249 return 0;
250 }