734ae880329d92eb22cdb4011092ad745c0d1b6d
[libdcp.git] / examples / make_dcp.cc
1 /*
2     Copyright (C) 2012-2014 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 /** @file examples/make_dcp.cc
21  *  @brief Shows how to make a DCP from some JPEG2000 and audio data.
22  */
23
24 #include <vector>
25 #include <string>
26
27 /* If you are using an installed libdcp, these #includes would need to be changed to
28 #include <dcp/dcp.h>
29 #include <dcp/cpl.h>
30 #include <dcp/mono_picture_asset.h>
31 ... etc. ...
32 */
33
34 #include "dcp.h"
35 #include "cpl.h"
36 #include "mono_picture_asset.h"
37 #include "mono_picture_asset_writer.h"
38 #include "sound_asset.h"
39 #include "sound_asset_writer.h"
40 #include "reel.h"
41 #include "file.h"
42 #include "reel_mono_picture_asset.h"
43 #include "reel_sound_asset.h"
44 #include <cmath>
45
46 int
47 main ()
48 {
49         /* Create a directory to put the DCP in */
50         boost::filesystem::create_directory ("DCP");
51
52         /* Make a picture asset.  This is a file which combines JPEG2000 files together to make
53            up the video of the DCP.  First, create the object, specifying a frame rate of 24 frames
54            per second.
55         */
56
57         boost::shared_ptr<dcp::MonoPictureAsset> picture_asset (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
58
59         /* Start off a write to it */
60         boost::shared_ptr<dcp::PictureAssetWriter> picture_writer = picture_asset->start_write ("DCP/picture.mxf", false);
61
62         /* Write 24 frames of the same JPEG2000 file */
63         dcp::File picture ("examples/help.j2c");
64         for (int i = 0; i < 24; ++i) {
65                 picture_writer->write (picture.data(), picture.size());
66         }
67
68         /* And finish off */
69         picture_writer->finalize ();
70
71         /* Now create a sound MXF.  As before, create an object and a writer.
72            When creating the object we specify the sampling rate (48kHz) and the number of channels (2).
73         */
74         boost::shared_ptr<dcp::SoundAsset> sound_asset (new dcp::SoundAsset(dcp::Fraction(24, 1), 48000, 2, dcp::LanguageTag("en-GB"), dcp::SMPTE));
75         /* Here we must also say which of our channels will have "real" sound data in them */
76         std::vector<dcp::Channel> active_channels;
77         active_channels.push_back (dcp::LEFT);
78         active_channels.push_back (dcp::RIGHT);
79         boost::shared_ptr<dcp::SoundAssetWriter> sound_writer = sound_asset->start_write ("DCP/sound.mxf", active_channels);
80
81         /* Write some sine waves */
82         float* audio[2];
83         audio[0] = new float[48000];
84         audio[1] = new float[48000];
85         for (int i = 0; i < 48000; ++i) {
86                 audio[0][i] = sin (2 * M_PI * i * 440 / 48000) * 0.25;
87                 audio[1][i] = sin (2 * M_PI * i * 880 / 48000) * 0.25;
88         }
89         sound_writer->write (audio, 48000);
90
91         /* And tidy up */
92         delete[] audio[0];
93         delete[] audio[1];
94         sound_writer->finalize ();
95
96         /* Now create a reel */
97         boost::shared_ptr<dcp::Reel> reel (new dcp::Reel ());
98
99         /* Add picture and sound to it.  The zeros are the `entry points', i.e. the first
100            (video) frame from the assets that the reel should play.
101         */
102         reel->add (boost::shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelMonoPictureAsset (picture_asset, 0)));
103         reel->add (boost::shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (sound_asset, 0)));
104
105         /* Make a CPL with this reel */
106         boost::shared_ptr<dcp::CPL> cpl (new dcp::CPL ("My film", dcp::FEATURE));
107         cpl->add (reel);
108
109         /* Write the DCP */
110         dcp::DCP dcp ("DCP");
111         dcp.add (cpl);
112         dcp.write_xml (dcp::SMPTE);
113
114         return 0;
115 }