77 lines
2.0 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_AUDIO_IMPL_MIXER_HPP
#define OUR_DICK_AUDIO_IMPL_MIXER_HPP
#include <atomic>
#include <vector>
#include <mutex>
#include <cstdlib> //size_t
#include "channel.hpp"
#include "audio/stream.hpp"
namespace audio::impl{
//channel reservation needs to be thread safe and lock free
class mixer
{
private:
//all thread stuff
std::atomic_bool m_paused;
std::atomic_bool m_exit;
std::atomic_bool m_operating;
std::vector<channel> m_channels;
//producer thread stuff
std::mutex m_copy_lock;
std::unique_lock<std::mutex> m_lk;
audio::stream m_output_sink;
public:
mixer(int output_channels, size_t channel_count);
mixer(const mixer&) = delete;
mixer(mixer&&) = delete;
~mixer();
void reserve_channels(size_t count);
std::vector<channel>& channels();
const std::vector<channel>& channels()const;
channel& get_channel(size_t i);
const channel& get_channel(size_t i)const;
void lock();
void consumer_lock();
void unlock();
void consumer_unlock();
bool is_active()const;
void set_active(bool b);
bool is_paused()const;
void pause();
void resume();
bool should_exit()const;
void exit();
size_t output_channels()const;
private:
static int callback(const void* /*input*/, void* output, unsigned long frame_count, unsigned long flags, mixer& mix);
};
}
#endif