#include "sound_openal.h"
-#if defined(_MSC_VER)
+#if defined(_WIN32)
#include <al.h>
#include <alc.h>
- #include <alext.h>
+ //#include <alext.h>
#elif defined(__APPLE__)
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#include <map>
#include <vector>
#include "utility.h" // myrand()
+#include "filesys.h"
#define BUFFER_SIZE 30000
char array[BUFFER_SIZE]; // Local fixed size array
vorbis_info *pInfo;
OggVorbis_File oggFile;
-
+
+ // Do a dumb-ass static string copy for old versions of ov_fopen
+ // because they expect a non-const char*
+ char nonconst[10000];
+ snprintf(nonconst, 10000, "%s", filepath.c_str());
// Try opening the given file
- if(ov_fopen(filepath.c_str(), &oggFile) != 0)
+ //if(ov_fopen(filepath.c_str(), &oggFile) != 0)
+ if(ov_fopen(nonconst, &oggFile) != 0)
{
infostream<<"Audio: Error opening "<<filepath<<" for decoding"<<std::endl;
return NULL;
return bufs[j];
}
- bool loadSound(const std::string &name,
- const std::string &filepath)
- {
- SoundBuffer *buf = loadOggFile(filepath);
- if(buf)
- addBuffer(name, buf);
- return false;
- }
- bool loadSound(const std::string &name,
- const std::vector<char> &filedata)
- {
- errorstream<<"OpenALSoundManager: Loading from filedata not"
- " implemented"<<std::endl;
- return false;
- }
-
PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
float volume)
{
if(!m_fetcher)
return NULL;
std::set<std::string> paths;
- std::set<std::vector<char> > datas;
+ std::set<std::string> datas;
m_fetcher->fetchSounds(name, paths, datas);
for(std::set<std::string>::iterator i = paths.begin();
i != paths.end(); i++){
- loadSound(name, *i);
+ loadSoundFile(name, *i);
}
- for(std::set<std::vector<char> >::iterator i = datas.begin();
+ for(std::set<std::string>::iterator i = datas.begin();
i != datas.end(); i++){
- loadSound(name, *i);
+ loadSoundData(name, *i);
}
return getBuffer(name);
}
/* Interface */
+ bool loadSoundFile(const std::string &name,
+ const std::string &filepath)
+ {
+ SoundBuffer *buf = loadOggFile(filepath);
+ if(buf)
+ addBuffer(name, buf);
+ return false;
+ }
+ bool loadSoundData(const std::string &name,
+ const std::string &filedata)
+ {
+ // The vorbis API sucks; just write it to a file and use vorbisfile
+ // TODO: Actually load it directly from memory
+ std::string basepath = porting::path_user + DIR_DELIM + "cache" +
+ DIR_DELIM + "tmp";
+ std::string path = basepath + DIR_DELIM + "tmp.ogg";
+ verbosestream<<"OpenALSoundManager::loadSoundData(): Writing "
+ <<"temporary file to ["<<path<<"]"<<std::endl;
+ fs::CreateAllDirs(basepath);
+ std::ofstream of(path.c_str(), std::ios::binary);
+ of.write(filedata.c_str(), filedata.size());
+ of.close();
+ return loadSoundFile(name, path);
+ }
+
void updateListener(v3f pos, v3f vel, v3f at, v3f up)
{
m_listener_pos = pos;
int playSound(const std::string &name, bool loop, float volume)
{
maintain();
+ if(name == "")
+ return 0;
SoundBuffer *buf = getFetchBuffer(name);
if(!buf){
infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
{
maintain();
+ if(name == "")
+ return 0;
SoundBuffer *buf = getFetchBuffer(name);
if(!buf){
infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
maintain();
deleteSound(sound);
}
+ bool soundExists(int sound)
+ {
+ maintain();
+ return (m_sounds_playing.count(sound) != 0);
+ }
+ void updateSoundPosition(int id, v3f pos)
+ {
+ std::map<int, PlayingSound*>::iterator i =
+ m_sounds_playing.find(id);
+ if(i == m_sounds_playing.end())
+ return;
+ PlayingSound *sound = i->second;
+
+ alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
+ alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
+ alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
+ alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
+ }
};
ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)