]> git.sesse.net Git - casparcg/blobdiff - core/mixer/audio/audio_mixer.cpp
2.1.0: Use const_ types consistently when no modification is expected, instead of...
[casparcg] / core / mixer / audio / audio_mixer.cpp
index a660743e4ea0d171d9e905cce5adb2c9e8c2c1e0..95c979fa8e9ae7c98f6f939c7bc9907436430487 100644 (file)
@@ -23,7 +23,7 @@
 \r
 #include "audio_mixer.h"\r
 \r
-#include <core/mixer/gpu/write_frame.h>\r
+#include <core/frame/frame.h>\r
 #include <core/frame/frame_transform.h>\r
 #include <common/diagnostics/graph.h>\r
 \r
@@ -39,7 +39,7 @@ namespace caspar { namespace core {
 struct audio_item\r
 {\r
        const void*                     tag;\r
-       frame_transform         transform;\r
+       audio_transform         transform;\r
        audio_buffer            audio_data;\r
 \r
        audio_item()\r
@@ -58,13 +58,13 @@ typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;
        \r
 struct audio_stream\r
 {\r
-       frame_transform         prev_transform;\r
+       audio_transform         prev_transform;\r
        audio_buffer_ps         audio_data;\r
 };\r
 \r
 struct audio_mixer::impl : boost::noncopyable\r
 {\r
-       std::stack<core::frame_transform>       transform_stack_;\r
+       std::stack<core::audio_transform>       transform_stack_;\r
        std::map<const void*, audio_stream>     audio_streams_;\r
        std::vector<audio_item>                         items_;\r
        std::vector<int>                                        audio_cadence_;\r
@@ -73,33 +73,30 @@ struct audio_mixer::impl : boost::noncopyable
 public:\r
        impl()\r
        {\r
-               transform_stack_.push(core::frame_transform());\r
+               transform_stack_.push(core::audio_transform());\r
        }\r
        \r
-       void begin(core::draw_frame& frame)\r
+       void push(const frame_transform& transform)\r
        {\r
-               transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());\r
+               transform_stack_.push(transform_stack_.top()*transform.audio_transform);\r
        }\r
 \r
-       void visit(core::write_frame& frame)\r
+       void visit(const const_frame& frame)\r
        {\r
-               if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty())\r
-                       return;\r
-\r
                audio_item item;\r
                item.tag                = frame.tag();\r
                item.transform  = transform_stack_.top();\r
-               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
+               item.audio_data = frame.audio_data();\r
                \r
                items_.push_back(std::move(item));              \r
        }\r
 \r
-       void begin(const core::frame_transform& transform)\r
+       void begin(const core::audio_transform& transform)\r
        {\r
                transform_stack_.push(transform_stack_.top()*transform);\r
        }\r
                \r
-       void end()\r
+       void pop()\r
        {\r
                transform_stack_.pop();\r
        }\r
@@ -114,6 +111,7 @@ public:
                }               \r
                \r
                std::map<const void*, audio_stream>     next_audio_streams;\r
+               std::vector<const void*> used_tags;\r
 \r
                BOOST_FOREACH(auto& item, items_)\r
                {                       \r
@@ -122,23 +120,35 @@ public:
                        auto next_transform = item.transform;\r
                        auto prev_transform = next_transform;\r
 \r
-                       const auto it = audio_streams_.find(item.tag);\r
+                       auto tag = item.tag;\r
+\r
+                       if(boost::range::find(used_tags, tag) != used_tags.end())\r
+                               continue;\r
+                       \r
+                       used_tags.push_back(tag);\r
+\r
+                       const auto it = audio_streams_.find(tag);\r
                        if(it != audio_streams_.end())\r
                        {       \r
                                prev_transform  = it->second.prev_transform;\r
                                next_audio              = std::move(it->second.audio_data);\r
                        }\r
+                       \r
+                       // Skip it if there is no existing audio stream and item has no audio-data.\r
+                       if(it == audio_streams_.end() && item.audio_data.empty()) \r
+                               continue;\r
                                                \r
                        const float prev_volume = static_cast<float>(prev_transform.volume);\r
                        const float next_volume = static_cast<float>(next_transform.volume);\r
-                                                                       \r
+                                               \r
+                       // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples.\r
                        auto alpha = (next_volume-prev_volume)/static_cast<float>(item.audio_data.size()/format_desc.audio_channels);\r
                        \r
                        for(size_t n = 0; n < item.audio_data.size(); ++n)\r
                                next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha));\r
                                                                                \r
-                       next_audio_streams[item.tag].prev_transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.\r
-                       next_audio_streams[item.tag].audio_data          = std::move(next_audio);                       \r
+                       next_audio_streams[tag].prev_transform  = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end.\r
+                       next_audio_streams[tag].audio_data               = std::move(next_audio);                       \r
                }                               \r
 \r
                items_.clear();\r
@@ -156,7 +166,7 @@ public:
                        });\r
 \r
                        if(nb_invalid_streams > 0)              \r
-                               CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";                   \r
+                               CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected. Appending empty samples.";                  \r
                }\r
 \r
                std::vector<float> result_ps(audio_cadence_.front(), 0.0f);\r
@@ -165,10 +175,7 @@ public:
                        //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size();\r
 \r
                        if(stream.audio_data.size() < result_ps.size())\r
-                       {\r
                                stream.audio_data.resize(result_ps.size(), 0.0f);\r
-                               CASPAR_LOG(trace) << L"[audio_mixer] Appended zero samples";\r
-                       }\r
 \r
                        auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus<float>());\r
                        stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out));\r
@@ -185,9 +192,9 @@ public:
 };\r
 \r
 audio_mixer::audio_mixer() : impl_(new impl()){}\r
-void audio_mixer::begin(core::draw_frame& frame){impl_->begin(frame);}\r
-void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);}\r
-void audio_mixer::end(){impl_->end();}\r
+void audio_mixer::push(const frame_transform& transform){impl_->push(transform);}\r
+void audio_mixer::visit(const const_frame& frame){impl_->visit(frame);}\r
+void audio_mixer::pop(){impl_->pop();}\r
 audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);}\r
 \r
 }}
\ No newline at end of file