Debian Codegraph @

extras/nodes/ma_vocoder_node/ma_vocoder_node_example.c

raw symbols SCIP
1 /*
2 Demonstrates how to apply an effect to a duplex stream using the node graph system.
3
4 This example applies a vocoder effect to the input stream before outputting it. A custom node
5 called `ma_vocoder_node` is used to achieve the effect which can be found in the extras folder in
6 the miniaudio repository. The vocoder node uses https://github.com/blastbay/voclib to achieve the
7 effect.
8 */
9 #include "../../../miniaudio.c"
10 #include "ma_vocoder_node.c"
11
12 #include <stdio.h>
13
14 #define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
15 #define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
16
17 static ma_waveform g_sourceData; /* The underlying data source of the excite node. */
18 static ma_audio_buffer_ref g_exciteData; /* The underlying data source of the source node. */
19 static ma_data_source_node g_sourceNode; /* A data source node containing the source data we'll be sending through to the vocoder. This will be routed into the first bus of the vocoder node. */
20 static ma_data_source_node g_exciteNode; /* A data source node containing the excite data we'll be sending through to the vocoder. This will be routed into the second bus of the vocoder node. */
21 static ma_vocoder_node g_vocoderNode; /* The vocoder node. */
22 static ma_node_graph g_nodeGraph;
23
24 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
25 {
26 /*
27 This example assumes the playback and capture sides use the same format and channel count. The
28 format must be f32.
29 */
30 if (pDevice->capture.format != DEVICE_FORMAT || pDevice->playback.format != DEVICE_FORMAT || pDevice->capture.channels != pDevice->playback.channels) {
31 return;
32 }
33
34 /*
35 The node graph system is a pulling style of API. At the lowest level of the chain will be a
36 node acting as a data source for the purpose of delivering the initial audio data. In our case,
37 the data source is our `pInput` buffer. We need to update the underlying data source so that it
38 read data from `pInput`.
39 */
40 ma_audio_buffer_ref_set_data(&g_exciteData, pInput, frameCount);
41
42 /* With the source buffer configured we can now read directly from the node graph. */
43 ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
44 }
45
46 int main(int argc, char** argv)
47 {
48 ma_result result;
49 ma_device_config deviceConfig;
50 ma_device device;
51 ma_node_graph_config nodeGraphConfig;
52 ma_vocoder_node_config vocoderNodeConfig;
53 ma_data_source_node_config sourceNodeConfig;
54 ma_data_source_node_config exciteNodeConfig;
55 ma_waveform_config waveformConfig;
56
57 deviceConfig = ma_device_config_init(ma_device_type_duplex);
58 deviceConfig.capture.pDeviceID = NULL;
59 deviceConfig.capture.format = DEVICE_FORMAT;
60 deviceConfig.capture.channels = DEVICE_CHANNELS;
61 deviceConfig.capture.shareMode = ma_share_mode_shared;
62 deviceConfig.playback.pDeviceID = NULL;
63 deviceConfig.playback.format = DEVICE_FORMAT;
64 deviceConfig.playback.channels = DEVICE_CHANNELS;
65 deviceConfig.dataCallback = data_callback;
66 result = ma_device_init(NULL, &deviceConfig, &device);
67 if (result != MA_SUCCESS) {
68 return result;
69 }
70
71
72 /* Now we can setup our node graph. */
73 nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
74
75 result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
76 if (result != MA_SUCCESS) {
77 printf("Failed to initialize node graph.");
78 goto done0;
79 }
80
81
82 /* Vocoder. Attached straight to the endpoint. */
83 vocoderNodeConfig = ma_vocoder_node_config_init(device.capture.channels, device.sampleRate);
84
85 result = ma_vocoder_node_init(&g_nodeGraph, &vocoderNodeConfig, NULL, &g_vocoderNode);
86 if (result != MA_SUCCESS) {
87 printf("Failed to initialize vocoder node.");
88 goto done1;
89 }
90
91 ma_node_attach_output_bus(&g_vocoderNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
92
93 /* Amplify the volume of the vocoder output because in my testing it is a bit quiet. */
94 ma_node_set_output_bus_volume(&g_vocoderNode, 0, 4);
95
96
97 /* Source/carrier. Attached to input bus 0 of the vocoder node. */
98 waveformConfig = ma_waveform_config_init(device.capture.format, device.capture.channels, device.sampleRate, ma_waveform_type_sawtooth, 1.0, 50);
99
100 result = ma_waveform_init(&waveformConfig, &g_sourceData);
101 if (result != MA_SUCCESS) {
102 printf("Failed to initialize waveform for excite node.");
103 goto done3;
104 }
105
106 sourceNodeConfig = ma_data_source_node_config_init(&g_sourceData);
107
108 result = ma_data_source_node_init(&g_nodeGraph, &sourceNodeConfig, NULL, &g_sourceNode);
109 if (result != MA_SUCCESS) {
110 printf("Failed to initialize excite node.");
111 goto done3;
112 }
113
114 ma_node_attach_output_bus(&g_sourceNode, 0, &g_vocoderNode, 0);
115
116
117 /* Excite/modulator. Attached to input bus 1 of the vocoder node. */
118 result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_exciteData);
119 if (result != MA_SUCCESS) {
120 printf("Failed to initialize audio buffer for source.");
121 goto done2;
122 }
123
124 exciteNodeConfig = ma_data_source_node_config_init(&g_exciteData);
125
126 result = ma_data_source_node_init(&g_nodeGraph, &exciteNodeConfig, NULL, &g_exciteNode);
127 if (result != MA_SUCCESS) {
128 printf("Failed to initialize source node.");
129 goto done2;
130 }
131
132 ma_node_attach_output_bus(&g_exciteNode, 0, &g_vocoderNode, 1);
133
134
135 ma_device_start(&device);
136
137 printf("Press Enter to quit...\n");
138 getchar();
139
140 /* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
141 ma_device_stop(&device);
142
143 /*done4:*/ ma_data_source_node_uninit(&g_exciteNode, NULL);
144 done3: ma_data_source_node_uninit(&g_sourceNode, NULL);
145 done2: ma_vocoder_node_uninit(&g_vocoderNode, NULL);
146 done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
147 done0: ma_device_uninit(&device);
148
149 (void)argc;
150 (void)argv;
151 return 0;
152 }