GigWriteTest.cpp

Go to the documentation of this file.
00001 #include "GigWriteTest.h"
00002 
00003 #include <iostream>
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 
00008 #include "../gig.h"
00009 #include "../helper.h"
00010 
00011 CPPUNIT_TEST_SUITE_REGISTRATION(GigWriteTest);
00012 
00013 using namespace std;
00014 
00015 // file name of the Gigasampler file we are going to create for these tests
00016 #define TEST_GIG_FILE_NAME "foo.gig"
00017 
00018 // four stupid little sample "waves"
00019 // (each having three sample points length, 16 bit depth, mono)
00020 int16_t sampleData1[] = { 1, 2, 3 };
00021 int16_t sampleData2[] = { 4, 5, 6 };
00022 int16_t sampleData3[] = { 7, 8, 9 };
00023 int16_t sampleData4[] = { 10,11,12 };
00024 
00025 // 1. Run) print the purpose of this test case first
00026 void GigWriteTest::printTestSuiteName() {
00027     cout << "\b \nTesting Gigasampler write support: " << flush;
00028 }
00029 
00030 // code executed when this test suite is created
00031 void GigWriteTest::setUp() {
00032 }
00033 
00034 // code executed when this test suite will be destroyed
00035 void GigWriteTest::tearDown() {
00036 }
00037 
00038 
00040 // The actual test cases (in order) ...
00041 
00042 // 2. Run) create a new Gigasampler file from scratch
00043 void GigWriteTest::createNewGigFile() {
00044     try {
00045         // create an empty Gigasampler file
00046         gig::File file;
00047         // we give it an internal name, not mandatory though
00048         file.pInfo->Name = "Foo Gigasampler File";
00049 
00050         // create four samples
00051         gig::Sample* pSample1 = file.AddSample();
00052         gig::Sample* pSample2 = file.AddSample();
00053         gig::Sample* pSample3 = file.AddSample();
00054         gig::Sample* pSample4 = file.AddSample();
00055         // give those samples a name (not mandatory)
00056         pSample1->pInfo->Name = "Foo Sample 1";
00057         pSample2->pInfo->Name = "Foo Sample 2";
00058         pSample3->pInfo->Name = "Foo Sample 3";
00059         pSample4->pInfo->Name = "Foo Sample 4";
00060         // set meta informations for those samples
00061         pSample1->Channels = 1; // mono
00062         pSample1->BitDepth = 16; // 16 bits
00063         pSample1->FrameSize = 16/*bitdepth*/ / 8/*1 byte are 8 bits*/ * 1/*mono*/;
00064         pSample1->SamplesPerSecond = 44100;
00065         pSample2->Channels = 1; // mono
00066         pSample2->BitDepth = 16; // 16 bits
00067         pSample2->FrameSize = 16 / 8 * 1;
00068         pSample2->SamplesPerSecond = 44100;
00069         pSample3->Channels = 1; // mono
00070         pSample3->BitDepth = 16; // 16 bits
00071         pSample3->FrameSize = 16 / 8 * 1;
00072         pSample3->SamplesPerSecond = 44100;
00073         pSample4->Channels = 1; // mono
00074         pSample4->BitDepth = 16; // 16 bits
00075         pSample4->FrameSize = 16 / 8 * 1;
00076         pSample4->SamplesPerSecond = 44100;
00077         // resize those samples to a length of three sample points
00078         // (again: _sample_points_ NOT bytes!) which is the length of our
00079         // ficticious samples from above. after the Save() call below we can
00080         // then directly write our sample data to disk by using the Write()
00081         // method, that is without having to load all the sample data into
00082         // RAM. for large instruments / .gig files this is definitely the way
00083         // to go
00084         pSample1->Resize(3);
00085         pSample2->Resize(3);
00086         pSample3->Resize(3);
00087         pSample4->Resize(3);
00088 
00089         // create four instruments
00090         gig::Instrument* pInstrument1 = file.AddInstrument();
00091         gig::Instrument* pInstrument2 = file.AddInstrument();
00092         gig::Instrument* pInstrument3 = file.AddInstrument();
00093         gig::Instrument* pInstrument4 = file.AddInstrument();
00094         // give them a name (not mandatory)
00095         pInstrument1->pInfo->Name = "Foo Instrument 1";
00096         pInstrument2->pInfo->Name = "Foo Instrument 2";
00097         pInstrument3->pInfo->Name = "Foo Instrument 3";
00098         pInstrument4->pInfo->Name = "Foo Instrument 4";
00099 
00100         // create one region for each instrument
00101         // in this example we do not add a dimension, so
00102         // every region will have exactly one DimensionRegion
00103         // also we assign a sample to each dimension region
00104         gig::Region* pRegion = pInstrument1->AddRegion();
00105         pRegion->SetSample(pSample1);
00106         pRegion->KeyRange.low  = 0;
00107         pRegion->KeyRange.high = 1;
00108         pRegion->VelocityRange.low  = 0;
00109         pRegion->VelocityRange.high = 1;
00110         pRegion->KeyGroup = 0;
00111         pRegion->pDimensionRegions[0]->pSample = pSample1;
00112 
00113         pRegion = pInstrument2->AddRegion();
00114         pRegion->SetSample(pSample2);
00115         pRegion->KeyRange.low  = 1;
00116         pRegion->KeyRange.high = 2;
00117         pRegion->VelocityRange.low  = 1;
00118         pRegion->VelocityRange.high = 2;
00119         pRegion->KeyGroup = 1;
00120         pRegion->pDimensionRegions[0]->pSample = pSample2;
00121 
00122         pRegion = pInstrument3->AddRegion();
00123         pRegion->SetSample(pSample3);
00124         pRegion->KeyRange.low  = 2;
00125         pRegion->KeyRange.high = 3;
00126         pRegion->VelocityRange.low  = 2;
00127         pRegion->VelocityRange.high = 3;
00128         pRegion->KeyGroup = 2;
00129         pRegion->pDimensionRegions[0]->pSample = pSample3;
00130 
00131         pRegion = pInstrument4->AddRegion();
00132         pRegion->SetSample(pSample4);
00133         pRegion->KeyRange.low  = 3;
00134         pRegion->KeyRange.high = 4;
00135         pRegion->VelocityRange.low  = 3;
00136         pRegion->VelocityRange.high = 4;
00137         pRegion->KeyGroup = 3;
00138         pRegion->pDimensionRegions[0]->pSample = pSample4;
00139 
00140         // save file ("physically") as of now
00141         file.Save(TEST_GIG_FILE_NAME);
00142     } catch (RIFF::Exception e) {
00143         std::cerr << "\nCould not create a new Gigasampler file from scratch:\n" << std::flush;
00144         e.PrintMessage();
00145         throw e; // stop further tests
00146     }
00147 }
00148 
00149 // 3. Run) test if the newly created Gigasampler file exists & can be opened
00150 void GigWriteTest::testOpenCreatedGigFile() {
00151     // try to open previously created Gigasampler file
00152     try {
00153         RIFF::File riff(TEST_GIG_FILE_NAME);
00154         gig::File file(&riff);
00155     } catch (RIFF::Exception e) {
00156         std::cerr << "\nCould not open newly created Gigasampler file:\n" << std::flush;
00157         e.PrintMessage();
00158         throw e; // stop further tests
00159     }
00160 }
00161 
00162 // 4. Run) test if the articulation informations of the newly created Gigasampler file were correctly written
00163 void GigWriteTest::testArticulationsOfCreatedGigFile() {
00164     try {
00165         // open previously created Gigasampler file
00166         RIFF::File riff(TEST_GIG_FILE_NAME);
00167         gig::File file(&riff);
00168         // check global file informations
00169         CPPUNIT_ASSERT(file.pInfo);
00170         CPPUNIT_ASSERT(file.pInfo->Name == "Foo Gigasampler File");
00171         // check amount of instruments and samples
00172         CPPUNIT_ASSERT(file.Instruments == 4);
00173         int iInstruments = 0;
00174         for (gig::Instrument* pInstrument = file.GetFirstInstrument(); pInstrument; pInstrument = file.GetNextInstrument()) iInstruments++;
00175         CPPUNIT_ASSERT(iInstruments == 4);
00176         int iSamples = 0;
00177         for (gig::Sample* pSample = file.GetFirstSample(); pSample; pSample = file.GetNextSample()) iSamples++;
00178         CPPUNIT_ASSERT(iSamples == 4);
00179         // check samples' meta informations
00180         int iSample = 1;
00181         for (gig::Sample* pSample = file.GetFirstSample(); pSample; pSample = file.GetNextSample()) {
00182             CPPUNIT_ASSERT(pSample->pInfo);
00183             std::string sOughtToBe = "Foo Sample " + ToString(iSample);
00184             CPPUNIT_ASSERT(pSample->pInfo->Name == sOughtToBe);
00185             CPPUNIT_ASSERT(pSample->GetSize() == 3); // three sample points
00186             CPPUNIT_ASSERT(pSample->SamplesTotal == 3); // three sample points
00187             CPPUNIT_ASSERT(pSample->Channels == 1); // mono
00188             CPPUNIT_ASSERT(pSample->BitDepth == 16); // bit depth 16 bits
00189             CPPUNIT_ASSERT(pSample->FrameSize == 16 / 8 * 1);
00190             CPPUNIT_ASSERT(pSample->SamplesPerSecond == 44100);
00191             iSample++;
00192         }
00193         // check instruments' meta informations
00194         int iInstrument = 1;
00195         for (gig::Instrument* pInstrument = file.GetFirstInstrument(); pInstrument; pInstrument = file.GetNextInstrument()) {
00196             CPPUNIT_ASSERT(pInstrument->pInfo);
00197             std::string sOughtToBe = "Foo Instrument " + ToString(iInstrument);
00198             CPPUNIT_ASSERT(pInstrument->pInfo->Name == sOughtToBe);
00199             gig::Region* pRegion = pInstrument->GetFirstRegion();
00200             CPPUNIT_ASSERT(pRegion);
00201             CPPUNIT_ASSERT(pRegion->Dimensions == 0);
00202             CPPUNIT_ASSERT(pRegion->DimensionRegions == 1);
00203             sOughtToBe = "Foo Sample " + ToString(iInstrument);
00204             CPPUNIT_ASSERT(pRegion->GetSample()->pInfo->Name == sOughtToBe);
00205             CPPUNIT_ASSERT(pRegion->KeyRange.low  == iInstrument - 1);
00206             CPPUNIT_ASSERT(pRegion->KeyRange.high == iInstrument);
00207             CPPUNIT_ASSERT(pRegion->VelocityRange.low  == iInstrument - 1);
00208             CPPUNIT_ASSERT(pRegion->VelocityRange.high == iInstrument);
00209             CPPUNIT_ASSERT(pRegion->KeyGroup  == iInstrument - 1);
00210             gig::DimensionRegion* pDimensionRegion = pRegion->GetDimensionRegionByValue((uint[8]){0,0,0,0,0,0,0,0});
00211             CPPUNIT_ASSERT(pDimensionRegion);
00212             CPPUNIT_ASSERT(pDimensionRegion->pSample->pInfo->Name == sOughtToBe);
00213             iInstrument++;
00214         }
00215     } catch (RIFF::Exception e) {
00216         std::cerr << "\nThere was an exception while checking the articulation data of the newly created Gigasampler file:\n" << std::flush;
00217         e.PrintMessage();
00218         throw e; // stop further tests
00219     }
00220 }
00221 
00222 // 5. Run) try to write sample data to that newly created Gigasampler file
00223 void GigWriteTest::testWriteSamples() {
00224     try {
00225         // open previously created Gigasampler file (in read/write mode)
00226         RIFF::File riff(TEST_GIG_FILE_NAME);
00227         riff.SetMode(RIFF::stream_mode_read_write);
00228         gig::File file(&riff);
00229         // until this point we just wrote the articulation data to the .gig file
00230         // and prepared the .gig file for writing our 4 example sample data, so
00231         // now as the file exists physically and the 'samples' are already
00232         // of the correct size we can now write the actual samples' data by
00233         // directly writing them to disk
00234         gig::Sample* pSample1 = file.GetFirstSample();
00235         gig::Sample* pSample2 = file.GetNextSample();
00236         gig::Sample* pSample3 = file.GetNextSample();
00237         gig::Sample* pSample4 = file.GetNextSample();
00238         CPPUNIT_ASSERT(pSample1);
00239         pSample1->Write(sampleData1, 3);
00240         CPPUNIT_ASSERT(pSample2);
00241         pSample2->Write(sampleData2, 3);
00242         CPPUNIT_ASSERT(pSample3);
00243         pSample3->Write(sampleData3, 3);
00244         CPPUNIT_ASSERT(pSample4);
00245         pSample4->Write(sampleData4, 3);
00246     } catch (RIFF::Exception e) {
00247         std::cerr << "\nCould not directly write samples to newly created Gigasampler file:\n" << std::flush;
00248         e.PrintMessage();
00249         throw e; // stop further tests
00250     }
00251 }
00252 
00253 // 6. Run) check the previously written samples' data
00254 void GigWriteTest::testSamplesData() {
00255     try {
00256         // open previously created Gigasampler file
00257         RIFF::File riff(TEST_GIG_FILE_NAME);
00258         gig::File file(&riff);
00259         // check samples' meta informations
00260         gig::Sample* pSample1 = file.GetFirstSample();
00261         gig::Sample* pSample2 = file.GetNextSample();
00262         gig::Sample* pSample3 = file.GetNextSample();
00263         gig::Sample* pSample4 = file.GetNextSample();
00264         CPPUNIT_ASSERT(pSample1);
00265         CPPUNIT_ASSERT(pSample2);
00266         CPPUNIT_ASSERT(pSample3);
00267         CPPUNIT_ASSERT(pSample4);
00268         gig::buffer_t sampleBuffer1 = pSample1->LoadSampleData();
00269         gig::buffer_t sampleBuffer2 = pSample2->LoadSampleData();
00270         gig::buffer_t sampleBuffer3 = pSample3->LoadSampleData();
00271         gig::buffer_t sampleBuffer4 = pSample4->LoadSampleData();
00272         CPPUNIT_ASSERT(sampleBuffer1.pStart);
00273         CPPUNIT_ASSERT(sampleBuffer2.pStart);
00274         CPPUNIT_ASSERT(sampleBuffer3.pStart);
00275         CPPUNIT_ASSERT(sampleBuffer4.pStart);
00276         CPPUNIT_ASSERT(sampleBuffer1.Size == pSample1->FrameSize * 3); // three sample points length
00277         CPPUNIT_ASSERT(sampleBuffer2.Size == pSample2->FrameSize * 3); // three sample points length
00278         CPPUNIT_ASSERT(sampleBuffer3.Size == pSample3->FrameSize * 3); // three sample points length
00279         CPPUNIT_ASSERT(sampleBuffer4.Size == pSample4->FrameSize * 3); // three sample points length
00280         // check samples' PCM data
00281         CPPUNIT_ASSERT(memcmp(sampleBuffer1.pStart, sampleData1, 3) == 0);
00282         CPPUNIT_ASSERT(memcmp(sampleBuffer2.pStart, sampleData2, 3) == 0);
00283         CPPUNIT_ASSERT(memcmp(sampleBuffer3.pStart, sampleData3, 3) == 0);
00284         CPPUNIT_ASSERT(memcmp(sampleBuffer4.pStart, sampleData4, 3) == 0);
00285     } catch (RIFF::Exception e) {
00286         std::cerr << "\nThere was an exception while checking the written samples' data:\n" << std::flush;
00287         e.PrintMessage();
00288         throw e; // stop further tests
00289     }
00290 }

Generated on Wed Dec 6 19:25:57 2006 for libgig by  doxygen 1.5.1