Ejemplo n.º 1
0
SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)
{
    SoundSourcePtr source;

    try {
        auto it = m_buffers.find(filename);
        if(it != m_buffers.end()) {
            source = SoundSourcePtr(new SoundSource);
            source->setBuffer(it->second);
        } else {
            SoundFilePtr soundFile = SoundFile::loadSoundFile(filename);
            if(!soundFile)
                return nullptr;

            if(soundFile->getSize() <= MAX_CACHE_SIZE) {
                source = SoundSourcePtr(new SoundSource);
                SoundBufferPtr buffer = SoundBufferPtr(new SoundBuffer);
                buffer->fillBuffer(soundFile);
                source->setBuffer(buffer);
                m_buffers[filename] = buffer;
                g_logger.warning(stdext::format("uncached sound '%s' requested to be played", filename));
            } else {
                StreamSoundSourcePtr streamSource(new StreamSoundSource);
                streamSource->setSoundFile(soundFile);
                source = streamSource;

    #if defined __linux && !defined OPENGL_ES
                // due to OpenAL implementation bug, stereo buffers are always downmixed to mono on linux systems
                // this is hack to work around the issue
                // solution taken from http://opensource.creative.com/pipermail/openal/2007-April/010355.html
                if(soundFile->getSampleFormat() == AL_FORMAT_STEREO16) {
                    CombinedSoundSourcePtr combinedSource(new CombinedSoundSource);

                    streamSource->downMix(StreamSoundSource::DownMixLeft);
                    streamSource->setRelative(true);
                    streamSource->setPosition(Point(-128, 0));
                    combinedSource->addSource(streamSource);

                    streamSource = StreamSoundSourcePtr(new StreamSoundSource);
                    streamSource->setSoundFile(SoundFile::loadSoundFile(filename));
                    streamSource->downMix(StreamSoundSource::DownMixRight);
                    streamSource->setRelative(true);
                    streamSource->setPosition(Point(128,0));
                    combinedSource->addSource(streamSource);

                    source = combinedSource;
                }
    #endif
            }
        }
    } catch(std::exception& e) {
        g_logger.error(stdext::format("failed to load sound source: '%s'", e.what()));
        return nullptr;
    }

    return source;
}
SoundSourcePtr
SoundChannel::prepare(const Pathname& filename, 
                      OpenALSoundSourceType type)
{
  SoundSourcePtr source = m_sound_manager.create_sound_source(filename, *this, type);
  if (!source)
  {
    std::cout << "SourceChannel::prepare: Couldn't load " << filename << std::endl;
    return SoundSourcePtr(new DummySoundSource());
  }
  else
  {
    source->update_gain();
    m_sound_sources.push_back(SoundSourcePtr(source));
    return source;
  }
}
SoundSourcePtr
SoundChannel::prepare(std::auto_ptr<SoundFile> sound_file, 
                      OpenALSoundSourceType type)
{
  switch(type)
  {
    case kStreamSoundSource:
    {
      SoundSourcePtr source(new StreamSoundSource(*this, sound_file));
      source->update_gain();
      m_sound_sources.push_back(SoundSourcePtr(source));
      return source;
    }
    break;

    case kStaticSoundSource:
      // FIXME: not implemented
      assert(!"not implemented");
      break;

    default:
      assert(!"never reached");
  }
}
Ejemplo n.º 4
0
SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)
{
    SoundSourcePtr source;

    try {
        auto it = m_buffers.find(filename);
        if(it != m_buffers.end()) {
            source = SoundSourcePtr(new SoundSource);
            source->setBuffer(it->second);
        } else {
#if defined __linux && !defined OPENGL_ES
            // due to OpenAL implementation bug, stereo buffers are always downmixed to mono on linux systems
            // this is hack to work around the issue
            // solution taken from http://opensource.creative.com/pipermail/openal/2007-April/010355.html
            CombinedSoundSourcePtr combinedSource(new CombinedSoundSource);
            StreamSoundSourcePtr streamSource;

            streamSource = StreamSoundSourcePtr(new StreamSoundSource);
            streamSource->downMix(StreamSoundSource::DownMixLeft);
            streamSource->setRelative(true);
            streamSource->setPosition(Point(-128, 0));
            combinedSource->addSource(streamSource);
            m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
                stdext::timer a;
                try {
                    return SoundFile::loadSoundFile(filename);
                } catch(std::exception& e) {
                    g_logger.error(e.what());
                    return nullptr;
                }
            });

            streamSource = StreamSoundSourcePtr(new StreamSoundSource);
            streamSource->downMix(StreamSoundSource::DownMixRight);
            streamSource->setRelative(true);
            streamSource->setPosition(Point(128,0));
            combinedSource->addSource(streamSource);
            m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
                try {
                    return SoundFile::loadSoundFile(filename);
                } catch(std::exception& e) {
                    g_logger.error(e.what());
                    return nullptr;
                }
            });

            source = combinedSource;
#else
            StreamSoundSourcePtr streamSource(new StreamSoundSource);
            m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
                try {
                    return SoundFile::loadSoundFile(filename);
                } catch(std::exception& e) {
                    g_logger.error(e.what());
                    return nullptr;
                }
            });
            source = streamSource;
#endif
        }
    } catch(std::exception& e) {
        g_logger.error(stdext::format("failed to load sound source: '%s'", e.what()));
        return nullptr;
    }

    return source;
}