Various tinkerings.
[libdcp.git] / examples / make_dcp.cc
1 /*
2     Copyright (C) 2012-2014 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 /** @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 <libdcp/dcp.h>
29 #include <libdcp/cpl.h>
30 #include <libdcp/mono_picture_asset.h>
31 ... etc. ...
32 */
33
34 #include "dcp.h"
35 #include "cpl.h"
36 #include "mono_picture_mxf.h"
37 #include "mono_picture_mxf_writer.h"
38 #include "sound_mxf.h"
39 #include "sound_mxf_writer.h"
40 #include "reel.h"
41 #include "file.h"
42 #include "reel_mono_picture_asset.h"
43 #include "reel_sound_asset.h"
44
45 int
46 main ()
47 {
48         /* Create a directory to put the DCP in */
49         boost::filesystem::create_directory ("DCP");
50         
51         /* Make a picture MXF.  This is a file which combines JPEG2000 files together to make
52            up the video of the DCP.  First, create the object, specifying a frame rate of 24 frames
53            per second.
54         */
55
56         boost::shared_ptr<dcp::MonoPictureMXF> picture_mxf (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
57
58         /* Start off a write to it */
59         boost::shared_ptr<dcp::PictureMXFWriter> picture_writer = picture_mxf->start_write ("DCP/picture.mxf", dcp::SMPTE, false);
60
61         /* Write 24 frames of the same JPEG2000 file */
62         dcp::File picture ("examples/help.j2c");
63         for (int i = 0; i < 24; ++i) {
64                 picture_writer->write (picture.data(), picture.size());
65         }
66
67         /* And finish off */
68         picture_writer->finalize ();
69
70         /* Now create a sound MXF.  As before, create an object and a writer.
71            When creating the object we specify the sampling rate (48kHz) and the number of channels (2).
72         */
73         boost::shared_ptr<dcp::SoundMXF> sound_mxf (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 2));
74         boost::shared_ptr<dcp::SoundMXFWriter> sound_writer = sound_mxf->start_write ("DCP/sound.mxf", dcp::SMPTE);
75
76         /* Write some sine waves */
77         float* audio[2];
78         audio[0] = new float[48000];
79         audio[1] = new float[48000];
80         for (int i = 0; i < 48000; ++i) {
81                 audio[0][i] = sin (2 * M_PI * i * 440 / 48000) * 0.25;
82                 audio[1][i] = sin (2 * M_PI * i * 880 / 48000) * 0.25;
83         }
84         sound_writer->write (audio, 48000);
85
86         /* And tidy up */
87         delete[] audio[0];
88         delete[] audio[1];
89         sound_writer->finalize ();
90
91         /* Now create a reel */
92         boost::shared_ptr<dcp::Reel> reel (new dcp::Reel ());
93
94         /* Add picture and sound to it.  The zeros are the `entry points', i.e. the first
95            (video) frame from the MXFs that the reel should play.
96         */
97         reel->add (boost::shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelMonoPictureAsset (picture_mxf, 0)));
98         reel->add (boost::shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (sound_mxf, 0)));
99
100         /* Make a CPL with this reel */
101         boost::shared_ptr<dcp::CPL> cpl (new dcp::CPL ("My film", dcp::FEATURE));
102         cpl->add (reel);
103
104         /* Write the DCP */
105         dcp::DCP dcp ("DCP");
106         dcp.add (cpl);
107         dcp.add (picture_mxf);
108         dcp.add (sound_mxf);
109         dcp.write_xml (dcp::SMPTE);
110         
111         return 0;
112 }