Debian Codegraph @

examples/simple_capture.c

raw symbols SCIP
1 /*
2 Demonstrates how to capture data from a microphone using the low-level API.
3
4 This example simply captures data from your default microphone until you press Enter. The output is saved to the file
5 specified on the command line.
6
7 Capturing works in a very similar way to playback. The only difference is the direction of data movement. Instead of
8 the application sending data to the device, the device will send data to the application. This example just writes the
9 data received by the microphone straight to a WAV file.
10 */
11 #include "../miniaudio.c"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
17 {
18 ma_encoder_write_pcm_frames((ma_encoder*)pDevice->pUserData, pInput, frameCount, NULL);
19
20 (void)pOutput;
21 }
22
23 int main(int argc, char** argv)
24 {
25 ma_result result;
26 ma_encoder_config encoderConfig;
27 ma_encoder encoder;
28 ma_device_config deviceConfig;
29 ma_device device;
30
31 if (argc < 2) {
32 printf("No output file.\n");
33 return -1;
34 }
35
36 encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
37
38 if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) {
39 printf("Failed to initialize output file.\n");
40 return -1;
41 }
42
43 deviceConfig = ma_device_config_init(ma_device_type_capture);
44 deviceConfig.capture.format = encoder.config.format;
45 deviceConfig.capture.channels = encoder.config.channels;
46 deviceConfig.sampleRate = encoder.config.sampleRate;
47 deviceConfig.dataCallback = data_callback;
48 deviceConfig.pUserData = &encoder;
49
50 result = ma_device_init(NULL, &deviceConfig, &device);
51 if (result != MA_SUCCESS) {
52 printf("Failed to initialize capture device.\n");
53 return -2;
54 }
55
56 result = ma_device_start(&device);
57 if (result != MA_SUCCESS) {
58 ma_device_uninit(&device);
59 printf("Failed to start device.\n");
60 return -3;
61 }
62
63 printf("Press Enter to stop recording...\n");
64 getchar();
65
66 ma_device_uninit(&device);
67 ma_encoder_uninit(&encoder);
68
69 return 0;
70 }