]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
* Merged MIXER CROP, MIXER ANCHOR, MIXER ROTATION and MIXER PERSPECTIVE from 2.0
[casparcg] / core / mixer / audio / audio_mixer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "audio_mixer.h"
25
26 #include <core/frame/frame.h>
27 #include <core/frame/frame_transform.h>
28 #include <common/diagnostics/graph.h>
29 #include <common/linq.h>
30
31 #include <boost/range/adaptors.hpp>
32 #include <boost/range/distance.hpp>
33
34 #include <map>
35 #include <stack>
36 #include <vector>
37
38 namespace caspar { namespace core {
39
40 struct audio_item
41 {
42         const void*                     tag                     = nullptr;
43         audio_transform         transform;
44         audio_buffer            audio_data;
45
46         audio_item()
47         {
48         }
49
50         audio_item(audio_item&& other)
51                 : tag(std::move(other.tag))
52                 , transform(std::move(other.transform))
53                 , audio_data(std::move(other.audio_data))
54         {
55         }
56 };
57
58 typedef cache_aligned_vector<float> audio_buffer_ps;
59         
60 struct audio_stream
61 {
62         audio_transform         prev_transform;
63         audio_buffer_ps         audio_data;
64         bool                            is_still                = false;
65 };
66
67 struct audio_mixer::impl : boost::noncopyable
68 {
69         std::stack<core::audio_transform>       transform_stack_;
70         std::map<const void*, audio_stream>     audio_streams_;
71         std::vector<audio_item>                         items_;
72         std::vector<int>                                        audio_cadence_;
73         video_format_desc                                       format_desc_;
74         float                                                           master_volume_                  = 1.0f;
75         float                                                           previous_master_volume_ = master_volume_;
76 public:
77         impl()
78         {
79                 transform_stack_.push(core::audio_transform());
80         }
81         
82         void push(const frame_transform& transform)
83         {
84                 transform_stack_.push(transform_stack_.top()*transform.audio_transform);
85         }
86
87         void visit(const const_frame& frame)
88         {
89                 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
90                         return;
91
92                 audio_item item;
93                 item.tag                = frame.stream_tag();
94                 item.transform  = transform_stack_.top();
95                 item.audio_data = frame.audio_data();
96
97                 if(item.transform.is_still)
98                         item.transform.volume = 0.0;
99                 
100                 items_.push_back(std::move(item));              
101         }
102
103         void begin(const core::audio_transform& transform)
104         {
105                 transform_stack_.push(transform_stack_.top()*transform);
106         }
107                 
108         void pop()
109         {
110                 transform_stack_.pop();
111         }
112
113         void set_master_volume(float volume)
114         {
115                 master_volume_ = volume;
116         }
117
118         float get_master_volume()
119         {
120                 return master_volume_;
121         }
122
123         audio_buffer mix(const video_format_desc& format_desc)
124         {       
125                 if(format_desc_ != format_desc)
126                 {
127                         audio_streams_.clear();
128                         audio_cadence_ = format_desc.audio_cadence;
129                         format_desc_ = format_desc;
130                 }               
131                 
132                 std::map<const void*, audio_stream>     next_audio_streams;
133                 std::vector<const void*> used_tags;
134
135                 for (auto& item : items_)
136                 {                       
137                         audio_buffer_ps next_audio;
138
139                         auto next_transform = item.transform;
140                         auto prev_transform = next_transform;
141
142                         auto tag = item.tag;
143
144                         if(boost::range::find(used_tags, tag) != used_tags.end())
145                                 continue;
146                         
147                         used_tags.push_back(tag);
148
149                         const auto it = audio_streams_.find(tag);
150                         if(it != audio_streams_.end())
151                         {       
152                                 prev_transform  = it->second.prev_transform;
153                                 next_audio              = std::move(it->second.audio_data);
154                         }
155                         
156                         // Skip it if there is no existing audio stream and item has no audio-data.
157                         if(it == audio_streams_.end() && item.audio_data.empty()) 
158                                 continue;
159                                                 
160                         const float prev_volume = static_cast<float>(prev_transform.volume) * previous_master_volume_;
161                         const float next_volume = static_cast<float>(next_transform.volume) * master_volume_;
162
163                         // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.
164                         auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);
165                         
166                         for(size_t n = 0; n < item.audio_data.size(); ++n)
167                         {
168                                 auto sample_multiplier = (prev_volume + (n/format_desc_.audio_channels) * alpha);
169                                 next_audio.push_back(item.audio_data[n] * sample_multiplier);
170                         } 
171                                                                                 
172                         next_audio_streams[tag].prev_transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
173                         next_audio_streams[tag].audio_data              = std::move(next_audio);        
174                         next_audio_streams[tag].is_still                = item.transform.is_still;
175                 }                               
176
177                 previous_master_volume_ = master_volume_;
178                 items_.clear();
179
180                 audio_streams_ = std::move(next_audio_streams);
181                 
182                 if(audio_streams_.empty())              
183                         audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_size(audio_cadence_.front()), 0.0f);
184
185                 { // sanity check
186
187                         auto nb_invalid_streams = cpplinq::from(audio_streams_)
188                                 .select(values())
189                                 .where([&](const audio_stream& x) { return x.audio_data.size() < audio_size(audio_cadence_.front()); })
190                                 .count();
191
192                         if(nb_invalid_streams > 0)              
193                                 CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";                   
194                 }
195                                 
196                 std::vector<float> result_ps(audio_size(audio_cadence_.front()), 0.0f);
197                 for (auto& stream : audio_streams_ | boost::adaptors::map_values)
198                 {
199                         if(stream.audio_data.size() < result_ps.size())
200                                 stream.audio_data.resize(result_ps.size(), 0.0f);
201
202                         auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
203                         stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
204                 }               
205                 
206                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
207                 
208                 audio_buffer result;
209                 result.reserve(result_ps.size());
210                 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});         
211                 
212                 return result;
213         }
214
215         size_t audio_size(size_t num_samples) const
216         {
217                 return num_samples * format_desc_.audio_channels;
218         }
219 };
220
221 audio_mixer::audio_mixer() : impl_(new impl()){}
222 void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}
223 void audio_mixer::visit(const const_frame& frame){impl_->visit(frame);}
224 void audio_mixer::pop(){impl_->pop();}
225 void audio_mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
226 float audio_mixer::get_master_volume() { return impl_->get_master_volume(); }
227 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){ return impl_->mix(format_desc); }
228
229 }}