2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "audio_mixer.h"
\r
26 #include <core/mixer/write_frame.h>
\r
27 #include <core/producer/frame/frame_transform.h>
\r
28 #include <core/monitor/monitor.h>
\r
29 #include <common/diagnostics/graph.h>
\r
30 #include "audio_util.h"
\r
32 #include <tbb/cache_aligned_allocator.h>
\r
34 #include <boost/range/adaptors.hpp>
\r
35 #include <boost/range/distance.hpp>
\r
41 namespace caspar { namespace core {
\r
46 frame_transform transform;
\r
47 audio_buffer audio_data;
\r
53 audio_item(audio_item&& other)
\r
54 : tag(std::move(other.tag))
\r
55 , transform(std::move(other.transform))
\r
56 , audio_data(std::move(other.audio_data))
\r
61 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
\r
65 frame_transform prev_transform;
\r
66 audio_buffer_ps audio_data;
\r
69 struct audio_mixer::implementation
\r
71 safe_ptr<diagnostics::graph> graph_;
\r
72 std::stack<core::frame_transform> transform_stack_;
\r
73 std::map<const void*, audio_stream> audio_streams_;
\r
74 std::vector<audio_item> items_;
\r
75 std::vector<size_t> audio_cadence_;
\r
76 video_format_desc format_desc_;
\r
77 channel_layout channel_layout_;
\r
78 float master_volume_;
\r
79 float previous_master_volume_;
\r
80 monitor::subject monitor_subject_;
\r
83 implementation(const safe_ptr<diagnostics::graph>& graph)
\r
85 , format_desc_(video_format_desc::get(video_format::invalid))
\r
86 , channel_layout_(channel_layout::stereo())
\r
87 , master_volume_(1.0f)
\r
88 , previous_master_volume_(master_volume_)
\r
89 , monitor_subject_(monitor::subject("/audio"))
\r
91 graph_->set_color("volume", diagnostics::color(1.0f, 0.8f, 0.1f));
\r
92 transform_stack_.push(core::frame_transform());
\r
95 void begin(core::basic_frame& frame)
\r
97 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());
\r
100 void visit(core::write_frame& frame)
\r
102 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
\r
106 item.tag = frame.tag();
\r
107 item.transform = transform_stack_.top();
\r
109 if (needs_rearranging(frame.get_channel_layout(), channel_layout_))
\r
111 auto src_view = frame.get_multichannel_view();
\r
113 audio_buffer rearranged_buffer;
\r
114 rearranged_buffer.resize(
\r
115 src_view.num_samples() * channel_layout_.num_channels);
\r
117 auto dst_view = make_multichannel_view<int32_t>(
\r
118 rearranged_buffer.begin(),
\r
119 rearranged_buffer.end(),
\r
122 bool rearrange_success = rearrange_or_rearrange_and_mix(
\r
123 src_view, dst_view, default_mix_config_repository());
\r
125 if (!rearrange_success)
\r
127 failed_rearrange(item.tag, src_view.channel_layout());
\r
130 item.audio_data = std::move(rearranged_buffer);
\r
134 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
137 items_.push_back(std::move(item));
\r
140 void begin(const core::frame_transform& transform)
\r
142 transform_stack_.push(transform_stack_.top()*transform);
\r
147 transform_stack_.pop();
\r
150 float get_master_volume() const
\r
152 return master_volume_;
\r
155 void set_master_volume(float volume)
\r
157 master_volume_ = volume;
\r
160 audio_buffer mix(const video_format_desc& format_desc, const channel_layout& layout)
\r
162 if(format_desc_ != format_desc)
\r
164 audio_streams_.clear();
\r
165 audio_cadence_ = format_desc.audio_cadence;
\r
166 format_desc_ = format_desc;
\r
167 channel_layout_ = layout;
\r
170 std::map<const void*, audio_stream> next_audio_streams;
\r
172 BOOST_FOREACH(auto& item, items_)
\r
174 audio_buffer_ps next_audio;
\r
176 auto next_transform = item.transform;
\r
177 auto prev_transform = next_transform;
\r
179 const auto it = audio_streams_.find(item.tag);
\r
180 if(it != audio_streams_.end())
\r
182 prev_transform = it->second.prev_transform;
\r
183 next_audio = std::move(it->second.audio_data);
\r
186 if(prev_transform.volume < 0.001 && next_transform.volume < 0.001)
\r
189 const float prev_volume = static_cast<float>(prev_transform.volume) * previous_master_volume_;
\r
190 const float next_volume = static_cast<float>(next_transform.volume) * master_volume_;
\r
192 auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/channel_layout_.num_channels);
\r
194 for(size_t n = 0; n < item.audio_data.size(); ++n)
\r
196 auto sample_multiplier = (prev_volume + (n/channel_layout_.num_channels) * alpha);
\r
197 next_audio.push_back(item.audio_data[n] * sample_multiplier);
\r
200 next_audio_streams[item.tag].prev_transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
\r
201 next_audio_streams[item.tag].audio_data = std::move(next_audio);
\r
204 previous_master_volume_ = master_volume_;
\r
207 audio_streams_ = std::move(next_audio_streams);
\r
209 if(audio_streams_.empty())
\r
210 audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_size(audio_cadence_.front()), 0.0f);
\r
214 auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x)
\r
216 return x.audio_data.size() < audio_size(audio_cadence_.front());
\r
219 if(nb_invalid_streams > 0)
\r
220 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";
\r
223 std::vector<float> result_ps(audio_size(audio_cadence_.front()), 0.0f);
\r
225 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)
\r
227 //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size();
\r
229 if(stream.audio_data.size() < result_ps.size())
\r
231 stream.audio_data.resize(result_ps.size(), 0.0f);
\r
232 CASPAR_LOG(trace) << L"[audio_mixer] Appended zero samples";
\r
235 auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
\r
236 stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
\r
239 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
242 audio_buffer result;
\r
243 result.reserve(result_ps.size());
\r
244 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});
\r
246 const int nb_channels = channel_layout_.num_channels;
\r
248 auto min = std::vector<int32_t>(nb_channels, std::numeric_limits<int32_t>::max());
\r
249 auto max = std::vector<int32_t>(nb_channels, std::numeric_limits<int32_t>::min());
\r
251 for (int n = 0; n < boost::lexical_cast<int>(result.size()); n += nb_channels)
\r
253 for (int k = 0; k < nb_channels; ++k)
\r
255 min[k] = std::min(min[k], result[n+k]);
\r
256 max[k] = std::max(max[k], result[n+k]);
\r
260 for (int k = 0; k < nb_channels; ++k)
\r
262 const auto pFS = std::max(-min[k], max[k]) / static_cast<float>(std::numeric_limits<int32_t>::max());
\r
263 const auto dBFS = 20.0f * std::log10(pFS);
\r
265 auto chan_str = boost::lexical_cast<std::string>(k+1);
\r
267 monitor_subject_ << monitor::message("/nb_channels") % nb_channels;
\r
268 monitor_subject_ << monitor::message("/"+ chan_str +"/pFS") % pFS;
\r
269 monitor_subject_ << monitor::message("/"+ chan_str +"/dBFS") % dBFS;
\r
275 size_t audio_size(size_t num_samples) const
\r
277 return num_samples * channel_layout_.num_channels;
\r
280 void failed_rearrange(const void* tag, const channel_layout& layout)
\r
282 if (audio_streams_.find(tag) != audio_streams_.end())
\r
283 return; // We don't want to flood the logs.
\r
285 CASPAR_LOG(warning)
\r
286 << L"[audio_mixer] Could not satisfactory down/upmix from "
\r
287 << layout.name << L" to " << channel_layout_.name
\r
288 << L" because no mix config was found for "
\r
289 << layout.layout_type << L" => " << channel_layout_.layout_type
\r
290 << L". This might cause audio to be lost.";
\r
294 audio_mixer::audio_mixer(const safe_ptr<diagnostics::graph>& graph) : impl_(new implementation(graph)){}
\r
295 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}
\r
296 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
297 void audio_mixer::end(){impl_->end();}
\r
298 float audio_mixer::get_master_volume() const { return impl_->get_master_volume(); }
\r
299 void audio_mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
\r
300 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc, const channel_layout& layout){return impl_->mix(format_desc, layout);}
\r
301 monitor::subject& audio_mixer::monitor_output(){return impl_->monitor_subject_;}
\r