Simplify and test audio sample rate alteration.
authorCarl Hetherington <cth@carlh.net>
Tue, 9 Oct 2012 16:13:28 +0000 (17:13 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 9 Oct 2012 16:13:28 +0000 (17:13 +0100)
src/lib/film_state.cc
test/test.cc

index 3cd7091ca69ebdb62a8706c652dc7113d63f8779..3d58a4fec666dc2e43b20c518cc799994e5fc36a 100644 (file)
@@ -283,18 +283,14 @@ FilmState::bytes_per_sample () const
 int
 FilmState::target_sample_rate () const
 {
+       /* Resample to a DCI-approved sample rate */
        double t = dcp_audio_sample_rate (audio_sample_rate);
+
+       /* Compensate for the fact that video will be rounded to the
+          nearest integer number of frames per second.
+       */
        if (rint (frames_per_second) != frames_per_second) {
-               if (fabs (frames_per_second - 23.976) < 1e-6 || (fabs (frames_per_second - 29.97) < 1e-6)) {
-                       /* 24fps or 30fps drop-frame ie {24,30} * 1000 / 1001 frames per second;
-                          hence we need to resample the audio to dcp_audio_sample_rate * 1000 / 1001
-                          so that when we play it back at dcp_audio_sample_rate it is sped up
-                          by the same amount that the video is
-                       */
-                       t *= double(1000) / 1001;
-               } else {
-                       throw EncodeError ("unknown fractional frame rate");
-               }
+               t *= frames_per_second / rint (frames_per_second);
        }
 
        return rint (t);
index ebee50ac05f131c802d23b3713a643e000796a21..d978d36a0044e507b10aeb2b41f271d67ea8355d 100644 (file)
@@ -362,3 +362,26 @@ BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
 
        BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
 }
+
+BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
+{
+       FilmState fs;
+       fs.frames_per_second = 24;
+
+       fs.audio_sample_rate = 48000;
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
+
+       fs.audio_sample_rate = 44100;
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
+
+       fs.audio_sample_rate = 80000;
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 96000);
+
+       fs.frames_per_second = 23.976;
+       fs.audio_sample_rate = 48000;
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
+
+       fs.frames_per_second = 29.97;
+       fs.audio_sample_rate = 48000;
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
+}