our_dick/include/audio/stream.hpp

85 lines
2.4 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_STREAM_HPP
#define OUR_DICK_AUDIO_STREAM_HPP
#include <utility> //forward
#include <portaudio.h>
#include "detail.hpp"
#include "error.hpp"
#include "stream_info.hpp"
#include <cstdlib>
namespace sfx{
class stream
{
private:
detail::callback_iface* m_cb = nullptr;
PaStream* m_stream = nullptr;
mutable PaError m_err = paNoError;
stream_info m_info;
public:
enum state{
STOP = paComplete,
CONTINUE = paContinue,
};
public:
//buffersize of 0 means to automatically pick a good size
//TODO: if samplerate is 0, use device's default sample rate
//TODO: allow choosing device by index
template<typename Callback, typename... Args>
stream(int in_c, int out_c, double samplerate, size_t buffersize, Callback&& cb, Args&&... cbargs):
m_cb(new detail::callback_impl<Callback,const stream_info&, Args...>(std::forward<Callback>(cb), m_info, std::forward<Args>(cbargs)...)),
m_stream(initialize_global_instance()),
m_err(open_default_stream(&m_stream, in_c, out_c, paFloat32, samplerate, buffersize, m_cb)),
m_info{out_c, in_c, samplerate}{}
~stream(void);
error start(void);
error stop(void);
error abort(void);
error close(void);
bool is_stopped(void)const;
bool is_active(void)const;
int input_count(void)const;
int output_count(void)const;
double samplerate(void)const;
error last_error(void)const;
private:
static PaStream* initialize_global_instance(void);
//to avoid using portaudio functions in the header
static int open_default_stream(PaStream**, int in_c, int out_c,
long unsigned int fmt, double samplerate,
size_t bufsize, detail::callback_iface* cb);
};
}
#endif