Merge pull request #9 from dcbullock/master
[asdcplib.git] / src / AtmosSyncChannel_Generator.h
1 /*
2 Copyright (c) 2013-2013, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    AtmosSyncChannel_Generator.h
28     \version $Id$
29     \brief   Dolby Atmos sync channel generator
30 */
31
32 #ifndef _ATMOSSYNCCHANNEL_GENERATOR_H_
33 #define _ATMOSSYNCCHANNEL_GENERATOR_H_
34
35 #include <AS_DCP.h>
36 #include "SyncEncoder.h"
37 #include "UUIDInformation.h"
38
39 #define INT24_MAX 8388607.0
40 #define INT24_MIN -8388608.0
41
42 namespace ASDCP
43 {
44     namespace ATMOS
45     {
46         static const ui32_t SYNC_CHANNEL = 14;
47     }
48
49     namespace PCM
50     {
51
52         static const ui16_t NUM_BYTES_PER_INT24 = 3;
53
54         class AtmosSyncChannelGenerator
55         {
56             SYNCENCODER  m_syncEncoder;
57             UUIDINFORMATION m_audioTrackUUID;
58             AudioDescriptor m_ADesc;
59             float *m_syncSignalBuffer;
60             ui32_t m_numSamplesPerFrame;
61             ui32_t m_currentFrameNumber;
62             ui32_t m_numBytesPerFrame;
63             bool m_isSyncEncoderInitialized;
64
65             ASDCP_NO_COPY_CONSTRUCT(AtmosSyncChannelGenerator);
66
67         public:
68             /**
69              * Constructor
70              *
71              * @param bitsPerSample the number of bits in each sample of pcm data
72              * @param sampleRate the sampling rate
73              * @param editRate the edit rate of the associated picture track.
74              * @param atmosUUID the UUID of the associated ATMOS track file.
75              *
76              */
77             AtmosSyncChannelGenerator(ui16_t bitsPerSample, ui32_t sampleRate,
78                                const ASDCP::Rational& editRate, const byte_t* uuid);
79             ~AtmosSyncChannelGenerator();
80
81             /**
82              * Set the frame number when seeking
83              * Use override the default starting frame number for a new track or
84              * to set the frame number when doing random access.
85              *
86              * @param frameNumber
87              *
88              */
89             void setFrameNumber(ui32_t frameNumber) { m_currentFrameNumber = frameNumber; };
90
91             /**
92              * Get the number of bytes per frame.
93              *
94              * @return Number of bytes per frame
95              *
96              */
97             ui32_t getBytesPerFrame() { return m_numBytesPerFrame; }
98
99             /**
100              * Generates the next frame of sync data.
101              * Generates the next frame of sync data and places it
102              * the frame buffer. Fails if the buffer is too small.
103              * **Automatically increments the frame number.**
104              *
105              * @param buf the buffer that the generated frame data will be written to.
106              *
107              * @return Kumu::RESULT_OK if the buffer is succesfully filled with sync
108              *  data for the next frame.
109              */
110             Result_t ReadFrame(FrameBuffer& buf);
111
112             /**
113              * Reset the frame count.
114              *
115              * @return Kumu::RESULT_OK
116              */
117             Result_t Reset();
118
119             /**
120              * Fill the AudioDescriptor with the relevant information.
121              *
122              * @return Kumu::RESULT_OK
123              */
124             Result_t FillAudioDescriptor(PCM::AudioDescriptor& ADesc) const;
125
126             /**
127              * Converts a sample float into
128              * 24-bit PCM data.
129              *
130              */
131             static inline i32_t convertSampleFloatToInt24(float sample)
132             {
133                 if (sample >= 0.0)
134                 {
135                     return (static_cast<i32_t>(sample * INT24_MAX) << 8);
136                 }
137                 else
138                 {
139                     return (static_cast<i32_t>(-sample * INT24_MIN) << 8);
140                 }
141             }
142         };
143
144     } // namespace PCM
145 } // namespace ASDCP
146
147 #endif // _ATMOSSYNCCHANNEL_GENERATOR_H_
148
149 //
150 // end AtmosSyncChannel_Generator.h
151 //