Debian Codegraph @

examples/custom_decoder.c

raw symbols SCIP
1 /*
2 Demonstrates how to implement a custom decoder.
3
4 This example implements two custom decoders:
5
6 * Vorbis via libvorbis
7 * Opus via libopus
8
9 A custom decoder must implement a data source. In this example, the libvorbis data source is called
10 `ma_libvorbis` and the Opus data source is called `ma_libopus`. These two objects are compatible
11 with the `ma_data_source` APIs and can be taken straight from this example and used in real code.
12
13 The custom decoding data sources (`ma_libvorbis` and `ma_libopus` in this example) are connected to
14 the decoder via the decoder config (`ma_decoder_config`). You need to implement a vtable for each
15 of your custom decoders. See `ma_decoding_backend_vtable` for the functions you need to implement.
16 The `onInitFile`, `onInitFileW` and `onInitMemory` functions are optional.
17 */
18 #include "../miniaudio.c"
19 #include "../extras/decoders/libvorbis/miniaudio_libvorbis.c"
20 #include "../extras/decoders/libopus/miniaudio_libopus.c"
21
22 #include <stdio.h>
23
24
25 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
26 {
27 ma_data_source* pDataSource = (ma_data_source*)pDevice->pUserData;
28 if (pDataSource == NULL) {
29 return;
30 }
31
32 ma_data_source_read_pcm_frames(pDataSource, pOutput, frameCount, NULL);
33
34 (void)pInput;
35 }
36
37 int main(int argc, char** argv)
38 {
39 ma_result result;
40 ma_decoder_config decoderConfig;
41 ma_decoder decoder;
42 ma_device_config deviceConfig;
43 ma_device device;
44 ma_format format;
45 ma_uint32 channels;
46 ma_uint32 sampleRate;
47
48 /*
49 Add your custom backend vtables here. The order in the array defines the order of priority. The
50 vtables will be passed in via the decoder config.
51 */
52 ma_decoding_backend_vtable* pCustomBackendVTables[] =
53 {
54 ma_decoding_backend_libvorbis,
55 ma_decoding_backend_libopus
56 };
57
58
59 if (argc < 2) {
60 printf("No input file.\n");
61 return -1;
62 }
63
64
65 /* Initialize the decoder. */
66 decoderConfig = ma_decoder_config_init_default();
67 decoderConfig.pCustomBackendUserData = NULL; /* None of our decoders require user data, so this can be set to null. */
68 decoderConfig.ppCustomBackendVTables = pCustomBackendVTables;
69 decoderConfig.customBackendCount = sizeof(pCustomBackendVTables) / sizeof(pCustomBackendVTables[0]);
70
71 result = ma_decoder_init_file(argv[1], &decoderConfig, &decoder);
72 if (result != MA_SUCCESS) {
73 printf("Failed to initialize decoder.");
74 return -1;
75 }
76
77 ma_data_source_set_looping(&decoder, MA_TRUE);
78
79
80 /* Initialize the device. */
81 result = ma_data_source_get_data_format(&decoder, &format, &channels, &sampleRate, NULL, 0);
82 if (result != MA_SUCCESS) {
83 printf("Failed to retrieve decoder data format.");
84 ma_decoder_uninit(&decoder);
85 return -1;
86 }
87
88 deviceConfig = ma_device_config_init(ma_device_type_playback);
89 deviceConfig.playback.format = format;
90 deviceConfig.playback.channels = channels;
91 deviceConfig.sampleRate = sampleRate;
92 deviceConfig.dataCallback = data_callback;
93 deviceConfig.pUserData = &decoder;
94
95 if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
96 printf("Failed to open playback device.\n");
97 ma_decoder_uninit(&decoder);
98 return -1;
99 }
100
101 if (ma_device_start(&device) != MA_SUCCESS) {
102 printf("Failed to start playback device.\n");
103 ma_device_uninit(&device);
104 ma_decoder_uninit(&decoder);
105 return -1;
106 }
107
108 printf("Press Enter to quit...");
109 getchar();
110
111 ma_device_uninit(&device);
112 ma_decoder_uninit(&decoder);
113
114 return 0;
115 }