]> git.sesse.net Git - casparcg/blobdiff - core/mixer/audio/audio_mixer.cpp
2.1.0: Moved gpu stuff into gpu folder.
[casparcg] / core / mixer / audio / audio_mixer.cpp
index 798a144263cca882950de7a25c1a1bc36c1167ee..a660743e4ea0d171d9e905cce5adb2c9e8c2c1e0 100644 (file)
@@ -1,29 +1,36 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
 *\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
 *\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
 *\r
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "audio_mixer.h"\r
 \r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/frame_transform.h>\r
+#include <core/mixer/gpu/write_frame.h>\r
+#include <core/frame/frame_transform.h>\r
+#include <common/diagnostics/graph.h>\r
 \r
+#include <boost/range/adaptors.hpp>\r
+#include <boost/range/distance.hpp>\r
+\r
+#include <map>\r
 #include <stack>\r
 #include <vector>\r
 \r
@@ -34,46 +41,57 @@ struct audio_item
        const void*                     tag;\r
        frame_transform         transform;\r
        audio_buffer            audio_data;\r
+\r
+       audio_item()\r
+       {\r
+       }\r
+\r
+       audio_item(audio_item&& other)\r
+               : tag(std::move(other.tag))\r
+               , transform(std::move(other.transform))\r
+               , audio_data(std::move(other.audio_data))\r
+       {\r
+       }\r
 };\r
+\r
+typedef std::vector<float, tbb::cache_aligned_allocator<float>> audio_buffer_ps;\r
        \r
-struct audio_mixer::implementation\r
+struct audio_stream\r
 {\r
-       std::stack<core::frame_transform>                               transform_stack_;\r
-       std::map<const void*, core::frame_transform>    prev_frame_transforms_;\r
-       const core::video_format_desc                                   format_desc_;\r
-       std::vector<audio_item>                                                 items;\r
+       frame_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::map<const void*, audio_stream>     audio_streams_;\r
+       std::vector<audio_item>                         items_;\r
+       std::vector<int>                                        audio_cadence_;\r
+       video_format_desc                                       format_desc_;\r
+       \r
 public:\r
-       implementation(const core::video_format_desc& format_desc)\r
-               : format_desc_(format_desc)\r
+       impl()\r
        {\r
                transform_stack_.push(core::frame_transform());\r
        }\r
        \r
-       void begin(core::basic_frame& frame)\r
+       void begin(core::draw_frame& frame)\r
        {\r
                transform_stack_.push(transform_stack_.top()*frame.get_frame_transform());\r
        }\r
 \r
        void visit(core::write_frame& frame)\r
        {\r
-               // We only care about the last field.\r
-               if(format_desc_.field_mode == field_mode::upper && transform_stack_.top().field_mode == field_mode::upper)\r
-                       return;\r
-\r
-               if(format_desc_.field_mode == field_mode::lower && transform_stack_.top().field_mode == field_mode::lower)\r
-                       return;\r
-\r
-               // Skip empty audio.\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());\r
-\r
-               items.push_back(item);          \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
+               \r
+               items_.push_back(std::move(item));              \r
        }\r
 \r
        void begin(const core::frame_transform& transform)\r
@@ -86,103 +104,90 @@ public:
                transform_stack_.pop();\r
        }\r
        \r
-       audio_buffer mix()\r
+       audio_buffer mix(const video_format_desc& format_desc)\r
        {       \r
-               // NOTE: auto data should be larger than format_desc_.audio_samples_per_frame to allow sse to read/write beyond size.\r
-\r
-               auto intermediate = std::vector<float, tbb::cache_aligned_allocator<float>>(format_desc_.audio_samples_per_frame+128, 0.0f);\r
-\r
-               std::map<const void*, core::frame_transform> next_frame_transforms;\r
+               if(format_desc_ != format_desc)\r
+               {\r
+                       audio_streams_.clear();\r
+                       audio_cadence_ = format_desc.audio_cadence;\r
+                       format_desc_ = format_desc;\r
+               }               \r
                \r
-               BOOST_FOREACH(auto& item, items)\r
-               {                       \r
-                       const auto next = item.transform;\r
-                       auto prev = next;\r
+               std::map<const void*, audio_stream>     next_audio_streams;\r
 \r
-                       const auto it = prev_frame_transforms_.find(item.tag);\r
-                       if(it != prev_frame_transforms_.end())\r
-                               prev = it->second;\r
-                               \r
-                       next_frame_transforms[item.tag] = next; // Store all active tags, inactive tags will be removed at the end.\r
+               BOOST_FOREACH(auto& item, items_)\r
+               {                       \r
+                       audio_buffer_ps next_audio;\r
 \r
-                       if(next.volume < 0.001 && prev.volume < 0.001)\r
-                               continue;\r
-                                                                       \r
-                       if(static_cast<size_t>(item.audio_data.size()) != format_desc_.audio_samples_per_frame)\r
-                               continue;\r
+                       auto next_transform = item.transform;\r
+                       auto prev_transform = next_transform;\r
 \r
-                       CASPAR_ASSERT(format_desc_.audio_channels == 2);\r
-                       CASPAR_ASSERT(format_desc_.audio_samples_per_frame % 4 == 0);\r
-                                               \r
-                       const float prev_volume = static_cast<float>(prev.volume);\r
-                       const float next_volume = static_cast<float>(next.volume);\r
-                       const float delta               = 1.0f/static_cast<float>(format_desc_.audio_samples_per_frame/format_desc_.audio_channels);\r
+                       const auto it = audio_streams_.find(item.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
-                       auto alpha_ps   = _mm_setr_ps(0.0f, 0.0f, delta, delta);\r
-                       auto delta2_ps  = _mm_set_ps1(delta*2.0f);\r
-                       auto prev_ps    = _mm_set_ps1(prev_volume);\r
-                       auto next_ps    = _mm_set_ps1(next_volume);     \r
-\r
-                       for(size_t n = 0; n < format_desc_.audio_samples_per_frame/4; ++n)\r
-                       {               \r
-                               auto next2_ps           = _mm_mul_ps(next_ps, alpha_ps);\r
-                               auto prev2_ps           = _mm_sub_ps(prev_ps, _mm_mul_ps(prev_ps, alpha_ps));\r
-                               auto volume_ps          = _mm_add_ps(next2_ps, prev2_ps);\r
-\r
-                               auto sample_ps          = _mm_cvtepi32_ps(_mm_load_si128(reinterpret_cast<__m128i*>(&item.audio_data[n*4])));\r
-                               auto res_sample_ps      = _mm_load_ps(&intermediate[n*4]);                                                                                      \r
-                               sample_ps                       = _mm_mul_ps(sample_ps, volume_ps);     \r
-                               res_sample_ps           = _mm_add_ps(sample_ps, res_sample_ps); \r
+                       const float prev_volume = static_cast<float>(prev_transform.volume);\r
+                       const float next_volume = static_cast<float>(next_transform.volume);\r
+                                                                       \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
+               }                               \r
 \r
-                               alpha_ps                        = _mm_add_ps(alpha_ps, delta2_ps);\r
+               items_.clear();\r
 \r
-                               _mm_store_ps(&intermediate[n*4], res_sample_ps);\r
-                       }\r
-               }\r
+               audio_streams_ = std::move(next_audio_streams);\r
                \r
-               auto result = audio_buffer(format_desc_.audio_samples_per_frame+128);   \r
-                       \r
-               auto intermediate_128 = reinterpret_cast<__m128i*>(intermediate.data());\r
-               auto result_128           = reinterpret_cast<__m128i*>(result.data());\r
+               if(audio_streams_.empty())              \r
+                       audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f);\r
                                \r
-               for(size_t n = 0; n < format_desc_.audio_samples_per_frame/32; ++n)\r
-               {       \r
-                       auto xmm0 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm1 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm2 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm3 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm4 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm5 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm6 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       auto xmm7 = _mm_load_ps(reinterpret_cast<float*>(intermediate_128++));\r
-                       \r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm0));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm1));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm2));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm3));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm4));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm5));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm6));\r
-                       _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm7));\r
+               { // sanity check\r
+\r
+                       auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x)\r
+                       {\r
+                               return x.audio_data.size() < audio_cadence_.front();\r
+                       });\r
+\r
+                       if(nb_invalid_streams > 0)              \r
+                               CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected.";                   \r
                }\r
 \r
-               items.clear();\r
-               prev_frame_transforms_ = std::move(next_frame_transforms);      \r
+               std::vector<float> result_ps(audio_cadence_.front(), 0.0f);\r
+               BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values)\r
+               {\r
+                       //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size();\r
 \r
-               result.resize(format_desc_.audio_samples_per_frame);\r
-               return std::move(result);\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
+               }               \r
+               \r
+               boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
+               \r
+               audio_buffer result;\r
+               result.reserve(result_ps.size());\r
+               boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast<int32_t>(sample);});         \r
+               \r
+               return result;\r
        }\r
 };\r
 \r
-audio_mixer::audio_mixer(const core::video_format_desc& format_desc) : impl_(new implementation(format_desc)){}\r
-void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);}\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
-audio_buffer audio_mixer::mix(){return impl_->mix();}\r
-audio_mixer& audio_mixer::operator=(audio_mixer&& other)\r
-{\r
-       impl_ = std::move(other.impl_);\r
-       return *this;\r
-}\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