Debian Codegraph @

examples/simple_duplex.c

raw symbols SCIP
1 /*
2 Demonstrates duplex mode which is where data is captured from a microphone and then output to a speaker device.
3
4 This example captures audio from the default microphone and then outputs it straight to the default playback device
5 without any kind of modification. If you wanted to, you could also apply filters and effects to the input stream
6 before outputting to the playback device.
7
8 Note that the microphone and playback device must run in lockstep. Any kind of timing deviation will result in audible
9 glitching which the backend may not be able to recover from. For this reason, miniaudio forces you to use the same
10 sample rate for both capture and playback. If internally the native sample rates differ, miniaudio will perform the
11 sample rate conversion for you automatically.
12 */
13 #include "../miniaudio.c"
14
15 #include <stdio.h>
16
17 #ifdef __EMSCRIPTEN__
18 void main_loop__em()
19 {
20 }
21 #endif
22
23 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
24 {
25 /* This example assumes the playback and capture sides use the same format and channel count. */
26 if (pDevice->capture.format != pDevice->playback.format || pDevice->capture.channels != pDevice->playback.channels) {
27 return;
28 }
29
30 /* In this example the format and channel count are the same for both input and output which means we can just memcpy(). */
31 MA_COPY_MEMORY(pOutput, pInput, frameCount * ma_get_bytes_per_frame(pDevice->capture.format, pDevice->capture.channels));
32 }
33
34 int main(int argc, char** argv)
35 {
36 ma_result result;
37 ma_device_config deviceConfig;
38 ma_device device;
39
40 deviceConfig = ma_device_config_init(ma_device_type_duplex);
41 deviceConfig.capture.pDeviceID = NULL;
42 deviceConfig.capture.format = ma_format_s16;
43 deviceConfig.capture.channels = 2;
44 deviceConfig.capture.shareMode = ma_share_mode_shared;
45 deviceConfig.playback.pDeviceID = NULL;
46 deviceConfig.playback.format = ma_format_s16;
47 deviceConfig.playback.channels = 2;
48 deviceConfig.dataCallback = data_callback;
49 result = ma_device_init(NULL, &deviceConfig, &device);
50 if (result != MA_SUCCESS) {
51 return result;
52 }
53
54 #ifdef __EMSCRIPTEN__
55 getchar();
56 #endif
57
58 ma_device_start(&device);
59
60 #ifdef __EMSCRIPTEN__
61 emscripten_set_main_loop(main_loop__em, 0, 1);
62 #else
63 printf("Press Enter to quit...\n");
64 getchar();
65 #endif
66
67 ma_device_uninit(&device);
68
69 (void)argc;
70 (void)argv;
71 return 0;
72 }