Skip to content

Commit

Permalink
Some fixes to new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Apr 9, 2021
1 parent 709d550 commit d6057e2
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions tests/cppunittest/FrameMapper_Tests.cpp
Expand Up @@ -493,34 +493,37 @@ TEST(distribute_samples) {
int num_seconds = 1;

// Source frame rates (varies the # of samples per frame)
vector<openshot::Fraction> rates = { openshot::Fraction(30,1),
openshot::Fraction(24,1) ,
openshot::Fraction(119,4),
openshot::Fraction(30000,1001) };
std::vector<openshot::Fraction> rates = {
openshot::Fraction(30,1),
openshot::Fraction(24,1) ,
openshot::Fraction(119,4),
openshot::Fraction(30000,1001)
};

for (auto& frame_rate : rates) {
// Init sin wave variables
int OFFSET = 0;
float AMPLITUDE = 0.75;
double ANGLE = 0.0;
int NUM_SAMPLES = 100;
double angle = 0.0;

// Create cache object to hold test frames
CacheMemory cache;
openshot::CacheMemory cache;

// Let's create some test frames
for (int64_t frame_number = 1; frame_number <= (frame_rate.ToFloat() * num_seconds * 2); frame_number++) {
for (int64_t frame_number = 1; frame_number <= (frame_rate.ToFloat() * num_seconds * 2); ++frame_number) {
// Create blank frame (with specific frame #, samples, and channels)
int sample_count = openshot::Frame::GetSamplesPerFrame(frame_number, frame_rate, sample_rate, channels);
std::shared_ptr<openshot::Frame> f(new openshot::Frame(frame_number, sample_count, channels));
auto f = std::make_shared<openshot::Frame>(frame_number, sample_count, channels);
f->SampleRate(sample_rate);

// Create test samples with sin wave (predictable values)
float *audio_buffer = new float[sample_count * 2];
for (int sample_number = 0; sample_number < sample_count; sample_number++) {
// Calculate sin wave
float sample_value = float(AMPLITUDE * sin(ANGLE) + OFFSET);
float sample_value = float(AMPLITUDE * sin(angle) + OFFSET);
audio_buffer[sample_number] = abs(sample_value);
ANGLE += (2 * M_PI) / NUM_SAMPLES;
angle += (2 * M_PI) / NUM_SAMPLES;
}

// Add custom audio samples to Frame (bool replaceSamples, int destChannel, int destStartSample, const float* source,
Expand All @@ -529,20 +532,23 @@ TEST(distribute_samples) {

// Add test frame to dummy reader
cache.Add(f);

delete[] audio_buffer;
}

// Create a default fraction (should be 1/1)
openshot::DummyReader r(frame_rate, 1920, 1080, sample_rate, channels, 30.0, &cache);
r.Open(); // Open the reader
r.Open();

// Target frame rates
vector<openshot::Fraction> mapped_rates = { openshot::Fraction(30,1),
openshot::Fraction(24,1) ,
openshot::Fraction(119,4),
openshot::Fraction(30000,1001) };
vector<openshot::Fraction> mapped_rates = {
openshot::Fraction(30,1),
openshot::Fraction(24,1),
openshot::Fraction(119,4),
openshot::Fraction(30000,1001)
};
for (auto &mapped_rate : mapped_rates) {
// Reset SIN wave
ANGLE = 0.0;
angle = 0.0;

// Map to different fps
FrameMapper map(&r, mapped_rate, PULLDOWN_NONE, sample_rate, channels, LAYOUT_STEREO);
Expand All @@ -556,8 +562,8 @@ TEST(distribute_samples) {
for (int sample_index = 0; sample_index < sample_count; sample_index++) {

// Calculate sin wave
float predicted_value = abs(float(AMPLITUDE * sin(ANGLE) + OFFSET));
ANGLE += (2 * M_PI) / NUM_SAMPLES;
float predicted_value = abs(float(AMPLITUDE * sin(angle) + OFFSET));
angle += (2 * M_PI) / NUM_SAMPLES;

// Verify each mapped sample value is correct (after being redistributed by the FrameMapper)
float mapped_value = map.GetFrame(frame_index)->GetAudioSample(0, sample_index, 1.0);
Expand Down Expand Up @@ -586,18 +592,16 @@ TEST(distribute_samples) {
t1.Open();

// Reset SIN wave
ANGLE = 0.0;
angle = 0.0;

for (int frame_index = starting_clip_frame; frame_index < (starting_clip_frame + (t1.info.fps.ToFloat() * num_seconds)); frame_index++) {
for (int sample_index = 0; sample_index < t1.GetFrame(frame_index)->GetAudioSamplesCount(); sample_index++) {
// Calculate sin wave
float predicted_value = abs(float(AMPLITUDE * sin(ANGLE) + OFFSET));
ANGLE += (2 * M_PI) / NUM_SAMPLES;
float predicted_value = abs(float(AMPLITUDE * sin(angle) + OFFSET));
angle += (2 * M_PI) / NUM_SAMPLES;

// Verify each mapped sample value is correct (after being redistributed by the FrameMapper)
float timeline_value = t1.GetFrame(frame_index)->GetAudioSample(0, sample_index, 1.0);

// Testing wave value X 2, since we have 2 overlapping clips
CHECK_CLOSE(predicted_value, timeline_value, 0.001);
}
}
Expand Down

0 comments on commit d6057e2

Please sign in to comment.