our_dick/src/audio/stream.cpp

80 lines
2.2 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/>.
*/
#include "audio/stream.hpp"
#include "audio/init.hpp"
#include "audio/error.hpp"
#include <portaudio.h>
namespace audio{
stream::~stream(){
close();
delete m_cb;
}
error stream::start(){
return error(m_err = Pa_StartStream(m_stream));
}
error stream::stop(){
return error(m_err = Pa_StopStream(m_stream));
}
error stream::abort(){
return error(m_err = Pa_AbortStream(m_stream));
}
error stream::close(){
if(m_stream){
m_err = Pa_CloseStream(m_stream);
m_stream = nullptr;
return error(m_err);
}
return error(paNotInitialized);
}
bool stream::is_stopped()const{
return (m_err = Pa_IsStreamStopped(m_stream)) == 1;
}
bool stream::is_active()const{
return (m_err = Pa_IsStreamActive(m_stream)) == 1;
}
error stream::last_error()const{
return error(m_err);
}
PaStream* stream::initialize_global_instance(){
pa_system::instance();
return nullptr;
}
int stream::open_default_stream(PaStream** stream, int in_c, int out_c,
int fmt, double samplerate,
size_t bufsize, detail::callback_iface* cb)
{
return Pa_OpenDefaultStream(stream, in_c, out_c, fmt, samplerate, bufsize, callback, cb);
}
int stream::callback(const void* input, void* output, unsigned long framecount,
const PaStreamCallbackTimeInfo* /*timeInfo*/, PaStreamCallbackFlags /*statusFlags*/,
void* userdata)
{
detail::callback_iface* cb = static_cast<detail::callback_iface*>(userdata);
return (*cb)(input, output, framecount);
}
}