Nsound  0.9.4
Functions
AudioPlayback.cc File Reference
#include <Nsound/Nsound.h>
#include <Nsound/AudioBackend.h>
#include <Nsound/AudioBackendLibao.h>
#include <Nsound/AudioBackendLibportaudio.h>
#include <Nsound/AudioBackendType.h>
#include <Nsound/AudioPlayback.h>
#include <Nsound/AudioStream.h>
#include <Nsound/Buffer.h>
#include <Nsound/GuitarBass.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
Include dependency graph for AudioPlayback.cc:

Go to the source code of this file.

Functions

AudioBackendallocate_backend (const AudioBackendType &type, const uint32 sample_rate, const uint32 channels, const uint32 bits_per_sample)
 
template<typename T >
void play_int (AudioBackend *backend, const AudioStream &a, const float64 &scale)
 
template<typename T >
void play_int (AudioBackend *backend, const Buffer &b, const float64 &scale)
 
template<typename T >
void _play (AudioPlayback *pb, AudioBackend **be, const T &audio)
 
std::string mylower (const std::string &x)
 

Function Documentation

AudioBackend* allocate_backend ( const AudioBackendType type,
const uint32  sample_rate,
const uint32  channels,
const uint32  bits_per_sample 
)

Definition at line 65 of file AudioPlayback.cc.

References Nsound::BACKEND_TYPE_LIBAO, Nsound::BACKEND_TYPE_LIBPORTAUDIO, and Nsound::BACKEND_TYPE_NONE.

Referenced by Nsound::AudioPlayback::AudioPlayback(), Nsound::AudioPlayback::initialize(), and Nsound::AudioPlayback::scanDevices().

70 {
71  AudioBackend * backend = NULL;
72 
73  switch(type)
74  {
75  case BACKEND_TYPE_LIBAO:
76  #ifdef NSOUND_LIBAO
77  backend = new AudioBackendLibao(
78  sample_rate,
79  channels,
80  bits_per_sample);
81  #endif
82  break;
83 
85  #ifdef NSOUND_LIBPORTAUDIO
86  backend = new AudioBackendLibportaudio(
87  sample_rate,
88  channels,
89  bits_per_sample);
90  #endif
91  break;
92 
93  case BACKEND_TYPE_NONE:
94  break;
95  }
96 
97  return backend;
98 }
template<typename T >
void play_int ( AudioBackend backend,
const AudioStream a,
const float64 scale 
)

Definition at line 317 of file AudioPlayback.cc.

References Nsound::AudioBackend::getChannels(), Nsound::AudioStream::getLength(), Nsound::AudioStream::getNChannels(), M_THROW, and Nsound::AudioBackend::play().

321 {
322  uint32 n_channels = a.getNChannels();
323 
324  // If the AudioStream is mono, just call play() for the buffer method and
325  // return.
326  if(n_channels == 1)
327  {
328  play_int<T>(backend, a[0], scale);
329  return;
330  }
331 
332  // If the number of channels in the AudioStream doesn't agree with the
333  // number of channels of the backend, print error.
334  if(n_channels != backend->getChannels())
335  {
336  M_THROW("Nsound::AudioPlayback::play():"
337  << ": AudioStream channels do not match backend channels ("
338  << n_channels
339  << " != "
340  << backend->getChannels()
341  << ")");
342  return;
343  }
344 
345  T * array = NULL;
346 
347  uint32 n_samples = a.getLength();
348 
349  array = new T [n_samples * n_channels];
350 
351  if(array == NULL)
352  {
353  M_THROW("Nsound::AudioPlayback::play():"
354  << ": failed allocate memory");
355  return;
356  }
357 
358  uint32 k = 0;
359  for(uint32 i = 0; i < n_samples; ++i)
360  {
361  for(uint32 j = 0; j < n_channels; ++j)
362  {
363  array[k] = static_cast<T>(a[j][i] * scale);
364  ++k;
365  }
366  }
367 
368  backend->play(
369  reinterpret_cast<void *>(array),
370  n_samples * n_channels * sizeof(T));
371 
372  delete array;
373 }
unsigned int uint32
Definition: Nsound.h:153
virtual void play(void *data, uint32 n_bytes)=0
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
#define M_THROW(message)
Definition: Macros.h:108
template<typename T >
void play_int ( AudioBackend backend,
const Buffer b,
const float64 scale 
)

Definition at line 377 of file AudioPlayback.cc.

References Nsound::AudioBackend::getChannels(), Nsound::Buffer::getLength(), and Nsound::AudioBackend::play().

381 {
382  uint32 n_samples = b.getLength();
383  uint32 n_channels = backend->getChannels();
384 
385  std::vector<T> array;
386 
387  array.reserve(n_samples * n_channels);
388 
389  uint32 k = 0;
390  for(uint32 i = 0; i < n_samples; ++i)
391  {
392  for(uint32 j = 0; j < n_channels; ++j)
393  {
394  array.push_back(static_cast<T>(b[i] * scale));
395  ++k;
396  }
397  }
398 
399  backend->play(
400  reinterpret_cast<void *>(array.data()),
401  n_samples * n_channels * sizeof(T));
402 }
unsigned int uint32
Definition: Nsound.h:153
virtual void play(void *data, uint32 n_bytes)=0
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
template<typename T >
void _play ( AudioPlayback pb,
AudioBackend **  be,
const T &  audio 
)

Definition at line 407 of file AudioPlayback.cc.

References Nsound::AudioPlayback::initialize(), and M_THROW.

408 {
409  pb->initialize();
410 
411  if(*be == NULL) return;
412 
413  uint32 bits = (*be)->getBitsPerSample();
414 
415  switch(bits)
416  {
417  case 16:
418  play_int<int16>(*be, audio, 32768.0);
419  break;
420  case 32:
421  play_int<int32>(*be, audio, 2147483648.0);
422  break;
423 
424  default:
425  M_THROW("Nsound::AudioPlayback::play():"
426  << ": Support for "
427  << bits
428  << "-bit playback not yet implemented");
429  break;
430  }
431 }
unsigned int uint32
Definition: Nsound.h:153
void initialize()
Initializes the backend and transitions to the BACKEND_READY state on success.
#define M_THROW(message)
Definition: Macros.h:108
std::string mylower ( const std::string &  x)

Definition at line 555 of file AudioPlayback.cc.

Referenced by Nsound::use().

556 {
557  std::string y = x;
558  std::transform(y.begin(), y.end(), y.begin(), ::tolower);
559  return y;
560 }