void SoundManager::poll() { static ticks_t lastUpdate = 0; ticks_t now = g_clock.millis(); if(now - lastUpdate < POLL_DELAY) return; lastUpdate = now; for(auto it = m_sources.begin(); it != m_sources.end();) { SoundSourcePtr source = *it; source->update(); if(!source->isPlaying()) it = m_sources.erase(it); else ++it; } if(m_musicSource) { m_musicSource->update(); if(!m_musicSource->isPlaying()) m_musicSource = nullptr; } if(m_context) { alcProcessContext(m_context); } }
void SoundManager::poll() { static ticks_t lastUpdate = 0; ticks_t now = g_clock.millis(); if(now - lastUpdate < POLL_DELAY) return; lastUpdate = now; ensureContext(); for(auto it = m_sources.begin(); it != m_sources.end();) { SoundSourcePtr source = *it; source->update(); if(!source->isPlaying()) it = m_sources.erase(it); else ++it; } for(auto it : m_channels) { it.second->update(); } if(m_context) { alcProcessContext(m_context); } }
SoundSourcePtr SoundChannel::play(const Pathname& filename) { SoundSourcePtr source = prepare(filename); source->play(); return source; }
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; }
void SoundManager::play(const std::string& filename) { if(!m_soundEnabled) return; SoundSourcePtr soundSource = createSoundSource(filename); if(!soundSource) { g_logger.error(stdext::format("unable to play '%s'", filename)); return; } soundSource->setRelative(true); soundSource->play(); m_sources.push_back(soundSource); }
void SoundManager::poll() { static ticks_t lastUpdate = 0; ticks_t now = g_clock.millis(); if(now - lastUpdate < POLL_DELAY) return; lastUpdate = now; ensureContext(); for(auto it = m_streamFiles.begin(); it != m_streamFiles.end();) { StreamSoundSourcePtr source = it->first; auto& future = it->second; if(future.is_ready()) { SoundFilePtr sound = future.get(); if(sound) source->setSoundFile(sound); else source->stop(); it = m_streamFiles.erase(it); } else { ++it; } } for(auto it = m_sources.begin(); it != m_sources.end();) { SoundSourcePtr source = *it; source->update(); if(!source->isPlaying()) it = m_sources.erase(it); else ++it; } for(auto it : m_channels) { it.second->update(); } if(m_context) { alcProcessContext(m_context); } }
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 SoundManager::play(std::string filename, float fadetime, float gain) { if(!m_audioEnabled) return nullptr; ensureContext(); if(gain == 0) gain = 1.0f; filename = resolveSoundFile(filename); SoundSourcePtr soundSource = createSoundSource(filename); if(!soundSource) { g_logger.error(stdext::format("unable to play '%s'", filename)); return nullptr; } soundSource->setName(filename); soundSource->setRelative(true); soundSource->setGain(gain); if(fadetime > 0) soundSource->setFading(StreamSoundSource::FadingOn, fadetime); soundSource->play(); m_sources.push_back(soundSource); return soundSource; }
SoundSourcePtr AudioSystem::LoadSource(const String & filename, float priority) { Hash2 hash(filename.c_str()); SoundSourcePtr p = _find(hash, filename); if (p == NULL) { p = new SoundSource(filename, filename); p->SetPriority(priority); mSourceMap.Insert(hash, p.c_ptr()); } if (priority < 0) { p->EnsureLoad(); } else { p->Load(); } return p; }
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; }