]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
2.0.0.2: Some restructuring. Removed gpu_frame_x and moved them into write/read-frame...
[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::deque<std::vector<short>> 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                 // 2 frames delay\r
43                 audio_data_.push_back(std::vector<short>());\r
44                 audio_data_.push_back(std::vector<short>());\r
45         }\r
46         \r
47         void begin(const core::basic_frame& frame)\r
48         {\r
49                 transform_stack_.push(transform_stack_.top()*frame.get_audio_transform());\r
50         }\r
51 \r
52         void visit(const core::write_frame& frame)\r
53         {\r
54                 if(!transform_stack_.top().get_has_audio())\r
55                         return;\r
56 \r
57                 auto& audio_data = frame.audio_data();\r
58                 auto tag = frame.tag(); // Get the identifier for the audio-stream.\r
59 \r
60                 if(audio_data_.back().empty())\r
61                         audio_data_.back().resize(audio_data.size(), 0);\r
62                 \r
63                 auto next = transform_stack_.top();\r
64                 auto prev = next;\r
65 \r
66                 auto it = prev_audio_transforms_.find(tag);\r
67                 if(it != prev_audio_transforms_.end())\r
68                         prev = it->second;\r
69                                 \r
70                 next_audio_transforms_[tag] = next; // Store all active tags, inactive tags will be removed in end_pass.\r
71                 \r
72                 auto next_gain = next.get_gain();\r
73                 auto prev_gain = prev.get_gain();\r
74                 \r
75                 if(next_gain < 0.001 && prev_gain < 0.001)\r
76                         return;\r
77 \r
78                 tbb::parallel_for\r
79                 (\r
80                         tbb::blocked_range<size_t>(0, audio_data.size()),\r
81                         [&](const tbb::blocked_range<size_t>& r)\r
82                         {\r
83                                 for(size_t n = r.begin(); n < r.end(); ++n)\r
84                                 {\r
85                                         double alpha = static_cast<double>(n)/static_cast<double>(audio_data_.back().size());\r
86                                         double sample_gain = prev_gain * (1.0 - alpha) + next_gain * alpha;\r
87                                         int sample = static_cast<int>(audio_data[n]);\r
88                                         sample = (static_cast<int>(sample_gain*static_cast<double>(1<<15))*sample)>>15;\r
89                                         audio_data_.back()[n] = static_cast<short>((static_cast<int>(audio_data_.back()[n]) + sample) & 0xFFFF);\r
90                                 }\r
91                         }\r
92                 );\r
93         }\r
94 \r
95 \r
96         void begin(const core::audio_transform& transform)\r
97         {\r
98                 transform_stack_.push(transform_stack_.top()*transform);\r
99         }\r
100                 \r
101         void end()\r
102         {\r
103                 transform_stack_.pop();\r
104         }\r
105 \r
106         std::vector<short> begin_pass()\r
107         {\r
108                 auto result = std::move(audio_data_.front());\r
109                 audio_data_.pop_front();\r
110                 \r
111                 audio_data_.push_back(std::vector<short>());\r
112 \r
113                 return result;\r
114         }\r
115 \r
116         void end_pass()\r
117         {\r
118                 prev_audio_transforms_ = std::move(next_audio_transforms_);\r
119         }\r
120 };\r
121 \r
122 audio_mixer::audio_mixer() : impl_(new implementation()){}\r
123 void audio_mixer::begin(const core::basic_frame& frame){impl_->begin(frame);}\r
124 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}\r
125 void audio_mixer::end(){impl_->end();}\r
126 std::vector<short> audio_mixer::begin_pass(){return impl_->begin_pass();}       \r
127 void audio_mixer::end_pass(){impl_->end_pass();}\r
128 \r
129 }}