Debian Codegraph @

examples/node_graph.c

raw symbols SCIP
1 /*
2 This example shows how to use the node graph system.
3
4 The node graph system can be used for doing complex mixing and effect processing. The idea is that
5 you have a number of nodes that are connected to each other to form a graph. At the end of the
6 graph is an endpoint which all nodes eventually connect to.
7
8 A node is used to do some kind of processing on zero or more input streams and produce one or more
9 output streams. Each node can have a number of inputs and outputs. Each of these is called a bus in
10 miniaudio. Some nodes, particularly data source nodes, have no inputs and instead generate their
11 outputs dynamically. All nodes will have at least one output or else it'll be disconnected from the
12 graph and will never get processed. Each output bus of a node will be connected to an input bus of
13 another node, but they don't all need to connect to the same input node. For example, a splitter
14 node has 1 input bus and 2 output buses and is used to duplicate a signal. You could then branch
15 off and have one output bus connected to one input node and the other connected to a different
16 input node, and then have two different effects process for each of the duplicated branches.
17
18 Any number of output buses can be connected to an input bus in which case the output buses will be
19 mixed before processing by the input node. This is how you would achieve the mixing part of the
20 node graph.
21
22 This example will be using the following node graph set up:
23
24 ```
25 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Data flows left to right >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
26
27 +---------------+ +-----------------+
28 | Data Source 1 =----+ +----------+ +----= Low Pass Filter =----+
29 +---------------+ | | =----+ +-----------------+ | +----------+
30 +----= Splitter | +----= ENDPOINT |
31 +---------------+ | | =----+ +-----------------+ | +----------+
32 | Data Source 2 =----+ +----------+ +----= Echo / Delay =----+
33 +---------------+ +-----------------+
34 ```
35
36 This does not represent a realistic real-world scenario, but it demonstrates how to make use of
37 mixing, multiple outputs and multiple effects.
38
39 The data source nodes are connected to the input of the splitter. They'll be mixed before being
40 processed by the splitter. The splitter has two output buses. In the graph above, one bus will be
41 routed to a low pass filter, whereas the other bus will be routed to an echo effect. Then, the
42 outputs of these two effects will be connected to the input bus of the endpoint. Because both of
43 the outputs are connected to the same input bus, they'll be mixed at that point.
44
45 The two data sources at the start of the graph have no inputs. They'll instead generate their
46 output by reading from a data source. The data source in this case will be one `ma_decoder` for
47 each input file specified on the command line.
48
49 You can also control the volume of an output bus. In this example, we set the volumes of the low
50 pass and echo effects so that one of them becomes more obvious than the other.
51
52 When you want to read from the graph, you simply call `ma_node_graph_read_pcm_frames()`.
53 */
54 #include "../miniaudio.c"
55
56 /* Data Format */
57 #define FORMAT ma_format_f32 /* Must always be f32. */
58 #define CHANNELS 2
59 #define SAMPLE_RATE 48000
60
61 /* Effect Properties */
62 #define LPF_BIAS 0.9f /* Higher values means more bias towards the low pass filter (the low pass filter will be more audible). Lower values means more bias towards the echo. Must be between 0 and 1. */
63 #define LPF_CUTOFF_FACTOR 80 /* High values = more filter. */
64 #define LPF_ORDER 8
65 #define DELAY_IN_SECONDS 0.2f
66 #define DECAY 0.5f /* Volume falloff for each echo. */
67
68 typedef struct
69 {
70 ma_data_source_node node; /* If you make this the first member, you can pass a pointer to this struct into any `ma_node_*` API and it will "Just Work". */
71 ma_decoder decoder;
72 } sound_node;
73
74 static ma_node_graph g_nodeGraph;
75 static ma_lpf_node g_lpfNode;
76 static ma_delay_node g_delayNode;
77 static ma_splitter_node g_splitterNode;
78 static sound_node* g_pSoundNodes;
79 static int g_soundNodeCount;
80
81 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
82 {
83 /*
84 Hearing the output of the node graph is as easy as reading straight into the output buffer. You just need to
85 make sure you use a consistent data format or else you'll need to do your own conversion.
86 */
87 ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
88
89 (void)pInput; /* Unused. */
90 (void)pDevice; /* Unused. */
91 }
92
93 int main(int argc, char** argv)
94 {
95 int iarg;
96 ma_result result;
97
98 /* We'll set up our nodes starting from the end and working our way back to the start. We'll need to set up the graph first. */
99 {
100 ma_node_graph_config nodeGraphConfig = ma_node_graph_config_init(CHANNELS);
101
102 result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
103 if (result != MA_SUCCESS) {
104 printf("ERROR: Failed to initialize node graph.");
105 return -1;
106 }
107 }
108
109
110 /* Low Pass Filter. */
111 {
112 ma_lpf_node_config lpfNodeConfig = ma_lpf_node_config_init(CHANNELS, SAMPLE_RATE, SAMPLE_RATE / LPF_CUTOFF_FACTOR, LPF_ORDER);
113
114 result = ma_lpf_node_init(&g_nodeGraph, &lpfNodeConfig, NULL, &g_lpfNode);
115 if (result != MA_SUCCESS) {
116 printf("ERROR: Failed to initialize low pass filter node.");
117 return -1;
118 }
119
120 /* Connect the output bus of the low pass filter node to the input bus of the endpoint. */
121 ma_node_attach_output_bus(&g_lpfNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
122
123 /* Set the volume of the low pass filter to make it more of less impactful. */
124 ma_node_set_output_bus_volume(&g_lpfNode, 0, LPF_BIAS);
125 }
126
127
128 /* Echo / Delay. */
129 {
130 ma_delay_node_config delayNodeConfig = ma_delay_node_config_init(CHANNELS, SAMPLE_RATE, (ma_uint32)(SAMPLE_RATE * DELAY_IN_SECONDS), DECAY);
131
132 result = ma_delay_node_init(&g_nodeGraph, &delayNodeConfig, NULL, &g_delayNode);
133 if (result != MA_SUCCESS) {
134 printf("ERROR: Failed to initialize delay node.");
135 return -1;
136 }
137
138 /* Connect the output bus of the delay node to the input bus of the endpoint. */
139 ma_node_attach_output_bus(&g_delayNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
140
141 /* Set the volume of the delay filter to make it more of less impactful. */
142 ma_node_set_output_bus_volume(&g_delayNode, 0, 1 - LPF_BIAS);
143 }
144
145
146 /* Splitter. */
147 {
148 ma_splitter_node_config splitterNodeConfig = ma_splitter_node_config_init(CHANNELS);
149
150 result = ma_splitter_node_init(&g_nodeGraph, &splitterNodeConfig, NULL, &g_splitterNode);
151 if (result != MA_SUCCESS) {
152 printf("ERROR: Failed to initialize splitter node.");
153 return -1;
154 }
155
156 /* Connect output bus 0 to the input bus of the low pass filter node, and output bus 1 to the input bus of the delay node. */
157 ma_node_attach_output_bus(&g_splitterNode, 0, &g_lpfNode, 0);
158 ma_node_attach_output_bus(&g_splitterNode, 1, &g_delayNode, 0);
159 }
160
161
162 /* Data sources. Ignore any that cannot be loaded. */
163 g_pSoundNodes = (sound_node*)ma_malloc(sizeof(*g_pSoundNodes) * argc-1, NULL);
164 if (g_pSoundNodes == NULL) {
165 printf("Failed to allocate memory for sounds.");
166 return -1;
167 }
168
169 g_soundNodeCount = 0;
170 for (iarg = 1; iarg < argc; iarg += 1) {
171 ma_decoder_config decoderConfig = ma_decoder_config_init(FORMAT, CHANNELS, SAMPLE_RATE);
172
173 result = ma_decoder_init_file(argv[iarg], &decoderConfig, &g_pSoundNodes[g_soundNodeCount].decoder);
174 if (result == MA_SUCCESS) {
175 ma_data_source_node_config dataSourceNodeConfig = ma_data_source_node_config_init(&g_pSoundNodes[g_soundNodeCount].decoder);
176
177 result = ma_data_source_node_init(&g_nodeGraph, &dataSourceNodeConfig, NULL, &g_pSoundNodes[g_soundNodeCount].node);
178 if (result == MA_SUCCESS) {
179 /* The data source node has been created successfully. Attach it to the splitter. */
180 ma_node_attach_output_bus(&g_pSoundNodes[g_soundNodeCount].node, 0, &g_splitterNode, 0);
181 g_soundNodeCount += 1;
182 } else {
183 printf("WARNING: Failed to init data source node for sound \"%s\". Ignoring.", argv[iarg]);
184 ma_decoder_uninit(&g_pSoundNodes[g_soundNodeCount].decoder);
185 }
186 } else {
187 printf("WARNING: Failed to load sound \"%s\". Ignoring.", argv[iarg]);
188 }
189 }
190
191 /* Everything has been initialized successfully so now we can set up a playback device so we can listen to the result. */
192 {
193 ma_device_config deviceConfig;
194 ma_device device;
195
196 deviceConfig = ma_device_config_init(ma_device_type_playback);
197 deviceConfig.playback.format = FORMAT;
198 deviceConfig.playback.channels = CHANNELS;
199 deviceConfig.sampleRate = SAMPLE_RATE;
200 deviceConfig.dataCallback = data_callback;
201 deviceConfig.pUserData = NULL;
202
203 result = ma_device_init(NULL, &deviceConfig, &device);
204 if (result != MA_SUCCESS) {
205 printf("ERROR: Failed to initialize device.");
206 goto cleanup_graph;
207 }
208
209 result = ma_device_start(&device);
210 if (result != MA_SUCCESS) {
211 ma_device_uninit(&device);
212 goto cleanup_graph;
213 }
214
215 printf("Press Enter to quit...\n");
216 getchar();
217
218 /* We're done. Clean up the device. */
219 ma_device_uninit(&device);
220 }
221
222
223 cleanup_graph:
224 {
225 /* It's good practice to tear down the graph from the lowest level nodes first. */
226 int iSound;
227
228 /* Sounds. */
229 for (iSound = 0; iSound < g_soundNodeCount; iSound += 1) {
230 ma_data_source_node_uninit(&g_pSoundNodes[iSound].node, NULL);
231 ma_decoder_uninit(&g_pSoundNodes[iSound].decoder);
232 }
233
234 /* Splitter. */
235 ma_splitter_node_uninit(&g_splitterNode, NULL);
236
237 /* Echo / Delay */
238 ma_delay_node_uninit(&g_delayNode, NULL);
239
240 /* Low Pass Filter */
241 ma_lpf_node_uninit(&g_lpfNode, NULL);
242
243 /* Node Graph */
244 ma_node_graph_uninit(&g_nodeGraph, NULL);
245 }
246
247 return 0;
248 }