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
31 namespace caspar { namespace core {
\r
36 frame_transform transform;
\r
37 audio_buffer audio_data;
\r
40 struct audio_mixer::implementation
\r
42 std::stack<core::frame_transform> transform_stack_;
\r
43 std::map<const void*, core::frame_transform> prev_frame_transforms_;
\r
44 std::vector<audio_item> items_;
\r
49 transform_stack_.push(core::frame_transform());
\r
52 void begin(core::basic_frame& frame)
\r
54 transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());
\r
57 void visit(core::write_frame& frame)
\r
59 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
\r
63 item.tag = frame.tag();
\r
64 item.transform = transform_stack_.top();
\r
65 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
67 items_.push_back(item);
\r
70 void begin(const core::frame_transform& transform)
\r
72 transform_stack_.push(transform_stack_.top()*transform);
\r
77 transform_stack_.pop();
\r
80 audio_buffer mix(const video_format_desc& format_desc)
\r
82 CASPAR_ASSERT(format_desc.audio_channels == 2);
\r
83 CASPAR_ASSERT(format_desc.audio_samples_per_frame % 4 == 0);
\r
85 // NOTE: auto data should be larger than format_desc_.audio_samples_per_frame to allow sse to read/write beyond size.
\r
87 auto intermediate = std::vector<float, tbb::cache_aligned_allocator<float>>(format_desc.audio_samples_per_frame+128, 0.0f);
\r
88 auto result = audio_buffer(format_desc.audio_samples_per_frame+128);
\r
89 auto result_128 = reinterpret_cast<__m128i*>(result.data());
\r
91 std::map<const void*, core::frame_transform> next_frame_transforms;
\r
93 BOOST_FOREACH(auto& item, items_)
\r
95 auto next = item.transform;
\r
98 const auto it = prev_frame_transforms_.find(item.tag);
\r
99 if(it != prev_frame_transforms_.end())
\r
102 next_frame_transforms[item.tag] = next; // Store all active tags, inactive tags will be removed at the end.
\r
104 if(prev.volume < 0.001 && next.volume < 0.001)
\r
107 if(item.audio_data.size() != format_desc.audio_samples_per_frame)
\r
110 const float prev_volume = static_cast<float>(prev.volume);
\r
111 const float next_volume = static_cast<float>(next.volume);
\r
113 auto alpha = (next_volume-prev_volume)/static_cast<float>(format_desc.audio_samples_per_frame/format_desc.audio_channels);
\r
114 auto alpha_ps = _mm_set_ps1(alpha*2.0f);
\r
115 auto volume_ps = _mm_setr_ps(prev_volume, prev_volume, prev_volume+alpha, prev_volume+alpha);
\r
117 if(&item != &items_.back())
\r
119 for(size_t n = 0; n < format_desc.audio_samples_per_frame/4; ++n)
\r
121 auto sample_ps = _mm_cvtepi32_ps(_mm_load_si128(reinterpret_cast<__m128i*>(&item.audio_data[n*4])));
\r
122 auto res_sample_ps = _mm_load_ps(&intermediate[n*4]);
\r
123 sample_ps = _mm_mul_ps(sample_ps, volume_ps);
\r
124 res_sample_ps = _mm_add_ps(sample_ps, res_sample_ps);
\r
126 volume_ps = _mm_add_ps(volume_ps, alpha_ps);
\r
128 _mm_store_ps(&intermediate[n*4], res_sample_ps);
\r
133 for(size_t n = 0; n < format_desc.audio_samples_per_frame/4; ++n)
\r
135 auto sample_ps = _mm_cvtepi32_ps(_mm_load_si128(reinterpret_cast<__m128i*>(&item.audio_data[n*4])));
\r
136 auto res_sample_ps = _mm_load_ps(&intermediate[n*4]);
\r
137 sample_ps = _mm_mul_ps(sample_ps, volume_ps);
\r
138 res_sample_ps = _mm_add_ps(sample_ps, res_sample_ps);
\r
140 _mm_stream_si128(result_128++, _mm_cvtps_epi32(res_sample_ps));
\r
146 prev_frame_transforms_ = std::move(next_frame_transforms);
\r
148 result.resize(format_desc.audio_samples_per_frame);
\r
149 return std::move(result);
\r
153 audio_mixer::audio_mixer() : impl_(new implementation()){}
\r
154 void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}
\r
155 void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}
\r
156 void audio_mixer::end(){impl_->end();}
\r
157 audio_buffer audio_mixer::mix(const video_format_desc& format_desc){return impl_->mix(format_desc);}
\r
158 audio_mixer& audio_mixer::operator=(audio_mixer&& other)
\r
160 impl_ = std::move(other.impl_);
\r