More end-to-end sound tests.
authorCarl Hetherington <cth@carlh.net>
Sat, 2 Sep 2017 22:02:35 +0000 (23:02 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 2 Sep 2017 22:02:35 +0000 (23:02 +0100)
test/ffmpeg_audio_only_test.cc
test/remake_id_test.cc
test/test.cc
test/test.h

index ebfffc3ebbba8ad729cd61683a192ab34b27cc72..bbeef0b11d03d1cac1d2ed9929ada0593082d3e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 #include "lib/job_manager.h"
 #include "lib/audio_buffers.h"
 #include "test.h"
+#include <dcp/sound_asset.h>
+#include <dcp/sound_asset_reader.h>
 #include <sndfile.h>
 #include <boost/test/unit_test.hpp>
+#include <iostream>
 
+using std::min;
 using boost::shared_ptr;
 
 static SNDFILE* ref = 0;
@@ -62,7 +66,7 @@ audio (boost::shared_ptr<AudioBuffers> audio, int channels)
 }
 
 /** Test the FFmpeg code with audio-only content */
-static void
+static shared_ptr<Film>
 test (boost::filesystem::path file)
 {
        shared_ptr<Film> film = new_test_film ("ffmpeg_audio_only_test");
@@ -78,7 +82,7 @@ test (boost::filesystem::path file)
        wait_for_jobs ();
        BOOST_CHECK (!JobManager::instance()->errors());
 
-       /* Compare the audio data we read with what libsndfile reads */
+       /* Compare the audio data player reads with what libsndfile reads */
 
        SF_INFO info;
        info.format = 0;
@@ -94,22 +98,105 @@ test (boost::filesystem::path file)
        while (!player->pass ()) {}
 
        sf_close (ref);
+       delete[] ref_buffer;
+
+       return film;
 }
 
 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
 {
        /* S16 */
-       test ("test/data/staircase.wav");
+       shared_ptr<Film> film = test ("test/data/staircase.wav");
+
+       /* Compare the audio data in the DCP with what libsndfile reads */
+
+       SF_INFO info;
+       info.format = 0;
+       ref = sf_open ("test/data/staircase.wav", SFM_READ, &info);
+       /* We don't want to test anything that requires resampling */
+       BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
+
+       int16_t* buffer = new int16_t[info.channels * 2000];
+
+       dcp::SoundAsset asset (dcp_file(film, "pcm"));
+       shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
+       for (int i = 0; i < asset.intrinsic_duration(); ++i) {
+               shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
+               sf_count_t this_time = min (info.frames, 2000L);
+               sf_readf_short (ref, buffer, this_time);
+               for (int j = 0; j < this_time; ++j) {
+                       BOOST_REQUIRE_EQUAL (frame->get(2, j) >> 8, buffer[j]);
+               }
+               info.frames -= this_time;
+       }
+
+       delete[] buffer;
 }
 
 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
 {
        /* S32 1 channel */
-       test ("test/data/sine_440.wav");
+       shared_ptr<Film> film = test ("test/data/sine_440.wav");
+
+       /* Compare the audio data in the DCP with what libsndfile reads */
+
+       SF_INFO info;
+       info.format = 0;
+       ref = sf_open ("test/data/sine_440.wav", SFM_READ, &info);
+       /* We don't want to test anything that requires resampling */
+       BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
+
+       int32_t* buffer = new int32_t[info.channels * 2000];
+
+       dcp::SoundAsset asset (dcp_file(film, "pcm"));
+       shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
+       for (int i = 0; i < asset.intrinsic_duration(); ++i) {
+               shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
+               sf_count_t this_time = min (info.frames, 2000L);
+               sf_readf_int (ref, buffer, this_time);
+               for (int j = 0; j < this_time; ++j) {
+                       int32_t s = frame->get(2, j);
+                       if (s > (1 << 23)) {
+                               s -= (1 << 24);
+                       }
+                       BOOST_REQUIRE_MESSAGE (abs(s - (buffer[j] / 256)) <= 1, "failed on asset frame " << i << " sample " << j);
+               }
+               info.frames -= this_time;
+       }
+
+       delete[] buffer;
 }
 
 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test3)
 {
        /* S24 1 channel */
-       test ("test/data/sine_24_48_440.wav");
+       shared_ptr<Film> film = test ("test/data/sine_24_48_440.wav");
+
+       /* Compare the audio data in the DCP with what libsndfile reads */
+
+       SF_INFO info;
+       info.format = 0;
+       ref = sf_open ("test/data/sine_24_48_440.wav", SFM_READ, &info);
+       /* We don't want to test anything that requires resampling */
+       BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
+
+       int32_t* buffer = new int32_t[info.channels * 2000];
+
+       dcp::SoundAsset asset (dcp_file(film, "pcm"));
+       shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
+       for (int i = 0; i < asset.intrinsic_duration(); ++i) {
+               shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
+               sf_count_t this_time = min (info.frames, 2000L);
+               sf_readf_int (ref, buffer, this_time);
+               for (int j = 0; j < this_time; ++j) {
+                       int32_t s = frame->get(2, j);
+                       if (s > (1 << 23)) {
+                               s -= (1 << 24);
+                       }
+                       BOOST_REQUIRE_MESSAGE (abs(s - buffer[j] /256) <= 1, "failed on asset frame " << i << " sample " << j);
+               }
+               info.frames -= this_time;
+       }
+
+       delete[] buffer;
 }
index d8bb5df830fcbbe8c75c6886a90bf458f5b5336b..8d924d7026561d9c920b05e0c91a1319162fa24d 100644 (file)
@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE (remake_id_test)
        BOOST_REQUIRE (!wait_for_jobs ());
 
        /* Copy the video file */
-       boost::filesystem::path first_video = video_file(film);
+       boost::filesystem::path first_video = dcp_file(film, "j2c");
        boost::filesystem::copy_file (first_video, first_video.string() + ".copy");
 
        /* Make a new DCP with the same video file */
index 9027b8b71bf139576f8805d9b2c882b8aeb1aeaa..95d84bcc907f92d62c6fe87a60a672e9cf999583 100644 (file)
@@ -492,10 +492,10 @@ check_one_frame (boost::filesystem::path dcp_dir, int64_t index, boost::filesyst
 }
 
 boost::filesystem::path
-video_file (shared_ptr<const Film> film)
+dcp_file (shared_ptr<const Film> film, string prefix)
 {
        boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->dir(film->dcp_name()));
-       while (i != boost::filesystem::directory_iterator() && !boost::algorithm::starts_with (i->path().leaf().string(), "j2c")) {
+       while (i != boost::filesystem::directory_iterator() && !boost::algorithm::starts_with (i->path().leaf().string(), prefix)) {
                ++i;
        }
 
index 75ab7b7f6b23b23aa5106ada6aaa5d68592b1602..b0fe648f32410cc9c64550e3fd653d2ed55a26b2 100644 (file)
@@ -38,5 +38,5 @@ extern void check_ffmpeg (boost::filesystem::path, boost::filesystem::path, int
 extern void check_image (boost::filesystem::path, boost::filesystem::path);
 extern boost::filesystem::path test_film_dir (std::string);
 extern void write_image (boost::shared_ptr<const Image> image, boost::filesystem::path file, std::string format);
-boost::filesystem::path video_file (boost::shared_ptr<const Film> film);
+boost::filesystem::path dcp_file (boost::shared_ptr<const Film> film, std::string prefix);
 void check_one_frame (boost::filesystem::path dcp, int64_t index, boost::filesystem::path ref);