From 3e829110a40f811a0216e3bc648bc7f2485f3b4d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 28 Jan 2016 11:41:18 +0100 Subject: Handle sndffile open failure. --- src/audiocachefile.cc | 11 +++++++++- test/audiocachefiletest.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc index 942afd3..039a46d 100644 --- a/src/audiocachefile.cc +++ b/src/audiocachefile.cc @@ -30,16 +30,21 @@ #include +#include + #include "audiocache.h" AudioCacheFile::AudioCacheFile(const std::string& filename) : filename(filename) { + std::memset(&sf_info, 0, sizeof(SF_INFO)); + fh = sf_open(filename.c_str(), SFM_READ, &sf_info); if(!fh) { ERR(audiofile,"SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh)); + return; } if(sf_info.frames == 0) @@ -75,7 +80,11 @@ size_t AudioCacheFile::getChannelCount() void AudioCacheFile::readChunk(const CacheChannels& channels, size_t pos, size_t num_samples) { - assert(fh != nullptr); // File handle must never be nullptr + //assert(fh != nullptr); // File handle must never be nullptr + if(!fh) + { + return; + } if((int)pos > sf_info.frames) { diff --git a/test/audiocachefiletest.cc b/test/audiocachefiletest.cc index fb454d2..7cb632b 100644 --- a/test/audiocachefiletest.cc +++ b/test/audiocachefiletest.cc @@ -52,6 +52,7 @@ class AudioCacheFileTest : public CppUnit::TestFixture CPPUNIT_TEST_SUITE(AudioCacheFileTest); CPPUNIT_TEST(refTest); CPPUNIT_TEST(readTest); + CPPUNIT_TEST(noFileTest); CPPUNIT_TEST_SUITE_END(); public: @@ -180,6 +181,57 @@ public: } } + void noFileTest() + { + size_t bufferSize = 64; + std::string filename = "kits/no-such-file.wav"; + + AudioCacheFile file(filename); + CPPUNIT_ASSERT_EQUAL(filename, file.getFilename()); + CPPUNIT_ASSERT_EQUAL(0u, file.getSize()); + CPPUNIT_ASSERT_EQUAL(0u, file.getChannelCount()); + + CacheChannels channels; + + sample_t samples[13][bufferSize]; + volatile bool ready[13]; + for(size_t c = 0; c < 13; ++c) + { + for(size_t i = 0; i < bufferSize; ++i) + { + samples[c][i] = 42.0f; + } + + channels.push_back( + { + c, // channel + samples[c], // samples + bufferSize, // max_num_samples + &ready[c] // ready + } + ); + } + + for(size_t c = 0; c < 13; ++c) + { + ready[c] = false; + } + + file.readChunk(channels, 0, bufferSize); + + for(size_t c = 0; c < 13; ++c) + { + CPPUNIT_ASSERT_EQUAL(false, ready[c]?true:false); + } + + for(size_t c = 0; c < 13; ++c) + { + for(size_t i = 0; i < bufferSize; ++i) + { + CPPUNIT_ASSERT_EQUAL(42.0f, samples[c][i]); + } + } + } }; // Registers the fixture into the 'registry' -- cgit v1.2.3