Add functions to play sounds

This commit is contained in:
Chris Robinson 2012-03-17 06:51:44 -07:00
parent a46f8ced05
commit 656863ec6e
4 changed files with 62 additions and 8 deletions

View file

@ -230,6 +230,22 @@ void OpenAL_Output::Deinitialize()
} }
Sound* OpenAL_Output::PlaySound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
float volume, float pitch, bool loop)
{
fail("PlaySound not yet supported");
return NULL;
}
Sound* OpenAL_Output::PlaySound3D(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
MWWorld::Ptr ptr, float volume, float pitch,
float min, float max, bool loop)
{
fail("PlaySound3D not yet supported");
return NULL;
}
Sound* OpenAL_Output::StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder) Sound* OpenAL_Output::StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder)
{ {
std::auto_ptr<OpenAL_SoundStream> sound; std::auto_ptr<OpenAL_SoundStream> sound;

View file

@ -22,6 +22,12 @@ namespace MWSound
virtual bool Initialize(const std::string &devname=""); virtual bool Initialize(const std::string &devname="");
virtual void Deinitialize(); virtual void Deinitialize();
virtual Sound *PlaySound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
float volume, float pitch, bool loop);
virtual Sound *PlaySound3D(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
MWWorld::Ptr ptr, float volume, float pitch,
float min, float max, bool loop);
virtual Sound *StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder); virtual Sound *StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder);
virtual void UpdateListener(float pos[3], float atdir[3], float updir[3]); virtual void UpdateListener(float pos[3], float atdir[3], float updir[3]);

View file

@ -4,6 +4,8 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include "../mwworld/ptr.hpp"
namespace MWSound namespace MWSound
{ {
class SoundManager; class SoundManager;
@ -17,6 +19,11 @@ namespace MWSound
virtual bool Initialize(const std::string &devname="") = 0; virtual bool Initialize(const std::string &devname="") = 0;
virtual void Deinitialize() = 0; virtual void Deinitialize() = 0;
virtual Sound *PlaySound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
float volume, float pitch, bool loop) = 0;
virtual Sound *PlaySound3D(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder,
MWWorld::Ptr ptr, float volume, float pitch,
float min, float max, bool loop) = 0;
virtual Sound *StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder) = 0; virtual Sound *StreamSound(const std::string &fname, std::auto_ptr<Sound_Decoder> decoder) = 0;
// FIXME: This should take an MWWorld::Ptr that represents the in-world camera // FIXME: This should take an MWWorld::Ptr that represents the in-world camera

View file

@ -120,7 +120,17 @@ namespace MWSound
float min, float max, float min, float max,
bool loop, bool untracked) bool loop, bool untracked)
{ {
//std::cout << "Cannot load " << file << ", skipping.\n"; try
{
Sound *sound;
std::auto_ptr<Sound_Decoder> decoder(new DEFAULT_DECODER);
sound = Output->PlaySound3D(file, decoder, ptr, volume, pitch, min, max, loop);
delete sound;
}
catch(std::exception &e)
{
std::cout <<"Sound play error: "<<e.what()<< std::endl;
}
} }
// Stop a sound and remove it from the list. If id="" then // Stop a sound and remove it from the list. If id="" then
@ -251,8 +261,6 @@ namespace MWSound
std::string filePath = Files::FileListLocator(mSoundFiles, filename, mFSStrict, true); std::string filePath = Files::FileListLocator(mSoundFiles, filename, mFSStrict, true);
if(!filePath.empty()) if(!filePath.empty())
add(filePath, ptr, "_say_sound", 1, 1, 100, 20000, false); add(filePath, ptr, "_say_sound", 1, 1, 100, 20000, false);
else
std::cout << "Sound file " << filename << " not found, skipping.\n";
} }
bool SoundManager::sayDone(MWWorld::Ptr ptr) const bool SoundManager::sayDone(MWWorld::Ptr ptr) const
@ -264,8 +272,23 @@ namespace MWSound
void SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop) void SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop)
{ {
float min, max; float min, max;
const std::string &file = lookup(soundId, volume, min, max); std::string file = lookup(soundId, volume, min, max);
std::cout << "Cannot play " << file << ", skipping.\n"; if(!file.empty())
{
try
{
Sound *sound;
std::auto_ptr<Sound_Decoder> decoder(new DEFAULT_DECODER);
sound = Output->PlaySound(file, decoder, volume, pitch, loop);
delete sound;
}
catch(std::exception &e)
{
std::cout <<"Sound play error: "<<e.what()<< std::endl;
}
}
else
std::cout << "Sound file " << soundId << " not found, skipping.\n";
} }
void SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId, void SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId,
@ -273,9 +296,11 @@ namespace MWSound
{ {
// Look up the sound in the ESM data // Look up the sound in the ESM data
float min, max; float min, max;
const std::string &file = lookup(soundId, volume, min, max); std::string file = lookup(soundId, volume, min, max);
if(file != "") if(!file.empty())
std::cout << "Cannot play " << file << ", skipping.\n"; add(file, ptr, soundId, volume, pitch, min, max, false);
else
std::cout << "Sound file " << soundId << " not found, skipping.\n";
} }
void SoundManager::stopSound3D(MWWorld::Ptr ptr, const std::string& soundId) void SoundManager::stopSound3D(MWWorld::Ptr ptr, const std::string& soundId)