85 lines
2.5 KiB
C++
85 lines
2.5 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_STREAM_HPP
|
|
#define OUR_DICK_AUDIO_STREAM_HPP
|
|
|
|
#include <utility> //forward
|
|
#include <portaudio.h>
|
|
|
|
#include "util/sequence.hpp"
|
|
#include "detail.hpp"
|
|
#include "error.hpp"
|
|
|
|
namespace audio{
|
|
|
|
class stream
|
|
{
|
|
private:
|
|
detail::callback_iface* m_cb = nullptr;
|
|
PaStream* m_stream = nullptr;
|
|
mutable PaError m_err = paNoError;
|
|
double m_samplerate;
|
|
int m_in_channels;
|
|
int m_out_channels;
|
|
|
|
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,Args...>(std::forward<Callback>(cb), 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_samplerate(samplerate),
|
|
m_in_channels(in_c),
|
|
m_out_channels(out_c){}
|
|
|
|
~stream();
|
|
|
|
error start();
|
|
error stop();
|
|
error abort();
|
|
error close();
|
|
|
|
bool is_stopped()const;
|
|
bool is_active()const;
|
|
|
|
int input_count()const;
|
|
int output_count()const;
|
|
|
|
double get_samplerate()const;
|
|
|
|
error last_error()const;
|
|
|
|
private:
|
|
static PaStream* initialize_global_instance();
|
|
//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);
|
|
static int callback(const void* input, void* output, unsigned long framecount,
|
|
const PaStreamCallbackTimeInfo* /*timeInfo*/, PaStreamCallbackFlags statusflags,
|
|
void* userdata);
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|