Bump waf to 2.0.27.
[libdcp.git] / test / sound_frame_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include "test.h"
22 #include "sound_frame.h"
23 #include "sound_asset.h"
24 #include "sound_asset_reader.h"
25 #include "exceptions.h"
26 #include <sndfile.h>
27
28 using boost::shared_ptr;
29
30 BOOST_AUTO_TEST_CASE (sound_frame_test)
31 {
32         int const frame_length = 2000;
33         int const channels = 6;
34
35         dcp::SoundAsset asset (
36                 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"
37                 );
38
39         shared_ptr<const dcp::SoundFrame> frame = asset.start_read()->get_frame(42);
40
41         BOOST_REQUIRE_EQUAL (frame->size(), channels * frame_length * 3);
42
43         boost::filesystem::path ref_file = private_test / "data" / "frame.wav";
44         SF_INFO info;
45         info.format = 0;
46         SNDFILE* sndfile = sf_open (ref_file.string().c_str(), SFM_READ, &info);
47         BOOST_REQUIRE (sndfile);
48
49         int ref_data[frame_length * channels];
50         int const read = sf_readf_int (sndfile, ref_data, frame_length);
51         BOOST_REQUIRE_EQUAL (read, frame_length);
52
53         uint8_t const * p = frame->data ();
54         for (int i = 0; i < (frame_length * channels); ++i) {
55                 int x = ref_data[i] >> 8;
56                 if (x < 0) {
57                         x = (1 << 24) + x;
58                 }
59                 int const y = p[0] | (p[1] << 8) | (p[2] << 16);
60                 BOOST_REQUIRE_EQUAL (x, y);
61                 p += 3;
62         }
63 }
64
65 BOOST_AUTO_TEST_CASE (sound_frame_test2)
66 {
67         BOOST_CHECK_THROW (dcp::SoundAsset("frobozz"), dcp::FileError);
68
69         dcp::SoundAsset asset (
70                 private_test /
71                 "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV/pcm_95734608-5d47-4d3f-bf5f-9e9186b66afa_.mxf"
72                 );
73
74         BOOST_CHECK_THROW (asset.start_read()->get_frame (99999999), dcp::DCPReadError);
75 }