Update loopback example to use the ma_encoder API.

This commit is contained in:
David Reid
2020-04-23 17:28:59 +10:00
parent 3645d9b0fd
commit e438ab0c9c
+15 -19
View File
@@ -6,21 +6,21 @@ To use loopback mode you just need to set the device type to ma_device_type_loop
properties. The output buffer in the callback will be null whereas the input buffer will be valid. properties. The output buffer in the callback will be null whereas the input buffer will be valid.
*/ */
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
#define DR_WAV_IMPLEMENTATION #define DR_WAV_IMPLEMENTATION
#include "../extras/dr_wav.h" #include "../extras/dr_wav.h"
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{ {
drwav* pWav = (drwav*)pDevice->pUserData; ma_encoder* pEncoder = (ma_encoder*)pDevice->pUserData;
MA_ASSERT(pWav != NULL); MA_ASSERT(pEncoder != NULL);
drwav_write_pcm_frames(pWav, frameCount, pInput); ma_encoder_write_pcm_frames(pEncoder, pInput, frameCount);
(void)pOutput; (void)pOutput;
} }
@@ -28,8 +28,8 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
ma_result result; ma_result result;
drwav_data_format wavFormat; ma_encoder_config encoderConfig;
drwav wav; ma_encoder encoder;
ma_device_config deviceConfig; ma_device_config deviceConfig;
ma_device device; ma_device device;
@@ -43,24 +43,20 @@ int main(int argc, char** argv)
return -1; return -1;
} }
wavFormat.container = drwav_container_riff; encoderConfig = ma_encoder_config_init(ma_resource_format_wav, ma_format_f32, 2, 44100);
wavFormat.format = DR_WAVE_FORMAT_IEEE_FLOAT;
wavFormat.channels = 2;
wavFormat.sampleRate = 44100;
wavFormat.bitsPerSample = 32;
if (drwav_init_file_write(&wav, argv[1], &wavFormat, NULL) == DRWAV_FALSE) { if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) {
printf("Failed to initialize output file.\n"); printf("Failed to initialize output file.\n");
return -1; return -1;
} }
deviceConfig = ma_device_config_init(ma_device_type_loopback); deviceConfig = ma_device_config_init(ma_device_type_loopback);
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. */ 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. */
deviceConfig.capture.format = ma_format_f32; deviceConfig.capture.format = encoder.config.format;
deviceConfig.capture.channels = wavFormat.channels; deviceConfig.capture.channels = encoder.config.channels;
deviceConfig.sampleRate = wavFormat.sampleRate; deviceConfig.sampleRate = encoder.config.sampleRate;
deviceConfig.dataCallback = data_callback; deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &wav; deviceConfig.pUserData = &encoder;
result = ma_device_init_ex(backends, sizeof(backends)/sizeof(backends[0]), NULL, &deviceConfig, &device); result = ma_device_init_ex(backends, sizeof(backends)/sizeof(backends[0]), NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
@@ -79,7 +75,7 @@ int main(int argc, char** argv)
getchar(); getchar();
ma_device_uninit(&device); ma_device_uninit(&device);
drwav_uninit(&wav); ma_encoder_uninit(&encoder);
return 0; return 0;
} }