55 #ifdef NSOUND_LIBPORTAUDIO
56 #include <portaudio.h>
59 using namespace Nsound;
65 #ifndef NSOUND_PLATFORM_OS_WINDOWS
66 #pragma GCC diagnostic ignored "-Wold-style-cast"
69 #ifdef NSOUND_PLATFORM_OS_WINDOWS
90 n_frames_per_buffer_(64),
126 #ifndef NSOUND_LIBPORTAUDIO
128 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
129 <<
": Nsound was not compiled with libportaudio support.\n");
134 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
135 <<
": backend not initialized yet\n");
143 const PaDeviceInfo * info = NULL;
149 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
150 <<
": Pa_GetDeviceInfo() failed\n");
155 std::stringstream ss;
157 ss <<
"libportaudio Default Driver Info:" << endl
158 <<
" deviceCount: " << Pa_GetDeviceCount() << endl
159 <<
" structVersion: " << info->structVersion << endl
160 <<
" name: " << info->name << endl
161 <<
" hostApi(index): " << info->hostApi << endl
162 <<
" maxInputChannels: " << info->maxInputChannels << endl
163 <<
" maxOutputChannels: " << info->maxOutputChannels << endl
164 <<
" defaultLowInputLatency: " << info->defaultLowInputLatency << endl
165 <<
" defaultHighInputLatency: " << info->defaultHighInputLatency << endl
166 <<
" defaultLowOutputLatency: " << info->defaultLowOutputLatency << endl
167 <<
" defaultHighOutputLatency: " << info->defaultHighOutputLatency << endl
168 <<
" defaultSampleRate: " << info->defaultSampleRate << endl;
170 const PaHostApiInfo * api_info = NULL;
172 api_info = Pa_GetHostApiInfo(info->hostApi);
176 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
177 <<
": Pa_GetHostApiInfo() failed\n");
181 ss <<
"libportaudio Default Host API Info:" << endl
182 <<
" structVersion: " << api_info->structVersion << endl
183 <<
" name: " << api_info->name << endl
184 <<
" deviceCount: " << api_info->deviceCount << endl
185 <<
" defaultInputDevice: " << api_info->defaultInputDevice << endl
186 <<
" defaultOutputDevice: " << api_info->defaultOutputDevice << endl;
196 #ifndef NSOUND_LIBPORTAUDIO
198 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
199 <<
": Nsound was not compiled with libportaudio support.\n");
208 std::stringstream ss;
210 PaError ecode = Pa_Initialize();
212 if(ecode != paNoError)
215 <<
"Nsound::AudioBackendLibportaudio::initialize():"
217 <<
": Pa_Initialize() failed"
219 << Pa_GetErrorText(ecode)
232 <<
"Nsound::AudioBackendLibportaudio::initialize():"
234 <<
": failed to allocate output params struct."
249 <<
"Nsound::AudioBackendLibportaudio::initialize():"
251 <<
": Pa_GetDefaultOutputDevice() failed"
282 <<
"Nsound::AudioBackendLibportaudio::initialize():"
285 <<
" bits per sample"
299 Pa_GetDeviceInfo(
out_params_->device)->defaultHighOutputLatency;
304 ecode = Pa_OpenStream(
314 if(ecode != paNoError)
317 <<
"Nsound::AudioBackendLibportaudio::initialize():"
319 <<
": Pa_OpenStream() failed"
321 << Pa_GetErrorText(ecode)
346 uint32 n_frames_per_buffer,
347 PaSampleFormat format)
353 int8 * data_ptr =
reinterpret_cast<int8 *
>(data);
354 return Pa_WriteStream(
356 reinterpret_cast<void *>(data_ptr + position),
357 n_frames_per_buffer);
362 int16 * data_ptr =
reinterpret_cast<int16 *
>(data);
363 return Pa_WriteStream(
365 reinterpret_cast<void *>(data_ptr + position),
366 n_frames_per_buffer);
371 int32 * data_ptr =
reinterpret_cast<int32 *
>(data);
372 return Pa_WriteStream(
374 reinterpret_cast<void *>(data_ptr + position),
375 n_frames_per_buffer);
379 M_THROW(
"Nsound::AudioBackendLibportaudio::initialize():"
380 <<
": can't handle bits per sample type: "
384 return paSampleFormatNotSupported;
391 #ifndef NSOUND_LIBPORTAUDIO
393 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
394 <<
": Nsound was not compiled with libportaudio support.\n");
411 <<
"Nsound::AudioBackendLibportaudio::play():"
422 PaError ecode = Pa_StartStream(
stream_);
424 if(ecode != paNoError)
427 <<
"Nsound::AudioBackendLibportaudio::play():"
429 <<
": Pa_StartStream() failed"
431 << Pa_GetErrorText(ecode)
463 <<
"Nsound::AudioBackendLibportaudio::initialize():"
465 <<
": can't handle bits per sample type: "
482 uint32 bytes_per_sample = bits / 8;
483 uint32 n_samples = n_bytes / bytes_per_sample / n_channels;
495 n_frames_per_buffer_,
498 if(ecode != paNoError)
501 <<
"Nsound::AudioBackendLibportaudio::play():"
503 <<
": Pa_WriteStream() failed"
505 << Pa_GetErrorText(ecode)
529 if(ecode != paNoError)
532 <<
"Nsound::AudioBackendLibportaudio::play():"
534 <<
": Pa_WriteStream() failed"
536 << Pa_GetErrorText(ecode)
559 #if defined(NSOUND_PLATFORM_OS_LINUX)
561 wait_time.tv_sec = 0;
562 wait_time.tv_nsec = 500000000;
564 nanosleep(&wait_time, NULL);
568 ecode = Pa_StopStream(
stream_);
570 if(ecode != paNoError)
573 <<
"Nsound::AudioBackendLibportaudio::play():"
575 <<
": Pa_StopStream() failed"
577 << Pa_GetErrorText(ecode)
600 std::transform(y.begin(), y.end(), y.begin(), ::tolower);
610 uint32 n_host_apis = Pa_GetHostApiCount();
612 for(
uint32 i = 0; i < n_host_apis; ++i)
614 const PaHostApiInfo * info = Pa_GetHostApiInfo(i);
618 std::string short_name =
lower(std::string(info->name));
621 <<
"Libportaudio: found driver '"
625 <<
", deviceCount = "
627 <<
", defaultOutputDevice = "
628 << info->defaultOutputDevice
629 <<
"\nPLAYBACK STARTING ...";
640 cout <<
" STOPPED\n";
654 std::stringstream ss(x);
662 M_THROW(
"Nsound::AudioBackendLibportaudio::setOption():"
663 <<
": could not convert '"
665 <<
"' to an integer.\n");
674 setOption(
const std::string & key,
const std::string & value)
676 #ifndef NSOUND_LIBPORTAUDIO
678 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
679 <<
": Nsound was not compiled with libportaudio support.\n");
683 std::string k =
lower(key);
685 if(k ==
"frames_per_buffer")
697 std::string driver_name =
lower(value);
699 uint32 n_host_apis = Pa_GetHostApiCount();
701 bool found_driver =
false;
703 for(
uint32 i = 0; i < n_host_apis; ++i)
705 const PaHostApiInfo * info = Pa_GetHostApiInfo(i);
709 std::string short_name =
lower(std::string(info->name));
711 if(driver_name == short_name)
722 M_THROW(
"Nsound::AudioBackendLibportaudio::setOption():"
723 <<
": failed to select driver '"
732 M_THROW(
"Nsound::AudioBackendLibportaudio::setOption():"
733 <<
": unrecognized key '"
746 #ifndef NSOUND_LIBPORTAUDIO
748 M_THROW(
"Nsound::AudioBackendLibportaudio::getInfo():"
749 <<
": Nsound was not compiled with libportaudio support.\n");
static std::string lower(const std::string &x)
uint32 n_frames_per_buffer_
AudioBackendLibportaudio(uint32 sample_rate=44100, uint32 channels=1, uint32 bits_per_sample=16)
std::stringstream error_buffer_
~AudioBackendLibportaudio()
PaError write_stream(PaStream *stream, void *data, uint32 position, uint32 n_frames_per_buffer, PaSampleFormat format)
static int32 integer(const std::string &x, Nsound::AudioBackend::State *state)
PaStreamParameters * out_params_
void setOption(const std::string &key, const std::string &value)
Set Libportaudio options.
void scanDevices(AudioPlayback &pb, const AudioStream &test_clip)
void play(void *data, uint32 n_bytes)
void play(const AudioStream &a)
Plays the AudioStream throuh the backend.
AudioBackendType getBackendType()