49 using namespace Nsound;
54 #ifdef NSOUND_LIBPORTAUDIO
56 #elif defined(NSOUND_LIBAO)
69 const uint32 bits_per_sample)
85 #ifdef NSOUND_LIBPORTAUDIO
120 sample_rate_(static_cast<
uint32>(sample_rate)),
122 bits_per_sample_(bits_per_sample),
133 M_THROW(
"Nsound::AudioPlayback::AudioPlayback():"
134 <<
": failed to create AudioBackend!");
181 std::stringstream ss;
183 ss <<
"Nsound::AudioPlayback::getError():"
185 <<
": backend not initialized"
200 std::stringstream ss;
202 ss <<
"Nsound::AudioPlayback::getInfo():"
204 <<
": backend not initialized"
259 M_THROW(
"Nsound::AudioPlayback::initialize():"
260 <<
": no backend selected or available");
276 M_THROW(
"Nsound::AudioPlayback::initialize():"
277 <<
": failed to initialize AudioBackend!");
286 M_THROW(
"Nsound::AudioPlayback::initialize():"
287 <<
": backend faild to initialize\n"
299 template <
typename T>
308 template <
typename T>
315 template <
typename T>
328 play_int<T>(backend, a[0], scale);
336 M_THROW(
"Nsound::AudioPlayback::play():"
337 <<
": AudioStream channels do not match backend channels ("
349 array =
new T [n_samples * n_channels];
353 M_THROW(
"Nsound::AudioPlayback::play():"
354 <<
": failed allocate memory");
359 for(
uint32 i = 0; i < n_samples; ++i)
361 for(
uint32 j = 0; j < n_channels; ++j)
363 array[k] =
static_cast<T
>(a[j][i] * scale);
369 reinterpret_cast<void *>(array),
370 n_samples * n_channels *
sizeof(T));
375 template <
typename T>
385 std::vector<T> array;
387 array.reserve(n_samples * n_channels);
390 for(
uint32 i = 0; i < n_samples; ++i)
392 for(
uint32 j = 0; j < n_channels; ++j)
394 array.push_back(static_cast<T>(b[i] * scale));
400 reinterpret_cast<void *>(array.data()),
401 n_samples * n_channels *
sizeof(T));
405 template <
typename T>
411 if(*be == NULL)
return;
413 uint32 bits = (*be)->getBitsPerSample();
418 play_int<int16>(*be, audio, 32768.0);
421 play_int<int32>(*be, audio, 2147483648.0);
425 M_THROW(
"Nsound::AudioPlayback::play():"
428 <<
"-bit playback not yet implemented");
437 _play<AudioStream>(
this, &
backend_, a);
455 test_clip << guitar.
play();
462 cout <<
"Nsound::AudioPlayback::scanDevices(): starting\n";
465 if(types.size() == 0)
467 cout <<
"No backends available\n";
471 for(
uint32 i = 0; i < types.size(); ++i)
473 cout <<
"Selecting backend '"
494 <<
"' failed to initialize\n"
504 cout <<
"Nsound::AudioPlayback::scanDevices(): finished\n";
510 setOption(
const std::string & key,
const std::string & value)
514 M_THROW(
"Nsound::AudioPlayback::setOption():"
515 <<
": backend is NULL");
520 M_THROW(
"Nsound::AudioPlayback::setOption():"
521 <<
": backend already initialized");
558 std::transform(y.begin(), y.end(), y.begin(), ::tolower);
564 use(
const std::string & backend)
566 std::string be =
mylower(backend);
568 if(be ==
"ao" || be ==
"libao")
573 if(be ==
"portaudio" || be ==
"libportaudio")
579 std::stringstream ss_buffer;
580 ss_buffer <<
"Nsound::use(): "
581 <<
"Unrecognized AudioBackend '"
588 ss_buffer <<
"Available backends are:" << endl;
590 for(
uint32 i = 0; i < vec.size(); ++i)
617 <<
"Unrecognized AudioBackendType "
618 << static_cast<uint32>(type));
622 std::vector< std::string >
626 std::vector< std::string > vec;
628 #if defined(NSOUND_LIBPORTAUDIO)
629 vec.push_back(
"portaudio");
632 #if defined(NSOUND_LIBAO)
639 std::vector< AudioBackendType >
643 std::vector< AudioBackendType > vec;
645 #if defined(NSOUND_LIBPORTAUDIO)
649 #if defined(NSOUND_LIBAO)
std::string getError()
Returns an error string describing any backend error.
static AudioBackendType getBackendType()
Gets the AudioBackendType that is currently set.
AudioBackend * allocate_backend(const AudioBackendType &type, const uint32 sample_rate, const uint32 channels, const uint32 bits_per_sample)
void initialize()
Initializes the backend and transitions to the BACKEND_READY state on success.
virtual void play(void *data, uint32 n_bytes)=0
virtual void scanDevices(AudioPlayback &pb, const AudioStream &test_clip)=0
void scanDevices()
Scans for devices and tries to play a test sound.
void setOption(const std::string &key, const std::string &value)
Sets an options, must be called before initialize().
AudioBackend::State getState()
Returns the backend state.
uint32 getLength() const
Returns the number of samples of audio data in the stream.
void _play(AudioPlayback *pb, AudioBackend **be, const T &audio)
virtual std::string getInfo()=0
virtual void setOption(const std::string &key, const std::string &value)=0
uint32 getBitsPerSample()
void use(const std::string &backend)
Selects the AudioBackend to use by name.
virtual void shutdown()=0
AudioPlayback(const float64 &sample_rate=44100.0, const uint32 channels=1, const uint32 bits_per_sample=16)
std::string getStateString()
Returns the backend state.
uint32 getLength() const
Returns the number of samples in the Buffer.
std::string mylower(const std::string &x)
void play_int(AudioBackend *backend, const AudioStream &a, const float64 &scale)
virtual std::string getError()=0
void shutdown()
Shuts down the backend.
static AudioBackendType backend_type_
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
void operator>>(const AudioStream &lhs, AudioPlayback &rhs)
static void setBackendType(const AudioBackendType ab)
Sets the AudioBackendType.
std::string getStateString()
Returns the backend state string.
AudioStream play()
Plays a demo for this instrument.
std::vector< AudioBackendType > getBackendTypes()
Returns a list of the available audio backends types.
std::string getInfo()
Returns information about the backend driver.
A Buffer for storing audio samples.
virtual void initialize()=0
uint32 getBitsPerSample()
void play(const AudioStream &a)
Plays the AudioStream throuh the backend.
std::vector< std::string > getBackends()
Returns a list of the available audio backends by name.