Use a premade SoundStream object for the output's streamSound functions

openmw-38
Chris Robinson 9 years ago
parent 2ee3265b66
commit 1407366e51

@ -799,9 +799,8 @@ void OpenAL_Output::updateSound(MWBase::SoundPtr sound)
} }
MWBase::SoundStreamPtr OpenAL_Output::streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) void OpenAL_Output::streamSound(DecoderPtr decoder, MWBase::SoundStreamPtr sound)
{ {
MWBase::SoundStreamPtr sound;
OpenAL_SoundStream *stream = 0; OpenAL_SoundStream *stream = 0;
ALuint source; ALuint source;
@ -810,19 +809,18 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound(DecoderPtr decoder, float base
source = mFreeSources.front(); source = mFreeSources.front();
mFreeSources.pop_front(); mFreeSources.pop_front();
if((flags&MWBase::SoundManager::Play_Loop)) if(sound->getIsLooping())
std::cout <<"Warning: cannot loop stream \""<<decoder->getName()<<"\""<< std::endl; std::cout <<"Warning: cannot loop stream \""<<decoder->getName()<<"\""<< std::endl;
try { try {
sound.reset(new Stream(1.0f, basevol, pitch, flags));
alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f); alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f);
alSourcef(source, AL_MAX_DISTANCE, 1000.0f); alSourcef(source, AL_MAX_DISTANCE, 1000.0f);
alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f); alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE); alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(source, AL_LOOPING, AL_FALSE); alSourcei(source, AL_LOOPING, AL_FALSE);
ALfloat gain = basevol; ALfloat gain = sound->getRealVolume();
if(!(flags&MWBase::SoundManager::Play_NoEnv) && mListenerEnv == Env_Underwater) ALfloat pitch = sound->getPitch();
if(sound->getUseEnv() && mListenerEnv == Env_Underwater)
{ {
gain *= 0.9f; gain *= 0.9f;
pitch *= 0.7f; pitch *= 0.7f;
@ -837,7 +835,6 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound(DecoderPtr decoder, float base
stream = new OpenAL_SoundStream(source, decoder); stream = new OpenAL_SoundStream(source, decoder);
mStreamThread->add(stream); mStreamThread->add(stream);
sound->mHandle = stream;
mActiveStreams.push_back(sound); mActiveStreams.push_back(sound);
} }
catch(std::exception&) { catch(std::exception&) {
@ -847,12 +844,11 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound(DecoderPtr decoder, float base
throw; throw;
} }
return sound; sound->mHandle = stream;
} }
MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, float volume, float basevol, float pitch, float mindist, float maxdist, int flags) void OpenAL_Output::streamSound3D(DecoderPtr decoder, MWBase::SoundStreamPtr sound)
{ {
MWBase::SoundStreamPtr sound;
OpenAL_SoundStream *stream = 0; OpenAL_SoundStream *stream = 0;
ALuint source; ALuint source;
@ -861,21 +857,22 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const os
source = mFreeSources.front(); source = mFreeSources.front();
mFreeSources.pop_front(); mFreeSources.pop_front();
if((flags&MWBase::SoundManager::Play_Loop)) if(sound->getIsLooping())
std::cout <<"Warning: cannot loop stream \""<<decoder->getName()<<"\""<< std::endl; std::cout <<"Warning: cannot loop stream \""<<decoder->getName()<<"\""<< std::endl;
try { try {
sound.reset(new Stream(pos, volume, basevol, pitch, mindist, maxdist, flags)); alSourcef(source, AL_REFERENCE_DISTANCE, sound->getMinDistance());
alSourcef(source, AL_MAX_DISTANCE, sound->getMaxDistance());
alSourcef(source, AL_REFERENCE_DISTANCE, mindist);
alSourcef(source, AL_MAX_DISTANCE, maxdist);
alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f); alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f);
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(source, AL_LOOPING, AL_FALSE); alSourcei(source, AL_LOOPING, AL_FALSE);
ALfloat gain = volume*basevol; const osg::Vec3f &pos = sound->getPosition();
ALfloat maxdist = sound->getMaxDistance();
ALfloat gain = sound->getRealVolume();
ALfloat pitch = sound->getPitch();
if((pos - mListenerPos).length2() > maxdist*maxdist) if((pos - mListenerPos).length2() > maxdist*maxdist)
gain = 0.0f; gain = 0.0f;
if(!(flags&MWBase::SoundManager::Play_NoEnv) && mListenerEnv == Env_Underwater) if(sound->getUseEnv() && mListenerEnv == Env_Underwater)
{ {
gain *= 0.9f; gain *= 0.9f;
pitch *= 0.7f; pitch *= 0.7f;
@ -890,7 +887,6 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const os
stream = new OpenAL_SoundStream(source, decoder); stream = new OpenAL_SoundStream(source, decoder);
mStreamThread->add(stream); mStreamThread->add(stream);
sound->mHandle = stream;
mActiveStreams.push_back(sound); mActiveStreams.push_back(sound);
} }
catch(std::exception&) { catch(std::exception&) {
@ -900,7 +896,7 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const os
throw; throw;
} }
return sound; sound->mHandle = stream;
} }
void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound) void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound)

@ -53,9 +53,8 @@ namespace MWSound
virtual bool isSoundPlaying(MWBase::SoundPtr sound); virtual bool isSoundPlaying(MWBase::SoundPtr sound);
virtual void updateSound(MWBase::SoundPtr sound); virtual void updateSound(MWBase::SoundPtr sound);
virtual MWBase::SoundStreamPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags); virtual void streamSound(DecoderPtr decoder, MWBase::SoundStreamPtr sound);
virtual MWBase::SoundStreamPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, virtual void streamSound3D(DecoderPtr decoder, MWBase::SoundStreamPtr sound);
float vol, float basevol, float pitch, float min, float max, int flags);
virtual void stopStream(MWBase::SoundStreamPtr sound); virtual void stopStream(MWBase::SoundStreamPtr sound);
virtual double getStreamDelay(MWBase::SoundStreamPtr sound); virtual double getStreamDelay(MWBase::SoundStreamPtr sound);
virtual double getStreamOffset(MWBase::SoundStreamPtr sound); virtual double getStreamOffset(MWBase::SoundStreamPtr sound);

@ -37,9 +37,8 @@ namespace MWSound
virtual bool isSoundPlaying(MWBase::SoundPtr sound) = 0; virtual bool isSoundPlaying(MWBase::SoundPtr sound) = 0;
virtual void updateSound(MWBase::SoundPtr sound) = 0; virtual void updateSound(MWBase::SoundPtr sound) = 0;
virtual MWBase::SoundStreamPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) = 0; virtual void streamSound(DecoderPtr decoder, MWBase::SoundStreamPtr sound) = 0;
virtual MWBase::SoundStreamPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, virtual void streamSound3D(DecoderPtr decoder, MWBase::SoundStreamPtr sound) = 0;
float vol, float basevol, float pitch, float min, float max, int flags) = 0;
virtual void stopStream(MWBase::SoundStreamPtr sound) = 0; virtual void stopStream(MWBase::SoundStreamPtr sound) = 0;
virtual double getStreamDelay(MWBase::SoundStreamPtr sound) = 0; virtual double getStreamDelay(MWBase::SoundStreamPtr sound) = 0;
virtual double getStreamOffset(MWBase::SoundStreamPtr sound) = 0; virtual double getStreamOffset(MWBase::SoundStreamPtr sound) = 0;

@ -251,15 +251,20 @@ namespace MWSound
static float minDistance = std::max(fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult, 1.0f); static float minDistance = std::max(fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult, 1.0f);
static float maxDistance = std::max(fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult, minDistance); static float maxDistance = std::max(fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult, minDistance);
MWBase::SoundStreamPtr sound;
float basevol = volumeFromType(Play_TypeVoice); float basevol = volumeFromType(Play_TypeVoice);
if(playlocal) if(playlocal)
return mOutput->streamSound(decoder, {
basevol, 1.0f, Play_Normal|Play_TypeVoice|Play_2D sound.reset(new Stream(1.0f, basevol, 1.0f, Play_Normal|Play_TypeVoice|Play_2D));
); mOutput->streamSound(decoder, sound);
return mOutput->streamSound3D(decoder, }
pos, 1.0f, basevol, 1.0f, minDistance, maxDistance, else
Play_Normal|Play_TypeVoice|Play_3D {
); sound.reset(new Stream(pos, 1.0f, basevol, 1.0f, minDistance, maxDistance,
Play_Normal|Play_TypeVoice|Play_3D));
mOutput->streamSound3D(decoder, sound);
}
return sound;
} }
@ -309,11 +314,13 @@ namespace MWSound
DecoderPtr decoder = getDecoder(); DecoderPtr decoder = getDecoder();
decoder->open(filename); decoder->open(filename);
mMusic = mOutput->streamSound(decoder, volumeFromType(Play_TypeMusic), mMusic.reset(new Stream(1.0f, volumeFromType(Play_TypeMusic), 1.0f,
1.0f, Play_NoEnv|Play_TypeMusic|Play_2D); Play_NoEnv|Play_TypeMusic|Play_2D));
mOutput->streamSound(decoder, mMusic);
} }
catch(std::exception &e) { catch(std::exception &e) {
std::cout << "Music Error: " << e.what() << "\n"; std::cout << "Music Error: " << e.what() << "\n";
mMusic.reset();
} }
} }
@ -473,7 +480,9 @@ namespace MWSound
return track; return track;
try try
{ {
track = mOutput->streamSound(decoder, volumeFromType(type), 1.0f, Play_NoEnv|type); track.reset(new Stream(1.0f, volumeFromType(type), 1.0f, Play_NoEnv|type|Play_2D));
mOutput->streamSound(decoder, track);
TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), track); TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), track);
mActiveTracks.insert(iter, track); mActiveTracks.insert(iter, track);
} }

Loading…
Cancel
Save