81 lines
2.2 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2021 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 sfx::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;
sfx::stream m_output_sink;
public:
mixer(int output_channels, size_t channel_count, size_t samplerate);
mixer(const mixer&) = delete;
mixer(mixer&&) = delete;
~mixer(void);
void reserve_channels(size_t count);
std::vector<channel>& channels(void);
const std::vector<channel>& channels(void)const;
channel& get_channel(size_t i);
const channel& get_channel(size_t i)const;
void lock(void);
void consumer_lock(void);
void unlock(void);
void consumer_unlock(void);
bool is_active(void)const;
void set_active(bool b);
bool is_paused(void)const;
void pause(void);
void resume(void);
bool should_exit(void)const;
void exit(void);
size_t output_count(void)const;
size_t samplerate(void)const;
size_t channel_count(void)const;
private:
static int callback(const void* /*input*/, void* output, unsigned long frame_count, unsigned long flags, const stream_info&, mixer& mix);
};
}
#endif