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