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/frame/frame.h>
\r
27 #include <core/frame/frame_transform.h>
\r
28 #include <common/diagnostics/graph.h>
\r
30 #include <boost/range/adaptors.hpp>
\r
31 #include <boost/range/distance.hpp>
\r
37 namespace caspar { namespace core {
\r
42 audio_transform transform;
\r
43 audio_buffer audio_data;
\r
49 audio_item(audio_item&& other)
\r
50 : tag(std::move(other.tag))
\r
51 , transform(std::move(other.transform))
\r
52 , audio_data(std::move(other.audio_data))
\r
57 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
\r
61 audio_transform prev_transform;
\r
62 audio_buffer_ps audio_data;
\r
65 struct audio_mixer::impl : boost::noncopyable
\r
67 std::stack<core::audio_transform> transform_stack_;
\r
68 std::map<const void*, audio_stream> audio_streams_;
\r
69 std::vector<audio_item> items_;
\r
70 std::vector<int> audio_cadence_;
\r
71 video_format_desc format_desc_;
\r
76 transform_stack_.push(core::audio_transform());
\r
79 void push(const frame_transform& transform)
\r
81 transform_stack_.push(transform_stack_.top()*transform.audio_transform);
\r
84 void visit(const mutable_frame& frame)
\r
87 item.tag = frame.tag();
\r
88 item.transform = transform_stack_.top();
\r
89 item.audio_data = frame.audio_data();
\r
91 items_.push_back(std::move(item));
\r
94 void begin(const core::audio_transform& transform)
\r
96 transform_stack_.push(transform_stack_.top()*transform);
\r
101 transform_stack_.pop();
\r
104 audio_buffer mix(const video_format_desc& format_desc)
\r
106 if(format_desc_ != format_desc)
\r
108 audio_streams_.clear();
\r
109 audio_cadence_ = format_desc.audio_cadence;
\r
110 format_desc_ = format_desc;
\r
113 std::map<const void*, audio_stream> next_audio_streams;
\r
114 std::vector<const void*> used_tags;
\r
116 BOOST_FOREACH(auto& item, items_)
\r
118 audio_buffer_ps next_audio;
\r
120 auto next_transform = item.transform;
\r
121 auto prev_transform = next_transform;
\r
123 auto tag = item.tag;
\r
125 if(boost::range::find(used_tags, tag) != used_tags.end())
\r
128 used_tags.push_back(tag);
\r
130 const auto it = audio_streams_.find(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 // Skip it if there is no existing audio stream and item has no audio-data.
\r
138 if(it == audio_streams_.end() && item.audio_data.empty())
\r
141 const float prev_volume = static_cast<float>(prev_transform.volume);
\r
142 const float next_volume = static_cast<float>(next_transform.volume);
\r
144 // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.
\r
145 auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);
\r
147 for(size_t n = 0; n < item.audio_data.size(); ++n)
\r
148 next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));
\r
150 next_audio_streams[tag].prev_transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
\r
151 next_audio_streams[tag].audio_data = std::move(next_audio);
\r
156 audio_streams_ = std::move(next_audio_streams);
\r
158 if(audio_streams_.empty())
\r
159 audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);
\r
163 auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x)
\r
165 return x.audio_data.size() < audio_cadence_.front();
\r
168 if(nb_invalid_streams > 0)
\r
169 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected. Appending empty samples.";
\r
172 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);
\r
173 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)
\r
175 //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size();
\r
177 if(stream.audio_data.size() < result_ps.size())
\r
178 stream.audio_data.resize(result_ps.size(), 0.0f);
\r
180 auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
\r
181 stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
\r
184 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
186 audio_buffer result;
\r
187 result.reserve(result_ps.size());
\r
188 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});
\r
194 audio_mixer::audio_mixer() : impl_(new impl()){}
\r
195 void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}
\r
196 void audio_mixer::visit(const mutable_frame& frame){impl_->visit(frame);}
\r
197 void audio_mixer::pop(){impl_->pop();}
\r
198 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);}
\r