1 |
// This little test application demonstrates how to create and modify |
2 |
// Gigasampler files with libgig 3.0.0. |
3 |
// |
4 |
// Date: 2006-04-29 |
5 |
// |
6 |
// Compile with: 'g++ -lgig -o gigwritedemo gigwritedemo.cpp' |
7 |
|
8 |
#include <gig.h> |
9 |
|
10 |
#include <stdlib.h> |
11 |
#include <stdio.h> |
12 |
#include <string.h> |
13 |
|
14 |
int main() { |
15 |
// four stupid little sample "waves" |
16 |
// (each having three sample points length, 16 bit depth, mono) |
17 |
int16_t sampleData1[] = { 1, 2, 3 }; |
18 |
int16_t sampleData2[] = { 4, 5, 6 }; |
19 |
int16_t sampleData3[] = { 7, 8, 9 }; |
20 |
int16_t sampleData4[] = { 10,11,12 }; |
21 |
try { |
22 |
// create an empty Gigasampler file |
23 |
gig::File file; |
24 |
// we give it an internal name, not mandatory though |
25 |
file.pInfo->Name = "Foo Gigasampler File"; |
26 |
|
27 |
// create four samples |
28 |
gig::Sample* pSample1 = file.AddSample(); |
29 |
gig::Sample* pSample2 = file.AddSample(); |
30 |
gig::Sample* pSample3 = file.AddSample(); |
31 |
gig::Sample* pSample4 = file.AddSample(); |
32 |
// give those samples a name (not mandatory) |
33 |
pSample1->pInfo->Name = "Foo Sample 1"; |
34 |
pSample2->pInfo->Name = "Foo Sample 2"; |
35 |
pSample3->pInfo->Name = "Foo Sample 3"; |
36 |
pSample4->pInfo->Name = "Foo Sample 4"; |
37 |
// set meta informations for those samples |
38 |
pSample1->Channels = 1; // mono |
39 |
pSample1->BitDepth = 16; // 16 bits |
40 |
pSample1->FrameSize = 16/*bitdepth*/ / 8/*1 byte are 8 bits*/ * 1/*mono*/; |
41 |
pSample1->SamplesPerSecond = 44100; |
42 |
pSample2->Channels = 1; // mono |
43 |
pSample2->BitDepth = 16; // 16 bits |
44 |
pSample2->FrameSize = 16 / 8 * 1; |
45 |
pSample2->SamplesPerSecond = 44100; |
46 |
pSample3->Channels = 1; // mono |
47 |
pSample3->BitDepth = 16; // 16 bits |
48 |
pSample3->FrameSize = 16 / 8 * 1; |
49 |
pSample3->SamplesPerSecond = 44100; |
50 |
pSample4->Channels = 1; // mono |
51 |
pSample4->BitDepth = 16; // 16 bits |
52 |
pSample4->FrameSize = 16 / 8 * 1; |
53 |
pSample4->SamplesPerSecond = 44100; |
54 |
// resize those samples to a length of three sample points |
55 |
// (again: _sample_points_ NOT bytes!) which is the length of our |
56 |
// ficticious samples from above. after the Save() call below we can |
57 |
// then directly write our sample data to disk by using the Write() |
58 |
// method, that is without having to load all the sample data into |
59 |
// RAM. for large instruments / .gig files this is definitely the way |
60 |
// to go |
61 |
pSample1->Resize(3); |
62 |
pSample2->Resize(3); |
63 |
pSample3->Resize(3); |
64 |
pSample4->Resize(3); |
65 |
|
66 |
// create four instruments |
67 |
gig::Instrument* pInstrument1 = file.AddInstrument(); |
68 |
gig::Instrument* pInstrument2 = file.AddInstrument(); |
69 |
gig::Instrument* pInstrument3 = file.AddInstrument(); |
70 |
gig::Instrument* pInstrument4 = file.AddInstrument(); |
71 |
// give them a name (not mandatory) |
72 |
pInstrument1->pInfo->Name = "Foo Instrument 1"; |
73 |
pInstrument2->pInfo->Name = "Foo Instrument 2"; |
74 |
pInstrument3->pInfo->Name = "Foo Instrument 3"; |
75 |
pInstrument4->pInfo->Name = "Foo Instrument 4"; |
76 |
|
77 |
// create one region for each instrument |
78 |
// in this example we do not add a dimension, so |
79 |
// every region will have exactly one DimensionRegion |
80 |
gig::Region* pRegion = pInstrument1->AddRegion(); |
81 |
pRegion->SetSample(pSample1); |
82 |
pRegion->pDimensionRegions[0]->pSample = pSample1; |
83 |
|
84 |
pRegion = pInstrument2->AddRegion(); |
85 |
pRegion->SetSample(pSample2); |
86 |
pRegion->pDimensionRegions[0]->pSample = pSample2; |
87 |
|
88 |
pRegion = pInstrument3->AddRegion(); |
89 |
pRegion->SetSample(pSample3); |
90 |
pRegion->pDimensionRegions[0]->pSample = pSample3; |
91 |
|
92 |
pRegion = pInstrument4->AddRegion(); |
93 |
pRegion->SetSample(pSample4); |
94 |
pRegion->pDimensionRegions[0]->pSample = pSample4; |
95 |
|
96 |
// save file as of now |
97 |
file.Save("foo.gig"); |
98 |
|
99 |
// now as the file exists physically and the 'samples' are already |
100 |
// of the correct size we can write the actual sample data by |
101 |
// directly writing to disk |
102 |
pSample1->Write(sampleData1, 3); |
103 |
pSample2->Write(sampleData2, 3); |
104 |
pSample3->Write(sampleData3, 3); |
105 |
pSample4->Write(sampleData4, 3); |
106 |
} catch (RIFF::Exception e) { |
107 |
e.PrintMessage(); |
108 |
return -1; |
109 |
} |
110 |
return 0; |
111 |
} |