]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
2.0.0.2: Improved int type size safety.
[casparcg] / core / mixer / audio / audio_mixer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "audio_mixer.h"\r
23 \r
24 #include <core/mixer/write_frame.h>\r
25 #include <core/producer/frame/audio_transform.h>\r
26 \r
27 namespace caspar { namespace core {\r
28         \r
29 struct audio_mixer::implementation\r
30 {\r
31         std::vector<int16_t> audio_data_;\r
32         std::stack<core::audio_transform> transform_stack_;\r
33 \r
34         std::map<int, core::audio_transform> prev_audio_transforms_;\r
35         std::map<int, core::audio_transform> next_audio_transforms_;\r
36 \r
37 public:\r
38         implementation()\r
39         {\r
40                 transform_stack_.push(core::audio_transform());\r
41         }\r
42         \r
43         void begin(const core::basic_frame& frame)\r
44         {\r
45                 transform_stack_.push(transform_stack_.top()*frame.get_audio_transform());\r
46         }\r
47 \r
48         void visit(const core::write_frame& frame)\r
49         {\r
50                 if(!transform_stack_.top().get_has_audio())\r
51                         return;\r
52 \r
53                 auto& audio_data = frame.audio_data();\r
54                 auto tag = frame.tag(); // Get the identifier for the audio-stream.\r
55 \r
56                 if(audio_data_.empty())\r
57                         audio_data_.resize(audio_data.size(), 0);\r
58                 \r
59                 auto next = transform_stack_.top();\r
60                 auto prev = next;\r
61 \r
62                 auto it = prev_audio_transforms_.find(tag);\r
63                 if(it != prev_audio_transforms_.end())\r
64                         prev = it->second;\r
65                                 \r
66                 next_audio_transforms_[tag] = next; // Store all active tags, inactive tags will be removed in end_pass.\r
67                 \r
68                 \r
69                 if(next.get_gain() < 0.001 && prev.get_gain() < 0.001)\r
70                         return;\r
71                 \r
72                 static const int BASE = 1<<15;\r
73 \r
74                 auto next_gain = static_cast<int32_t>(next.get_gain()*BASE);\r
75                 auto prev_gain = static_cast<int32_t>(prev.get_gain()*BASE);\r
76                 \r
77                 int n_samples = audio_data_.size();\r
78 \r
79                 tbb::parallel_for\r
80                 (\r
81                         tbb::blocked_range<size_t>(0, audio_data.size()),\r
82                         [&](const tbb::blocked_range<size_t>& r)\r
83                         {\r
84                                 for(size_t n = r.begin(); n < r.end(); ++n)\r
85                                 {\r
86                                         int sample_gain = (prev_gain - (prev_gain * n)/n_samples) + (next_gain * n)/n_samples;\r
87                                         \r
88                                         int sample = (static_cast<int32_t>(audio_data[n])*sample_gain)/BASE;\r
89                                         \r
90                                         audio_data_[n] = static_cast<int16_t>((static_cast<int32_t>(audio_data_[n]) + sample) & 0xFFFF);\r
91                                 }\r
92                         }\r
93                 );\r
94         }\r
95 \r
96 \r
97         void begin(const core::audio_transform& transform)\r
98         {\r
99                 transform_stack_.push(transform_stack_.top()*transform);\r
100         }\r
101                 \r
102         void end()\r
103         {\r
104                 transform_stack_.pop();\r
105         }\r
106 \r
107 \r
108         std::vector<short> mix()\r
109         {\r
110                 prev_audio_transforms_ = std::move(next_audio_transforms_);     \r
111                 return std::move(audio_data_);\r
112         }\r
113 };\r
114 \r
115 audio_mixer::audio_mixer() : impl_(new implementation()){}\r
116 void audio_mixer::begin(const core::basic_frame& frame){impl_->begin(frame);}\r
117 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}\r
118 void audio_mixer::end(){impl_->end();}\r
119 std::vector<short> audio_mixer::mix(){return impl_->mix();}\r
120 \r
121 }}