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
00016 #define TEST_GIG_FILE_NAME "foo.gig"
00017
00018
00019
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
00026 void GigWriteTest::printTestSuiteName() {
00027 cout << "\b \nTesting Gigasampler write support: " << flush;
00028 }
00029
00030
00031 void GigWriteTest::setUp() {
00032 }
00033
00034
00035 void GigWriteTest::tearDown() {
00036 }
00037
00038
00040
00041
00042
00043 void GigWriteTest::createNewGigFile() {
00044 try {
00045
00046 gig::File file;
00047
00048 file.pInfo->Name = "Foo Gigasampler File";
00049
00050
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
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
00061 pSample1->Channels = 1;
00062 pSample1->BitDepth = 16;
00063 pSample1->FrameSize = 16 / 8 * 1;
00064 pSample1->SamplesPerSecond = 44100;
00065 pSample2->Channels = 1;
00066 pSample2->BitDepth = 16;
00067 pSample2->FrameSize = 16 / 8 * 1;
00068 pSample2->SamplesPerSecond = 44100;
00069 pSample3->Channels = 1;
00070 pSample3->BitDepth = 16;
00071 pSample3->FrameSize = 16 / 8 * 1;
00072 pSample3->SamplesPerSecond = 44100;
00073 pSample4->Channels = 1;
00074 pSample4->BitDepth = 16;
00075 pSample4->FrameSize = 16 / 8 * 1;
00076 pSample4->SamplesPerSecond = 44100;
00077
00078
00079
00080
00081
00082
00083
00084 pSample1->Resize(3);
00085 pSample2->Resize(3);
00086 pSample3->Resize(3);
00087 pSample4->Resize(3);
00088
00089
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
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
00101
00102
00103
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
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;
00146 }
00147 }
00148
00149
00150 void GigWriteTest::testOpenCreatedGigFile() {
00151
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;
00159 }
00160 }
00161
00162
00163 void GigWriteTest::testArticulationsOfCreatedGigFile() {
00164 try {
00165
00166 RIFF::File riff(TEST_GIG_FILE_NAME);
00167 gig::File file(&riff);
00168
00169 CPPUNIT_ASSERT(file.pInfo);
00170 CPPUNIT_ASSERT(file.pInfo->Name == "Foo Gigasampler File");
00171
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
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);
00186 CPPUNIT_ASSERT(pSample->SamplesTotal == 3);
00187 CPPUNIT_ASSERT(pSample->Channels == 1);
00188 CPPUNIT_ASSERT(pSample->BitDepth == 16);
00189 CPPUNIT_ASSERT(pSample->FrameSize == 16 / 8 * 1);
00190 CPPUNIT_ASSERT(pSample->SamplesPerSecond == 44100);
00191 iSample++;
00192 }
00193
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;
00219 }
00220 }
00221
00222
00223 void GigWriteTest::testWriteSamples() {
00224 try {
00225
00226 RIFF::File riff(TEST_GIG_FILE_NAME);
00227 riff.SetMode(RIFF::stream_mode_read_write);
00228 gig::File file(&riff);
00229
00230
00231
00232
00233
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;
00250 }
00251 }
00252
00253
00254 void GigWriteTest::testSamplesData() {
00255 try {
00256
00257 RIFF::File riff(TEST_GIG_FILE_NAME);
00258 gig::File file(&riff);
00259
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);
00277 CPPUNIT_ASSERT(sampleBuffer2.Size == pSample2->FrameSize * 3);
00278 CPPUNIT_ASSERT(sampleBuffer3.Size == pSample3->FrameSize * 3);
00279 CPPUNIT_ASSERT(sampleBuffer4.Size == pSample4->FrameSize * 3);
00280
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;
00289 }
00290 }