Debian Codegraph @

extras/nodes/ma_ltrim_node/ma_ltrim_node_example.c

raw symbols SCIP
1 #include "../../../miniaudio.c"
2 #include "ma_ltrim_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 0 /* The input file will determine the channel count. */
8 #define DEVICE_SAMPLE_RATE 0 /* The input file will determine the sample rate. */
9
10 static ma_decoder g_decoder; /* The decoder that we'll read data from. */
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_ltrim_node g_trimNode; /* The trim 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 (void)pInput;
18 (void)pDevice;
19
20 /* All we need to do is read from the node graph. */
21 ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
22 }
23
24 int main(int argc, char** argv)
25 {
26 ma_result result;
27 ma_decoder_config decoderConfig;
28 ma_device_config deviceConfig;
29 ma_device device;
30 ma_node_graph_config nodeGraphConfig;
31 ma_ltrim_node_config trimNodeConfig;
32 ma_data_source_node_config dataSupplyNodeConfig;
33
34 if (argc < 1) {
35 printf("No input file.\n");
36 return -1;
37 }
38
39
40 /* Decoder. */
41 decoderConfig = ma_decoder_config_init(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE);
42
43 result = ma_decoder_init_file(argv[1], &decoderConfig, &g_decoder);
44 if (result != MA_SUCCESS) {
45 printf("Failed to load decoder.\n");
46 return -1;
47 }
48
49
50 /* Device. */
51 deviceConfig = ma_device_config_init(ma_device_type_playback);
52 deviceConfig.playback.pDeviceID = NULL;
53 deviceConfig.playback.format = g_decoder.outputFormat;
54 deviceConfig.playback.channels = g_decoder.outputChannels;
55 deviceConfig.sampleRate = g_decoder.outputSampleRate;
56 deviceConfig.dataCallback = data_callback;
57 result = ma_device_init(NULL, &deviceConfig, &device);
58 if (result != MA_SUCCESS) {
59 return result;
60 }
61
62
63 /* Node graph. */
64 nodeGraphConfig = ma_node_graph_config_init(device.playback.channels);
65
66 result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
67 if (result != MA_SUCCESS) {
68 printf("Failed to initialize node graph.");
69 goto done0;
70 }
71
72
73 /* Trimmer. Attached straight to the endpoint. Input will be the data source node. */
74 trimNodeConfig = ma_ltrim_node_config_init(device.playback.channels, 0);
75
76 result = ma_ltrim_node_init(&g_nodeGraph, &trimNodeConfig, NULL, &g_trimNode);
77 if (result != MA_SUCCESS) {
78 printf("Failed to initialize ltrim node.");
79 goto done1;
80 }
81
82 ma_node_attach_output_bus(&g_trimNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
83
84
85 /* Data supply. */
86 dataSupplyNodeConfig = ma_data_source_node_config_init(&g_decoder);
87
88 result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
89 if (result != MA_SUCCESS) {
90 printf("Failed to initialize data source node.");
91 goto done2;
92 }
93
94 ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_trimNode, 0);
95
96
97
98 /* Now we just start the device and wait for the user to terminate the program. */
99 ma_device_start(&device);
100
101 printf("Press Enter to quit...\n");
102 getchar();
103
104 /* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
105 ma_device_stop(&device);
106
107
108 /*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
109 done2: ma_ltrim_node_uninit(&g_trimNode, NULL);
110 done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
111 done0: ma_device_uninit(&device);
112
113 return 0;
114 }