2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 #include "../../stdafx.h"
\r
22 #include "audio_mixer.h"
\r
24 #include <core/mixer/write_frame.h>
\r
25 #include <core/producer/frame/frame_transform.h>
\r
27 #include <tbb/cache_aligned_allocator.h>
\r
29 #include <boost/range.hpp>
\r
30 #include <boost/range/algorithm_ext/push_back.hpp>
\r
38 namespace caspar { namespace core {
\r
43 frame_transform transform;
\r
44 audio_buffer audio_data;
\r
50 audio_item(audio_item&& other)
\r
51 : tag(std::move(other.tag))
\r
52 , transform(std::move(other.transform))
\r
53 , audio_data(std::move(other.audio_data))
\r
58 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
\r
62 frame_transform transform;
\r
63 audio_buffer_ps audio_data;
\r
66 struct audio_mixer::implementation
\r
68 std::stack<core::frame_transform> transform_stack_;
\r
69 std::map<const void*, audio_stream> audio_streams_;
\r
70 std::vector<audio_item> items_;
\r
71 std::vector<size_t> audio_cadence_;
\r
72 video_format_desc format_desc_;
\r
76 : format_desc_(video_format_desc::get(video_format::invalid))
\r
78 transform_stack_.push(core::frame_transform());
\r
81 void begin(core::basic_frame& frame)
\r
83 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());
\r
86 void visit(core::write_frame& frame)
\r
88 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
\r
92 item.tag = frame.tag();
\r
93 item.transform = transform_stack_.top();
\r
94 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
96 items_.push_back(std::move(item));
\r
99 void begin(const core::frame_transform& transform)
\r
101 transform_stack_.push(transform_stack_.top()*transform);
\r
106 transform_stack_.pop();
\r
109 audio_buffer mix(const video_format_desc& format_desc)
\r
111 if(format_desc_ != format_desc)
\r
113 audio_streams_.clear();
\r
114 audio_cadence_ = format_desc.audio_cadence;
\r
115 format_desc_ = format_desc;
\r
118 std::map<const void*, audio_stream> next_audio_streams_;
\r
120 BOOST_FOREACH(auto& item, items_)
\r
122 audio_buffer_ps next_audio;
\r
124 auto next_transform = item.transform;
\r
125 auto prev_transform = next_transform;
\r
127 const auto it = audio_streams_.find(item.tag);
\r
128 if(it != audio_streams_.end())
\r
130 prev_transform = it->second.transform;
\r
131 next_audio = std::move(it->second.audio_data);
\r
134 if(prev_transform.volume < 0.001 && next_transform.volume < 0.001)
\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 audio_buffer_ps result;
\r
143 result.reserve(item.audio_data.size());
\r
144 for(size_t n = 0; n < item.audio_data.size()/2; ++n)
\r
146 result.push_back(item.audio_data[n*2+0] * (prev_volume + n * alpha));
\r
147 result.push_back(item.audio_data[n*2+1] * (prev_volume + n * alpha));
\r
150 boost::range::push_back(next_audio, std::move(result));
\r
152 next_audio_streams_[item.tag].transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
\r
153 next_audio_streams_[item.tag].audio_data = std::move(next_audio);
\r
157 audio_streams_ = std::move(next_audio_streams_);
\r
158 next_audio_streams_.clear();
\r
160 if(audio_streams_.empty())
\r
161 audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);
\r
163 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);
\r
164 BOOST_FOREACH(auto& stream, audio_streams_)
\r
166 auto& audio_data = stream.second.audio_data;
\r
168 if(audio_data.size() < audio_cadence_.front())
\r
170 CASPAR_LOG(warning) << "[audio_mixer] Incorrect frame audio cadence, prepending silence: " << audio_cadence_.front()-audio_data.size();
\r
171 boost::range::push_back(audio_data, audio_buffer_ps(audio_cadence_.front()-audio_data.size(), 0.0f));
\r
174 for(size_t n = 0; n < audio_cadence_.front(); ++n)
\r
175 result_ps[n] += audio_data[n];
\r
177 audio_data.erase(std::begin(audio_data), std::begin(audio_data) + audio_cadence_.front());
\r
180 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
182 audio_buffer result;
\r
183 result.reserve(result_ps.size());
\r
184 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});
\r
189 audio_mixer::audio_mixer() : impl_(new implementation()){}
\r
190 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}
\r
191 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
192 void audio_mixer::end(){impl_->end();}
\r
193 audio_buffer audio_mixer::mix(const video_format_desc& format_desc){return impl_->mix(format_desc);}
\r
194 audio_mixer& audio_mixer::operator=(audio_mixer&& other)
\r
196 impl_ = std::move(other.impl_);
\r