2010-03-15 15:11:48 -04:00
|
|
|
#include "tests/utils.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
#include "audiographer/general/silence_trimmer.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
using namespace AudioGrapher;
|
|
|
|
|
|
|
|
class SilenceTrimmerTest : public CppUnit::TestFixture
|
|
|
|
{
|
|
|
|
CPPUNIT_TEST_SUITE (SilenceTrimmerTest);
|
|
|
|
CPPUNIT_TEST (testExceptions);
|
|
|
|
CPPUNIT_TEST (testFullBuffers);
|
|
|
|
CPPUNIT_TEST (testPartialBuffers);
|
|
|
|
CPPUNIT_TEST (testAddSilenceBeginning);
|
|
|
|
CPPUNIT_TEST (testAddSilenceEnd);
|
|
|
|
CPPUNIT_TEST_SUITE_END ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
samples = 128;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
random_data = TestUtils::init_random_data(samples);
|
2009-12-27 09:46:23 -05:00
|
|
|
random_data[0] = 0.5;
|
2017-09-18 12:39:17 -04:00
|
|
|
random_data[samples - 1] = 0.5;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
zero_data = new float[samples];
|
|
|
|
memset(zero_data, 0, samples * sizeof(float));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
half_random_data = TestUtils::init_random_data(samples);
|
|
|
|
memset(half_random_data, 0, (samples / 2) * sizeof(float));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
trimmer.reset (new SilenceTrimmer<float> (samples / 2));
|
2009-12-27 09:46:23 -05:00
|
|
|
sink.reset (new AppendingVectorSink<float>());
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->set_trim_beginning (true);
|
|
|
|
trimmer->set_trim_end (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
delete [] zero_data;
|
|
|
|
delete [] half_random_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFullBuffers()
|
|
|
|
{
|
|
|
|
trimmer->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_processed);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_processed);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (sink->get_array(), random_data, samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_processed);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (3 * samples, samples_processed);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (sink->get_array(), random_data, samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[samples], zero_data, samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[2 * samples], random_data, samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (3 * samples, samples_processed);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testPartialBuffers()
|
|
|
|
{
|
|
|
|
trimmer->add_output (sink);
|
2017-09-18 12:39:17 -04:00
|
|
|
trimmer->reset (samples / 4);
|
2010-03-15 15:11:48 -04:00
|
|
|
trimmer->set_trim_beginning (true);
|
|
|
|
trimmer->set_trim_end (true);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (half_random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples / 2, samples_processed);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (sink->get_array(), &half_random_data[samples / 2], samples / 2));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples / 2, samples_processed);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (half_random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * samples + samples / 2, samples_processed);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[samples + samples / 2], half_random_data, samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testExceptions()
|
|
|
|
{
|
|
|
|
{
|
2010-03-15 15:11:48 -04:00
|
|
|
CPPUNIT_ASSERT_THROW (trimmer->reset (0), Exception);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testAddSilenceBeginning()
|
|
|
|
{
|
|
|
|
trimmer->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t silence = samples / 2;
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->add_silence_to_beginning (silence);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (sink->get_array(), zero_data, silence));
|
2017-09-18 12:39:17 -04:00
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[silence], random_data, samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testAddSilenceEnd()
|
|
|
|
{
|
|
|
|
trimmer->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t silence = samples / 3;
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->add_silence_to_end (silence);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
trimmer->process (c);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
c.set_flag (ProcessContext<float>::EndOfInput);
|
|
|
|
trimmer->process (c);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_processed = sink->get_data().size();
|
|
|
|
samplecnt_t total_samples = 2 * samples + silence;
|
|
|
|
CPPUNIT_ASSERT_EQUAL (total_samples, samples_processed);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (sink->get_array(), random_data, samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[samples], random_data, samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&sink->get_array()[samples * 2], zero_data, silence));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SilenceTrimmer<float> > trimmer;
|
|
|
|
std::shared_ptr<AppendingVectorSink<float> > sink;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
float * random_data;
|
|
|
|
float * zero_data;
|
|
|
|
float * half_random_data;
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (SilenceTrimmerTest);
|