Debian Codegraph @

examples/data_source_chaining.c

raw symbols SCIP
1 /*
2 Demonstrates one way to chain together a number of data sources so they play back seamlessly
3 without gaps.
4
5 This example uses the chaining system built into the `ma_data_source` API. It will take every sound
6 passed onto the command line in order, and then loop back and start again. When looping a chain of
7 data sources, you need only link the last data source back to the first one.
8
9 To play a chain of data sources, you first need to set up your chain. To set the data source that
10 should be played after another, you have two options:
11
12 * Set a pointer to a specific data source
13 * Set a callback that will fire when the next data source needs to be retrieved
14
15 The first option is good for simple scenarios. The second option is useful if you need to perform
16 some action when the end of a sound is reached. This example will be using both.
17
18 When reading data from a chain, you always read from the head data source. Internally miniaudio
19 will track a pointer to the data source in the chain that is currently playing. If you don't
20 consistently read from the head data source this state will become inconsistent and things won't
21 work correctly. When using a chain, this pointer needs to be reset if you need to play the
22 chain again from the start:
23
24 ```c
25 ma_data_source_set_current(&headDataSource, &headDataSource);
26 ma_data_source_seek_to_pcm_frame(&headDataSource, 0);
27 ```
28
29 The code above is setting the "current" data source in the chain to the head data source, thereby
30 starting the chain from the start again. It is also seeking the head data source back to the start
31 so that playback starts from the start as expected. You do not need to seek non-head items back to
32 the start as miniaudio will do that for you internally.
33 */
34 #include "../miniaudio.c"
35
36 #include <stdio.h>
37
38 /*
39 For simplicity, this example requires the device to use floating point samples.
40 */
41 #define SAMPLE_FORMAT ma_format_f32
42 #define CHANNEL_COUNT 2
43 #define SAMPLE_RATE 48000
44
45 ma_uint32 g_decoderCount;
46 ma_decoder* g_pDecoders;
47
48 static ma_data_source* next_callback_tail(ma_data_source* pDataSource)
49 {
50 (void)pDataSource; /* Unused. */
51
52 if (g_decoderCount > 0) { /* <-- We check for this in main() so should never happen. */
53 return NULL;
54 }
55
56 /*
57 This will be fired when the last item in the chain has reached the end. In this example we want
58 to loop back to the start, so we need only return a pointer back to the head.
59 */
60 return &g_pDecoders[0];
61 }
62
63 static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
64 {
65 /*
66 We can just read from the first decoder and miniaudio will resolve the chain for us. Note that
67 if you want to loop the chain, like we're doing in this example, you need to set the `loop`
68 parameter to false, or else only the current data source will be looped.
69 */
70 ma_data_source_read_pcm_frames(&g_pDecoders[0], pOutput, frameCount, NULL);
71
72 /* Unused in this example. */
73 (void)pDevice;
74 (void)pInput;
75 }
76
77 int main(int argc, char** argv)
78 {
79 ma_result result = MA_SUCCESS;
80 ma_uint32 iDecoder;
81 ma_decoder_config decoderConfig;
82 ma_device_config deviceConfig;
83 ma_device device;
84
85 if (argc < 2) {
86 printf("No input files.\n");
87 return -1;
88 }
89
90 g_decoderCount = argc-1;
91 g_pDecoders = (ma_decoder*)malloc(sizeof(*g_pDecoders) * g_decoderCount);
92
93 /* In this example, all decoders need to have the same output format. */
94 decoderConfig = ma_decoder_config_init(SAMPLE_FORMAT, CHANNEL_COUNT, SAMPLE_RATE);
95 for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) {
96 result = ma_decoder_init_file(argv[1+iDecoder], &decoderConfig, &g_pDecoders[iDecoder]);
97 if (result != MA_SUCCESS) {
98 ma_uint32 iDecoder2;
99 for (iDecoder2 = 0; iDecoder2 < iDecoder; ++iDecoder2) {
100 ma_decoder_uninit(&g_pDecoders[iDecoder2]);
101 }
102 free(g_pDecoders);
103
104 printf("Failed to load %s.\n", argv[1+iDecoder]);
105 return -1;
106 }
107 }
108
109 /*
110 We're going to set up our decoders to run one after the other, but then have the last one loop back
111 to the first one. For demonstration purposes we're going to use the callback method for the last
112 data source.
113 */
114 for (iDecoder = 0; iDecoder < g_decoderCount-1; iDecoder += 1) {
115 ma_data_source_set_next(&g_pDecoders[iDecoder], &g_pDecoders[iDecoder+1]);
116 }
117
118 /*
119 For the last data source we'll loop back to the start, but for demonstration purposes we'll use a
120 callback to determine the next data source in the chain.
121 */
122 ma_data_source_set_next_callback(&g_pDecoders[g_decoderCount-1], next_callback_tail);
123
124
125 /*
126 The data source chain has been established so now we can get the device up and running so we
127 can listen to it.
128 */
129 deviceConfig = ma_device_config_init(ma_device_type_playback);
130 deviceConfig.playback.format = SAMPLE_FORMAT;
131 deviceConfig.playback.channels = CHANNEL_COUNT;
132 deviceConfig.sampleRate = SAMPLE_RATE;
133 deviceConfig.dataCallback = data_callback;
134 deviceConfig.pUserData = NULL;
135
136 result = ma_device_init(NULL, &deviceConfig, &device);
137 if (result != MA_SUCCESS) {
138 printf("Failed to open playback device.\n");
139 goto done_decoders;
140 }
141
142 result = ma_device_start(&device);
143 if (result != MA_SUCCESS) {
144 printf("Failed to start playback device.\n");
145 goto done;
146 }
147
148 printf("Press Enter to quit...");
149 getchar();
150
151 done:
152 ma_device_uninit(&device);
153
154 done_decoders:
155 for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) {
156 ma_decoder_uninit(&g_pDecoders[iDecoder]);
157 }
158 free(g_pDecoders);
159
160 return 0;
161 }