Sound: readWAV: avoid common exceptions.
Avoid exceptions for the common ‘file not found’ case, and instead return false / nullptr. Erik says it’s fine.
This commit is contained in:
@@ -380,7 +380,8 @@ namespace simgear
|
|||||||
ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size, ALfloat& freqf, unsigned int& block_align)
|
ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size, ALfloat& freqf, unsigned int& block_align)
|
||||||
{
|
{
|
||||||
if (!path.exists()) {
|
if (!path.exists()) {
|
||||||
throw sg_io_exception("loadWAVFromFile: file not found", path);
|
SG_LOG(SG_IO, SG_DEV_ALERT, "loadWAVFromFile: file not found:" << path);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer b;
|
Buffer b;
|
||||||
@@ -395,13 +396,15 @@ ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size,
|
|||||||
fd = gzopen(ps.c_str(), "rb");
|
fd = gzopen(ps.c_str(), "rb");
|
||||||
#endif
|
#endif
|
||||||
if (!fd) {
|
if (!fd) {
|
||||||
throw sg_io_exception("loadWAVFromFile: unable to open file", path);
|
SG_LOG(SG_IO, SG_DEV_ALERT, "loadWAVFromFile: unable to open file:" << path);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loadWavFile(fd, &b);
|
loadWavFile(fd, &b);
|
||||||
} catch (sg_exception& e) {
|
} catch (sg_exception& e) {
|
||||||
throw sg_io_exception(e.getFormattedMessage() + "\nfor: " + path.str());
|
SG_LOG(SG_IO, SG_DEV_ALERT, "loadWAVFromFile:" << e.getFormattedMessage() << "\nfor: " << path);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALvoid* data = b.data;
|
ALvoid* data = b.data;
|
||||||
|
|||||||
@@ -813,13 +813,14 @@ bool SGSoundMgr::load( const std::string &samplepath,
|
|||||||
|
|
||||||
auto data = simgear::loadWAVFromFile(samplepath, format, size, freqf, blocksz);
|
auto data = simgear::loadWAVFromFile(samplepath, format, size, freqf, blocksz);
|
||||||
freq = (ALsizei)freqf;
|
freq = (ALsizei)freqf;
|
||||||
if (data == nullptr) {
|
if (!data) {
|
||||||
throw sg_io_exception("Failed to load wav file", sg_location(samplepath));
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == AL_FORMAT_STEREO8 || format == AL_FORMAT_STEREO16) {
|
if (format == AL_FORMAT_STEREO8 || format == AL_FORMAT_STEREO16) {
|
||||||
free(data);
|
free(data);
|
||||||
throw sg_io_exception("Warning: STEREO files are not supported for 3D audio effects: " + samplepath);
|
SG_LOG(SG_IO, SG_DEV_ALERT, "Warning: STEREO files are not supported for 3D audio effects: " << samplepath);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*dbuf = (void *)data;
|
*dbuf = (void *)data;
|
||||||
|
|||||||
Reference in New Issue
Block a user