Debian Codegraph @

extras/nodes/ma_reverb_node/ma_reverb_node_example.c

raw symbols SCIP
1 #include "../../../miniaudio.c"
2 #include "ma_reverb_node.c"
3
4 #include <stdio.h>
5
6 #define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
7 #define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
8 #define DEVICE_SAMPLE_RATE 48000 /* Cannot be less than 22050 for this example. */
9
10 static ma_audio_buffer_ref g_dataSupply; /* The underlying data source of the source node. */
11 static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
12 static ma_reverb_node g_reverbNode; /* The reverb node. */
13 static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
14
15 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
16 {
17 /*
18 This example assumes the playback and capture sides use the same format and channel count. The
19 format must be f32.
20 */
21 if (pDevice->capture.format != DEVICE_FORMAT || pDevice->playback.format != DEVICE_FORMAT || pDevice->capture.channels != pDevice->playback.channels) {
22 return;
23 }
24
25 /*
26 The node graph system is a pulling style of API. At the lowest level of the chain will be a
27 node acting as a data source for the purpose of delivering the initial audio data. In our case,
28 the data source is our `pInput` buffer. We need to update the underlying data source so that it
29 read data from `pInput`.
30 */
31 ma_audio_buffer_ref_set_data(&g_dataSupply, pInput, frameCount);
32
33 /* With the source buffer configured we can now read directly from the node graph. */
34 ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
35 }
36
37 int main(int argc, char** argv)
38 {
39 ma_result result;
40 ma_device_config deviceConfig;
41 ma_device device;
42 ma_node_graph_config nodeGraphConfig;
43 ma_reverb_node_config reverbNodeConfig;
44 ma_data_source_node_config dataSupplyNodeConfig;
45
46 deviceConfig = ma_device_config_init(ma_device_type_duplex);
47 deviceConfig.capture.pDeviceID = NULL;
48 deviceConfig.capture.format = DEVICE_FORMAT;
49 deviceConfig.capture.channels = DEVICE_CHANNELS;
50 deviceConfig.capture.shareMode = ma_share_mode_shared;
51 deviceConfig.playback.pDeviceID = NULL;
52 deviceConfig.playback.format = DEVICE_FORMAT;
53 deviceConfig.playback.channels = DEVICE_CHANNELS;
54 deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
55 deviceConfig.dataCallback = data_callback;
56 result = ma_device_init(NULL, &deviceConfig, &device);
57 if (result != MA_SUCCESS) {
58 return result;
59 }
60
61
62 /* Node graph. */
63 nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
64
65 result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
66 if (result != MA_SUCCESS) {
67 printf("Failed to initialize node graph.");
68 goto done0;
69 }
70
71
72 /* Reverb. Attached straight to the endpoint. */
73 reverbNodeConfig = ma_reverb_node_config_init(device.capture.channels, device.sampleRate);
74
75 result = ma_reverb_node_init(&g_nodeGraph, &reverbNodeConfig, NULL, &g_reverbNode);
76 if (result != MA_SUCCESS) {
77 printf("Failed to initialize reverb node.");
78 goto done1;
79 }
80
81 ma_node_attach_output_bus(&g_reverbNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
82
83
84 /* Data supply. Attached to input bus 0 of the reverb node. */
85 result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_dataSupply);
86 if (result != MA_SUCCESS) {
87 printf("Failed to initialize audio buffer for source.");
88 goto done2;
89 }
90
91 dataSupplyNodeConfig = ma_data_source_node_config_init(&g_dataSupply);
92
93 result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
94 if (result != MA_SUCCESS) {
95 printf("Failed to initialize source node.");
96 goto done2;
97 }
98
99 ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_reverbNode, 0);
100
101
102
103 /* Now we just start the device and wait for the user to terminate the program. */
104 ma_device_start(&device);
105
106 printf("Press Enter to quit...\n");
107 getchar();
108
109 /* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
110 ma_device_stop(&device);
111
112
113 /*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
114 done2: ma_reverb_node_uninit(&g_reverbNode, NULL);
115 done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
116 done0: ma_device_uninit(&device);
117
118 (void)argc;
119 (void)argv;
120
121 return 0;
122 }