2 * Copyright (c) 2011 Sveriges Television AB <info@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 <common/diagnostics/graph.h>
\r
30 #include <tbb/cache_aligned_allocator.h>
\r
32 #include <boost/range/adaptors.hpp>
\r
33 #include <boost/range/distance.hpp>
\r
39 namespace caspar { namespace core {
\r
44 frame_transform transform;
\r
45 audio_buffer audio_data;
\r
51 audio_item(audio_item&& other)
\r
52 : tag(std::move(other.tag))
\r
53 , transform(std::move(other.transform))
\r
54 , audio_data(std::move(other.audio_data))
\r
59 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
\r
63 frame_transform prev_transform;
\r
64 audio_buffer_ps audio_data;
\r
67 struct audio_mixer::implementation
\r
69 safe_ptr<diagnostics::graph> graph_;
\r
70 std::stack<core::frame_transform> transform_stack_;
\r
71 std::map<const void*, audio_stream> audio_streams_;
\r
72 std::vector<audio_item> items_;
\r
73 std::vector<int> audio_cadence_;
\r
74 video_format_desc format_desc_;
\r
77 implementation(const safe_ptr<diagnostics::graph>& graph)
\r
80 graph_->set_color("volume", diagnostics::color(1.0f, 0.8f, 0.1f));
\r
81 transform_stack_.push(core::frame_transform());
\r
84 void begin(core::basic_frame& frame)
\r
86 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());
\r
89 void visit(core::write_frame& frame)
\r
91 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
\r
95 item.tag = frame.tag();
\r
96 item.transform = transform_stack_.top();
\r
97 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
99 items_.push_back(std::move(item));
\r
102 void begin(const core::frame_transform& transform)
\r
104 transform_stack_.push(transform_stack_.top()*transform);
\r
109 transform_stack_.pop();
\r
112 audio_buffer mix(const video_format_desc& format_desc)
\r
114 if(format_desc_ != format_desc)
\r
116 audio_streams_.clear();
\r
117 audio_cadence_ = format_desc.audio_cadence;
\r
118 format_desc_ = format_desc;
\r
121 std::map<const void*, audio_stream> next_audio_streams;
\r
123 BOOST_FOREACH(auto& item, items_)
\r
125 audio_buffer_ps next_audio;
\r
127 auto next_transform = item.transform;
\r
128 auto prev_transform = next_transform;
\r
130 const auto it = audio_streams_.find(item.tag);
\r
131 if(it != audio_streams_.end())
\r
133 prev_transform = it->second.prev_transform;
\r
134 next_audio = std::move(it->second.audio_data);
\r
137 const float prev_volume = static_cast<float>(prev_transform.volume);
\r
138 const float next_volume = static_cast<float>(next_transform.volume);
\r
140 auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);
\r
142 for(size_t n = 0; n < item.audio_data.size(); ++n)
\r
143 next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));
\r
145 next_audio_streams[item.tag].prev_transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
\r
146 next_audio_streams[item.tag].audio_data = std::move(next_audio);
\r
151 audio_streams_ = std::move(next_audio_streams);
\r
153 if(audio_streams_.empty())
\r
154 audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);
\r
158 auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x)
\r
160 return x.audio_data.size() < audio_cadence_.front();
\r
163 if(nb_invalid_streams > 0)
\r
164 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";
\r
167 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);
\r
168 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)
\r
170 //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size();
\r
172 if(stream.audio_data.size() < result_ps.size())
\r
174 stream.audio_data.resize(result_ps.size(), 0.0f);
\r
175 CASPAR_LOG(trace) << L"[audio_mixer] Appended zero samples";
\r
178 auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
\r
179 stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
\r
182 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
184 audio_buffer result;
\r
185 result.reserve(result_ps.size());
\r
186 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});
\r
188 auto max = boost::range::max_element(result);
\r
190 graph_->set_value("volume", static_cast<double>(std::abs(*max))/std::numeric_limits<int32_t>::max());
\r
196 audio_mixer::audio_mixer(const safe_ptr<diagnostics::graph>& graph) : impl_(new implementation(graph)){}
\r
197 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}
\r
198 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
199 void audio_mixer::end(){impl_->end();}
\r
200 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);}
\r