const fix.
[dcpomatic.git] / src / lib / audio_decoder.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "audio_decoder.h"
22 #include "audio_buffers.h"
23 #include "audio_content.h"
24 #include "log.h"
25 #include "compose.hpp"
26 #include <boost/foreach.hpp>
27 #include <iostream>
28
29 #include "i18n.h"
30
31 using std::cout;
32 using std::map;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, shared_ptr<Log> log)
37         : DecoderPart (parent, log)
38 {
39         BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
40                 _positions[i] = 0;
41         }
42 }
43
44 void
45 AudioDecoder::emit (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
46 {
47         if (ignore ()) {
48                 return;
49         }
50
51         if (_positions[stream] == 0) {
52                 _positions[stream] = time.frames_round (stream->frame_rate ());
53         }
54
55         Data (stream, ContentAudio (data, _positions[stream]));
56         _positions[stream] += data->frames();
57 }
58
59 ContentTime
60 AudioDecoder::position () const
61 {
62         optional<ContentTime> p;
63         for (map<AudioStreamPtr, Frame>::const_iterator i = _positions.begin(); i != _positions.end(); ++i) {
64                 ContentTime const ct = ContentTime::from_frames (i->second, i->first->frame_rate ());
65                 if (!p || ct < *p) {
66                         p = ct;
67                 }
68         }
69
70         return p.get_value_or(ContentTime());
71 }
72
73 void
74 AudioDecoder::seek ()
75 {
76         for (map<AudioStreamPtr, Frame>::iterator i = _positions.begin(); i != _positions.end(); ++i) {
77                 i->second = 0;
78         }
79 }