#include "audiosynth/AudioMath.h" #include "unittest/TestSuite.h" #define TOLERANCE 0.0001f static void testResample(void) { float samples1[5] = {0, 1, 0, -1, 0}; float result[15]; AudioFrameIndex framesRead = 0, framesWritten = 0; convertAudioSamples(samples1, 5, 1, 1, 4, result, 15, 1, 1, 4, &framesRead, &framesWritten, NULL); TestCase_assertUIntEqual(framesRead, 5); TestCase_assertUIntEqual(framesWritten, 5); TestCase_assertFloatApproximate(result[0], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[1], 1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[2], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[3], -1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[4], 0.0f, TOLERANCE); convertAudioSamples(samples1, 5, 1, 1, 4, result, 15, 1, 2, 4, &framesRead, &framesWritten, NULL); TestCase_assertUIntEqual(framesRead, 5); TestCase_assertUIntEqual(framesWritten, 9); TestCase_assertFloatApproximate(result[0], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[1], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[2], 1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[3], 1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[4], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[5], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[6], -1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[7], -1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[8], 0.0f, TOLERANCE); convertAudioSamples(samples1, 5, 1, 2, 4, result, 15, 1, 1, 4, &framesRead, &framesWritten, NULL); TestCase_assertUIntEqual(framesRead, 5); TestCase_assertUIntEqual(framesWritten, 3); TestCase_assertFloatApproximate(result[0], 0.5f, TOLERANCE); TestCase_assertFloatApproximate(result[1], -0.5f, TOLERANCE); TestCase_assertFloatApproximate(result[2], 0.0f, TOLERANCE); convertAudioSamples(samples1, 5, 1, 2, 4, result, 15, 1, 3, 4, &framesRead, &framesWritten, NULL); TestCase_assertUIntEqual(framesRead, 5); TestCase_assertUIntEqual(framesWritten, 7); TestCase_assertFloatApproximate(result[0], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[1], 0.5f, TOLERANCE); TestCase_assertFloatApproximate(result[2], 1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[3], 0.0f, TOLERANCE); TestCase_assertFloatApproximate(result[4], -0.5f, TOLERANCE); TestCase_assertFloatApproximate(result[5], -1.0f, TOLERANCE); TestCase_assertFloatApproximate(result[6], 0.0f, TOLERANCE); convertAudioSamples(samples1, 5, 1, 3, 4, result, 15, 1, 2, 4, &framesRead, &framesWritten, NULL); TestCase_assertUIntEqual(framesRead, 5); TestCase_assertUIntEqual(framesWritten, 3); TestCase_assertFloatApproximate(result[0], 0.333333f, TOLERANCE); TestCase_assertFloatApproximate(result[1], 0.333333f, TOLERANCE); TestCase_assertFloatApproximate(result[2], -0.666667f, TOLERANCE); AudioResampleState resampleState = initAudioResampleState(); convertAudioSamples(samples1, 5, 1, 3, 4, result, 1, 1, 2, 4, &framesRead, &framesWritten, &resampleState); TestCase_assertUIntEqual(framesRead, 2); TestCase_assertUIntEqual(framesWritten, 1); TestCase_assertFloatApproximate(result[0], 0.333333f, TOLERANCE); convertAudioSamples(samples1 + 2, 3, 1, 3, 4, result, 1, 1, 2, 4, &framesRead, &framesWritten, &resampleState); TestCase_assertUIntEqual(framesRead, 1); TestCase_assertUIntEqual(framesWritten, 1); TestCase_assertFloatApproximate(result[0], 0.333333f, TOLERANCE); convertAudioSamples(samples1 + 3, 2, 1, 3, 4, result, 1, 1, 2, 4, &framesRead, &framesWritten, &resampleState); TestCase_assertUIntEqual(framesRead, 2); TestCase_assertUIntEqual(framesWritten, 1); TestCase_assertFloatApproximate(result[0], -0.666667f, TOLERANCE); } TEST_SUITE(AudioMathTest, testResample)