2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG 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.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
24 #include "audio_mixer.h"
26 #include <core/frame/frame.h>
27 #include <core/frame/frame_transform.h>
28 #include <core/frame/audio_channel_layout.h>
29 #include <core/monitor/monitor.h>
31 #include <common/diagnostics/graph.h>
32 #include <common/linq.h>
34 #include <boost/range/adaptors.hpp>
35 #include <boost/range/distance.hpp>
36 #include <boost/lexical_cast.hpp>
42 namespace caspar { namespace core {
46 const void* tag = nullptr;
47 audio_transform transform;
48 audio_buffer audio_data;
49 audio_channel_layout channel_layout = audio_channel_layout::invalid();
55 audio_item(audio_item&& other)
56 : tag(std::move(other.tag))
57 , transform(std::move(other.transform))
58 , audio_data(std::move(other.audio_data))
59 , channel_layout(std::move(other.channel_layout))
64 typedef cache_aligned_vector<float> audio_buffer_ps;
68 audio_transform prev_transform;
69 audio_buffer_ps audio_data;
70 std::unique_ptr<audio_channel_remapper> channel_remapper;
71 bool remapping_failed = false;
72 bool is_still = false;
75 struct audio_mixer::impl : boost::noncopyable
77 monitor::subject monitor_subject_ { "/audio" };
78 std::stack<core::audio_transform> transform_stack_;
79 std::map<const void*, audio_stream> audio_streams_;
80 std::vector<audio_item> items_;
81 std::vector<int> audio_cadence_;
82 video_format_desc format_desc_;
83 audio_channel_layout channel_layout_ = audio_channel_layout::invalid();
84 float master_volume_ = 1.0f;
85 float previous_master_volume_ = master_volume_;
86 spl::shared_ptr<diagnostics::graph> graph_;
88 impl(spl::shared_ptr<diagnostics::graph> graph)
89 : graph_(std::move(graph))
91 graph_->set_color("volume", diagnostics::color(1.0f, 0.8f, 0.1f));
92 transform_stack_.push(core::audio_transform());
95 void push(const frame_transform& transform)
97 transform_stack_.push(transform_stack_.top()*transform.audio_transform);
100 void visit(const const_frame& frame)
102 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
106 item.tag = frame.stream_tag();
107 item.transform = transform_stack_.top();
108 item.audio_data = frame.audio_data();
109 item.channel_layout = frame.audio_channel_layout();
111 if(item.transform.is_still)
112 item.transform.volume = 0.0;
114 items_.push_back(std::move(item));
117 void begin(const core::audio_transform& transform)
119 transform_stack_.push(transform_stack_.top()*transform);
124 transform_stack_.pop();
127 void set_master_volume(float volume)
129 master_volume_ = volume;
132 float get_master_volume()
134 return master_volume_;
137 audio_buffer mix(const video_format_desc& format_desc, const audio_channel_layout& channel_layout)
139 if(format_desc_ != format_desc || channel_layout_ != channel_layout)
141 audio_streams_.clear();
142 audio_cadence_ = format_desc.audio_cadence;
143 format_desc_ = format_desc;
144 channel_layout_ = channel_layout;
147 std::map<const void*, audio_stream> next_audio_streams;
148 std::vector<const void*> used_tags;
150 for (auto& item : items_)
152 audio_buffer_ps next_audio;
153 std::unique_ptr<audio_channel_remapper> channel_remapper;
154 bool remapping_failed = false;
156 auto next_transform = item.transform;
157 auto prev_transform = next_transform;
161 if(boost::range::find(used_tags, tag) != used_tags.end())
164 used_tags.push_back(tag);
166 const auto it = audio_streams_.find(tag);
167 if (it != audio_streams_.end())
169 prev_transform = it->second.prev_transform;
170 next_audio = std::move(it->second.audio_data);
171 channel_remapper = std::move(it->second.channel_remapper);
172 remapping_failed = it->second.remapping_failed;
175 if (remapping_failed)
177 CASPAR_LOG(trace) << "[audio_mixer] audio channel remapping already failed for stream.";
178 next_audio_streams[tag].remapping_failed = true;
182 // Skip it if there is no existing audio stream and item has no audio-data.
183 if(it == audio_streams_.end() && item.audio_data.empty())
186 if (item.channel_layout == audio_channel_layout::invalid())
188 CASPAR_LOG(debug) << "[audio_mixer] invalid audio channel layout for item";
192 if (!channel_remapper)
196 channel_remapper.reset(new audio_channel_remapper(item.channel_layout, channel_layout_));
200 CASPAR_LOG_CURRENT_EXCEPTION();
201 CASPAR_LOG(error) << "[audio_mixer] audio channel remapping failed for stream.";
202 next_audio_streams[tag].remapping_failed = true;
207 item.audio_data = channel_remapper->mix_and_rearrange(item.audio_data);
209 const float prev_volume = static_cast<float>(prev_transform.volume) * previous_master_volume_;
210 const float next_volume = static_cast<float>(next_transform.volume) * master_volume_;
212 // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.
213 auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/channel_layout_.num_channels);
215 for(size_t n = 0; n < item.audio_data.size(); ++n)
217 auto sample_multiplier = (prev_volume + (n / channel_layout_.num_channels) * alpha);
218 next_audio.push_back(item.audio_data.data()[n] * sample_multiplier);
221 next_audio_streams[tag].prev_transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
222 next_audio_streams[tag].audio_data = std::move(next_audio);
223 next_audio_streams[tag].channel_remapper = std::move(channel_remapper);
224 next_audio_streams[tag].remapping_failed = remapping_failed;
225 next_audio_streams[tag].is_still = item.transform.is_still;
228 previous_master_volume_ = master_volume_;
231 audio_streams_ = std::move(next_audio_streams);
233 if(audio_streams_.empty())
234 audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_size(audio_cadence_.front()), 0.0f);
238 auto nb_invalid_streams = cpplinq::from(audio_streams_)
240 .where([&](const audio_stream& x)
242 return !x.remapping_failed && x.audio_data.size() < audio_size(audio_cadence_.front());
246 if(nb_invalid_streams > 0)
247 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";
250 std::vector<float> result_ps(audio_size(audio_cadence_.front()), 0.0f);
251 for (auto& stream : audio_streams_ | boost::adaptors::map_values)
253 if(stream.audio_data.size() < result_ps.size())
254 stream.audio_data.resize(result_ps.size(), 0.0f);
256 auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
257 stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
260 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
262 auto result_owner = spl::make_shared<mutable_audio_buffer>();
263 auto& result = *result_owner;
264 result.reserve(result_ps.size());
265 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});
267 const int num_channels = channel_layout_.num_channels;
268 monitor_subject_ << monitor::message("/nb_channels") % num_channels;
270 auto max = std::vector<int32_t>(num_channels, std::numeric_limits<int32_t>::min());
272 for (size_t n = 0; n < result.size(); n += num_channels)
273 for (int ch = 0; ch < num_channels; ++ch)
274 max[ch] = std::max(max[ch], std::abs(result[n + ch]));
276 // Makes the dBFS of silence => -dynamic range of 32bit LPCM => about -192 dBFS
277 // Otherwise it would be -infinity
278 static const auto MIN_PFS = 0.5f / static_cast<float>(std::numeric_limits<int32_t>::max());
280 for (int i = 0; i < num_channels; ++i)
282 const auto pFS = max[i] / static_cast<float>(std::numeric_limits<int32_t>::max());
283 const auto dBFS = 20.0f * std::log10(std::max(MIN_PFS, pFS));
285 auto chan_str = boost::lexical_cast<std::string>(i + 1);
287 monitor_subject_ << monitor::message("/" + chan_str + "/pFS") % pFS;
288 monitor_subject_ << monitor::message("/" + chan_str + "/dBFS") % dBFS;
291 graph_->set_value("volume", static_cast<double>(*boost::max_element(max)) / std::numeric_limits<int32_t>::max());
293 return caspar::array<int32_t>(result.data(), result.size(), true, std::move(result_owner));
296 size_t audio_size(size_t num_samples) const
298 return num_samples * channel_layout_.num_channels;
302 audio_mixer::audio_mixer(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
303 void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}
304 void audio_mixer::visit(const const_frame& frame){impl_->visit(frame);}
305 void audio_mixer::pop(){impl_->pop();}
306 void audio_mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
307 float audio_mixer::get_master_volume() { return impl_->get_master_volume(); }
308 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc, const audio_channel_layout& channel_layout){ return impl_->mix(format_desc, channel_layout); }
309 monitor::subject& audio_mixer::monitor_output(){ return impl_->monitor_subject_; }