2101e977485c1ed4543b5e77e6600258d3b1c428
[dcpomatic.git] / test / ffmpeg_audio_only_test.cc
1 /*
2     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/film.h"
22 #include "lib/ffmpeg_content.h"
23 #include "lib/dcp_content_type.h"
24 #include "lib/player.h"
25 #include "lib/job_manager.h"
26 #include "lib/audio_buffers.h"
27 #include "test.h"
28 #include <sndfile.h>
29 #include <boost/test/unit_test.hpp>
30 #include <boost/make_shared.hpp>
31
32 using boost::shared_ptr;
33 using boost::make_shared;
34
35 /** Test the FFmpeg code with audio-only content */
36 static void
37 test (boost::filesystem::path file)
38 {
39         shared_ptr<Film> film = new_test_film ("ffmpeg_audio_only_test");
40         film->set_name ("test_film");
41         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
42         shared_ptr<FFmpegContent> c = make_shared<FFmpegContent> (film, file);
43         film->examine_and_add_content (c);
44         wait_for_jobs ();
45         film->write_metadata ();
46
47         /* See if can make a DCP without any errors */
48         film->make_dcp ();
49         wait_for_jobs ();
50         BOOST_CHECK (!JobManager::instance()->errors());
51
52         /* Compare the audio data we read with what libsndfile reads */
53
54         SF_INFO info;
55         info.format = 0;
56         SNDFILE* ref = sf_open (file.string().c_str(), SFM_READ, &info);
57         /* We don't want to test anything that requires resampling */
58         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
59         float* ref_buffer = new float[info.samplerate * info.channels];
60
61         shared_ptr<Player> player = make_shared<Player> (film, film->playlist ());
62
63         for (DCPTime t; t < film->length(); t += DCPTime::from_seconds (1)) {
64                 int const N = sf_readf_float (ref, ref_buffer, info.samplerate);
65                 shared_ptr<AudioBuffers> b = player->get_audio (t, DCPTime::from_frames (N, info.samplerate), true);
66                 for (int i = 0; i < N; ++i) {
67                         switch (info.channels) {
68                         case 1:
69                                 BOOST_CHECK_EQUAL (ref_buffer[i], b->data(2)[i]);
70                                 break;
71                         case 2:
72                                 BOOST_CHECK_EQUAL (ref_buffer[i*2 + 0], b->data(0)[i]);
73                                 BOOST_CHECK_EQUAL (ref_buffer[i*2 + 1], b->data(1)[i]);
74                                 break;
75                         default:
76                                 BOOST_REQUIRE (false);
77                         }
78                 }
79         }
80
81         sf_close (ref);
82 }
83
84 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
85 {
86         /* S16 */
87         test ("test/data/staircase.wav");
88 }
89
90 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
91 {
92         /* S32 1 channel */
93         test ("test/data/sine_440.wav");
94 }