Audio conversion issues? #394
-
I am completely new to audio processing and I'm trying to get the loopback recording as signed int16 PCM #include <iostream>
#include "opus_codec.h"
#define MA_NO_GENERATION
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include <cstdlib>
#include <cstdio>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
// Setup
auto* pFile = (FILE*)pDevice->pUserData;
MA_ASSERT(pFile != nullptr);
std::cout << "Captured frame of size " << frameCount << " - " << (frameCount * 2 * sizeof(short)) << "\n";
// TEMPORARY TEST: Covert and write pcm data
static auto* buf = calloc(1, frameCount * 2 * sizeof(short));
ma_convert_pcm_frames_format(buf, ma_format_s16, pInput, pDevice->capture.format, frameCount, 2, ma_dither_mode_triangle);
fwrite(buf, frameCount * 2 * sizeof(short), 1, pFile);
}
int main()
{
ma_result result;
ma_encoder_config encoderConfig;
ma_device_config deviceConfig;
ma_device device;
FILE* pcmFile = fopen("test.pcm", "w");
// Select wasapi as the backend (since it supports loopback)
ma_backend backends[] = {
ma_backend_wasapi
};
// Setup device capture config
deviceConfig = ma_device_config_init(ma_device_type_loopback);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.channels = 0;
deviceConfig.sampleRate = 0;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = pcmFile;
// Initialize device
result = ma_device_init_ex(backends, sizeof(backends) / sizeof(backends[0]), NULL, &deviceConfig, &device);
if (result != MA_SUCCESS)
{
printf("Failed to initialize loopback device.\n");
fclose(pcmFile);
return -2;
}
// TEST: Output information about the selected device
printf("%u %u %u\n", device.capture.format, device.capture.channels, device.sampleRate);
// Start capturing
result = ma_device_start(&device);
if (result != MA_SUCCESS)
{
ma_device_uninit(&device);
fclose(pcmFile);
printf("Failed to start device.\n");
return -3;
}
// Wait until we exit
printf("Press Enter to stop recording...\n");
getchar();
// Cleanup
ma_device_uninit(&device);
fclose(pcmFile);
return 0;
} Here is the output, I suspect maybe I'm writing more data than available? but then again I'm using calloc so it would just be 0s? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Can you post the output with A second bug is that you're opening the file in text mode and not byte mode. Open the file with "wb" instead of "w". A piece of general advice - get miniaudio to do the data conversion for you internally. If you always want to use |
Beta Was this translation helpful? Give feedback.
Can you post the output with
#define MA_DEBUG_OUTPUT
? One bug you certainly have is that you've specified a channel count of 0 in the device config which tells miniaudio to use the device's native channel count which could be anything, but in the data callback you've hard coded it to 2. Try changing the channel count in the device config to 2.A second bug is that you're opening the file in text mode and not byte mode. Open the file with "wb" instead of "w".
A piece of general advice - get miniaudio to do the data conversion for you internally. If you always want to use
ma_format_s16
for your final output, just set the format in the device config:deviceConfig.capture.format = ma_format_s16
…