Commit 4e91c638 authored by David Reid's avatar David Reid

Delete old tests.

parent 673dce19
#define MA_LOG_LEVEL MA_LOG_LEVEL_VERBOSE
#define MA_DEBUG_OUTPUT
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
int print_context_info(ma_context* pContext)
{
ma_result result = MA_SUCCESS;
ma_device_info* pPlaybackDeviceInfos;
ma_uint32 playbackDeviceCount;
ma_device_info* pCaptureDeviceInfos;
ma_uint32 captureDeviceCount;
printf("BACKEND: %s\n", ma_get_backend_name(pContext->backend));
// Enumeration.
printf(" Enumerating Devices... ");
{
result = ma_context_get_devices(pContext, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount);
if (result == MA_SUCCESS) {
printf("Done\n");
} else {
printf("Failed\n");
goto done;
}
printf(" Playback Devices (%d)\n", playbackDeviceCount);
for (ma_uint32 iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) {
printf(" %d: %s\n", iDevice, pPlaybackDeviceInfos[iDevice].name);
}
printf(" Capture Devices (%d)\n", captureDeviceCount);
for (ma_uint32 iDevice = 0; iDevice < captureDeviceCount; ++iDevice) {
printf(" %d: %s\n", iDevice, pCaptureDeviceInfos[iDevice].name);
}
}
// Device Information.
printf(" Getting Device Information...\n");
{
printf(" Playback Devices (%d)\n", playbackDeviceCount);
for (ma_uint32 iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) {
printf(" %d: %s\n", iDevice, pPlaybackDeviceInfos[iDevice].name);
result = ma_context_get_device_info(pContext, ma_device_type_playback, &pPlaybackDeviceInfos[iDevice].id, ma_share_mode_shared, &pPlaybackDeviceInfos[iDevice]);
if (result == MA_SUCCESS) {
printf(" Name: %s\n", pPlaybackDeviceInfos[iDevice].name);
printf(" Min Channels: %d\n", pPlaybackDeviceInfos[iDevice].minChannels);
printf(" Max Channels: %d\n", pPlaybackDeviceInfos[iDevice].maxChannels);
printf(" Min Sample Rate: %d\n", pPlaybackDeviceInfos[iDevice].minSampleRate);
printf(" Max Sample Rate: %d\n", pPlaybackDeviceInfos[iDevice].maxSampleRate);
printf(" Format Count: %d\n", pPlaybackDeviceInfos[iDevice].formatCount);
for (ma_uint32 iFormat = 0; iFormat < pPlaybackDeviceInfos[iDevice].formatCount; ++iFormat) {
printf(" %s\n", ma_get_format_name(pPlaybackDeviceInfos[iDevice].formats[iFormat]));
}
} else {
printf(" ERROR\n");
}
}
printf(" Capture Devices (%d)\n", captureDeviceCount);
for (ma_uint32 iDevice = 0; iDevice < captureDeviceCount; ++iDevice) {
printf(" %d: %s\n", iDevice, pCaptureDeviceInfos[iDevice].name);
result = ma_context_get_device_info(pContext, ma_device_type_capture, &pCaptureDeviceInfos[iDevice].id, ma_share_mode_shared, &pCaptureDeviceInfos[iDevice]);
if (result == MA_SUCCESS) {
printf(" Name: %s\n", pCaptureDeviceInfos[iDevice].name);
printf(" Min Channels: %d\n", pCaptureDeviceInfos[iDevice].minChannels);
printf(" Max Channels: %d\n", pCaptureDeviceInfos[iDevice].maxChannels);
printf(" Min Sample Rate: %d\n", pCaptureDeviceInfos[iDevice].minSampleRate);
printf(" Max Sample Rate: %d\n", pCaptureDeviceInfos[iDevice].maxSampleRate);
printf(" Format Count: %d\n", pCaptureDeviceInfos[iDevice].formatCount);
for (ma_uint32 iFormat = 0; iFormat < pCaptureDeviceInfos[iDevice].formatCount; ++iFormat) {
printf(" %s\n", ma_get_format_name(pCaptureDeviceInfos[iDevice].formats[iFormat]));
}
} else {
printf(" ERROR\n");
}
}
}
done:
printf("\n");
return (result == MA_SUCCESS) ? 0 : -1;
}
int print_device_info(ma_device* pDevice)
{
printf("DEVICE NAME: %s\n", pDevice->name);
printf(" Format: %s -> %s\n", ma_get_format_name(pDevice->format), ma_get_format_name(pDevice->internalFormat));
printf(" Channels: %d -> %d\n", pDevice->channels, pDevice->internalChannels);
printf(" Sample Rate: %d -> %d\n", pDevice->sampleRate, pDevice->internalSampleRate);
printf(" Buffer Size: %d\n", pDevice->bufferSizeInFrames);
printf(" Periods: %d\n", pDevice->periods);
return 0;
}
ma_uint32 on_send(ma_device* pDevice, ma_uint32 frameCount, void* pFramesOut)
{
ma_sine_wave* pSineWave = (ma_sine_wave*)pDevice->pUserData;
ma_assert(pSineWave != NULL);
float* pFramesOutF32 = (float*)pFramesOut;
for (ma_uint32 iFrame = 0; iFrame < frameCount; ++iFrame) {
float sample;
ma_sine_wave_read(pSineWave, 1, &sample);
for (ma_uint32 iChannel = 0; iChannel < pDevice->channels; ++iChannel) {
pFramesOutF32[iChannel] = sample;
}
pFramesOutF32 += pDevice->channels;
}
return frameCount;
}
int main(int argc, char** argv)
{
ma_result result;
ma_sine_wave sineWave;
result = ma_sine_wave_init(0.2, 400, 44100, &sineWave);
if (result != MA_SUCCESS) {
printf("Failed to initialize sine wave.\n");
return -1;
}
// Separate context for this test.
ma_context_config contextConfig = ma_context_config_init(NULL); // <-- Don't need a log callback because we're using debug output instead.
ma_context context;
result = ma_context_init(NULL, 0, &contextConfig, &context);
if (result != MA_SUCCESS) {
printf("Failed to initialize context.\n");
return -1;
}
print_context_info(&context);
// Device.
ma_device_config deviceConfig = ma_device_config_init_playback(ma_format_f32, 2, 44100, on_send);
deviceConfig.bufferSizeInFrames = 32768;
ma_device device;
result = ma_device_init(&context, ma_device_type_playback, NULL, &deviceConfig, &sineWave, &device);
if (result != MA_SUCCESS) {
ma_context_uninit(&context);
printf("Failed to initialize device.\n");
return -1;
}
print_device_info(&device);
// Start playback.
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
ma_device_uninit(&device);
ma_context_uninit(&context);
printf("Failed to start device.\n");
return -1;
}
printf("Press Enter to quit...\n");
getchar();
ma_device_uninit(&device);
ma_context_uninit(&context);
return 0;
}
#define MA_DEBUG_OUTPUT
#define MA_USE_REFERENCE_CONVERSION_APIS
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
// Two converters are needed here. One for converting f32 samples from the sine wave generator to the input format,
// and another for converting the input format to the output format for device output.
ma_sine_wave sineWave;
ma_format_converter converterIn;
ma_format_converter converterOut;
ma_uint32 on_convert_samples_in(ma_format_converter* pConverter, ma_uint32 frameCount, void* pFrames, void* pUserData)
{
(void)pUserData;
ma_assert(pConverter->config.formatIn == ma_format_f32);
ma_sine_wave* pSineWave = (ma_sine_wave*)pConverter->config.pUserData;
ma_assert(pSineWave);
return (ma_uint32)ma_sine_wave_read_f32(pSineWave, frameCount, (float*)pFrames);
}
ma_uint32 on_convert_samples_out(ma_format_converter* pConverter, ma_uint32 frameCount, void* pFrames, void* pUserData)
{
(void)pUserData;
ma_format_converter* pConverterIn = (ma_format_converter*)pConverter->config.pUserData;
ma_assert(pConverterIn != NULL);
return (ma_uint32)ma_format_converter_read(pConverterIn, frameCount, pFrames, NULL);
}
void on_send_to_device__original(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
ma_assert(pDevice->playback.format == ma_format_f32);
ma_assert(pDevice->playback.channels == 1);
ma_sine_wave_read_f32(&sineWave, frameCount, (float*)pOutput);
(void)pDevice;
(void)pInput;
}
void on_send_to_device__dithered(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
ma_assert(pDevice->playback.channels == 1);
ma_format_converter* pConverter = (ma_format_converter*)pDevice->pUserData;
ma_assert(pConverter != NULL);
ma_assert(pDevice->playback.format == pConverter->config.formatOut);
ma_format_converter_read(pConverter, frameCount, pOutput, NULL);
(void)pInput;
}
int do_dithering_test()
{
ma_device_config config;
ma_device device;
ma_result result;
config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 1;
config.sampleRate = 0;
config.dataCallback = on_send_to_device__original;
// We first play the sound the way it's meant to be played.
result = ma_device_init(NULL, &config, &device);
if (result != MA_SUCCESS) {
return -1;
}
ma_sine_wave_init(0.5, 400, device.sampleRate, &sineWave);
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
return -2;
}
printf("Press Enter to play enable dithering.\n");
getchar();
ma_device_uninit(&device);
ma_format srcFormat = ma_format_s24;
ma_format dstFormat = ma_format_u8;
ma_dither_mode ditherMode = ma_dither_mode_triangle;
ma_format_converter_config converterInConfig = ma_format_converter_config_init_new();
converterInConfig.formatIn = ma_format_f32; // <-- From the sine wave generator.
converterInConfig.formatOut = srcFormat;
converterInConfig.channels = config.playback.channels;
converterInConfig.ditherMode = ma_dither_mode_none;
converterInConfig.onRead = on_convert_samples_in;
converterInConfig.pUserData = &sineWave;
result = ma_format_converter_init(&converterInConfig, &converterIn);
if (result != MA_SUCCESS) {
return -3;
}
ma_format_converter_config converterOutConfig = ma_format_converter_config_init_new();
converterOutConfig.formatIn = srcFormat;
converterOutConfig.formatOut = dstFormat;
converterOutConfig.channels = config.playback.channels;
converterOutConfig.ditherMode = ditherMode;
converterOutConfig.onRead = on_convert_samples_out;
converterOutConfig.pUserData = &converterIn;
result = ma_format_converter_init(&converterOutConfig, &converterOut);
if (result != MA_SUCCESS) {
return -3;
}
config.playback.format = dstFormat;
config.dataCallback = on_send_to_device__dithered;
config.pUserData = &converterOut;
result = ma_device_init(NULL, &config, &device);
if (result != MA_SUCCESS) {
return -1;
}
// Now we play the sound after it's run through a dithered format converter.
ma_sine_wave_init(0.5, 400, device.sampleRate, &sineWave);
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
return -2;
}
printf("Press Enter to stop.\n");
getchar();
return 0;
}
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
do_dithering_test();
return 0;
}
\ No newline at end of file
// Just a simple test to check that MA_NO_DEVICE_IO compiles.
#include "../extras/dr_flac.h"
#include "../extras/dr_mp3.h"
#include "../extras/dr_wav.h"
#define MA_NO_DEVICE_IO
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
ma_result result = MA_ERROR;
ma_pcm_converter_config dspConfig = ma_pcm_converter_config_init_new();
ma_pcm_converter converter;
result = ma_pcm_converter_init(&dspConfig, &converter);
ma_decoder_config decoderConfig = ma_decoder_config_init(ma_format_unknown, 0, 0);
ma_decoder decoder;
result = ma_decoder_init_file("res/sine_s16_mono_48000.wav", &decoderConfig, &decoder);
return result;
}
#define DR_FLAC_IMPLEMENTATION
#include "../extras/dr_flac.h"
#define DR_MP3_IMPLEMENTATION
#include "../extras/dr_mp3.h"
#define DR_WAV_IMPLEMENTATION
#include "../extras/dr_wav.h"
This diff is collapsed.
#define MA_NO_SSE2
#define MA_NO_AVX2
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
// There is a usage pattern for resampling that miniaudio does not properly support which is where the client continuously
// reads samples until ma_src_read() returns 0. The problem with this pattern is that is consumes the samples sitting
// in the window which are needed to compute the next samples in future calls to ma_src_read() (assuming the client
// has re-filled the resampler's input data).
/*
for (;;) {
fill_src_input_data(&src, someData);
float buffer[4096]
while ((framesRead = ma_src_read(&src, ...) != 0) {
do_something_with_resampled_data(buffer);
}
}
*/
// In the use case above, the very last samples that are read from ma_src_read() will not have future samples to draw
// from in order to calculate the correct interpolation factor which in turn results in crackling.
ma_uint32 sampleRateIn = 0;
ma_uint32 sampleRateOut = 0;
ma_sine_wave sineWave; // <-- This is the source data.
ma_src src;
float srcInput[1024];
ma_uint32 srcNextSampleIndex = ma_countof(srcInput);
void reload_src_input()
{
ma_sine_wave_read_f32(&sineWave, ma_countof(srcInput), srcInput);
srcNextSampleIndex = 0;
}
ma_uint32 on_src(ma_src* pSRC, ma_uint32 frameCount, void** ppSamplesOut, void* pUserData)
{
ma_assert(pSRC != NULL);
ma_assert(pSRC->config.channels == 1);
(void)pUserData;
// Only read as much as is available in the input buffer. Do not reload the buffer here.
ma_uint32 framesAvailable = ma_countof(srcInput) - srcNextSampleIndex;
ma_uint32 framesToRead = frameCount;
if (framesToRead > framesAvailable) {
framesToRead = framesAvailable;
}
ma_copy_memory(ppSamplesOut[0], srcInput + srcNextSampleIndex, sizeof(float)*framesToRead);
srcNextSampleIndex += framesToRead;
return framesToRead;
}
void on_send_to_device(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
(void)pDevice;
(void)pInput;
ma_assert(pDevice->playback.format == ma_format_f32);
ma_assert(pDevice->playback.channels == 1);
float* pFramesF32 = (float*)pOutput;
// To reproduce the case we are needing to test, we need to read from the SRC in a very specific way. We keep looping
// until we've read the requested frame count, however we have an inner loop that keeps running until ma_src_read()
// returns 0, in which case we need to reload the SRC's input data and keep going.
ma_uint32 totalFramesRead = 0;
while (totalFramesRead < frameCount) {
ma_uint32 framesRemaining = frameCount - totalFramesRead;
ma_uint32 maxFramesToRead = 128;
ma_uint32 framesToRead = framesRemaining;
if (framesToRead > maxFramesToRead) {
framesToRead = maxFramesToRead;
}
ma_uint32 framesRead = (ma_uint32)ma_src_read_deinterleaved(&src, framesToRead, (void**)&pFramesF32, NULL);
if (framesRead == 0) {
reload_src_input();
}
totalFramesRead += framesRead;
pFramesF32 += framesRead;
}
ma_assert(totalFramesRead == frameCount);
}
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 1;
config.dataCallback = on_send_to_device;
ma_device device;
ma_result result;
config.bufferSizeInFrames = 8192*1;
// We first play the sound the way it's meant to be played.
result = ma_device_init(NULL, &config, &device);
if (result != MA_SUCCESS) {
return -1;
}
// For this test, we need the sine wave to be a different format to the device.
sampleRateOut = device.sampleRate;
sampleRateIn = (sampleRateOut == 44100) ? 48000 : 44100;
ma_sine_wave_init(0.2, 400, sampleRateIn, &sineWave);
ma_src_config srcConfig = ma_src_config_init(sampleRateIn, sampleRateOut, 1, on_src, NULL);
srcConfig.algorithm = ma_src_algorithm_sinc;
srcConfig.neverConsumeEndOfInput = MA_TRUE;
result = ma_src_init(&srcConfig, &src);
if (result != MA_SUCCESS) {
printf("Failed to create SRC.\n");
return -1;
}
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
return -2;
}
printf("Press Enter to quit...\n");
getchar();
ma_device_uninit(&device);
return 0;
}
#include <stdio.h>
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
ma_sine_wave sineWave;
ma_uint32 framesWritten;
ma_event stopEvent;
ma_bool32 isInitialRun = MA_TRUE;
void on_stop(ma_device* pDevice)
{
(void)pDevice;
printf("STOPPED\n");
}
void on_data(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
(void)pInput; /* Not used yet. */
/* Output exactly one second of data. Pad the end with silence. */
ma_uint32 framesRemaining = pDevice->sampleRate - framesWritten;
ma_uint32 framesToProcess = frameCount;
if (framesToProcess > framesRemaining && isInitialRun) {
framesToProcess = framesRemaining;
}
ma_sine_wave_read_f32_ex(&sineWave, framesToProcess, pDevice->playback.channels, ma_stream_layout_interleaved, (float**)&pOutput);
if (isInitialRun) {
framesWritten += framesToProcess;
}
ma_assert(framesWritten <= pDevice->sampleRate);
if (framesWritten >= pDevice->sampleRate) {
if (isInitialRun) {
printf("STOPPING [AUDIO THREAD]...\n");
ma_event_signal(&stopEvent);
isInitialRun = MA_FALSE;
}
}
}
int main(int argc, char** argv)
{
ma_result result;
(void)argc;
(void)argv;
ma_backend backend = ma_backend_wasapi;
ma_sine_wave_init(0.25, 400, 44100, &sineWave);
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 44100;
config.dataCallback = on_data;
config.stopCallback = on_stop;
config.bufferSizeInFrames = 16384;
ma_device device;
result = ma_device_init_ex(&backend, 1, NULL, &config, &device);
if (result != MA_SUCCESS) {
printf("Failed to initialize device.\n");
return result;
}
result = ma_event_init(device.pContext, &stopEvent);
if (result != MA_SUCCESS) {
printf("Failed to initialize stop event.\n");
return result;
}
ma_device_start(&device);
/* We wait for the stop event, stop the device, then ask the user to press any key to restart. This checks that the device can restart after stopping. */
ma_event_wait(&stopEvent);
printf("STOPPING [MAIN THREAD]...\n");
ma_device_stop(&device);
printf("Press Enter to restart...\n");
getchar();
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
printf("Failed to restart the device.\n");
ma_device_uninit(&device);
return -1;
}
printf("Press Enter to quit...\n");
getchar();
ma_device_uninit(&device);
return 0;
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment