]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
2.0.2: audio_mixer: Fixed incorrect naming.
[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/frame_transform.h>\r
26 \r
27 #include <tbb/cache_aligned_allocator.h>\r
28 \r
29 #include <boost/range/adaptors.hpp>\r
30 #include <boost/range/distance.hpp>\r
31 \r
32 #include <map>\r
33 #include <stack>\r
34 #include <vector>\r
35 \r
36 namespace caspar { namespace core {\r
37 \r
38 struct audio_item\r
39 {\r
40         const void*                     tag;\r
41         frame_transform         transform;\r
42         audio_buffer            audio_data;\r
43 \r
44         audio_item()\r
45         {\r
46         }\r
47 \r
48         audio_item(audio_item&& other)\r
49                 : tag(std::move(other.tag))\r
50                 , transform(std::move(other.transform))\r
51                 , audio_data(std::move(other.audio_data))\r
52         {\r
53         }\r
54 };\r
55 \r
56 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;\r
57         \r
58 struct audio_stream\r
59 {\r
60         frame_transform         transform;\r
61         audio_buffer_ps         audio_data;\r
62 };\r
63 \r
64 struct audio_mixer::implementation\r
65 {\r
66         std::stack<core::frame_transform>       transform_stack_;\r
67         std::map<const void*, audio_stream>     audio_streams_;\r
68         std::vector<audio_item>                         items_;\r
69         std::vector<size_t>                                     audio_cadence_;\r
70         video_format_desc                                       format_desc_;\r
71         \r
72 public:\r
73         implementation()\r
74                 : format_desc_(video_format_desc::get(video_format::invalid))\r
75         {\r
76                 transform_stack_.push(core::frame_transform());\r
77         }\r
78         \r
79         void begin(core::basic_frame& frame)\r
80         {\r
81                 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());\r
82         }\r
83 \r
84         void visit(core::write_frame& frame)\r
85         {\r
86                 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())\r
87                         return;\r
88 \r
89                 audio_item item;\r
90                 item.tag                = frame.tag();\r
91                 item.transform  = transform_stack_.top();\r
92                 item.audio_data = std::move(frame.audio_data()); // Note: We don't need to care about upper/lower since audio_data is removed/moved from the last field.\r
93                 \r
94                 items_.push_back(std::move(item));              \r
95         }\r
96 \r
97         void begin(const core::frame_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         audio_buffer mix(const video_format_desc& format_desc)\r
108         {       \r
109                 if(format_desc_ != format_desc)\r
110                 {\r
111                         audio_streams_.clear();\r
112                         audio_cadence_ = format_desc.audio_cadence;\r
113                         format_desc_ = format_desc;\r
114                 }               \r
115                 \r
116                 std::map<const void*, audio_stream>     next_audio_streams;\r
117 \r
118                 BOOST_FOREACH(auto& item, items_)\r
119                 {                       \r
120                         audio_buffer_ps next_audio;\r
121 \r
122                         auto next_transform = item.transform;\r
123                         auto prev_transform = next_transform;\r
124 \r
125                         const auto it = audio_streams_.find(item.tag);\r
126                         if(it != audio_streams_.end())\r
127                         {       \r
128                                 prev_transform  = it->second.transform;\r
129                                 next_audio              = std::move(it->second.audio_data);\r
130                         }\r
131 \r
132                         if(prev_transform.volume < 0.001 && next_transform.volume < 0.001)\r
133                                 continue;\r
134                         \r
135                         const float prev_volume = static_cast<float>(prev_transform.volume);\r
136                         const float next_volume = static_cast<float>(next_transform.volume);\r
137                                                                         \r
138                         auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);\r
139                         \r
140                         for(size_t n = 0; n < item.audio_data.size(); ++n)\r
141                                 next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));\r
142                                                                                 \r
143                         next_audio_streams[item.tag].transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.\r
144                         next_audio_streams[item.tag].audio_data = std::move(next_audio);                        \r
145                 }                               \r
146 \r
147                 items_.clear();\r
148 \r
149                 audio_streams_ = std::move(next_audio_streams);\r
150                 \r
151                 if(audio_streams_.empty())              \r
152                         audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);\r
153                                 \r
154                 { // sanity check\r
155 \r
156                         auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x)\r
157                         {\r
158                                 return x.audio_data.size() < audio_cadence_.front();\r
159                         });\r
160 \r
161                         if(nb_invalid_streams > 0)              \r
162                                 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";                   \r
163                 }\r
164 \r
165                 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);\r
166                 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)\r
167                 {\r
168                         auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());\r
169                         stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));\r
170                 }               \r
171                 \r
172                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
173                 \r
174                 audio_buffer result;\r
175                 result.reserve(result_ps.size());\r
176                 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});                                         \r
177                 return result;\r
178         }\r
179 };\r
180 \r
181 audio_mixer::audio_mixer() : impl_(new implementation()){}\r
182 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}\r
183 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}\r
184 void audio_mixer::end(){impl_->end();}\r
185 audio_buffer audio_mixer::mix(const video_format_desc& format_desc){return impl_->mix(format_desc);}\r
186 audio_mixer& audio_mixer::operator=(audio_mixer&& other)\r
187 {\r
188         impl_ = std::move(other.impl_);\r
189         return *this;\r
190 }\r
191 \r
192 }}