/[svn]/libgig/trunk/src/testcases/GigWriteTest.cpp
ViewVC logotype

Annotation of /libgig/trunk/src/testcases/GigWriteTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 917 - (hide annotations) (download)
Sun Aug 20 14:50:36 2006 UTC (17 years, 8 months ago) by schoenebeck
File size: 7426 byte(s)
* added CPPUnit test cases (at the moment primarily for automatic check
  of Gigasampler write support)

1 schoenebeck 917 #include "GigWriteTest.h"
2    
3     #include <iostream>
4     #include <stdlib.h>
5     #include <stdio.h>
6     #include <string.h>
7    
8     #include "../gig.h"
9    
10     CPPUNIT_TEST_SUITE_REGISTRATION(GigWriteTest);
11    
12     using namespace std;
13    
14     // file name of the Gigasampler file we are going to create for these tests
15     #define TEST_GIG_FILE_NAME "foo.gig"
16    
17     // four stupid little sample "waves"
18     // (each having three sample points length, 16 bit depth, mono)
19     int16_t sampleData1[] = { 1, 2, 3 };
20     int16_t sampleData2[] = { 4, 5, 6 };
21     int16_t sampleData3[] = { 7, 8, 9 };
22     int16_t sampleData4[] = { 10,11,12 };
23    
24     void GigWriteTest::printTestSuiteName() {
25     cout << "\b \nTesting Gigasampler write support: " << flush;
26     }
27    
28     // code executed when this test suite is created
29     void GigWriteTest::setUp() {
30     }
31    
32     // code executed when this test suite will be destroyed
33     void GigWriteTest::tearDown() {
34     }
35    
36    
37     /////////////////////////////////////////////////////////////////////////////
38     // The actual test cases (in order) ...
39    
40     // 1.) create a new Gigasampler file from scratch
41     void GigWriteTest::createNewGigFile() {
42     try {
43     // create an empty Gigasampler file
44     gig::File file;
45     // we give it an internal name, not mandatory though
46     file.pInfo->Name = "Foo Gigasampler File";
47    
48     // create four samples
49     gig::Sample* pSample1 = file.AddSample();
50     gig::Sample* pSample2 = file.AddSample();
51     gig::Sample* pSample3 = file.AddSample();
52     gig::Sample* pSample4 = file.AddSample();
53     // give those samples a name (not mandatory)
54     pSample1->pInfo->Name = "Foo Sample 1";
55     pSample2->pInfo->Name = "Foo Sample 2";
56     pSample3->pInfo->Name = "Foo Sample 3";
57     pSample4->pInfo->Name = "Foo Sample 4";
58     // set meta informations for those samples
59     pSample1->Channels = 1; // mono
60     pSample1->BitDepth = 16; // 16 bits
61     pSample1->FrameSize = 16/*bitdepth*/ / 8/*1 byte are 8 bits*/ * 1/*mono*/;
62     pSample1->SamplesPerSecond = 44100;
63     pSample2->Channels = 1; // mono
64     pSample2->BitDepth = 16; // 16 bits
65     pSample2->FrameSize = 16 / 8 * 1;
66     pSample2->SamplesPerSecond = 44100;
67     pSample3->Channels = 1; // mono
68     pSample3->BitDepth = 16; // 16 bits
69     pSample3->FrameSize = 16 / 8 * 1;
70     pSample3->SamplesPerSecond = 44100;
71     pSample4->Channels = 1; // mono
72     pSample4->BitDepth = 16; // 16 bits
73     pSample4->FrameSize = 16 / 8 * 1;
74     pSample4->SamplesPerSecond = 44100;
75     // resize those samples to a length of three sample points
76     // (again: _sample_points_ NOT bytes!) which is the length of our
77     // ficticious samples from above. after the Save() call below we can
78     // then directly write our sample data to disk by using the Write()
79     // method, that is without having to load all the sample data into
80     // RAM. for large instruments / .gig files this is definitely the way
81     // to go
82     pSample1->Resize(3);
83     pSample2->Resize(3);
84     pSample3->Resize(3);
85     pSample4->Resize(3);
86    
87     // create four instruments
88     gig::Instrument* pInstrument1 = file.AddInstrument();
89     gig::Instrument* pInstrument2 = file.AddInstrument();
90     gig::Instrument* pInstrument3 = file.AddInstrument();
91     gig::Instrument* pInstrument4 = file.AddInstrument();
92     // give them a name (not mandatory)
93     pInstrument1->pInfo->Name = "Foo Instrument 1";
94     pInstrument2->pInfo->Name = "Foo Instrument 2";
95     pInstrument3->pInfo->Name = "Foo Instrument 3";
96     pInstrument4->pInfo->Name = "Foo Instrument 4";
97    
98     // create one region for each instrument
99     // in this example we do not add a dimension, so
100     // every region will have exactly one DimensionRegion
101     // also we assign a sample to each dimension region
102     gig::Region* pRegion = pInstrument1->AddRegion();
103     pRegion->SetSample(pSample1);
104     pRegion->pDimensionRegions[0]->pSample = pSample1;
105    
106     pRegion = pInstrument2->AddRegion();
107     pRegion->SetSample(pSample2);
108     pRegion->pDimensionRegions[0]->pSample = pSample2;
109    
110     pRegion = pInstrument3->AddRegion();
111     pRegion->SetSample(pSample3);
112     pRegion->pDimensionRegions[0]->pSample = pSample3;
113    
114     pRegion = pInstrument4->AddRegion();
115     pRegion->SetSample(pSample4);
116     pRegion->pDimensionRegions[0]->pSample = pSample4;
117    
118     // save file ("physically") as of now
119     file.Save(TEST_GIG_FILE_NAME);
120     } catch (RIFF::Exception e) {
121     std::cerr << "\nCould not create a new Gigasampler file from scratch:\n" << std::flush;
122     e.PrintMessage();
123     throw e; // stop further tests
124     }
125     }
126    
127     // 2.) test if the newly created Gigasampler file exists & can be opened
128     void GigWriteTest::testOpenCreatedGigFile() {
129     // try to open previously created Gigasampler file
130     try {
131     RIFF::File riff(TEST_GIG_FILE_NAME);
132     gig::File file(&riff);
133     } catch (RIFF::Exception e) {
134     std::cerr << "\nCould not open newly created Gigasampler file:\n" << std::flush;
135     e.PrintMessage();
136     throw e; // stop further tests
137     }
138     }
139    
140     // 3.) test if the articulation informations of the newly created Gigasampler file were correctly written
141     void GigWriteTest::testArticulationsOfCreatedGigFile() {
142     try {
143     // open previously created Gigasampler file
144     RIFF::File riff(TEST_GIG_FILE_NAME);
145     gig::File file(&riff);
146     // check amount of instruments and samples
147     CPPUNIT_ASSERT(file.Instruments == 4);
148     int iSamples = 0;
149     for (gig::Sample* pSample = file.GetFirstSample(); pSample; pSample = file.GetNextSample()) iSamples++;
150     CPPUNIT_ASSERT(iSamples == 4);
151    
152     //TODO: check all articulations previously set in createNewGigFile()
153    
154     } catch (RIFF::Exception e) {
155     std::cerr << "\nThere was an exception while checking the articulation data of the newly created Gigasampler file:\n" << std::flush;
156     e.PrintMessage();
157     throw e; // stop further tests
158     }
159     }
160    
161     // 4.) try to write sample data to that newly created Gigasampler file
162     void GigWriteTest::testWriteSamples() {
163     try {
164     // open previously created Gigasampler file
165     RIFF::File riff(TEST_GIG_FILE_NAME);
166     gig::File file(&riff);
167     // until this point we just wrote the articulation data to the .gig file
168     // and prepared the .gig file for writing our 4 example sample data, so
169     // now as the file exists physically and the 'samples' are already
170     // of the correct size we can now write the actual samples' data by
171     // directly writing them to disk
172     gig::Sample* pSample1 = file.GetFirstSample();
173     gig::Sample* pSample2 = file.GetNextSample();
174     gig::Sample* pSample3 = file.GetNextSample();
175     gig::Sample* pSample4 = file.GetNextSample();
176     CPPUNIT_ASSERT(pSample1);
177     pSample1->Write(sampleData1, 3);
178     CPPUNIT_ASSERT(pSample2);
179     pSample2->Write(sampleData2, 3);
180     CPPUNIT_ASSERT(pSample3);
181     pSample3->Write(sampleData3, 3);
182     CPPUNIT_ASSERT(pSample4);
183     pSample4->Write(sampleData4, 3);
184     } catch (RIFF::Exception e) {
185     std::cerr << "\nCould not directly write samples to newly created Gigasampler file:\n" << std::flush;
186     e.PrintMessage();
187     throw e; // stop further tests
188     }
189     }

  ViewVC Help
Powered by ViewVC