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:
Automatic Release Builder
2020-10-15 17:00:30 +01:00
parent 32ccdaec6f
commit 444e2ffb2d
2 changed files with 10 additions and 6 deletions

View File

@@ -380,7 +380,8 @@ namespace simgear
ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size, ALfloat& freqf, unsigned int& block_align)
{
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;
@@ -395,13 +396,15 @@ ALvoid* loadWAVFromFile(const SGPath& path, unsigned int& format, ALsizei& size,
fd = gzopen(ps.c_str(), "rb");
#endif
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 {
loadWavFile(fd, &b);
} 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;

View File

@@ -813,13 +813,14 @@ bool SGSoundMgr::load( const std::string &samplepath,
auto data = simgear::loadWAVFromFile(samplepath, format, size, freqf, blocksz);
freq = (ALsizei)freqf;
if (data == nullptr) {
throw sg_io_exception("Failed to load wav file", sg_location(samplepath));
if (!data) {
return false;
}
if (format == AL_FORMAT_STEREO8 || format == AL_FORMAT_STEREO16) {
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;