]> git.sesse.net Git - casparcg/blob - core/mixer/audio/audio_mixer.cpp
set svn:eol-style native on .h and .cpp files
[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
30 #include <boost/range/adaptors.hpp>
31 #include <boost/range/distance.hpp>
32
33 #include <map>
34 #include <stack>
35 #include <vector>
36
37 namespace caspar { namespace core {
38
39 struct audio_item
40 {
41         const void*                     tag;
42         audio_transform         transform;
43         audio_buffer            audio_data;
44
45         audio_item()
46         {
47         }
48
49         audio_item(audio_item&& other)
50                 : tag(std::move(other.tag))
51                 , transform(std::move(other.transform))
52                 , audio_data(std::move(other.audio_data))
53         {
54         }
55 };
56
57 typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
58         
59 struct audio_stream
60 {
61         audio_transform         prev_transform;
62         audio_buffer_ps         audio_data;
63         bool                            is_still;
64
65         audio_stream() 
66                 : is_still(false)
67         {
68         }
69 };
70
71 struct audio_mixer::impl : boost::noncopyable
72 {
73         std::stack<core::audio_transform>       transform_stack_;
74         std::map<const void*, audio_stream>     audio_streams_;
75         std::vector<audio_item>                         items_;
76         std::vector<int>                                        audio_cadence_;
77         video_format_desc                                       format_desc_;
78         
79 public:
80         impl()
81         {
82                 transform_stack_.push(core::audio_transform());
83         }
84         
85         void push(const frame_transform& transform)
86         {
87                 transform_stack_.push(transform_stack_.top()*transform.audio_transform);
88         }
89
90         void visit(const const_frame& frame)
91         {
92                 if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())
93                         return;
94
95                 audio_item item;
96                 item.tag                = frame.stream_tag();
97                 item.transform  = transform_stack_.top();
98                 item.audio_data = frame.audio_data();
99
100                 if(item.transform.is_still)
101                         item.transform.volume = 0.0;
102                 
103                 items_.push_back(std::move(item));              
104         }
105
106         void begin(const core::audio_transform& transform)
107         {
108                 transform_stack_.push(transform_stack_.top()*transform);
109         }
110                 
111         void pop()
112         {
113                 transform_stack_.pop();
114         }
115         
116         audio_buffer mix(const video_format_desc& format_desc)
117         {       
118                 if(format_desc_ != format_desc)
119                 {
120                         audio_streams_.clear();
121                         audio_cadence_ = format_desc.audio_cadence;
122                         format_desc_ = format_desc;
123                 }               
124                 
125                 std::map<const void*, audio_stream>     next_audio_streams;
126                 std::vector<const void*> used_tags;
127
128                 BOOST_FOREACH(auto& item, items_)
129                 {                       
130                         audio_buffer_ps next_audio;
131
132                         auto next_transform = item.transform;
133                         auto prev_transform = next_transform;
134
135                         auto tag = item.tag;
136
137                         if(boost::range::find(used_tags, tag) != used_tags.end())
138                                 continue;
139                         
140                         used_tags.push_back(tag);
141
142                         const auto it = audio_streams_.find(tag);
143                         if(it != audio_streams_.end())
144                         {       
145                                 prev_transform  = it->second.prev_transform;
146                                 next_audio              = std::move(it->second.audio_data);
147                         }
148                         
149                         // Skip it if there is no existing audio stream and item has no audio-data.
150                         if(it == audio_streams_.end() && item.audio_data.empty()) 
151                                 continue;
152                                                 
153                         const float prev_volume = static_cast<float>(prev_transform.volume);
154                         const float next_volume = static_cast<float>(next_transform.volume);
155                                                 
156                         // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.
157                         auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);
158                         
159                         for(size_t n = 0; n < item.audio_data.size(); ++n)
160                                 next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));
161                                                                                 
162                         next_audio_streams[tag].prev_transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.
163                         next_audio_streams[tag].audio_data              = std::move(next_audio);        
164                         next_audio_streams[tag].is_still                = item.transform.is_still;
165                 }                               
166
167                 items_.clear();
168
169                 audio_streams_ = std::move(next_audio_streams);
170                 
171                 if(audio_streams_.empty())              
172                         audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);
173                                 
174                 std::vector<float> result_ps(audio_cadence_.front(), 0.0f);
175                 BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)
176                 {
177                         if(stream.audio_data.size() < result_ps.size())
178                                 stream.audio_data.resize(result_ps.size(), 0.0f);
179
180                         auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());
181                         stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));
182                 }               
183                 
184                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
185                 
186                 audio_buffer result;
187                 result.reserve(result_ps.size());
188                 boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});         
189                 
190                 return result;
191         }
192 };
193
194 audio_mixer::audio_mixer() : impl_(new impl()){}
195 void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}
196 void audio_mixer::visit(const const_frame& frame){impl_->visit(frame);}
197 void audio_mixer::pop(){impl_->pop();}
198 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);}
199
200 }}