]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
2.1.0: -ffmpeg_producer: Removed "audio_resampler" in favor of "swresample" API....
[casparcg] / core / mixer / audio / audio_mixer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "audio_mixer.h"\r
25 \r
26 #include <core/frame/frame.h>\r
27 #include <core/frame/frame_transform.h>\r
28 #include <common/diagnostics/graph.h>\r
29 \r
30 #include <boost/range/adaptors.hpp>\r
31 #include <boost/range/distance.hpp>\r
32 \r
33 #include <map>\r
34 #include <stack>\r
35 #include <vector>\r
36 \r
37 namespace caspar { namespace core {\r
38 \r
39 struct audio_item\r
40 {\r
41         const void*                     tag;\r
42         audio_transform         transform;\r
43         audio_buffer            audio_data;\r
44 \r
45         audio_item()\r
46         {\r
47         }\r
48 \r
49         audio_item(audio_item&& other)\r
50                 : tag(std::move(other.tag))\r
51                 , transform(std::move(other.transform))\r
52                 , audio_data(std::move(other.audio_data))\r
53         {\r
54         }\r
55 };\r
56 \r
57 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;\r
58         \r
59 struct audio_stream\r
60 {\r
61         audio_transform         prev_transform;\r
62         audio_buffer_ps         audio_data;\r
63         bool                            is_still;\r
64 \r
65         audio_stream() \r
66                 : is_still(false)\r
67         {\r
68         }\r
69 };\r
70 \r
71 struct audio_mixer::impl : boost::noncopyable\r
72 {\r
73         std::stack<core::audio_transform>       transform_stack_;\r
74         std::map<const void*, audio_stream>     audio_streams_;\r
75         std::vector<audio_item>                         items_;\r
76         std::vector<int>                                        audio_cadence_;\r
77         video_format_desc                                       format_desc_;\r
78         \r
79 public:\r
80         impl()\r
81         {\r
82                 transform_stack_.push(core::audio_transform());\r
83         }\r
84         \r
85         void push(const frame_transform& transform)\r
86         {\r
87                 transform_stack_.push(transform_stack_.top()*transform.audio_transform);\r
88         }\r
89 \r
90         void visit(const const_frame& frame)\r
91         {\r
92                 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())\r
93                         return;\r
94 \r
95                 audio_item item;\r
96                 item.tag                = frame.stream_tag();\r
97                 item.transform  = transform_stack_.top();\r
98                 item.audio_data = frame.audio_data();\r
99 \r
100                 if(item.transform.is_still)\r
101                         item.transform.volume = 0.0;\r
102                 \r
103                 items_.push_back(std::move(item));              \r
104         }\r
105 \r
106         void begin(const core::audio_transform& transform)\r
107         {\r
108                 transform_stack_.push(transform_stack_.top()*transform);\r
109         }\r
110                 \r
111         void pop()\r
112         {\r
113                 transform_stack_.pop();\r
114         }\r
115         \r
116         audio_buffer mix(const video_format_desc& format_desc)\r
117         {       \r
118                 if(format_desc_ != format_desc)\r
119                 {\r
120                         audio_streams_.clear();\r
121                         audio_cadence_ = format_desc.audio_cadence;\r
122                         format_desc_ = format_desc;\r
123                 }               \r
124                 \r
125                 std::map<const void*, audio_stream>     next_audio_streams;\r
126                 std::vector<const void*> used_tags;\r
127 \r
128                 BOOST_FOREACH(auto& item, items_)\r
129                 {                       \r
130                         audio_buffer_ps next_audio;\r
131 \r
132                         auto next_transform = item.transform;\r
133                         auto prev_transform = next_transform;\r
134 \r
135                         auto tag = item.tag;\r
136 \r
137                         if(boost::range::find(used_tags, tag) != used_tags.end())\r
138                                 continue;\r
139                         \r
140                         used_tags.push_back(tag);\r
141 \r
142                         const auto it = audio_streams_.find(tag);\r
143                         if(it != audio_streams_.end())\r
144                         {       \r
145                                 prev_transform  = it->second.prev_transform;\r
146                                 next_audio              = std::move(it->second.audio_data);\r
147                         }\r
148                         \r
149                         // Skip it if there is no existing audio stream and item has no audio-data.\r
150                         if(it == audio_streams_.end() && item.audio_data.empty()) \r
151                                 continue;\r
152                                                 \r
153                         const float prev_volume = static_cast<float>(prev_transform.volume);\r
154                         const float next_volume = static_cast<float>(next_transform.volume);\r
155                                                 \r
156                         // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.\r
157                         auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);\r
158                         \r
159                         for(size_t n = 0; n < item.audio_data.size(); ++n)\r
160                                 next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));\r
161                                                                                 \r
162                         next_audio_streams[tag].prev_transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.\r
163                         next_audio_streams[tag].audio_data              = std::move(next_audio);        \r
164                         next_audio_streams[tag].is_still                = item.transform.is_still;\r
165                 }                               \r
166 \r
167                 items_.clear();\r
168 \r
169                 audio_streams_ = std::move(next_audio_streams);\r
170                 \r
171                 if(audio_streams_.empty())              \r
172                         audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);\r
173                                 \r
174                 { // sanity check\r
175                         BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)\r
176                         {\r
177                                 if(stream.audio_data.size() < audio_cadence_.front() && !stream.is_still)\r
178                                 {\r
179                                         stream.audio_data.resize(audio_cadence_.front(), 0.0f);\r
180                                         CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected. Appending empty samples.";          \r
181                                 }\r
182                         }               \r
183                 }\r
184 \r
185                 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);\r
186                 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)\r
187                 {\r
188                         CASPAR_ASSERT(stream.audio_data.size() == result_ps.size());\r
189                         auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());\r
190                         stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));\r
191                 }               \r
192                 \r
193                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
194                 \r
195                 audio_buffer result;\r
196                 result.reserve(result_ps.size());\r
197                 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});         \r
198                 \r
199                 return result;\r
200         }\r
201 };\r
202 \r
203 audio_mixer::audio_mixer() : impl_(new impl()){}\r
204 void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}\r
205 void audio_mixer::visit(const const_frame& frame){impl_->visit(frame);}\r
206 void audio_mixer::pop(){impl_->pop();}\r
207 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);}\r
208 \r
209 }}