X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fmixer%2Faudio%2Faudio_mixer.cpp;h=a1e624890087f7f8cc4e7ff2a234e84c8a007ed9;hb=d1a8a306f4ccb5350aeaf41057e78ef3afc0b39e;hp=50ce413c4f707393c78215358209cdba00482129;hpb=4339e2b3466b78ed27cbe88592eb2e247c5c13a4;p=casparcg diff --git a/core/mixer/audio/audio_mixer.cpp b/core/mixer/audio/audio_mixer.cpp index 50ce413c4..a1e624890 100644 --- a/core/mixer/audio/audio_mixer.cpp +++ b/core/mixer/audio/audio_mixer.cpp @@ -1,191 +1,200 @@ /* -* copyright (c) 2010 Sveriges Television AB +* Copyright (c) 2011 Sveriges Television AB * -* This file is part of CasparCG. +* This file is part of CasparCG (www.casparcg.com). * -* CasparCG is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. * -* CasparCG is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with CasparCG. If not, see . +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . * +* Author: Robert Nagy, ronag89@gmail.com */ + #include "../../stdafx.h" #include "audio_mixer.h" -#include -#include +#include +#include +#include -#include - -#include +#include +#include +#include #include -#include +#include namespace caspar { namespace core { struct audio_item { const void* tag; - frame_transform transform; + audio_transform transform; audio_buffer audio_data; + + audio_item() + { + } + + audio_item(audio_item&& other) + : tag(std::move(other.tag)) + , transform(std::move(other.transform)) + , audio_data(std::move(other.audio_data)) + { + } }; + +typedef std::vector> audio_buffer_ps; -struct audio_mixer::implementation +struct audio_stream { - std::stack transform_stack_; - std::map prev_frame_transforms_; - const core::video_format_desc format_desc_; - std::vector items; + audio_transform prev_transform; + audio_buffer_ps audio_data; +}; +struct audio_mixer::impl : boost::noncopyable +{ + std::stack transform_stack_; + std::map audio_streams_; + std::vector items_; + std::vector audio_cadence_; + video_format_desc format_desc_; + public: - implementation(const core::video_format_desc& format_desc) - : format_desc_(format_desc) + impl() { - transform_stack_.push(core::frame_transform()); + transform_stack_.push(core::audio_transform()); } - void begin(core::basic_frame& frame) + void push(const frame_transform& transform) { - transform_stack_.push(transform_stack_.top()*frame.get_frame_transform()); + transform_stack_.push(transform_stack_.top()*transform.audio_transform); } - void visit(core::write_frame& frame) + void visit(const data_frame& frame) { - // We only care about the last field. - if(format_desc_.field_mode == field_mode::upper && transform_stack_.top().field_mode == field_mode::upper) - return; - - if(format_desc_.field_mode == field_mode::lower && transform_stack_.top().field_mode == field_mode::lower) - return; - - // Skip empty audio. - if(transform_stack_.top().volume < 0.002 || frame.audio_data().empty()) - return; - audio_item item; item.tag = frame.tag(); item.transform = transform_stack_.top(); - item.audio_data = std::move(frame.audio_data()); - - items.push_back(item); + item.audio_data = frame.audio_data(); + + items_.push_back(std::move(item)); } - void begin(const core::frame_transform& transform) + void begin(const core::audio_transform& transform) { transform_stack_.push(transform_stack_.top()*transform); } - void end() + void pop() { transform_stack_.pop(); } - audio_buffer mix() + audio_buffer mix(const video_format_desc& format_desc) { - auto intermediate = std::vector>(format_desc_.audio_samples_per_frame+128, 0.0f); + if(format_desc_ != format_desc) + { + audio_streams_.clear(); + audio_cadence_ = format_desc.audio_cadence; + format_desc_ = format_desc; + } + + std::map next_audio_streams; + std::vector used_tags; - std::map next_frame_transforms; + BOOST_FOREACH(auto& item, items_) + { + audio_buffer_ps next_audio; - BOOST_FOREACH(auto& item, items) - { - const auto next = item.transform; - auto prev = next; + auto next_transform = item.transform; + auto prev_transform = next_transform; - const auto it = prev_frame_transforms_.find(item.tag); - if(it != prev_frame_transforms_.end()) - prev = it->second; - - next_frame_transforms[item.tag] = next; // Store all active tags, inactive tags will be removed at the end. - - if(next.volume < 0.001 && prev.volume < 0.001) + auto tag = item.tag; + + if(boost::range::find(used_tags, tag) != used_tags.end()) continue; - - if(static_cast(item.audio_data.size()) != format_desc_.audio_samples_per_frame) + + used_tags.push_back(tag); + + const auto it = audio_streams_.find(tag); + if(it != audio_streams_.end()) + { + prev_transform = it->second.prev_transform; + next_audio = std::move(it->second.audio_data); + } + + // Skip it if there is no existing audio stream and item has no audio-data. + if(it == audio_streams_.end() && item.audio_data.empty()) continue; - - CASPAR_ASSERT(format_desc_.audio_channels == 2); - CASPAR_ASSERT(format_desc_.audio_samples_per_frame % 4 == 0); - const float prev_volume = static_cast(prev.volume); - const float next_volume = static_cast(next.volume); - const float delta = 1.0f/static_cast(format_desc_.audio_samples_per_frame/2); + const float prev_volume = static_cast(prev_transform.volume); + const float next_volume = static_cast(next_transform.volume); + + // TODO: Move volume mixing into code below, in order to support audio sample counts not corresponding to frame audio samples. + auto alpha = (next_volume-prev_volume)/static_cast(item.audio_data.size()/format_desc.audio_channels); - tbb::parallel_for - ( - tbb::blocked_range(0, format_desc_.audio_samples_per_frame/4), - [&](const tbb::blocked_range& r) - { - for(size_t n = r.begin(); n < r.end(); ++n) - { - const float alpha0 = (n*2) * delta; - const float volume0 = prev_volume * (1.0f - alpha0) + next_volume * alpha0; - const float volume1 = prev_volume * (1.0f - alpha0 + delta) + next_volume * (alpha0 + delta); - - auto sample_epi32 = _mm_load_si128(reinterpret_cast<__m128i*>(&item.audio_data[n*4])); - auto res_sample_ps = _mm_load_ps(&intermediate[n*4]); - - auto sample_ps = _mm_cvtepi32_ps(sample_epi32); - sample_ps = _mm_mul_ps(sample_ps, _mm_setr_ps(volume1, volume1, volume0, volume0)); - res_sample_ps = _mm_add_ps(sample_ps, res_sample_ps); - - _mm_store_ps(&intermediate[n*4], res_sample_ps); - } - } - ); - } + for(size_t n = 0; n < item.audio_data.size(); ++n) + next_audio.push_back(item.audio_data[n] * (prev_volume + (n/format_desc_.audio_channels) * alpha)); + + next_audio_streams[tag].prev_transform = std::move(next_transform); // Store all active tags, inactive tags will be removed at the end. + next_audio_streams[tag].audio_data = std::move(next_audio); + } + + items_.clear(); + + audio_streams_ = std::move(next_audio_streams); - auto result = audio_buffer(format_desc_.audio_samples_per_frame+128, 0); + if(audio_streams_.empty()) + audio_streams_[nullptr].audio_data = audio_buffer_ps(audio_cadence_.front(), 0.0f); + + { // sanity check - auto intermediate_128 = reinterpret_cast<__m128i*>(intermediate.data()); - auto result_128 = reinterpret_cast<__m128i*>(result.data()); - for(size_t n = 0; n < format_desc_.audio_samples_per_frame/32; ++n) - { - auto xmm0 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm1 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm2 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm3 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm4 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm5 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm6 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - auto xmm7 = _mm_load_ps(reinterpret_cast(intermediate_128++)); - - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm0)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm1)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm2)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm3)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm4)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm5)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm6)); - _mm_stream_si128(result_128++, _mm_cvtps_epi32(xmm7)); + auto nb_invalid_streams = boost::count_if(audio_streams_ | boost::adaptors::map_values, [&](const audio_stream& x) + { + return x.audio_data.size() < audio_cadence_.front(); + }); + + if(nb_invalid_streams > 0) + CASPAR_LOG(trace) << "[audio_mixer] Incorrect frame audio cadence detected. Appending empty samples."; } - items.clear(); - prev_frame_transforms_ = std::move(next_frame_transforms); + std::vector result_ps(audio_cadence_.front(), 0.0f); + BOOST_FOREACH(auto& stream, audio_streams_ | boost::adaptors::map_values) + { + //CASPAR_LOG(debug) << stream.audio_data.size() << L" : " << result_ps.size(); + + if(stream.audio_data.size() < result_ps.size()) + stream.audio_data.resize(result_ps.size(), 0.0f); - result.resize(format_desc_.audio_samples_per_frame); - return std::move(result); + auto out = boost::range::transform(result_ps, stream.audio_data, std::begin(result_ps), std::plus()); + stream.audio_data.erase(std::begin(stream.audio_data), std::begin(stream.audio_data) + std::distance(std::begin(result_ps), out)); + } + + boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1); + + audio_buffer result; + result.reserve(result_ps.size()); + boost::range::transform(result_ps, std::back_inserter(result), [](float sample){return static_cast(sample);}); + + return result; } }; -audio_mixer::audio_mixer(const core::video_format_desc& format_desc) : impl_(new implementation(format_desc)){} -void audio_mixer::begin(core::basic_frame& frame){impl_->begin(frame);} -void audio_mixer::visit(core::write_frame& frame){impl_->visit(frame);} -void audio_mixer::end(){impl_->end();} -audio_buffer audio_mixer::mix(){return impl_->mix();} -audio_mixer& audio_mixer::operator=(audio_mixer&& other) -{ - impl_ = std::move(other.impl_); - return *this; -} +audio_mixer::audio_mixer() : impl_(new impl()){} +void audio_mixer::push(const frame_transform& transform){impl_->push(transform);} +void audio_mixer::visit(const data_frame& frame){impl_->visit(frame);} +void audio_mixer::pop(){impl_->pop();} +audio_buffer audio_mixer::operator()(const video_format_desc& format_desc){return impl_->mix(format_desc);} }} \ No newline at end of file