Nsound  0.9.4
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
Nsound::AudioBackendLibao Class Reference

#include <Nsound/AudioBackendLibao.h>

Inheritance diagram for Nsound::AudioBackendLibao:
Inheritance graph
[legend]

Public Types

enum  State { BACKEND_NOT_INITIALIZED, BACKEND_READY, BACKEND_ERROR }
 

Public Member Functions

 AudioBackendLibao (uint32 sample_rate=44100, uint32 channels=1, uint32 bits_per_sample=16)
 
 ~AudioBackendLibao ()
 
AudioBackendType getBackendType ()
 
std::string getError ()
 
std::string getInfo ()
 
void initialize ()
 
void play (void *data, uint32 n_bytes)
 
void scanDevices (AudioPlayback &pb, const AudioStream &test_clip)
 
void setOption (const std::string &key, const std::string &value)
 Set libao options. More...
 
void shutdown ()
 
uint32 getBitsPerSample ()
 
uint32 getChannels ()
 
uint32 getSampleRate ()
 
State getState ()
 
std::string getStateString ()
 Returns the backend state. More...
 

Static Public Member Functions

static std::string getStateString (const AudioBackend::State &state)
 Returns the backend state string. More...
 

Protected Attributes

uint32 sample_rate_
 
uint32 channels_
 
uint32 bits_per_sample_
 
State state_
 

Private Member Functions

 AudioBackendLibao (const AudioBackendLibao &copy)
 
AudioBackendLibaooperator= (const AudioBackendLibao &rhs)
 

Private Attributes

std::vector< std::string > options_
 
std::stringstream error_buffer_
 
int32 driver_id_
 
ao_device * device_ptr_
 

Detailed Description

Definition at line 50 of file AudioBackendLibao.h.

Member Enumeration Documentation

Enumerator
BACKEND_NOT_INITIALIZED 
BACKEND_READY 
BACKEND_ERROR 

Definition at line 52 of file AudioBackend.h.

Constructor & Destructor Documentation

AudioBackendLibao::AudioBackendLibao ( uint32  sample_rate = 44100,
uint32  channels = 1,
uint32  bits_per_sample = 16 
)

Definition at line 75 of file AudioBackendLibao.cc.

References initialize().

79  :
80  AudioBackend(sample_rate, channels, bits_per_sample),
81  options_(),
82  error_buffer_(""),
83  driver_id_(-1),
84  device_ptr_(NULL)
85 {
86  initialize();
87 };
AudioBackend(uint32 sample_rate=44100, uint32 channels=1, uint32 bits_per_sample=16)
Definition: AudioBackend.h:59
std::vector< std::string > options_
std::stringstream error_buffer_
AudioBackendLibao::~AudioBackendLibao ( )

Definition at line 90 of file AudioBackendLibao.cc.

References shutdown().

91 {
92  shutdown();
93 }
Nsound::AudioBackendLibao::AudioBackendLibao ( const AudioBackendLibao copy)
inlineprivate

Definition at line 111 of file AudioBackendLibao.h.

112  :
113  AudioBackend(),
114  options_(),
115  error_buffer_(""),
116  driver_id_(-1),
117  device_ptr_(NULL){};
AudioBackend(uint32 sample_rate=44100, uint32 channels=1, uint32 bits_per_sample=16)
Definition: AudioBackend.h:59
std::vector< std::string > options_
std::stringstream error_buffer_

Member Function Documentation

AudioBackendType AudioBackendLibao::getBackendType ( )
virtual

Implements Nsound::AudioBackend.

Definition at line 97 of file AudioBackendLibao.cc.

References Nsound::BACKEND_TYPE_LIBAO.

std::string AudioBackendLibao::getError ( )
virtual

Implements Nsound::AudioBackend.

Definition at line 104 of file AudioBackendLibao.cc.

References error_buffer_.

Referenced by getInfo().

105 {
106  return error_buffer_.str();
107 }
std::stringstream error_buffer_
std::string AudioBackendLibao::getInfo ( )
virtual

Implements Nsound::AudioBackend.

Definition at line 161 of file AudioBackendLibao.cc.

References Nsound::AudioBackend::BACKEND_ERROR, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, driver_id_, getError(), M_THROW, print_info(), and Nsound::AudioBackend::state_.

162 {
163  #ifndef NSOUND_LIBAO
165  M_THROW("Nsound::AudioBackendLibao::getInfo():"
166  << ": Nsound was not compiled with libao support.\n");
167  return;
168  #else
170  {
171  return "Nsound::AudioBackendLibao::getInfo(): "
172  "Backend not initialized yet";
173  }
174  else if(state_ == BACKEND_ERROR)
175  {
176  return getError();
177  }
178 
179  ao_info * info = ao_driver_info(driver_id_);
180 
181  if(info == NULL)
182  {
183  return "Nsound::AudioBackendLibao::getInfo(): "
184  "ao_driver_info() failed";
185  }
186 
187  return print_info(info);
188  #endif
189 }
#define M_THROW(message)
Definition: Macros.h:108
std::string print_info(ao_info *info)
void AudioBackendLibao::initialize ( )
virtual

Implements Nsound::AudioBackend.

Definition at line 193 of file AudioBackendLibao.cc.

References Nsound::AudioBackend::BACKEND_ERROR, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, Nsound::AudioBackend::BACKEND_READY, Nsound::AudioBackend::bits_per_sample_, Nsound::AudioBackend::channels_, device_ptr_, driver_id_, error_buffer_, M_THROW, options_, Nsound::AudioBackend::sample_rate_, and Nsound::AudioBackend::state_.

Referenced by AudioBackendLibao(), and scanDevices().

194 {
195  #ifndef NSOUND_LIBAO
197  M_THROW("Nsound::AudioBackendLibao::initialize():"
198  << ": Nsound was not compiled with libao support.\n");
199  return;
200  #else
201 
202  // Only initialize if the back end has not been initialized yet.
204  {
205  return;
206  }
207 
208  ao_initialize();
209 
210  if(driver_id_ < 0)
211  {
212  driver_id_ = ao_default_driver_id();
213  }
214 
215  if(driver_id_ < 0)
216  {
217  ao_shutdown();
218 
219  error_buffer_ << "ao_default_driver_id() failed" << endl;
220 
222 
223  return;
224  }
225 
226  // Setup the sample format.
227  ao_sample_format format;
228 
229  memset(&format, 0, sizeof(ao_sample_format));
230 
231  format.bits = bits_per_sample_;
232  format.rate = sample_rate_;
233  format.channels = channels_;
234 
235  #ifdef NSOUND_LITTLE_ENDIAN
236  format.byte_format = AO_FMT_LITTLE;
237  #else
238  format.byte_format = AO_FMT_BIG;
239  #endif
240 
241  // Need to add config check for the matrix field, for now, just
242  // disable.
243 
244 //~ char matrix[16];
245 //~ format.matrix = matrix;
246 
247 //~ switch(channels_)
248 //~ {
249 //~ case 1:
250 //~ sprintf(matrix, "M");
251 //~ break;
252 
253 //~ case 2:
254 //~ sprintf(matrix, "L,R");
255 //~ break;
256 
257 //~ default:
258 //~ error_buffer_
259 //~ << "Nsound::AudioBackendLibao::initialize(): "
260 //~ << "Don't know how to map "
261 //~ << channels_
262 //~ << " channels to the ao_sample_format.matrix"
263 //~ << endl;
264 
265 //~ shutdown();
266 //~ state_ = BACKEND_ERROR;
267 
268 //~ return;
269 //~ }
270 
271  // Setup any options.
272 
273  ao_option * options = NULL;
274 
275  uint32 n_options = static_cast<uint32>(options_.size());
276 
277  int32 ecode = 0;
278 
279  if(n_options >= 2)
280  {
281  for(uint32 i = 0; i < n_options; i += 2)
282  {
283  ecode = ao_append_option(
284  &options,
285  options_[i ].c_str(),
286  options_[i + 1].c_str());
287 
288  if(ecode != 1)
289  {
290  ao_shutdown();
291 
293  << "Nsound::AudioBackendLibao::initialize():"
294  << __LINE__
295  << ": error appending libao '"
296  << options_[i]
297  << "' : '"
298  << options_[i + 1]
299  << "' device option"
300  << endl;
301 
303 
304  return;
305  }
306  }
307  }
308 
309  device_ptr_ = ao_open_live(driver_id_, &format, options);
310 
311  ao_free_options(options);
312 
313  if(device_ptr_ == NULL)
314  {
315  ao_shutdown();
316 
318  << "Nsound::AudioBackendLibao::initialize():"
319  << __LINE__
320  << ": ao_open_live() failed:"
321  << endl;
322 
323  switch(errno)
324  {
325  case AO_ENODRIVER:
327  << "No driver corresponds to driver_id ("
328  << driver_id_
329  << ")"
330  << endl;
331  break;
332 
333  case AO_ENOTLIVE:
335  << "This driver is not a live output device"
336  << endl;
337  break;
338 
339  case AO_EBADOPTION:
341  << "A valid option key has an invalid value"
342  << endl;
343  break;
344 
345  case AO_EOPENDEVICE:
347  << "Cannot open the device (for example, if "
348  << "/dev/dsp cannot be opened for writing)"
349  << endl;
350  break;
351 
352  case AO_EFAIL:
354  << "Any other cause of failure"
355  << endl;
356  break;
357 
358  default:
360  << "Reason unknown"
361  << endl;
362  break;
363  }
364 
366 
367  return;
368  }
369 
371  #endif
372 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< std::string > options_
#define M_THROW(message)
Definition: Macros.h:108
signed int int32
Definition: Nsound.h:142
std::stringstream error_buffer_
void AudioBackendLibao::play ( void *  data,
uint32  n_bytes 
)
virtual

Implements Nsound::AudioBackend.

Definition at line 376 of file AudioBackendLibao.cc.

References Nsound::AudioBackend::BACKEND_ERROR, Nsound::AudioBackend::BACKEND_READY, device_ptr_, error_buffer_, M_THROW, and Nsound::AudioBackend::state_.

377 {
378  #ifndef NSOUND_LIBAO
380  M_THROW("Nsound::AudioBackendLibao::play():"
381  << ": Nsound was not compiled with libao support.\n");
382  return;
383  #else
384 
385  if(state_ != BACKEND_READY)
386  {
387  return;
388  }
389 
390  if(n_bytes == 0)
391  {
392  return;
393  }
394 
395  if(data == NULL)
396  {
398  << "AudioBackendLibao::play():"
399  << __LINE__
400  << ": data == NULL"
401  << endl;
402 
404 
405  return;
406  }
407 
408  int32 ecode = 0;
409 
410  ecode = ao_play(
411  device_ptr_,
412  reinterpret_cast<char *>(data),
413  n_bytes);
414 
415  if(ecode == 0)
416  {
418 
419  ao_close(device_ptr_);
420  ao_shutdown();
421 
423  << "AudioBackendLibao::play():"
424  << __LINE__
425  << ": ao_play() failed"
426  << endl;
427 
428  return;
429  }
430 
431  #endif
432 }
#define M_THROW(message)
Definition: Macros.h:108
signed int int32
Definition: Nsound.h:142
std::stringstream error_buffer_
void AudioBackendLibao::scanDevices ( AudioPlayback pb,
const AudioStream test_clip 
)
virtual

Implements Nsound::AudioBackend.

Definition at line 467 of file AudioBackendLibao.cc.

References driver_id_, driver_types, initialize(), N_DRIVER_TYPES, Nsound::AudioPlayback::play(), and shutdown().

468 {
469  for(uint32 i = 0; i < N_DRIVER_TYPES; ++i)
470  {
471  int32 id = ao_driver_id(driver_types[i].c_str());
472 
473  if(id >= 0)
474  {
475  shutdown();
476  driver_id_ = id;
477  initialize();
478 
479  cout << "Libao: found driver '"
480  << driver_types[i].c_str()
481  << "', id = "
482  << id
483  << "\nPLAYBACK STARTING ...";
484  cout.flush();
485 
486  pb.play(test_clip);
487 
488  cout << " STOPPED\n";
489  cout.flush();
490 
491  shutdown();
492  }
493  }
494 }
unsigned int uint32
Definition: Nsound.h:153
signed int int32
Definition: Nsound.h:142
const uint32 N_DRIVER_TYPES
std::string driver_types[N_DRIVER_TYPES]
void play(const AudioStream &a)
Plays the AudioStream throuh the backend.
void AudioBackendLibao::setOption ( const std::string &  key,
const std::string &  value 
)
virtual

Set libao options.

Available options:

Key Value
"driver" "alsa", "arts", "axis", "esd", "irix", "macosx", "nas", "oss", "pulse", "roar", "sndio", "sun", "wmm"

Each libao driver has specific options. Please read the labao documentation for the options here.

Implements Nsound::AudioBackend.

Definition at line 498 of file AudioBackendLibao.cc.

References Nsound::AudioBackend::BACKEND_ERROR, driver_id_, integer(), lower(), M_THROW, options_, and Nsound::AudioBackend::state_.

499 {
500  #ifndef NSOUND_LIBAO
502  M_THROW("Nsound::AudioBackendLibao::setOption():"
503  << ": Nsound was not compiled with libao support.\n");
504  return;
505  #else
506 
507  std::string k = lower(key);
508 
509  // Special handeling if key == "id"
510  if(k == "id")
511  {
512  int32 id = integer(value, &state_);
513  if(id >= 0) driver_id_ = id;
514  }
515  else
516  if(k == "driver")
517  {
518  int32 id = ao_driver_id(value.c_str());
519 
520  cerr << "key = 'driver', value = '"
521  << value.c_str()
522  << "', id = "
523  << id
524  << "\n";
525  cerr.flush();
526 
527  if(id >= 0)
528  {
529  driver_id_ = id;
530  }
531  else
532  {
534  M_THROW("Nsound::AudioBackendLibao::setOption():"
535  << ": failed to select driver '"
536  << value
537  << "'\n");
538  return;
539  }
540  }
541  else
542  {
543  options_.push_back(key);
544  options_.push_back(value);
545  }
546  #endif
547 }
static int32 integer(const std::string &x, Nsound::AudioBackend::State *state)
static std::string lower(const std::string &x)
std::vector< std::string > options_
#define M_THROW(message)
Definition: Macros.h:108
signed int int32
Definition: Nsound.h:142
void AudioBackendLibao::shutdown ( )
virtual

Implements Nsound::AudioBackend.

Definition at line 551 of file AudioBackendLibao.cc.

References Nsound::AudioBackend::BACKEND_ERROR, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, device_ptr_, driver_id_, M_THROW, and Nsound::AudioBackend::state_.

Referenced by scanDevices(), and ~AudioBackendLibao().

552 {
553  #ifndef NSOUND_LIBAO
555  M_THROW("Nsound::AudioBackendLibao::shutdown():"
556  << ": Nsound was not compiled with libao support.\n");
557  return;
558  #else
559  if(device_ptr_ != NULL)
560  {
561  ao_close(device_ptr_);
562  device_ptr_ = NULL;
563  driver_id_ = -1;
564  }
565 
566  ao_shutdown();
568  #endif
569 }
#define M_THROW(message)
Definition: Macros.h:108
AudioBackendLibao& Nsound::AudioBackendLibao::operator= ( const AudioBackendLibao rhs)
inlineprivate

Definition at line 119 of file AudioBackendLibao.h.

119 {return *this;};
uint32 Nsound::AudioBackend::getBitsPerSample ( )
inlineinherited

Definition at line 77 of file AudioBackend.h.

References Nsound::AudioBackend::bits_per_sample_.

Referenced by Nsound::AudioPlayback::getBitsPerSample().

77 {return bits_per_sample_;};
uint32 Nsound::AudioBackend::getChannels ( )
inlineinherited

Definition at line 80 of file AudioBackend.h.

References Nsound::AudioBackend::channels_.

Referenced by Nsound::AudioPlayback::getChannels(), and play_int().

80 {return channels_;};
uint32 Nsound::AudioBackend::getSampleRate ( )
inlineinherited

Definition at line 91 of file AudioBackend.h.

References Nsound::AudioBackend::sample_rate_.

Referenced by Nsound::AudioPlayback::getSampleRate().

91 {return sample_rate_;};
State Nsound::AudioBackend::getState ( )
inlineinherited
std::string AudioBackend::getStateString ( )
inherited

Returns the backend state.

Definition at line 43 of file AudioBackend.cc.

References Nsound::AudioBackend::state_, and state_strings.

Referenced by Nsound::AudioPlayback::getStateString().

44 {
45  return state_strings[state_];
46 }
std::string state_strings[3]
Definition: AudioBackend.cc:33
std::string AudioBackend::getStateString ( const AudioBackend::State state)
staticinherited

Returns the backend state string.

Definition at line 51 of file AudioBackend.cc.

References Nsound::AudioBackend::BACKEND_ERROR, Nsound::AudioBackend::BACKEND_NOT_INITIALIZED, and state_strings.

52 {
53  if( state >= BACKEND_NOT_INITIALIZED &&
54  state <= BACKEND_ERROR)
55  {
56  return state_strings[state];
57  }
58 
59  return "Unknown Backend State";
60 }
std::string state_strings[3]
Definition: AudioBackend.cc:33

Member Data Documentation

std::vector< std::string > Nsound::AudioBackendLibao::options_
private

Definition at line 119 of file AudioBackendLibao.h.

Referenced by initialize(), and setOption().

std::stringstream Nsound::AudioBackendLibao::error_buffer_
private

Definition at line 122 of file AudioBackendLibao.h.

Referenced by getError(), initialize(), and play().

int32 Nsound::AudioBackendLibao::driver_id_
private

Definition at line 124 of file AudioBackendLibao.h.

Referenced by getInfo(), initialize(), scanDevices(), setOption(), and shutdown().

ao_device* Nsound::AudioBackendLibao::device_ptr_
private

Definition at line 125 of file AudioBackendLibao.h.

Referenced by initialize(), play(), and shutdown().

uint32 Nsound::AudioBackend::sample_rate_
protectedinherited
uint32 Nsound::AudioBackend::channels_
protectedinherited
uint32 Nsound::AudioBackend::bits_per_sample_
protectedinherited
State Nsound::AudioBackend::state_
protectedinherited

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