2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "oal_consumer.h"
\r
24 #include <common/exception/exceptions.h>
\r
25 #include <common/diagnostics/graph.h>
\r
26 #include <common/log.h>
\r
27 #include <common/utf.h>
\r
29 #include <core/consumer/frame_consumer.h>
\r
30 #include <core/mixer/audio/audio_util.h>
\r
31 #include <core/video_format.h>
\r
33 #include <core/mixer/read_frame.h>
\r
35 #include <SFML/Audio/SoundStream.hpp>
\r
37 #include <boost/circular_buffer.hpp>
\r
38 #include <boost/property_tree/ptree.hpp>
\r
39 #include <boost/timer.hpp>
\r
41 #include <tbb/concurrent_queue.h>
\r
43 namespace caspar { namespace oal {
\r
45 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;
\r
47 struct oal_consumer : public core::frame_consumer, public sf::SoundStream
\r
49 safe_ptr<diagnostics::graph> graph_;
\r
50 boost::timer perf_timer_;
\r
53 tbb::concurrent_bounded_queue<std::shared_ptr<audio_buffer_16>> input_;
\r
54 boost::circular_buffer<audio_buffer_16> container_;
\r
55 tbb::atomic<bool> is_running_;
\r
56 core::audio_buffer temp;
\r
58 core::video_format_desc format_desc_;
\r
62 , channel_index_(-1)
\r
64 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
65 diagnostics::register_graph(graph_);
\r
68 input_.set_capacity(1);
\r
73 is_running_ = false;
\r
74 input_.try_push(std::make_shared<audio_buffer_16>());
\r
75 input_.try_push(std::make_shared<audio_buffer_16>());
\r
77 input_.try_push(std::make_shared<audio_buffer_16>());
\r
78 input_.try_push(std::make_shared<audio_buffer_16>());
\r
80 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
\r
85 virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override
\r
87 format_desc_ = format_desc;
\r
88 channel_index_ = channel_index;
\r
89 graph_->set_text(print());
\r
91 if(Status() != Playing)
\r
93 sf::SoundStream::Initialize(2, 48000);
\r
96 CASPAR_LOG(info) << print() << " Sucessfully Initialized.";
\r
99 virtual bool send(const safe_ptr<core::read_frame>& frame) override
\r
101 input_.push(std::make_shared<audio_buffer_16>(core::audio_32_to_16(frame->audio_data())));
\r
105 virtual std::wstring print() const override
\r
107 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
\r
110 virtual boost::property_tree::wptree info() const override
\r
112 boost::property_tree::wptree info;
\r
113 info.add(L"type", L"oal-consumer");
\r
117 virtual int buffer_depth() const override
\r
124 virtual bool OnGetData(sf::SoundStream::Chunk& data) override
\r
126 std::shared_ptr<audio_buffer_16> audio_data;
\r
127 input_.pop(audio_data);
\r
129 container_.push_back(std::move(*audio_data));
\r
130 data.Samples = container_.back().data();
\r
131 data.NbSamples = container_.back().size();
\r
133 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
\r
134 perf_timer_.restart();
\r
136 return is_running_;
\r
139 virtual int index() const override
\r
145 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
147 if(params.size() < 1 || params[0] != L"AUDIO")
\r
148 return core::frame_consumer::empty();
\r
150 return make_safe<oal_consumer>();
\r
153 safe_ptr<core::frame_consumer> create_consumer()
\r
155 return make_safe<oal_consumer>();
\r