Nsound  0.9.4
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Nsound::AudioPlayback Class Reference

#include <Nsound/AudioPlayback.h>

Public Member Functions

 AudioPlayback (const float64 &sample_rate=44100.0, const uint32 channels=1, const uint32 bits_per_sample=16)
 
 ~AudioPlayback ()
 
uint32 getBitsPerSample ()
 
void setBitsPerSample (uint32 bits_per_sample)
 
uint32 getChannels ()
 
void setChannels (uint32 channels)
 
std::string getError ()
 Returns an error string describing any backend error. More...
 
std::string getInfo ()
 Returns information about the backend driver. More...
 
uint32 getSampleRate ()
 
void setSampleRate (uint32 sample_rate)
 
AudioBackend::State getState ()
 Returns the backend state. More...
 
std::string getStateString ()
 Returns the backend state string. More...
 
std::string getStateString (const AudioBackend::State &state)
 Returns the backend state string. More...
 
void initialize ()
 Initializes the backend and transitions to the BACKEND_READY state on success. More...
 
void play (const AudioStream &a)
 Plays the AudioStream throuh the backend. More...
 
void play (const Buffer &b)
 Plays the Buffer through the backend. More...
 
void scanDevices ()
 Scans for devices and tries to play a test sound. More...
 
void setOption (const std::string &key, const std::string &value)
 Sets an options, must be called before initialize(). More...
 
void shutdown ()
 Shuts down the backend. More...
 

Static Public Member Functions

static void setBackendType (const AudioBackendType ab)
 Sets the AudioBackendType. More...
 
static AudioBackendType getBackendType ()
 Gets the AudioBackendType that is currently set. More...
 

Private Member Functions

 AudioPlayback (const AudioPlayback &copy)
 
AudioPlaybackoperator= (const AudioPlayback &rhs)
 

Private Attributes

uint32 sample_rate_
 
uint32 channels_
 
uint32 bits_per_sample_
 
AudioBackendbackend_
 

Static Private Attributes

static AudioBackendType backend_type_ = BACKEND_TYPE_NONE
 

Friends

void operator>> (const AudioStream &lhs, AudioPlayback &rhs)
 
void operator>> (const Buffer &lhs, AudioPlayback &rhs)
 

Detailed Description

Definition at line 50 of file AudioPlayback.h.

Constructor & Destructor Documentation

AudioPlayback::AudioPlayback ( const float64 sample_rate = 44100.0,
const uint32  channels = 1,
const uint32  bits_per_sample = 16 
)

Definition at line 115 of file AudioPlayback.cc.

References allocate_backend(), backend_, backend_type_, Nsound::BACKEND_TYPE_NONE, bits_per_sample_, channels_, M_THROW, and sample_rate_.

119  :
120  sample_rate_(static_cast<uint32>(sample_rate)),
121  channels_(channels),
122  bits_per_sample_(bits_per_sample),
123  backend_(NULL)
124 {
127  sample_rate_,
128  channels_,
130 
131  if(BACKEND_TYPE_NONE != backend_type_ && backend_ == NULL)
132  {
133  M_THROW("Nsound::AudioPlayback::AudioPlayback():"
134  << ": failed to create AudioBackend!");
135  }
136 };
AudioBackend * allocate_backend(const AudioBackendType &type, const uint32 sample_rate, const uint32 channels, const uint32 bits_per_sample)
AudioBackend * backend_
static AudioBackendType backend_type_
#define M_THROW(message)
Definition: Macros.h:108
AudioPlayback::~AudioPlayback ( )

Definition at line 139 of file AudioPlayback.cc.

References backend_, and Nsound::AudioBackend::shutdown().

140 {
141  if(backend_ != NULL)
142  {
143  backend_->shutdown();
144  delete backend_;
145  }
146 }
virtual void shutdown()=0
AudioBackend * backend_
Nsound::AudioPlayback::AudioPlayback ( const AudioPlayback copy)
inlineprivate

Definition at line 143 of file AudioPlayback.h.

144  :
145  sample_rate_(copy.sample_rate_),
146  channels_(copy.channels_),
147  bits_per_sample_(copy.bits_per_sample_),
148  backend_(NULL){};
AudioBackend * backend_

Member Function Documentation

void AudioPlayback::setBackendType ( const AudioBackendType  ab)
static

Sets the AudioBackendType.

Definition at line 102 of file AudioPlayback.cc.

References backend_type_.

Referenced by Nsound::use().

103 {
104  backend_type_ = ab;
105 }
static AudioBackendType backend_type_
AudioBackendType AudioPlayback::getBackendType ( )
static

Gets the AudioBackendType that is currently set.

Definition at line 109 of file AudioPlayback.cc.

References backend_type_.

110 {
111  return backend_type_;
112 }
static AudioBackendType backend_type_
uint32 AudioPlayback::getBitsPerSample ( )

Definition at line 150 of file AudioPlayback.cc.

References backend_, bits_per_sample_, and Nsound::AudioBackend::getBitsPerSample().

151 {
152  if(backend_ != NULL)
153  {
154  return backend_->getBitsPerSample();
155  }
156 
157  return bits_per_sample_;
158 }
AudioBackend * backend_
uint32 getBitsPerSample()
Definition: AudioBackend.h:77
void Nsound::AudioPlayback::setBitsPerSample ( uint32  bits_per_sample)
inline

Definition at line 75 of file AudioPlayback.h.

References bits_per_sample_.

76  {bits_per_sample_ = bits_per_sample;};
uint32 AudioPlayback::getChannels ( )

Definition at line 162 of file AudioPlayback.cc.

References backend_, channels_, and Nsound::AudioBackend::getChannels().

163 {
164  if(backend_ != NULL)
165  {
166  return backend_->getChannels();
167  }
168 
169  return channels_;
170 }
AudioBackend * backend_
void Nsound::AudioPlayback::setChannels ( uint32  channels)
inline

Definition at line 82 of file AudioPlayback.h.

References channels_.

83  {channels_ = channels;};
std::string AudioPlayback::getError ( )

Returns an error string describing any backend error.

Definition at line 174 of file AudioPlayback.cc.

References backend_, and Nsound::AudioBackend::getError().

175 {
176  if(backend_ != NULL)
177  {
178  return backend_->getError();
179  }
180 
181  std::stringstream ss;
182 
183  ss << "Nsound::AudioPlayback::getError():"
184  << __LINE__
185  << ": backend not initialized"
186  << endl;
187 
188  return ss.str();
189 }
AudioBackend * backend_
virtual std::string getError()=0
std::string AudioPlayback::getInfo ( )

Returns information about the backend driver.

Definition at line 193 of file AudioPlayback.cc.

References backend_, and Nsound::AudioBackend::getInfo().

194 {
195  if(backend_ != NULL)
196  {
197  return backend_->getInfo();
198  }
199 
200  std::stringstream ss;
201 
202  ss << "Nsound::AudioPlayback::getInfo():"
203  << __LINE__
204  << ": backend not initialized"
205  << endl;
206 
207  return ss.str();
208 }
virtual std::string getInfo()=0
AudioBackend * backend_
uint32 AudioPlayback::getSampleRate ( )

Definition at line 212 of file AudioPlayback.cc.

References backend_, Nsound::AudioBackend::getSampleRate(), and sample_rate_.

213 {
214  if(backend_ != NULL)
215  {
216  return backend_->getSampleRate();
217  }
218 
219  return sample_rate_;
220 }
AudioBackend * backend_
void Nsound::AudioPlayback::setSampleRate ( uint32  sample_rate)
inline

Definition at line 97 of file AudioPlayback.h.

References sample_rate_.

98  {sample_rate_ = sample_rate;};
AudioBackend::State AudioPlayback::getState ( )

Returns the backend state.

Definition at line 224 of file AudioPlayback.cc.

References backend_, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, and Nsound::AudioBackend::getState().

225 {
226  if(backend_ != NULL)
227  {
228  return backend_->getState();
229  }
230 
232 }
AudioBackend * backend_
std::string AudioPlayback::getStateString ( )

Returns the backend state string.

Definition at line 236 of file AudioPlayback.cc.

References backend_, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, and Nsound::AudioBackend::getStateString().

237 {
238  if(backend_ != NULL)
239  {
240  return backend_->getStateString();
241  }
242 
244 }
AudioBackend * backend_
std::string getStateString()
Returns the backend state.
Definition: AudioBackend.cc:43
std::string AudioPlayback::getStateString ( const AudioBackend::State state)

Returns the backend state string.

Definition at line 248 of file AudioPlayback.cc.

References Nsound::AudioBackend::getStateString().

249 {
250  return AudioBackend::getStateString(state);
251 }
std::string getStateString()
Returns the backend state.
Definition: AudioBackend.cc:43
void AudioPlayback::initialize ( )

Initializes the backend and transitions to the BACKEND_READY state on success.

Definition at line 255 of file AudioPlayback.cc.

References allocate_backend(), backend_, Nsound::AudioBackend::BACKEND_READY, backend_type_, Nsound::BACKEND_TYPE_NONE, bits_per_sample_, channels_, Nsound::AudioBackend::getError(), Nsound::AudioBackend::getState(), Nsound::AudioBackend::initialize(), M_THROW, and sample_rate_.

Referenced by _play(), and scanDevices().

256 {
258  {
259  M_THROW("Nsound::AudioPlayback::initialize():"
260  << ": no backend selected or available");
261  return;
262  }
263 
264  // If the backend is currently NULL, recreate it with current settings
265  if(backend_ == NULL)
266  {
269  sample_rate_,
270  channels_,
272  }
273 
274  if(backend_ == NULL)
275  {
276  M_THROW("Nsound::AudioPlayback::initialize():"
277  << ": failed to initialize AudioBackend!");
278 
279  return;
280  }
281 
282  backend_->initialize();
283 
285  {
286  M_THROW("Nsound::AudioPlayback::initialize():"
287  << ": backend faild to initialize\n"
288  << "Backend error: "
289  << backend_->getError());
290  }
291 }
AudioBackend * allocate_backend(const AudioBackendType &type, const uint32 sample_rate, const uint32 channels, const uint32 bits_per_sample)
AudioBackend * backend_
virtual std::string getError()=0
static AudioBackendType backend_type_
#define M_THROW(message)
Definition: Macros.h:108
virtual void initialize()=0
void AudioPlayback::play ( const AudioStream a)

Plays the AudioStream throuh the backend.

Definition at line 435 of file AudioPlayback.cc.

References backend_.

Referenced by Nsound::operator>>(), Nsound::AudioBackendLibao::scanDevices(), and Nsound::AudioBackendLibportaudio::scanDevices().

436 {
437  _play<AudioStream>(this, &backend_, a);
438 }
AudioBackend * backend_
void AudioPlayback::play ( const Buffer b)

Plays the Buffer through the backend.

Definition at line 442 of file AudioPlayback.cc.

References backend_.

443 {
444  _play<Buffer>(this, &backend_, b);
445 }
AudioBackend * backend_
void AudioPlayback::scanDevices ( )

Scans for devices and tries to play a test sound.

Definition at line 449 of file AudioPlayback.cc.

References allocate_backend(), backend_, Nsound::AudioBackend::BACKEND_READY, bits_per_sample_, channels_, Nsound::getBackends(), Nsound::getBackendTypes(), Nsound::AudioBackend::getError(), Nsound::AudioBackend::getState(), initialize(), Nsound::GuitarBass::play(), sample_rate_, and Nsound::AudioBackend::scanDevices().

450 {
451  GuitarBass guitar(sample_rate_);
452 
453  AudioStream test_clip(sample_rate_, channels_);
454 
455  test_clip << guitar.play();
456 
457  initialize();
458 
459  std::vector< std::string > names = Nsound::getBackends();
460  std::vector< AudioBackendType > types = Nsound::getBackendTypes();
461 
462  cout << "Nsound::AudioPlayback::scanDevices(): starting\n";
463  cout.flush();
464 
465  if(types.size() == 0)
466  {
467  cout << "No backends available\n";
468  cout.flush();
469  }
470 
471  for(uint32 i = 0; i < types.size(); ++i)
472  {
473  cout << "Selecting backend '"
474  << names[i]
475  << "'\n";
476  cout.flush();
477 
478  AudioBackend * orig = backend_;
479 
481  types[i],
482  sample_rate_,
483  channels_,
485 
487  {
488  backend_->scanDevices(*this, test_clip);
489  }
490  else
491  {
492  cout << "Backend '"
493  << names[i]
494  << "' failed to initialize\n"
495  << backend_->getError();
496  cout.flush();
497  }
498 
499  delete backend_;
500 
501  backend_ = orig;
502  }
503 
504  cout << "Nsound::AudioPlayback::scanDevices(): finished\n";
505  cout.flush();
506 }
unsigned int uint32
Definition: Nsound.h:153
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 scanDevices(AudioPlayback &pb, const AudioStream &test_clip)=0
AudioBackend * backend_
virtual std::string getError()=0
std::vector< AudioBackendType > getBackendTypes()
Returns a list of the available audio backends types.
std::vector< std::string > getBackends()
Returns a list of the available audio backends by name.
Class Drum.
Definition: GuitarBass.h:50
void AudioPlayback::setOption ( const std::string &  key,
const std::string &  value 
)

Sets an options, must be called before initialize().

Definition at line 510 of file AudioPlayback.cc.

References backend_, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, Nsound::AudioBackend::getState(), M_THROW, and Nsound::AudioBackend::setOption().

511 {
512  if(backend_ == NULL)
513  {
514  M_THROW("Nsound::AudioPlayback::setOption():"
515  << ": backend is NULL");
516  return;
517  }
519  {
520  M_THROW("Nsound::AudioPlayback::setOption():"
521  << ": backend already initialized");
522  return;
523  }
524 
525  backend_->setOption(key, value);
526 }
virtual void setOption(const std::string &key, const std::string &value)=0
AudioBackend * backend_
#define M_THROW(message)
Definition: Macros.h:108
void AudioPlayback::shutdown ( )

Shuts down the backend.

Definition at line 530 of file AudioPlayback.cc.

References backend_, and Nsound::AudioBackend::shutdown().

531 {
532  if(backend_ != NULL)
533  {
534  backend_->shutdown();
535  delete backend_;
536  backend_ = NULL;
537  }
538 }
virtual void shutdown()=0
AudioBackend * backend_
AudioPlayback& Nsound::AudioPlayback::operator= ( const AudioPlayback rhs)
inlineprivate

Definition at line 150 of file AudioPlayback.h.

150 {return *this;};

Friends And Related Function Documentation

void operator>> ( const AudioStream lhs,
AudioPlayback rhs 
)
friend
void operator>> ( const Buffer lhs,
AudioPlayback rhs 
)
friend

Member Data Documentation

uint32 Nsound::AudioPlayback::sample_rate_
private

Definition at line 150 of file AudioPlayback.h.

Referenced by AudioPlayback(), getSampleRate(), initialize(), scanDevices(), and setSampleRate().

uint32 Nsound::AudioPlayback::channels_
private

Definition at line 155 of file AudioPlayback.h.

Referenced by AudioPlayback(), getChannels(), initialize(), scanDevices(), and setChannels().

uint32 Nsound::AudioPlayback::bits_per_sample_
private
AudioBackend* Nsound::AudioPlayback::backend_
private
AudioBackendType AudioPlayback::backend_type_ = BACKEND_TYPE_NONE
staticprivate

Definition at line 160 of file AudioPlayback.h.

Referenced by AudioPlayback(), getBackendType(), initialize(), and setBackendType().


The documentation for this class was generated from the following files: