mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-21 15:56:58 +02:00
Add generation tests.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
#define MINIAUDIO_IMPLEMENTATION
|
||||
#include "../../miniaudio.h"
|
||||
|
||||
#define DR_WAV_IMPLEMENTATION
|
||||
#include "../../extras/dr_wav.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static drwav_data_format drwav_data_format_from_minaudio_format(ma_format format, ma_uint32 channels, ma_uint32 sampleRate)
|
||||
{
|
||||
drwav_data_format wavFormat;
|
||||
|
||||
wavFormat.container = drwav_container_riff;
|
||||
wavFormat.channels = channels;
|
||||
wavFormat.sampleRate = sampleRate;
|
||||
wavFormat.bitsPerSample = ma_get_bytes_per_sample(format) * 8;
|
||||
|
||||
if (format == ma_format_f32) {
|
||||
wavFormat.format = DR_WAVE_FORMAT_IEEE_FLOAT;
|
||||
} else {
|
||||
wavFormat.format = DR_WAVE_FORMAT_PCM;
|
||||
}
|
||||
|
||||
return wavFormat;
|
||||
}
|
||||
|
||||
#include "ma_test_generation_noise.c"
|
||||
#include "ma_test_generation_waveform.c"
|
||||
|
||||
#define MAX_TESTS 64
|
||||
|
||||
typedef int (* ma_test_entry_proc)(int argc, char** argv);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char* pName;
|
||||
ma_test_entry_proc onEntry;
|
||||
} ma_test;
|
||||
|
||||
struct
|
||||
{
|
||||
ma_test pTests[MAX_TESTS];
|
||||
size_t count;
|
||||
} g_Tests;
|
||||
|
||||
ma_result ma_register_test(const char* pName, ma_test_entry_proc onEntry)
|
||||
{
|
||||
MA_ASSERT(pName != NULL);
|
||||
MA_ASSERT(onEntry != NULL);
|
||||
|
||||
if (g_Tests.count >= MAX_TESTS) {
|
||||
printf("Failed to register test %s because there are too many tests already registered. Increase the value of MAX_TESTS\n", pName);
|
||||
return MA_INVALID_OPERATION;
|
||||
}
|
||||
|
||||
g_Tests.pTests[g_Tests.count].pName = pName;
|
||||
g_Tests.pTests[g_Tests.count].onEntry = onEntry;
|
||||
g_Tests.count += 1;
|
||||
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
size_t iTest;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
result = ma_register_test("Noise", test_entry__noise);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = ma_register_test("Waveform", test_entry__waveform);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (iTest = 0; iTest < g_Tests.count; iTest += 1) {
|
||||
printf("=== BEGIN %s ===\n", g_Tests.pTests[iTest].pName);
|
||||
result = g_Tests.pTests[iTest].onEntry(argc, argv);
|
||||
printf("=== END %s : %s ===\n", g_Tests.pTests[iTest].pName, (result == 0) ? "PASSED" : "FAILED");
|
||||
|
||||
if (result != 0) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return -1; /* Something failed. */
|
||||
} else {
|
||||
return 0; /* Everything passed. */
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
|
||||
static drwav_data_format drwav_data_format_from_noise_config(const ma_noise_config* pNoiseConfig)
|
||||
{
|
||||
MA_ASSERT(pNoiseConfig != NULL);
|
||||
|
||||
return drwav_data_format_from_minaudio_format(pNoiseConfig->format, pNoiseConfig->channels, 48000);
|
||||
}
|
||||
|
||||
ma_result test_noise__by_format_and_type(ma_format format, ma_waveform_type type, const char* pFileName)
|
||||
{
|
||||
ma_result result;
|
||||
ma_noise_config noiseConfig;
|
||||
ma_noise noise;
|
||||
drwav_data_format wavFormat;
|
||||
drwav wav;
|
||||
ma_uint32 iFrame;
|
||||
|
||||
printf(" %s\n", pFileName);
|
||||
|
||||
noiseConfig = ma_noise_config_init(format, 2, type, 0, 0.1);
|
||||
result = ma_noise_init(&noiseConfig, &noise);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
wavFormat = drwav_data_format_from_noise_config(&noiseConfig);
|
||||
if (!drwav_init_file_write(&wav, pFileName, &wavFormat, NULL)) {
|
||||
return MA_ERROR; /* Could not open file for writing. */
|
||||
}
|
||||
|
||||
/* We'll do a few seconds of data. */
|
||||
for (iFrame = 0; iFrame < wavFormat.sampleRate * 10; iFrame += 1) {
|
||||
ma_uint8 temp[1024];
|
||||
ma_noise_read_pcm_frames(&noise, temp, 1);
|
||||
drwav_write_pcm_frames(&wav, 1, temp);
|
||||
}
|
||||
|
||||
drwav_uninit(&wav);
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
ma_result test_noise__f32()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
|
||||
result = test_noise__by_format_and_type(ma_format_f32, ma_noise_type_white, "output/noise_f32_white.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
ma_result test_noise__s16()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
|
||||
result = test_noise__by_format_and_type(ma_format_s16, ma_noise_type_white, "output/noise_s16_white.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
ma_result test_noise__u8()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
|
||||
result = test_noise__by_format_and_type(ma_format_u8, ma_noise_type_white, "output/noise_u8_white.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
int test_entry__noise(int argc, char** argv)
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
result = test_noise__f32();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_noise__s16();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_noise__u8();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
|
||||
static drwav_data_format drwav_data_format_from_waveform_config(const ma_waveform_config* pWaveformConfig)
|
||||
{
|
||||
MA_ASSERT(pWaveformConfig != NULL);
|
||||
|
||||
return drwav_data_format_from_minaudio_format(pWaveformConfig->format, pWaveformConfig->channels, pWaveformConfig->sampleRate);
|
||||
}
|
||||
|
||||
ma_result test_waveform__by_format_and_type(ma_format format, ma_waveform_type type, double amplitude, const char* pFileName)
|
||||
{
|
||||
ma_result result;
|
||||
ma_waveform_config waveformConfig;
|
||||
ma_waveform waveform;
|
||||
drwav_data_format wavFormat;
|
||||
drwav wav;
|
||||
ma_uint32 iFrame;
|
||||
|
||||
printf(" %s\n", pFileName);
|
||||
|
||||
waveformConfig = ma_waveform_config_init(format, 2, 48000, type, amplitude, 220);
|
||||
result = ma_waveform_init(&waveformConfig, &waveform);
|
||||
if (result != MA_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
wavFormat = drwav_data_format_from_waveform_config(&waveformConfig);
|
||||
if (!drwav_init_file_write(&wav, pFileName, &wavFormat, NULL)) {
|
||||
return MA_ERROR; /* Could not open file for writing. */
|
||||
}
|
||||
|
||||
/* We'll do a few seconds of data. */
|
||||
for (iFrame = 0; iFrame < wavFormat.sampleRate * 10; iFrame += 1) {
|
||||
float temp[MA_MAX_CHANNELS];
|
||||
ma_waveform_read_pcm_frames(&waveform, temp, 1);
|
||||
drwav_write_pcm_frames(&wav, 1, temp);
|
||||
}
|
||||
|
||||
drwav_uninit(&wav);
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
|
||||
ma_result test_waveform__f32()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
double amplitude = 0.2;
|
||||
|
||||
/* Sine */
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_sine, +amplitude, "output/waveform_f32_sine.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_sine, -amplitude, "output/waveform_f32_sine_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Square */
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_square, +amplitude, "output/waveform_f32_square.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_square, -amplitude, "output/waveform_f32_square_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Triangle */
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_triangle, +amplitude, "output/waveform_f32_triangle.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_triangle, -amplitude, "output/waveform_f32_triangle_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Sawtooth */
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_sawtooth, +amplitude, "output/waveform_f32_sawtooth.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_f32, ma_waveform_type_sawtooth, -amplitude, "output/waveform_f32_sawtooth_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
ma_result test_waveform__s16()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
double amplitude = 0.2;
|
||||
|
||||
/* Sine */
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_sine, +amplitude, "output/waveform_s16_sine.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_sine, -amplitude, "output/waveform_s16_sine_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Square */
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_square, +amplitude, "output/waveform_s16_square.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_square, -amplitude, "output/waveform_s16_square_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Triangle */
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_triangle, +amplitude, "output/waveform_s16_triangle.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_triangle, -amplitude, "output/waveform_s16_triangle_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Sawtooth */
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_sawtooth, +amplitude, "output/waveform_s16_sawtooth.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_s16, ma_waveform_type_sawtooth, -amplitude, "output/waveform_s16_sawtooth_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
ma_result test_waveform__u8()
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
double amplitude = 0.2;
|
||||
|
||||
/* Sine */
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_sine, +amplitude, "output/waveform_u8_sine.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_sine, -amplitude, "output/waveform_u8_sine_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Square */
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_square, +amplitude, "output/waveform_u8_square.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_square, -amplitude, "output/waveform_u8_square_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Triangle */
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_triangle, +amplitude, "output/waveform_u8_triangle.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_triangle, -amplitude, "output/waveform_u8_triangle_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
/* Sawtooth */
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_sawtooth, +amplitude, "output/waveform_u8_sawtooth.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__by_format_and_type(ma_format_u8, ma_waveform_type_sawtooth, -amplitude, "output/waveform_u8_sawtooth_neg.wav");
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
|
||||
if (hasError) {
|
||||
return MA_ERROR;
|
||||
} else {
|
||||
return MA_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
int test_entry__waveform(int argc, char** argv)
|
||||
{
|
||||
ma_result result;
|
||||
ma_bool32 hasError = MA_FALSE;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
result = test_waveform__f32();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__s16();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
result = test_waveform__u8();
|
||||
if (result != MA_SUCCESS) {
|
||||
hasError = MA_TRUE;
|
||||
}
|
||||
|
||||
if (hasError) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user