00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __GIG_H__
00025 #define __GIG_H__
00026
00027 #include "DLS.h"
00028
00029 #include <math.h>
00030 #include <string.h>
00031
00037 #define INITIAL_SAMPLE_BUFFER_SIZE 512000 // 512 kB
00038
00039 #if WORDS_BIGENDIAN
00040 # define LIST_TYPE_3PRG 0x33707267
00041 # define LIST_TYPE_3EWL 0x3365776C
00042 # define CHUNK_ID_SMPL 0x736D706C
00043 # define CHUNK_ID_3GIX 0x33676978
00044 # define CHUNK_ID_3EWA 0x33657761
00045 # define CHUNK_ID_3LNK 0x336C6E6B
00046 # define CHUNK_ID_3EWG 0x33657767
00047 # define CHUNK_ID_EWAV 0x65776176
00048 #else // little endian
00049 # define LIST_TYPE_3PRG 0x67727033
00050 # define LIST_TYPE_3EWL 0x6C776533
00051 # define CHUNK_ID_SMPL 0x6C706D73
00052 # define CHUNK_ID_3GIX 0x78696733
00053 # define CHUNK_ID_3EWA 0x61776533
00054 # define CHUNK_ID_3LNK 0x6B6E6C33
00055 # define CHUNK_ID_3EWG 0x67776533
00056 # define CHUNK_ID_EWAV 0x76617765
00057 #endif // WORDS_BIGENDIAN
00058
00060 #define GIG_EXP_DECODE(x) (pow(1.000000008813822, x))
00061 #define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01))
00062 #define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03)
00063 #define GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(x) ((x >> 1) & 0x03)
00064 #define GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(x) ((x >> 3) & 0x03)
00065 #define GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(x) ((x >> 5) & 0x03)
00066
00068 namespace gig {
00069
00070 typedef std::string String;
00071
00073 struct range_t {
00074 uint8_t low;
00075 uint8_t high;
00076 };
00077
00079 struct buffer_t {
00080 void* pStart;
00081 unsigned long Size;
00082 unsigned long NullExtensionSize;
00083 buffer_t() {
00084 pStart = NULL;
00085 Size = 0;
00086 NullExtensionSize = 0;
00087 }
00088 };
00089
00091 typedef enum {
00092 loop_type_normal = 0x00000000,
00093 loop_type_bidirectional = 0x00000001,
00094 loop_type_backward = 0x00000002
00095 } loop_type_t;
00096
00098 typedef enum {
00099 smpte_format_no_offset = 0x00000000,
00100 smpte_format_24_frames = 0x00000018,
00101 smpte_format_25_frames = 0x00000019,
00102 smpte_format_30_frames_dropping = 0x0000001D,
00103 smpte_format_30_frames = 0x0000001E
00104 } smpte_format_t;
00105
00107 typedef enum {
00108 curve_type_nonlinear = 0,
00109 curve_type_linear = 1,
00110 curve_type_special = 2,
00111 curve_type_unknown = 0xffffffff
00112 } curve_type_t;
00113
00115 typedef enum {
00116 dim_bypass_ctrl_none,
00117 dim_bypass_ctrl_94,
00118 dim_bypass_ctrl_95
00119 } dim_bypass_ctrl_t;
00120
00122 typedef enum {
00123 lfo3_ctrl_internal = 0x00,
00124 lfo3_ctrl_modwheel = 0x01,
00125 lfo3_ctrl_aftertouch = 0x02,
00126 lfo3_ctrl_internal_modwheel = 0x03,
00127 lfo3_ctrl_internal_aftertouch = 0x04
00128 } lfo3_ctrl_t;
00129
00131 typedef enum {
00132 lfo2_ctrl_internal = 0x00,
00133 lfo2_ctrl_modwheel = 0x01,
00134 lfo2_ctrl_foot = 0x02,
00135 lfo2_ctrl_internal_modwheel = 0x03,
00136 lfo2_ctrl_internal_foot = 0x04
00137 } lfo2_ctrl_t;
00138
00140 typedef enum {
00141 lfo1_ctrl_internal = 0x00,
00142 lfo1_ctrl_modwheel = 0x01,
00143 lfo1_ctrl_breath = 0x02,
00144 lfo1_ctrl_internal_modwheel = 0x03,
00145 lfo1_ctrl_internal_breath = 0x04
00146 } lfo1_ctrl_t;
00147
00149 typedef enum {
00150 vcf_cutoff_ctrl_none = 0x00,
00151 vcf_cutoff_ctrl_modwheel = 0x81,
00152 vcf_cutoff_ctrl_effect1 = 0x8c,
00153 vcf_cutoff_ctrl_effect2 = 0x8d,
00154 vcf_cutoff_ctrl_breath = 0x82,
00155 vcf_cutoff_ctrl_foot = 0x84,
00156 vcf_cutoff_ctrl_sustainpedal = 0xc0,
00157 vcf_cutoff_ctrl_softpedal = 0xc3,
00158 vcf_cutoff_ctrl_genpurpose7 = 0xd2,
00159 vcf_cutoff_ctrl_genpurpose8 = 0xd3,
00160 vcf_cutoff_ctrl_aftertouch = 0x80
00161 } vcf_cutoff_ctrl_t;
00162
00164 typedef enum {
00165 vcf_res_ctrl_none = 0xffffffff,
00166 vcf_res_ctrl_genpurpose3 = 0,
00167 vcf_res_ctrl_genpurpose4 = 1,
00168 vcf_res_ctrl_genpurpose5 = 2,
00169 vcf_res_ctrl_genpurpose6 = 3
00170 } vcf_res_ctrl_t;
00171
00180 struct leverage_ctrl_t {
00181 typedef enum {
00182 type_none = 0x00,
00183 type_channelaftertouch = 0x2f,
00184 type_velocity = 0xff,
00185 type_controlchange = 0xfe
00186 } type_t;
00187
00188 type_t type;
00189 uint controller_number;
00190 };
00191
00197 typedef leverage_ctrl_t attenuation_ctrl_t;
00198
00204 typedef leverage_ctrl_t eg1_ctrl_t;
00205
00211 typedef leverage_ctrl_t eg2_ctrl_t;
00212
00220 typedef enum {
00221 dimension_none = 0x00,
00222 dimension_samplechannel = 0x80,
00223 dimension_layer = 0x81,
00224 dimension_velocity = 0x82,
00225 dimension_channelaftertouch = 0x83,
00226 dimension_releasetrigger = 0x84,
00227 dimension_keyboard = 0x85,
00228 dimension_roundrobin = 0x86,
00229 dimension_random = 0x87,
00230 dimension_modwheel = 0x01,
00231 dimension_breath = 0x02,
00232 dimension_foot = 0x04,
00233 dimension_portamentotime = 0x05,
00234 dimension_effect1 = 0x0c,
00235 dimension_effect2 = 0x0d,
00236 dimension_genpurpose1 = 0x10,
00237 dimension_genpurpose2 = 0x11,
00238 dimension_genpurpose3 = 0x12,
00239 dimension_genpurpose4 = 0x13,
00240 dimension_sustainpedal = 0x40,
00241 dimension_portamento = 0x41,
00242 dimension_sostenutopedal = 0x42,
00243 dimension_softpedal = 0x43,
00244 dimension_genpurpose5 = 0x30,
00245 dimension_genpurpose6 = 0x31,
00246 dimension_genpurpose7 = 0x32,
00247 dimension_genpurpose8 = 0x33,
00248 dimension_effect1depth = 0x5b,
00249 dimension_effect2depth = 0x5c,
00250 dimension_effect3depth = 0x5d,
00251 dimension_effect4depth = 0x5e,
00252 dimension_effect5depth = 0x5f
00253 } dimension_t;
00254
00259 typedef enum {
00260 split_type_normal,
00261 split_type_customvelocity,
00262 split_type_bit
00263 } split_type_t;
00264
00266 struct dimension_def_t {
00267 dimension_t dimension;
00268 uint8_t bits;
00269 uint8_t zones;
00270 split_type_t split_type;
00271 range_t* ranges;
00272 unsigned int zone_size;
00273 };
00274
00276 typedef enum {
00277 vcf_type_lowpass = 0x00,
00278 vcf_type_lowpassturbo = 0xff,
00279 vcf_type_bandpass = 0x01,
00280 vcf_type_highpass = 0x02,
00281 vcf_type_bandreject = 0x03
00282 } vcf_type_t;
00283
00291 struct crossfade_t {
00292 #if WORDS_BIGENDIAN
00293 uint8_t out_end;
00294 uint8_t out_start;
00295 uint8_t in_end;
00296 uint8_t in_start;
00297 #else // little endian
00298 uint8_t in_start;
00299 uint8_t in_end;
00300 uint8_t out_start;
00301 uint8_t out_end;
00302 #endif // WORDS_BIGENDIAN
00303 };
00304
00306 struct playback_state_t {
00307 unsigned long position;
00308 bool reverse;
00309 unsigned long loop_cycles_left;
00310 };
00311
00324 struct progress_t {
00325 void (*callback)(progress_t*);
00326 float factor;
00327 void* custom;
00328 float __range_min;
00329 float __range_max;
00330 progress_t();
00331 };
00332
00333
00334 class File;
00335 class Instrument;
00336 class Sample;
00337 class Region;
00338
00351 class DimensionRegion : protected DLS::Sampler {
00352 public:
00353 uint8_t VelocityUpperLimit;
00354 Sample* pSample;
00355
00356 uint16_t EG1PreAttack;
00357 double EG1Attack;
00358 double EG1Decay1;
00359 double EG1Decay2;
00360 bool EG1InfiniteSustain;
00361 uint16_t EG1Sustain;
00362 double EG1Release;
00363 bool EG1Hold;
00364 eg1_ctrl_t EG1Controller;
00365 bool EG1ControllerInvert;
00366 uint8_t EG1ControllerAttackInfluence;
00367 uint8_t EG1ControllerDecayInfluence;
00368 uint8_t EG1ControllerReleaseInfluence;
00369 double LFO1Frequency;
00370 uint16_t LFO1InternalDepth;
00371 uint16_t LFO1ControlDepth;
00372 lfo1_ctrl_t LFO1Controller;
00373 bool LFO1FlipPhase;
00374 bool LFO1Sync;
00375
00376 uint16_t EG2PreAttack;
00377 double EG2Attack;
00378 double EG2Decay1;
00379 double EG2Decay2;
00380 bool EG2InfiniteSustain;
00381 uint16_t EG2Sustain;
00382 double EG2Release;
00383 eg2_ctrl_t EG2Controller;
00384 bool EG2ControllerInvert;
00385 uint8_t EG2ControllerAttackInfluence;
00386 uint8_t EG2ControllerDecayInfluence;
00387 uint8_t EG2ControllerReleaseInfluence;
00388 double LFO2Frequency;
00389 uint16_t LFO2InternalDepth;
00390 uint16_t LFO2ControlDepth;
00391 lfo2_ctrl_t LFO2Controller;
00392 bool LFO2FlipPhase;
00393 bool LFO2Sync;
00394
00395 double EG3Attack;
00396 int16_t EG3Depth;
00397 double LFO3Frequency;
00398 int16_t LFO3InternalDepth;
00399 int16_t LFO3ControlDepth;
00400 lfo3_ctrl_t LFO3Controller;
00401 bool LFO3Sync;
00402
00403 bool VCFEnabled;
00404 vcf_type_t VCFType;
00405 vcf_cutoff_ctrl_t VCFCutoffController;
00406 uint8_t VCFCutoff;
00407 curve_type_t VCFVelocityCurve;
00408 uint8_t VCFVelocityScale;
00409 uint8_t VCFVelocityDynamicRange;
00410 uint8_t VCFResonance;
00411 bool VCFResonanceDynamic;
00412 vcf_res_ctrl_t VCFResonanceController;
00413 bool VCFKeyboardTracking;
00414 uint8_t VCFKeyboardTrackingBreakpoint;
00415
00416 curve_type_t VelocityResponseCurve;
00417 uint8_t VelocityResponseDepth;
00418 uint8_t VelocityResponseCurveScaling;
00419 curve_type_t ReleaseVelocityResponseCurve;
00420 uint8_t ReleaseVelocityResponseDepth;
00421 uint8_t ReleaseTriggerDecay;
00422
00423 crossfade_t Crossfade;
00424 bool PitchTrack;
00425 dim_bypass_ctrl_t DimensionBypass;
00426 int8_t Pan;
00427 bool SelfMask;
00428 attenuation_ctrl_t AttenuationController;
00429 bool InvertAttenuationController;
00430 uint8_t AttenuationControllerThreshold;
00431 uint8_t ChannelOffset;
00432 bool SustainDefeat;
00433 bool MSDecode;
00434 uint16_t SampleStartOffset;
00435 double SampleAttenuation;
00436
00437
00438 DLS::Sampler::UnityNote;
00439 DLS::Sampler::FineTune;
00440 DLS::Sampler::Gain;
00441 DLS::Sampler::SampleLoops;
00442 DLS::Sampler::pSampleLoops;
00443
00444
00445 double GetVelocityAttenuation(uint8_t MIDIKeyVelocity);
00446 protected:
00447 DimensionRegion(RIFF::List* _3ewl);
00448 ~DimensionRegion();
00449 friend class Region;
00450 private:
00451 typedef enum {
00452 _lev_ctrl_none = 0x00,
00453 _lev_ctrl_modwheel = 0x03,
00454 _lev_ctrl_breath = 0x05,
00455 _lev_ctrl_foot = 0x07,
00456 _lev_ctrl_effect1 = 0x0d,
00457 _lev_ctrl_effect2 = 0x0f,
00458 _lev_ctrl_genpurpose1 = 0x11,
00459 _lev_ctrl_genpurpose2 = 0x13,
00460 _lev_ctrl_genpurpose3 = 0x15,
00461 _lev_ctrl_genpurpose4 = 0x17,
00462 _lev_ctrl_portamentotime = 0x0b,
00463 _lev_ctrl_sustainpedal = 0x01,
00464 _lev_ctrl_portamento = 0x19,
00465 _lev_ctrl_sostenutopedal = 0x1b,
00466 _lev_ctrl_softpedal = 0x09,
00467 _lev_ctrl_genpurpose5 = 0x1d,
00468 _lev_ctrl_genpurpose6 = 0x1f,
00469 _lev_ctrl_genpurpose7 = 0x21,
00470 _lev_ctrl_genpurpose8 = 0x23,
00471 _lev_ctrl_effect1depth = 0x25,
00472 _lev_ctrl_effect2depth = 0x27,
00473 _lev_ctrl_effect3depth = 0x29,
00474 _lev_ctrl_effect4depth = 0x2b,
00475 _lev_ctrl_effect5depth = 0x2d,
00476 _lev_ctrl_channelaftertouch = 0x2f,
00477 _lev_ctrl_velocity = 0xff
00478 } _lev_ctrl_t;
00479 typedef std::map<uint32_t, double*> VelocityTableMap;
00480
00481 static uint Instances;
00482 static VelocityTableMap* pVelocityTables;
00483 double* pVelocityAttenuationTable;
00484
00485 leverage_ctrl_t DecodeLeverageController(_lev_ctrl_t EncodedController);
00486 double* CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling);
00487 };
00488
00490 class Sample : public DLS::Sample {
00491 public:
00492 uint16_t SampleGroup;
00493 uint32_t Manufacturer;
00494 uint32_t Product;
00495 uint32_t SamplePeriod;
00496 uint32_t MIDIUnityNote;
00497 uint32_t FineTune;
00498 smpte_format_t SMPTEFormat;
00499 uint32_t SMPTEOffset;
00500 uint32_t Loops;
00501 uint32_t LoopID;
00502 loop_type_t LoopType;
00503 uint32_t LoopStart;
00504 uint32_t LoopEnd;
00505 uint32_t LoopSize;
00506 uint32_t LoopFraction;
00507 uint32_t LoopPlayCount;
00508 bool Compressed;
00509 uint32_t TruncatedBits;
00510 bool Dithered;
00511
00512
00513 buffer_t LoadSampleData();
00514 buffer_t LoadSampleData(unsigned long SampleCount);
00515 buffer_t LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount);
00516 buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount);
00517 buffer_t GetCache();
00518
00519 static buffer_t CreateDecompressionBuffer(unsigned long MaxReadSize);
00520 static void DestroyDecompressionBuffer(buffer_t& DecompressionBuffer);
00521
00522 void ReleaseSampleData();
00523 unsigned long SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence = RIFF::stream_start);
00524 unsigned long GetPos();
00525 unsigned long Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer = NULL);
00526 unsigned long ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer = NULL);
00527 protected:
00528 static unsigned int Instances;
00529 static buffer_t InternalDecompressionBuffer;
00530 unsigned long FrameOffset;
00531 unsigned long* FrameTable;
00532 unsigned long SamplePos;
00533 unsigned long SamplesInLastFrame;
00534 unsigned long WorstCaseFrameSize;
00535 unsigned long SamplesPerFrame;
00536 buffer_t RAMCache;
00537
00538 Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset);
00539 ~Sample();
00548 inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
00549 switch (WordSize) {
00550 case 1: {
00551 uint8_t* pDst = (uint8_t*) pData;
00552 uint8_t cache;
00553 unsigned long lo = 0, hi = AreaSize - 1;
00554 for (; lo < hi; hi--, lo++) {
00555 cache = pDst[lo];
00556 pDst[lo] = pDst[hi];
00557 pDst[hi] = cache;
00558 }
00559 break;
00560 }
00561 case 2: {
00562 uint16_t* pDst = (uint16_t*) pData;
00563 uint16_t cache;
00564 unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
00565 for (; lo < hi; hi--, lo++) {
00566 cache = pDst[lo];
00567 pDst[lo] = pDst[hi];
00568 pDst[hi] = cache;
00569 }
00570 break;
00571 }
00572 case 4: {
00573 uint32_t* pDst = (uint32_t*) pData;
00574 uint32_t cache;
00575 unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
00576 for (; lo < hi; hi--, lo++) {
00577 cache = pDst[lo];
00578 pDst[lo] = pDst[hi];
00579 pDst[hi] = cache;
00580 }
00581 break;
00582 }
00583 default: {
00584 uint8_t* pCache = new uint8_t[WordSize];
00585 unsigned long lo = 0, hi = AreaSize - WordSize;
00586 for (; lo < hi; hi -= WordSize, lo += WordSize) {
00587 memcpy(pCache, (uint8_t*) pData + lo, WordSize);
00588 memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
00589 memcpy((uint8_t*) pData + hi, pCache, WordSize);
00590 }
00591 delete[] pCache;
00592 break;
00593 }
00594 }
00595 }
00596 inline long Min(long A, long B) {
00597 return (A > B) ? B : A;
00598 }
00599 inline long Abs(long val) { return (val > 0) ? val : -val; }
00600
00601
00602 inline unsigned long GuessSize(unsigned long samples) {
00603
00604
00605
00606
00607
00608
00609 const unsigned long size =
00610 BitDepth == 24 ? samples + (samples >> 1) + (samples >> 8) * 13
00611 : samples + (samples >> 10) * 5;
00612
00613
00614 return (Channels == 2 ? size << 1 : size) + WorstCaseFrameSize;
00615 }
00616
00617
00618
00619 inline unsigned long WorstCaseMaxSamples(buffer_t* pDecompressionBuffer) {
00620 return (unsigned long) ((float)pDecompressionBuffer->Size / (float)WorstCaseFrameSize * (float)SamplesPerFrame);
00621 }
00622 private:
00623 void ScanCompressedSample();
00624 friend class File;
00625 friend class Region;
00626 };
00627
00628
00630 class Region : public DLS::Region {
00631 public:
00632 unsigned int Dimensions;
00633 dimension_def_t pDimensionDefinitions[8];
00634 uint32_t DimensionRegions;
00635 DimensionRegion* pDimensionRegions[256];
00636 unsigned int Layers;
00637
00638 DimensionRegion* GetDimensionRegionByValue(const uint DimValues[8]);
00639 DimensionRegion* GetDimensionRegionByBit(const uint8_t DimBits[8]);
00640 Sample* GetSample();
00641 protected:
00642 uint8_t VelocityTable[128];
00643
00644 Region(Instrument* pInstrument, RIFF::List* rgnList);
00645 void LoadDimensionRegions(RIFF::List* rgn);
00646 Sample* GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress = NULL);
00647 ~Region();
00648 friend class Instrument;
00649 };
00650
00652 class Instrument : protected DLS::Instrument {
00653 public:
00654
00655 DLS::Resource::pInfo;
00656 DLS::Resource::pDLSID;
00657
00658 DLS::Instrument::IsDrum;
00659 DLS::Instrument::MIDIBank;
00660 DLS::Instrument::MIDIBankCoarse;
00661 DLS::Instrument::MIDIBankFine;
00662 DLS::Instrument::MIDIProgram;
00663 DLS::Instrument::Regions;
00664
00665 int32_t Attenuation;
00666 uint16_t EffectSend;
00667 int16_t FineTune;
00668 uint16_t PitchbendRange;
00669 bool PianoReleaseMode;
00670 range_t DimensionKeyRange;
00671
00672
00673
00674 DLS::Resource::GetParent;
00675
00676 Region* GetFirstRegion();
00677 Region* GetNextRegion();
00678
00679 Region* GetRegion(unsigned int Key);
00680 protected:
00681 Region** pRegions;
00682 Region* RegionKeyTable[128];
00683 int RegionIndex;
00684
00685 Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress = NULL);
00686 ~Instrument();
00687 friend class File;
00688 };
00689
00690
00692 class File : protected DLS::File {
00693 public:
00694
00695 DLS::Resource::pInfo;
00696 DLS::Resource::pDLSID;
00697
00698 DLS::File::pVersion;
00699 DLS::File::Instruments;
00700
00701
00702 DLS::Resource::GetParent;
00703
00704 File(RIFF::File* pRIFF);
00705 Sample* GetFirstSample(progress_t* pProgress = NULL);
00706 Sample* GetNextSample();
00707 Instrument* GetFirstInstrument();
00708 Instrument* GetNextInstrument();
00709 Instrument* GetInstrument(uint index, progress_t* pProgress = NULL);
00710 ~File();
00711 protected:
00712 typedef std::list<Sample*> SampleList;
00713 typedef std::list<Instrument*> InstrumentList;
00714
00715 SampleList* pSamples;
00716 SampleList::iterator SamplesIterator;
00717 InstrumentList* pInstruments;
00718 InstrumentList::iterator InstrumentsIterator;
00719
00720 void LoadSamples(progress_t* pProgress = NULL);
00721 void LoadInstruments(progress_t* pProgress = NULL);
00722 friend class Region;
00723 };
00724
00726 class Exception : public DLS::Exception {
00727 public:
00728 Exception(String Message);
00729 void PrintMessage();
00730 };
00731
00732 String libraryName();
00733 String libraryVersion();
00734
00735 }
00736
00737 #endif // __GIG_H__