| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 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; |
| 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 |
} |