SoundManager::SoundManager()
  : device(0), context(0), sound_enabled(false), music_source(0),
    music_enabled(true)
{
  try {
    device = alcOpenDevice(0);
    if(device == 0) {
      print_openal_version();
      throw std::runtime_error("Couldn't open audio device.");
    }

    int attributes[] = { 0 };
    context = alcCreateContext(device, attributes);
    check_alc_error("Couldn't create audio context: ");
    alcMakeContextCurrent(context);
    check_alc_error("Couldn't select audio context: ");

    check_al_error("Audio error after init: ");
    sound_enabled = true;
  } catch(std::exception& e) {
    device = 0;
    context = 0;
    log_warning << "Couldn't initialize audio device:" << e.what() << std::endl;
    print_openal_version();
    throw e;
  }
}
Ejemplo n.º 2
0
bool system::init()
{
    if(m_d->m_pDevice) {
        return true;
    }
    
    m_d->m_pDevice = alcOpenDevice(NULL);
    
    if(!m_d->m_pDevice) {
        std::cerr << "Default sound device not present" << std::endl;
        return false;
    }
    
    m_d->m_pContext = alcCreateContext(m_d->m_pDevice, NULL);
    if(!check_alc_error()) {
        return false;
    }
    
    alcMakeContextCurrent(m_d->m_pContext);
    
    ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    ALfloat ListenerOri[] = { 1.0, 1.0, 0.0,  0.0, 0.0, 1.0 };
    
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
    
    return true;
}
void
SoundManager::update()
{
  static float lasttime = real_time;

  if(real_time - lasttime < 0.3)
    return;
  lasttime = real_time;

  // update and check for finished sound sources
  for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
    SoundSource* source = *i;

    source->update();
    
    if(!source->playing()) {
      delete source;
      i = sources.erase(i);
    } else {
      ++i;
    }
  }
  // check streaming sounds
  if(music_source) {
    music_source->update();
  }
  
  alcProcessContext(context);
  check_alc_error("Error while processing audio context: ");
}
void
SoundManager::update()
{
  if (!sound_enabled)
    return;

  // check for finished sound sources
  for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
    SoundSource* source = *i;
    if(!source->playing()) {
      delete source;
      i = sources.erase(i);
    } else {
      ++i;
    }
  }
  // check streaming sounds
  if(music_source) {
    music_source->update();
  }
  
  if(next_music_source && (!music_source || !music_source->playing())) {
    delete music_source;
    music_source = next_music_source;
    //music_source->setFading(StreamSoundSource::FadingOn, 1.0f);
    music_source->play();
    next_music_source = 0;
  }
  
  alcProcessContext(context);
  check_alc_error("Error while processing audio context: ");
}
Ejemplo n.º 5
0
SoundManager::SoundManager() :
  device(0), 
  context(0), 
  sound_enabled(false), 
  buffers(),
  sources(),
  update_list(),
  music_source(0),
  music_enabled(false),
  current_music()
{
  try {
    device = alcOpenDevice(0);
    if (device == NULL) {
      throw std::runtime_error("Couldn't open audio device.");
    }

    int attributes[] = { 0 };
    context = alcCreateContext(device, attributes);
    check_alc_error("Couldn't create audio context: ");
    alcMakeContextCurrent(context);
    check_alc_error("Couldn't select audio context: ");

    check_al_error("Audio error after init: ");
    sound_enabled = true;
    music_enabled = true;
  } catch(std::exception& e) {
    if(context != NULL) {
      alcDestroyContext(context);
      context = NULL; 
    }
    if(device != NULL) {
      alcCloseDevice(device);
      device = NULL;
    }
    log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
    print_openal_version();
  }
}
Ejemplo n.º 6
0
void
SoundManager::update()
{
  static Uint32 lasttime = SDL_GetTicks();
  Uint32 now = SDL_GetTicks();

  if(now - lasttime < 300)
    return;
  lasttime = now;

  // update and check for finished sound sources
  for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
    OpenALSoundSource* source = *i;

    source->update();

    if(!source->playing()) {
      delete source;
      i = sources.erase(i);
    } else {
      ++i;
    }
  }
  // check streaming sounds
  if(music_source) {
    music_source->update();
  }

  if (context)
  {
    alcProcessContext(context);
    check_alc_error("Error while processing audio context: ");
  }

  //run update() for stream_sound_source
  StreamSoundSources::iterator s = update_list.begin();
  while( s != update_list.end() ){
    (*s)->update();
    s++;
  }
}