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 "../gpu/gpu_write_frame.h"
\r
26 #include <core/producer/frame/audio_transform.h>
\r
28 namespace caspar { namespace mixer {
\r
30 struct audio_mixer::implementation
\r
32 std::vector<short> audio_data_;
\r
33 std::stack<core::audio_transform> transform_stack_;
\r
35 std::map<int, core::audio_transform> prev_audio_transforms_;
\r
36 std::map<int, core::audio_transform> next_audio_transforms_;
\r
41 transform_stack_.push(core::audio_transform());
\r
44 void begin(const core::basic_frame& frame)
\r
46 transform_stack_.push(transform_stack_.top()*frame.get_audio_transform());
\r
49 void visit(const core::write_frame& frame)
\r
51 if(!transform_stack_.top().get_has_audio())
\r
54 auto& audio_data = frame.audio_data();
\r
55 auto tag = frame.tag(); // Get the identifier for the audio-stream.
\r
57 if(audio_data_.empty())
\r
58 audio_data_.resize(audio_data.size(), 0);
\r
60 auto next = transform_stack_.top();
\r
63 auto it = prev_audio_transforms_.find(tag);
\r
64 if(it != prev_audio_transforms_.end())
\r
67 next_audio_transforms_[tag] = next; // Store all active tags, inactive tags will be removed in end_pass.
\r
69 auto next_gain = next.get_gain();
\r
70 auto prev_gain = prev.get_gain();
\r
72 if(next_gain < 0.001 && prev_gain < 0.001)
\r
77 tbb::blocked_range<size_t>(0, audio_data.size()),
\r
78 [&](const tbb::blocked_range<size_t>& r)
\r
80 for(size_t n = r.begin(); n < r.end(); ++n)
\r
82 double alpha = static_cast<double>(n)/static_cast<double>(audio_data_.size());
\r
83 double sample_gain = prev_gain * (1.0 - alpha) + next_gain * alpha;
\r
84 int sample = static_cast<int>(audio_data[n]);
\r
85 sample = (static_cast<int>(sample_gain*static_cast<double>(1<<15))*sample)>>15;
\r
86 audio_data_[n] = static_cast<short>((static_cast<int>(audio_data_[n]) + sample) & 0xFFFF);
\r
93 void begin(const core::audio_transform& transform)
\r
95 transform_stack_.push(transform_stack_.top()*transform);
\r
100 transform_stack_.pop();
\r
103 std::vector<short> begin_pass()
\r
105 return std::move(audio_data_);
\r
110 prev_audio_transforms_ = std::move(next_audio_transforms_);
\r
114 audio_mixer::audio_mixer() : impl_(new implementation()){}
\r
115 void audio_mixer::begin(const core::basic_frame& frame){impl_->begin(frame);}
\r
116 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
117 void audio_mixer::end(){impl_->end();}
\r
118 std::vector<short> audio_mixer::begin_pass(){return impl_->begin_pass();}
\r
119 void audio_mixer::end_pass(){impl_->end_pass();}
\r