Debian Codegraph @

examples/simple_loopback.c

raw symbols SCIP
1 /*
2 Demonstrates how to implement loopback recording.
3
4 This example simply captures data from your default playback device until you press Enter. The output is saved to the
5 file specified on the command line.
6
7 Loopback mode is when you record audio that is played from a given speaker. It is only supported on WASAPI, but can be
8 used indirectly with PulseAudio by choosing the appropriate loopback device after enumeration.
9
10 To use loopback mode you just need to set the device type to ma_device_type_loopback and set the capture device config
11 properties. The output buffer in the callback will be null whereas the input buffer will be valid.
12 */
13 #include "../miniaudio.c"
14
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
19 {
20 ma_encoder_write_pcm_frames((ma_encoder*)pDevice->pUserData, pInput, frameCount, NULL);
21
22 (void)pOutput;
23 }
24
25 int main(int argc, char** argv)
26 {
27 ma_result result;
28 ma_encoder_config encoderConfig;
29 ma_encoder encoder;
30 ma_device_config deviceConfig;
31 ma_device device;
32
33 /* Loopback mode is currently only supported on WASAPI. */
34 ma_backend backends[] = {
35 ma_backend_wasapi
36 };
37
38 if (argc < 2) {
39 printf("No output file.\n");
40 return -1;
41 }
42
43 encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
44
45 if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) {
46 printf("Failed to initialize output file.\n");
47 return -1;
48 }
49
50 deviceConfig = ma_device_config_init(ma_device_type_loopback);
51 deviceConfig.capture.pDeviceID = NULL; /* Use default device for this example. Set this to the ID of a _playback_ device if you want to capture from a specific device. */
52 deviceConfig.capture.format = encoder.config.format;
53 deviceConfig.capture.channels = encoder.config.channels;
54 deviceConfig.sampleRate = encoder.config.sampleRate;
55 deviceConfig.dataCallback = data_callback;
56 deviceConfig.pUserData = &encoder;
57
58 result = ma_device_init_ex(backends, sizeof(backends)/sizeof(backends[0]), NULL, &deviceConfig, &device);
59 if (result != MA_SUCCESS) {
60 printf("Failed to initialize loopback device.\n");
61 return -2;
62 }
63
64 result = ma_device_start(&device);
65 if (result != MA_SUCCESS) {
66 ma_device_uninit(&device);
67 printf("Failed to start device.\n");
68 return -3;
69 }
70
71 printf("Press Enter to stop recording...\n");
72 getchar();
73
74 ma_device_uninit(&device);
75 ma_encoder_uninit(&encoder);
76
77 return 0;
78 }