A number of SMPTE subtitle syntax fixes.
[libdcp.git] / test / sound_frame_test.cc
1 /*
2     Copyright (C) 2015 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 <boost/test/unit_test.hpp>
21 #include "test.h"
22 #include "sound_frame.h"
23 #include "exceptions.h"
24 #include <sndfile.h>
25
26 BOOST_AUTO_TEST_CASE (sound_frame_test)
27 {
28         int const frame_length = 2000;
29         int const channels = 6;
30
31         dcp::SoundFrame frame (
32                 private_test / "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV/pcm_95734608-5d47-4d3f-bf5f-9e9186b66afa_.mxf",
33                 42,
34                 0
35                 );
36
37         BOOST_REQUIRE_EQUAL (frame.size(), channels * frame_length * 3);
38
39         boost::filesystem::path ref_file = private_test / "data" / "frame.wav";
40         SF_INFO info;
41         info.format = 0;
42         SNDFILE* sndfile = sf_open (ref_file.string().c_str(), SFM_READ, &info);
43         BOOST_REQUIRE (sndfile);
44
45         int ref_data[frame_length * channels];
46         int const read = sf_readf_int (sndfile, ref_data, frame_length);
47         BOOST_REQUIRE_EQUAL (read, frame_length);
48
49         uint8_t const * p = frame.data ();
50         for (int i = 0; i < (frame_length * channels); ++i) {
51                 int x = ref_data[i] >> 8;
52                 if (x < 0) {
53                         x = (1 << 24) + x;
54                 }
55                 int const y = p[0] | (p[1] << 8) | (p[2] << 16);
56                 BOOST_REQUIRE_EQUAL (x, y);
57                 p += 3;
58         }
59 }
60
61 BOOST_AUTO_TEST_CASE (sound_frame_test2)
62 {
63         BOOST_CHECK_THROW (dcp::SoundFrame ("frobozz", 42, 0), dcp::FileError);
64         BOOST_CHECK_THROW (dcp::SoundFrame (
65                                    private_test /
66                                    "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV/pcm_95734608-5d47-4d3f-bf5f-9e9186b66afa_.mxf",
67                                    999999999, 0
68                                    ), dcp::DCPReadError
69                 );
70 }