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/parallel_for.h>
\r
32 namespace caspar { namespace core {
\r
37 frame_transform transform;
\r
38 std::vector<int16_t> audio_data;
\r
41 struct audio_mixer::implementation
\r
43 std::stack<core::frame_transform> transform_stack_;
\r
44 std::map<const void*, core::frame_transform> prev_frame_transforms_;
\r
45 const core::video_format_desc format_desc_;
\r
46 std::vector<audio_item> items;
\r
49 implementation(const core::video_format_desc& format_desc)
\r
50 : format_desc_(format_desc)
\r
52 transform_stack_.push(core::frame_transform());
\r
55 void begin(core::basic_frame& frame)
\r
57 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());
\r
60 void visit(const core::write_frame& frame)
\r
62 // We only care about the last field.
\r
63 if(format_desc_.field_mode == field_mode::upper && transform_stack_.top().field_mode == field_mode::upper)
\r
66 if(format_desc_.field_mode == field_mode::lower && transform_stack_.top().field_mode == field_mode::lower)
\r
69 // Skip empty audio.
\r
70 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
\r
74 item.tag = frame.tag();
\r
75 item.transform = transform_stack_.top();
\r
76 item.audio_data = std::vector<int16_t>(frame.audio_data().begin(), frame.audio_data().end());
\r
78 items.push_back(item);
\r
81 void begin(const core::frame_transform& transform)
\r
83 transform_stack_.push(transform_stack_.top()*transform);
\r
88 transform_stack_.pop();
\r
91 std::vector<int16_t> mix()
\r
93 auto result = std::vector<int16_t>(format_desc_.audio_samples_per_frame);
\r
95 std::map<const void*, core::frame_transform> next_frame_transforms;
\r
97 BOOST_FOREACH(auto& item, items)
\r
99 const auto next = item.transform;
\r
102 const auto it = prev_frame_transforms_.find(item.tag);
\r
103 if(it != prev_frame_transforms_.end())
\r
106 next_frame_transforms[item.tag] = next; // Store all active tags, inactive tags will be removed at the end.
\r
108 if(next.volume < 0.001 && prev.volume < 0.001)
\r
111 static const int BASE = 1<<15;
\r
113 const auto next_volume = static_cast<int>(next.volume*BASE);
\r
114 const auto prev_volume = static_cast<int>(prev.volume*BASE);
\r
116 const int n_samples = result.size();
\r
118 const auto in_size = static_cast<size_t>(item.audio_data.size());
\r
119 CASPAR_VERIFY(in_size == 0 || in_size == result.size());
\r
121 if(in_size > result.size())
\r
126 tbb::blocked_range<size_t>(0, item.audio_data.size()),
\r
127 [&](const tbb::blocked_range<size_t>& r)
\r
129 for(size_t n = r.begin(); n < r.end(); ++n)
\r
131 const int sample_volume = (prev_volume - (prev_volume * n)/n_samples) + (next_volume * n)/n_samples;
\r
132 const int sample = (static_cast<int>(item.audio_data[n])*sample_volume)/BASE;
\r
133 result[n] = static_cast<int16_t>((static_cast<int>(result[n]) + sample) & 0xFFFF);
\r
140 prev_frame_transforms_ = std::move(next_frame_transforms);
\r
142 return std::move(result);
\r
146 audio_mixer::audio_mixer(const core::video_format_desc& format_desc) : impl_(new implementation(format_desc)){}
\r
147 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}
\r
148 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
149 void audio_mixer::end(){impl_->end();}
\r
150 std::vector<int16_t> audio_mixer::mix(){return impl_->mix();}
\r
151 audio_mixer& audio_mixer::operator=(audio_mixer&& other)
\r
153 impl_ = std::move(other.impl_);
\r