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 double GetVelocityRelease(uint8_t MIDIKeyVelocity);
00447
00448 protected:
00449 DimensionRegion(RIFF::List* _3ewl);
00450 ~DimensionRegion();
00451 friend class Region;
00452 private:
00453 typedef enum {
00454 _lev_ctrl_none = 0x00,
00455 _lev_ctrl_modwheel = 0x03,
00456 _lev_ctrl_breath = 0x05,
00457 _lev_ctrl_foot = 0x07,
00458 _lev_ctrl_effect1 = 0x0d,
00459 _lev_ctrl_effect2 = 0x0f,
00460 _lev_ctrl_genpurpose1 = 0x11,
00461 _lev_ctrl_genpurpose2 = 0x13,
00462 _lev_ctrl_genpurpose3 = 0x15,
00463 _lev_ctrl_genpurpose4 = 0x17,
00464 _lev_ctrl_portamentotime = 0x0b,
00465 _lev_ctrl_sustainpedal = 0x01,
00466 _lev_ctrl_portamento = 0x19,
00467 _lev_ctrl_sostenutopedal = 0x1b,
00468 _lev_ctrl_softpedal = 0x09,
00469 _lev_ctrl_genpurpose5 = 0x1d,
00470 _lev_ctrl_genpurpose6 = 0x1f,
00471 _lev_ctrl_genpurpose7 = 0x21,
00472 _lev_ctrl_genpurpose8 = 0x23,
00473 _lev_ctrl_effect1depth = 0x25,
00474 _lev_ctrl_effect2depth = 0x27,
00475 _lev_ctrl_effect3depth = 0x29,
00476 _lev_ctrl_effect4depth = 0x2b,
00477 _lev_ctrl_effect5depth = 0x2d,
00478 _lev_ctrl_channelaftertouch = 0x2f,
00479 _lev_ctrl_velocity = 0xff
00480 } _lev_ctrl_t;
00481 typedef std::map<uint32_t, double*> VelocityTableMap;
00482
00483 static uint Instances;
00484 static VelocityTableMap* pVelocityTables;
00485 double* pVelocityAttenuationTable;
00486 double* pVelocityReleaseTable;
00487
00488 leverage_ctrl_t DecodeLeverageController(_lev_ctrl_t EncodedController);
00489 double* GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling);
00490 double* CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling);
00491 };
00492
00494 class Sample : public DLS::Sample {
00495 public:
00496 uint16_t SampleGroup;
00497 uint32_t Manufacturer;
00498 uint32_t Product;
00499 uint32_t SamplePeriod;
00500 uint32_t MIDIUnityNote;
00501 uint32_t FineTune;
00502 smpte_format_t SMPTEFormat;
00503 uint32_t SMPTEOffset;
00504 uint32_t Loops;
00505 uint32_t LoopID;
00506 loop_type_t LoopType;
00507 uint32_t LoopStart;
00508 uint32_t LoopEnd;
00509 uint32_t LoopSize;
00510 uint32_t LoopFraction;
00511 uint32_t LoopPlayCount;
00512 bool Compressed;
00513 uint32_t TruncatedBits;
00514 bool Dithered;
00515
00516
00517 buffer_t LoadSampleData();
00518 buffer_t LoadSampleData(unsigned long SampleCount);
00519 buffer_t LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount);
00520 buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount);
00521 buffer_t GetCache();
00522
00523 static buffer_t CreateDecompressionBuffer(unsigned long MaxReadSize);
00524 static void DestroyDecompressionBuffer(buffer_t& DecompressionBuffer);
00525
00526 void ReleaseSampleData();
00527 unsigned long SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence = RIFF::stream_start);
00528 unsigned long GetPos();
00529 unsigned long Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer = NULL);
00530 unsigned long ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer = NULL);
00531 protected:
00532 static unsigned int Instances;
00533 static buffer_t InternalDecompressionBuffer;
00534 unsigned long FrameOffset;
00535 unsigned long* FrameTable;
00536 unsigned long SamplePos;
00537 unsigned long SamplesInLastFrame;
00538 unsigned long WorstCaseFrameSize;
00539 unsigned long SamplesPerFrame;
00540 buffer_t RAMCache;
00541
00542 Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset);
00543 ~Sample();
00552 inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
00553 switch (WordSize) {
00554 case 1: {
00555 uint8_t* pDst = (uint8_t*) pData;
00556 uint8_t cache;
00557 unsigned long lo = 0, hi = AreaSize - 1;
00558 for (; lo < hi; hi--, lo++) {
00559 cache = pDst[lo];
00560 pDst[lo] = pDst[hi];
00561 pDst[hi] = cache;
00562 }
00563 break;
00564 }
00565 case 2: {
00566 uint16_t* pDst = (uint16_t*) pData;
00567 uint16_t cache;
00568 unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
00569 for (; lo < hi; hi--, lo++) {
00570 cache = pDst[lo];
00571 pDst[lo] = pDst[hi];
00572 pDst[hi] = cache;
00573 }
00574 break;
00575 }
00576 case 4: {
00577 uint32_t* pDst = (uint32_t*) pData;
00578 uint32_t cache;
00579 unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
00580 for (; lo < hi; hi--, lo++) {
00581 cache = pDst[lo];
00582 pDst[lo] = pDst[hi];
00583 pDst[hi] = cache;
00584 }
00585 break;
00586 }
00587 default: {
00588 uint8_t* pCache = new uint8_t[WordSize];
00589 unsigned long lo = 0, hi = AreaSize - WordSize;
00590 for (; lo < hi; hi -= WordSize, lo += WordSize) {
00591 memcpy(pCache, (uint8_t*) pData + lo, WordSize);
00592 memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
00593 memcpy((uint8_t*) pData + hi, pCache, WordSize);
00594 }
00595 delete[] pCache;
00596 break;
00597 }
00598 }
00599 }
00600 inline long Min(long A, long B) {
00601 return (A > B) ? B : A;
00602 }
00603 inline long Abs(long val) { return (val > 0) ? val : -val; }
00604
00605
00606 inline unsigned long GuessSize(unsigned long samples) {
00607
00608
00609
00610
00611
00612
00613 const unsigned long size =
00614 BitDepth == 24 ? samples + (samples >> 1) + (samples >> 8) * 13
00615 : samples + (samples >> 10) * 5;
00616
00617
00618 return (Channels == 2 ? size << 1 : size) + WorstCaseFrameSize;
00619 }
00620
00621
00622
00623 inline unsigned long WorstCaseMaxSamples(buffer_t* pDecompressionBuffer) {
00624 return (unsigned long) ((float)pDecompressionBuffer->Size / (float)WorstCaseFrameSize * (float)SamplesPerFrame);
00625 }
00626 private:
00627 void ScanCompressedSample();
00628 friend class File;
00629 friend class Region;
00630 };
00631
00632
00634 class Region : public DLS::Region {
00635 public:
00636 unsigned int Dimensions;
00637 dimension_def_t pDimensionDefinitions[8];
00638 uint32_t DimensionRegions;
00639 DimensionRegion* pDimensionRegions[256];
00640 unsigned int Layers;
00641
00642 DimensionRegion* GetDimensionRegionByValue(const uint DimValues[8]);
00643 DimensionRegion* GetDimensionRegionByBit(const uint8_t DimBits[8]);
00644 Sample* GetSample();
00645 protected:
00646 uint8_t VelocityTable[128];
00647
00648 Region(Instrument* pInstrument, RIFF::List* rgnList);
00649 void LoadDimensionRegions(RIFF::List* rgn);
00650 Sample* GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress = NULL);
00651 ~Region();
00652 friend class Instrument;
00653 };
00654
00656 class Instrument : protected DLS::Instrument {
00657 public:
00658
00659 DLS::Resource::pInfo;
00660 DLS::Resource::pDLSID;
00661
00662 DLS::Instrument::IsDrum;
00663 DLS::Instrument::MIDIBank;
00664 DLS::Instrument::MIDIBankCoarse;
00665 DLS::Instrument::MIDIBankFine;
00666 DLS::Instrument::MIDIProgram;
00667 DLS::Instrument::Regions;
00668
00669 int32_t Attenuation;
00670 uint16_t EffectSend;
00671 int16_t FineTune;
00672 uint16_t PitchbendRange;
00673 bool PianoReleaseMode;
00674 range_t DimensionKeyRange;
00675
00676
00677
00678 DLS::Resource::GetParent;
00679
00680 Region* GetFirstRegion();
00681 Region* GetNextRegion();
00682
00683 Region* GetRegion(unsigned int Key);
00684 protected:
00685 Region** pRegions;
00686 Region* RegionKeyTable[128];
00687 int RegionIndex;
00688
00689 Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress = NULL);
00690 ~Instrument();
00691 friend class File;
00692 };
00693
00694
00696 class File : protected DLS::File {
00697 public:
00698
00699 DLS::Resource::pInfo;
00700 DLS::Resource::pDLSID;
00701
00702 DLS::File::pVersion;
00703 DLS::File::Instruments;
00704
00705
00706 DLS::Resource::GetParent;
00707
00708 File(RIFF::File* pRIFF);
00709 Sample* GetFirstSample(progress_t* pProgress = NULL);
00710 Sample* GetNextSample();
00711 Instrument* GetFirstInstrument();
00712 Instrument* GetNextInstrument();
00713 Instrument* GetInstrument(uint index, progress_t* pProgress = NULL);
00714 ~File();
00715 protected:
00716 typedef std::list<Sample*> SampleList;
00717 typedef std::list<Instrument*> InstrumentList;
00718
00719 SampleList* pSamples;
00720 SampleList::iterator SamplesIterator;
00721 InstrumentList* pInstruments;
00722 InstrumentList::iterator InstrumentsIterator;
00723
00724 void LoadSamples(progress_t* pProgress = NULL);
00725 void LoadInstruments(progress_t* pProgress = NULL);
00726 friend class Region;
00727 };
00728
00730 class Exception : public DLS::Exception {
00731 public:
00732 Exception(String Message);
00733 void PrintMessage();
00734 };
00735
00736 String libraryName();
00737 String libraryVersion();
00738
00739 }
00740
00741 #endif // __GIG_H__