1
#include "audio_dev.hpp"
8
class pa19_record: public record::dev {
9
static int c_callback(const void* input, void*, unsigned long frames, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void* userdata)
11
pa19_record& self = *static_cast<pa19_record*>(userdata);
12
int16_t const* iptr = static_cast<int16_t const*>(input);
14
std::vector<sample_t> buf(frames * self.s.channels());
15
std::transform(iptr, iptr + buf.size(), buf.begin(), conv_from_s16);
16
pcm_data data(&buf[0], frames, self.s.channels());
17
self.s.callback()(data, self.s);
18
} catch (std::exception& e) {
19
self.s.debug(std::string("Exception from recording callback: ") + e.what());
26
PaError err = Pa_Initialize();
27
if( err != paNoError ) throw std::runtime_error(std::string("Cannot initialize PortAudio: ") + Pa_GetErrorText(err));
29
~init() { Pa_Terminate(); }
33
strm( pa19_record* rec) {
35
if (rec->s.subdev().empty()) {
36
p.device = Pa_GetDefaultInputDevice();
38
std::istringstream iss(rec->s.subdev());
40
if (!iss.eof() || p.device < 0 || p.device > Pa_GetDeviceCount() - 1) throw std::invalid_argument("Invalid PortAudio device number");
42
p.channelCount = rec->s.channels();
43
p.sampleFormat = paInt16;
44
if (rec->s.frames() != settings::high) p.suggestedLatency = Pa_GetDeviceInfo(p.device)->defaultLowInputLatency;
45
else p.suggestedLatency = Pa_GetDeviceInfo(p.device)->defaultHighInputLatency;
46
p.hostApiSpecificStreamInfo = NULL;
47
PaError err = Pa_OpenStream(&handle, &p, NULL, rec->s.rate(), 50, paClipOff, rec->c_callback, rec);
48
if (err != paNoError) throw std::runtime_error("Cannot open PortAudio audio stream " + rec->s.subdev() + ": " + Pa_GetErrorText(err));
50
~strm() { Pa_CloseStream(handle); }
53
pa19_record(settings& s_orig): s(s_orig), initialize(), stream(this) {
54
PaError err = Pa_StartStream(stream.handle);
55
if( err != paNoError ) throw std::runtime_error("Cannot start PortAudio audio stream " + s.subdev() + ": " + Pa_GetErrorText(err));
59
boost::plugin::simple<record_plugin, pa19_record> r(devinfo("pa", "PortAudio v19 PCM capture. Device number as settings (otherwise PA default)."));