Skip to content

Commit

Permalink
Port new test to Catch2
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Apr 9, 2021
1 parent d6057e2 commit 96e165a
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions tests/FrameMapper.cpp
Expand Up @@ -482,6 +482,139 @@ TEST_CASE( "redistribute_samples_per_frame", "[libopenshot][framemapper]" ) {
r.Close();
}

TEST_CASE( "Distribute samples", "[libopenshot][framemapper]" ) {
// This test verifies that audio data can be redistributed correctly
// between common and uncommon frame rates
int sample_rate = 48000;
int channels = 2;
int num_seconds = 1;

// Source frame rates (varies the # of samples per frame)
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;
int NUM_SAMPLES = 100;
double angle = 0.0;

// Create cache object to hold test frames
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) {
// Create blank frame (with specific frame #, samples, and channels)
int sample_count = openshot::Frame::GetSamplesPerFrame(frame_number, frame_rate, sample_rate, 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);
audio_buffer[sample_number] = abs(sample_value);
angle += (2 * M_PI) / NUM_SAMPLES;
}

// Add custom audio samples to Frame (bool replaceSamples, int destChannel, int destStartSample, const float* source,
f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2

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

delete[] audio_buffer;
}

openshot::DummyReader r(frame_rate, 1920, 1080, sample_rate, channels, 30.0, &cache);
r.Open();

// Target frame rates
std::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;

// Map to different fps
FrameMapper map(&r, mapped_rate, PULLDOWN_NONE, sample_rate, channels, LAYOUT_STEREO);
map.info.has_audio = true;
map.Open();

// Loop through samples, and verify FrameMapper didn't mess up individual sample values
int num_samples = 0;
for (int frame_index = 1; frame_index <= (map.info.fps.ToInt() * num_seconds); frame_index++) {
int sample_count = map.GetFrame(frame_index)->GetAudioSamplesCount();
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;

// 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);
CHECK(predicted_value == Approx(mapped_value).margin(0.001));
}
// Increment sample value
num_samples += map.GetFrame(frame_index)->GetAudioSamplesCount();
}

float clip_position = 3.77;
int starting_clip_frame = round(clip_position * map.info.fps.ToFloat()) + 1;

// Create Timeline (same specs as reader)
Timeline t1(map.info.width, map.info.height, map.info.fps, map.info.sample_rate, map.info.channels,
map.info.channel_layout);

Clip c1;
c1.Reader(&map);
c1.Layer(1);
c1.Position(clip_position);
c1.Start(0.0);
c1.End(10.0);

// Add clips
t1.AddClip(&c1);
t1.Open();

// Reset SIN wave
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;

// 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);
CHECK(predicted_value == Approx(timeline_value).margin(0.001));
}
}

// Close mapper
map.Close();
t1.Close();
}

// Clean up reader
r.Close();
cache.Clear();

} // for rates
}

TEST_CASE( "Json", "[libopenshot][framemapper]" )
{
DummyReader r(Fraction(30,1), 1280, 720, 48000, 2, 5.0);
Expand Down

0 comments on commit 96e165a

Please sign in to comment.