From c03bd6049a635ae42c0ca2cc3bc88fc1f489b477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Tue, 17 Jan 2017 14:06:53 +0100 Subject: Make DrumkitCreator clean up the created files in the destructor. --- test/audiocachefiletest.cc | 6 +- test/audiocachetest.cc | 10 +-- test/drumkit_creator.cc | 96 +++++++++++++++------------- test/drumkit_creator.h | 153 +++++++++++++++++++++++++-------------------- test/drumkitcreatortest.cc | 6 +- test/enginetest.cc | 6 +- test/lv2.cc | 14 +++-- test/memcheckertest.cc | 7 ++- test/resource_test.cc | 4 +- 9 files changed, 170 insertions(+), 132 deletions(-) diff --git a/test/audiocachefiletest.cc b/test/audiocachefiletest.cc index 7ae2f95..83aece8 100644 --- a/test/audiocachefiletest.cc +++ b/test/audiocachefiletest.cc @@ -61,6 +61,8 @@ class AudioCacheFileTest CPPUNIT_TEST(noFileTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -68,7 +70,7 @@ public: void refTest() { // Create the audio file - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct tests TestableAudioCacheFiles audiofiles; @@ -92,7 +94,7 @@ public: printf("Test buffer size: %d samples\n", (int)buffer_size); // Create the audio file - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct tests AudioFile* ref_file[13]; diff --git a/test/audiocachetest.cc b/test/audiocachetest.cc index a3d62c9..546a5c4 100644 --- a/test/audiocachetest.cc +++ b/test/audiocachetest.cc @@ -44,6 +44,8 @@ class AudioCacheTest CPPUNIT_TEST(multiChannelThreaded); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -140,7 +142,7 @@ public: { printf("\nsinglechannel_nonthreaded()\n"); - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct test int channel = 0; @@ -152,7 +154,7 @@ public: { printf("\nsinglechannel_threaded()\n"); - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct test int channel = 0; @@ -164,7 +166,7 @@ public: { printf("\nmultichannel_nonthreaded()\n"); - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct test int channel = 0; @@ -178,7 +180,7 @@ public: { printf("\nmultichannel_threaded()\n"); - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct test int channel = 0; diff --git a/test/drumkit_creator.cc b/test/drumkit_creator.cc index f773c6f..9fae6e3 100644 --- a/test/drumkit_creator.cc +++ b/test/drumkit_creator.cc @@ -26,6 +26,8 @@ */ #include "drumkit_creator.h" +#include + #include "../src/random.h" #include @@ -35,25 +37,30 @@ #include #include -// FIXME: unix-centric paths, automatically clean up the drumkit directory - -namespace drumkit_creator +DrumkitCreator::~DrumkitCreator() { + for (const auto& file: created_files) + { + auto error = unlink(file.c_str()); + + if (error) { + std::cerr << "File could not be deleted in DrumkitCreator destructor" + << std::endl; + } + } -std::vector createData(const WavInfo& wav_info, - std::size_t number_of_channels); -std::string createTemporaryDirectory(const std::string& name); -void createInstrument(const InstrumentData& data, std::size_t number_of_channels, - const std::string& dir); -std::string createDrumkitFile(const DrumkitData& data, const std::string& dir); + for (const auto& dir: created_directories) + { + auto error = rmdir(dir.c_str()); -bool is_valid(const DrumkitData& data) -{ - // TODO Check the consistency of the data. - return true; + if (error) { + std::cerr << "Directory could not be deleted in DrumkitCreator destructor" + << std::endl; + } + } } -std::string create(const DrumkitData& data) +std::string DrumkitCreator::create(const DrumkitData& data) { std::string drumkit_filename; @@ -73,14 +80,13 @@ std::string create(const DrumkitData& data) } else { - // TODO report error - std::cout << "ERROR: create" << std::endl; + throw "DrumkitData not valid"; } return drumkit_filename; } -void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir) +void DrumkitCreator::createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir) { SF_INFO sfinfo; sfinfo.samplerate = 44100; @@ -90,21 +96,19 @@ void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const st std::string filename = dir + "/" + wav_info.filename; auto sndfile = sf_open(filename.c_str(), SFM_WRITE, &sfinfo); if (!sndfile) { - std::cout << "ERROR: Wav file couldn't be created: " - << sf_strerror(sndfile) << std::endl; - return; + throw "The wav file could not be created"; } + created_files.push_back(filename); + auto data_vec = createData(wav_info, number_of_channels); - - auto written_count = sf_write_raw(sndfile, data_vec.data(), 2*data_vec.size()); - std::cout << "Written: " << written_count << std::endl; + sf_write_raw(sndfile, data_vec.data(), 2*data_vec.size()); sf_write_sync(sndfile); sf_close(sndfile); } -std::string createStdKit(const std::string& name) +std::string DrumkitCreator::createStdKit(const std::string& name) { std::vector wav_infos = { WavInfo("1011.wav", 1, 0x1110), @@ -127,7 +131,7 @@ std::string createStdKit(const std::string& name) return create(kit_data); } -std::string createSmallKit(const std::string& name) +std::string DrumkitCreator::createSmallKit(const std::string& name) { std::vector wav_infos = { WavInfo("small_instr.wav", 549833) @@ -149,7 +153,7 @@ std::string createSmallKit(const std::string& name) return create(kit_data); } -std::string createHugeKit(const std::string& name) +std::string DrumkitCreator::createHugeKit(const std::string& name) { std::vector wav_infos = { WavInfo("huge_instr.wav", 549833) @@ -177,7 +181,7 @@ std::string createHugeKit(const std::string& name) return create(kit_data); } -std::string createSingleChannelWav(const std::string& name) +std::string DrumkitCreator::createSingleChannelWav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -189,7 +193,7 @@ std::string createSingleChannelWav(const std::string& name) return dir + "/" + name; } -std::string createMultiChannelWav(const std::string& name) +std::string DrumkitCreator::createMultiChannelWav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -201,7 +205,7 @@ std::string createMultiChannelWav(const std::string& name) return dir + "/" + name; } -std::string create0000Wav(const std::string& name) +std::string DrumkitCreator::create0000Wav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -213,7 +217,7 @@ std::string create0000Wav(const std::string& name) return dir + "/" + name; } -std::string createStdMidimap(const std::string& name) +std::string DrumkitCreator::createStdMidimap(const std::string& name) { auto dir = createTemporaryDirectory("midimap"); @@ -228,12 +232,12 @@ std::string createStdMidimap(const std::string& name) file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << content; } else { - // TODO print error - std::cout << "ERROR: instrument" << std::endl; + throw "File could not be opened"; } file.close(); @@ -241,23 +245,29 @@ std::string createStdMidimap(const std::string& name) } // -// Helper functions +// private member functions // -std::string createTemporaryDirectory(const std::string& name) +bool DrumkitCreator::is_valid(const DrumkitData& data) +{ + // TODO Check the consistency of the data. + return true; +} + +std::string DrumkitCreator::createTemporaryDirectory(const std::string& name) { - // FIXME Use cross-platform temporary directory std::string dir_template = "/tmp/drumgizmo_" + name + "XXXXXX"; const auto dir_name = mkdtemp(&dir_template[0]); if (dir_name) { + created_directories.push_back(dir_name); return std::string(dir_name); } return ""; } -std::vector createData(const WavInfo& wav_info, std::size_t number_of_channels) +auto DrumkitCreator::createData(const WavInfo& wav_info, std::size_t number_of_channels) -> std::vector { std::vector data_vec(number_of_channels * wav_info.length, wav_info.sample); if (wav_info.is_random) @@ -274,7 +284,7 @@ std::vector createData(const WavInfo& wav_info, std::size_t number_of_ch return data_vec; } -void createInstrument(const InstrumentData& data, std::size_t number_of_channels, +void DrumkitCreator::createInstrument(const InstrumentData& data, std::size_t number_of_channels, const std::string& dir) { std::string prefix = "\n" @@ -307,17 +317,17 @@ void createInstrument(const InstrumentData& data, std::size_t number_of_channels file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << prefix + samples + postfix; } else { - // TODO print error - std::cout << "ERROR: instrument" << std::endl; + throw "File could not be opened"; } file.close(); } -std::string createDrumkitFile(const DrumkitData& data, const std::string& dir) +std::string DrumkitCreator::createDrumkitFile(const DrumkitData& data, const std::string& dir) { // Pre- and postfix string std::string prefix = "\n" @@ -355,16 +365,14 @@ std::string createDrumkitFile(const DrumkitData& data, const std::string& dir) file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << prefix + channels + instruments + postfix; } else { - // TODO print error - std::cout << "ERROR: drumkit" << std::endl; + throw "File could not be opened"; } file.close(); return filename; } - -} // end drumkit_creator diff --git a/test/drumkit_creator.h b/test/drumkit_creator.h index 93a6f36..7c63e6a 100644 --- a/test/drumkit_creator.h +++ b/test/drumkit_creator.h @@ -30,72 +30,91 @@ #include #include -namespace drumkit_creator +class DrumkitCreator { - -using Sample = uint16_t; - -//! If is_random is true then this overrules the sample member. If is_random -//! is false however, every sample is chosen as the one in the member variable. -struct WavInfo -{ - const std::string filename; - const std::size_t length; - - const bool is_random; - const Sample sample; - - WavInfo(const std::string& filename, std::size_t length) - : filename(filename), length(length), is_random(true), sample(0) {} - - WavInfo(const std::string& filename, std::size_t length, Sample sample) - : filename(filename), length(length), is_random(false), sample(sample) {} -}; - -struct Audiofile -{ - WavInfo* wav_info; - std::size_t filechannel; -}; - -struct SampleData -{ - std::string name; - // Vector of non-owning pointers and therefore it is raw. - std::vector audiofiles; +public: + using Sample = uint16_t; + + //! If is_random is true then this overrules the sample member. If is_random + //! is false however, every sample is chosen as the one in the member variable. + struct WavInfo + { + const std::string filename; + const std::size_t length; + + const bool is_random; + const Sample sample; + + WavInfo(const std::string& filename, std::size_t length) + : filename(filename), length(length), is_random(true), sample(0) {} + + WavInfo(const std::string& filename, std::size_t length, Sample sample) + : filename(filename), length(length), is_random(false), sample(sample) {} + }; + + struct Audiofile + { + WavInfo* wav_info; + std::size_t filechannel; + }; + + struct SampleData + { + std::string name; + // Vector of non-owning pointers and therefore it is raw. + std::vector audiofiles; + }; + + struct InstrumentData + { + std::string name; + std::string filename; + std::vector sample_data; + }; + + struct DrumkitData + { + std::string name; + std::size_t number_of_channels; + std::vector instruments; + std::vector wav_infos; + }; + + DrumkitCreator() = default; + + //! This destructor removes all the created temporary files and directories. + ~DrumkitCreator(); + + //! Creates a drumkit from data in the temporary directory of the OS and + //! returns its filename. + std::string create(const DrumkitData& data); + + //! Creates a single wav file + void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir); + + //! Those functions create some special wav files, drumkits, and midimaps + //@{ + std::string createStdKit(const std::string& name); + std::string createSmallKit(const std::string& name); + std::string createHugeKit(const std::string& name); + + std::string createSingleChannelWav(const std::string& name); + std::string createMultiChannelWav(const std::string& name); + std::string create0000Wav(const std::string& name); + + std::string createStdMidimap(const std::string& name); + //@} + +private: + std::vector created_files; + std::vector created_directories; + + bool is_valid(const DrumkitData& data); + + std::string createTemporaryDirectory(const std::string& name); + std::vector createData(const WavInfo& wav_info, + std::size_t number_of_channels); + void createInstrument(const InstrumentData& data, std::size_t number_of_channels, + const std::string& dir); + std::string createDrumkitFile(const DrumkitData& data, const std::string& dir); }; - -struct InstrumentData -{ - std::string name; - std::string filename; - std::vector sample_data; -}; - -struct DrumkitData -{ - std::string name; - std::size_t number_of_channels; - std::vector instruments; - std::vector wav_infos; -}; - -//! Creates a drumkit from data in the temporary directory of the OS and -//! returns its filename. -std::string create(const DrumkitData& data); - -//! Creates a single wav file -void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir); - -//! Those functions create some special wav files, drumkits, and midimaps -std::string createStdKit(const std::string& name); -std::string createSmallKit(const std::string& name); -std::string createHugeKit(const std::string& name); - -std::string createSingleChannelWav(const std::string& name); -std::string createMultiChannelWav(const std::string& name); -std::string create0000Wav(const std::string& name); - -std::string createStdMidimap(const std::string& name); - -} // end drumkit_creator diff --git a/test/drumkitcreatortest.cc b/test/drumkitcreatortest.cc index 852f407..1b6321b 100644 --- a/test/drumkitcreatortest.cc +++ b/test/drumkitcreatortest.cc @@ -28,8 +28,6 @@ #include "drumkit_creator.h" -using namespace drumkit_creator; - class DrumkitcreatorTest : public CppUnit::TestFixture { @@ -37,6 +35,8 @@ class DrumkitcreatorTest CPPUNIT_TEST(testTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() { @@ -50,7 +50,7 @@ public: //! This just creates some drumkit. void testTest() { - drumkit_creator::createStdKit("stdkit"); + drumkit_creator.createStdKit("stdkit"); } }; diff --git a/test/enginetest.cc b/test/enginetest.cc index 7079bad..5e33fe2 100644 --- a/test/enginetest.cc +++ b/test/enginetest.cc @@ -71,6 +71,8 @@ class test_engine : public CppUnit::TestFixture CPPUNIT_TEST(loading); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -84,8 +86,8 @@ public: dg.setFrameSize(100); // Create drumkits - auto kit1_file = drumkit_creator::createStdKit("kit1"); - auto kit2_file = drumkit_creator::createStdKit("kit2"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); + auto kit2_file = drumkit_creator.createStdKit("kit2"); // Switch kits emmidiately without giving the loader time to work: for(int i = 0; i < 100; ++i) diff --git a/test/lv2.cc b/test/lv2.cc index 4ad6d94..78bf342 100644 --- a/test/lv2.cc +++ b/test/lv2.cc @@ -62,6 +62,8 @@ class test_lv2 : public CppUnit::TestFixture CPPUNIT_TEST(test1); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -110,9 +112,9 @@ public: ""; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; @@ -177,9 +179,9 @@ public: ""; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; @@ -256,9 +258,9 @@ public: ""; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; diff --git a/test/memcheckertest.cc b/test/memcheckertest.cc index 125ff6c..da5eae6 100644 --- a/test/memcheckertest.cc +++ b/test/memcheckertest.cc @@ -57,10 +57,11 @@ private: Settings settings; DrumKit kit; Random random; + DrumkitCreator drumkit_creator; - const std::string small_kit_path = drumkit_creator::createSmallKit("small_kit"); - const std::string huge_kit_path = drumkit_creator::createHugeKit("huge_kit"); - const std::string audiofile = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + const std::string small_kit_path = drumkit_creator.createSmallKit("small_kit"); + const std::string huge_kit_path = drumkit_creator.createHugeKit("huge_kit"); + const std::string audiofile = drumkit_creator.createMultiChannelWav("multi_channel.wav"); public: void setUp() { diff --git a/test/resource_test.cc b/test/resource_test.cc index c8650a5..d6d9221 100644 --- a/test/resource_test.cc +++ b/test/resource_test.cc @@ -49,13 +49,15 @@ class ResourceTest : public CppUnit::TestFixture CPPUNIT_TEST(internalReadTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} void externalReadTest() { - auto filename = drumkit_creator::create0000Wav("0000.wav"); + auto filename = drumkit_creator.create0000Wav("0000.wav"); ResourceTester rc(filename); CPPUNIT_ASSERT(!rc.probeIsInternal()); -- cgit v1.2.3