--- libgig/trunk/src/gig.h 2003/12/26 16:15:31 24 +++ libgig/trunk/src/gig.h 2005/02/10 19:16:31 365 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003 by Christian Schoenebeck * - * * + * Copyright (C) 2003, 2004 by Christian Schoenebeck * + * * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -63,10 +63,6 @@ #define GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(x) ((x >> 1) & 0x03) #define GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(x) ((x >> 3) & 0x03) #define GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(x) ((x >> 5) & 0x03) -//TODO: the transformation functions are not very accurate compared to the original ones -#define GIG_VELOCITY_TRANSFORM_NONLINEAR(x,dynamic,scale) ((1.0-1.0/pow(x,1.0/(129.0-x))) * (1.0+scale/20.0) + (5.0-dynamic)*pow(x/300.0* (1.0+2.0*scale/128.0),2)) -#define GIG_VELOCITY_TRANSFORM_LINEAR(x,dynamic,scale) ((1.0+scale*3.0/128.0)/110.0*x+(5.0-dynamic)/5.0+(5.0-dynamic)*scale) -#define GIG_VELOCITY_TRANSFORM_SPECIAL(x,dynamic,scale) ((1.0+9.0*scale/129.0)*(1.0-1.0/pow(x,1.0/(129.0-x))+pow(3.0*x/pow(129,2),2)+pow((5.0-dynamic)*x/500.0,2))) /** Gigasampler specific classes and definitions */ namespace gig { @@ -168,35 +164,46 @@ vcf_res_ctrl_genpurpose6 = 3 ///< General Purpose Controller 6 (Button, MIDI Controller 81) } vcf_res_ctrl_t; - /** Defines how attenuation (=gain / VCA) is controlled by. */ - typedef enum { - attenuation_ctrl_none = 0x00, - attenuation_ctrl_modwheel = 0x03, ///< Modulation Wheel (MIDI Controller 1) - attenuation_ctrl_breath = 0x05, ///< Breath Controller (Coarse, MIDI Controller 2) - attenuation_ctrl_foot = 0x07, ///< Foot Pedal (Coarse, MIDI Controller 4) - attenuation_ctrl_effect1 = 0x0d, ///< Effect Controller 1 (Coarse, MIDI Controller 12) - attenuation_ctrl_effect2 = 0x0f, ///< Effect Controller 2 (Coarse, MIDI Controller 13) - attenuation_ctrl_genpurpose1 = 0x11, ///< General Purpose Controller 1 (Slider, MIDI Controller 16) - attenuation_ctrl_genpurpose2 = 0x13, ///< General Purpose Controller 2 (Slider, MIDI Controller 17) - attenuation_ctrl_genpurpose3 = 0x15, ///< General Purpose Controller 3 (Slider, MIDI Controller 18) - attenuation_ctrl_genpurpose4 = 0x17, ///< General Purpose Controller 4 (Slider, MIDI Controller 19) - attenuation_ctrl_portamentotime = 0x0b, ///< Portamento Time (Coarse, MIDI Controller 5) - attenuation_ctrl_sustainpedal = 0x01, ///< Sustain Pedal (MIDI Controller 64) - attenuation_ctrl_portamento = 0x19, ///< Portamento (MIDI Controller 65) - attenuation_ctrl_sostenutopedal = 0x1b, ///< Sostenuto Pedal (MIDI Controller 66) - attenuation_ctrl_softpedal = 0x09, ///< Soft Pedal (MIDI Controller 67) - attenuation_ctrl_genpurpose5 = 0x1d, ///< General Purpose Controller 5 (Button, MIDI Controller 80) - attenuation_ctrl_genpurpose6 = 0x1f, ///< General Purpose Controller 6 (Button, MIDI Controller 81) - attenuation_ctrl_genpurpose7 = 0x21, ///< General Purpose Controller 7 (Button, MIDI Controller 82) - attenuation_ctrl_genpurpose8 = 0x23, ///< General Purpose Controller 8 (Button, MIDI Controller 83) - attenuation_ctrl_effect1depth = 0x25, ///< Effect 1 Depth (MIDI Controller 91) - attenuation_ctrl_effect2depth = 0x27, ///< Effect 2 Depth (MIDI Controller 92) - attenuation_ctrl_effect3depth = 0x29, ///< Effect 3 Depth (MIDI Controller 93) - attenuation_ctrl_effect4depth = 0x2b, ///< Effect 4 Depth (MIDI Controller 94) - attenuation_ctrl_effect5depth = 0x2d, ///< Effect 5 Depth (MIDI Controller 95) - attenuation_ctrl_channelaftertouch = 0x2f, ///< Channel Key Pressure - attenuation_ctrl_velocity = 0xff ///< Key Velocity - } attenuation_ctrl_t, eg1_ctrl_t, eg2_ctrl_t; + /** + * Defines a controller that has a certain contrained influence on a + * particular synthesis parameter (used to define attenuation controller, + * EG1 controller and EG2 controller). + * + * You should use the respective typedef (means either + * attenuation_ctrl_t, eg1_ctrl_t or eg2_ctrl_t) in your code! + */ + struct leverage_ctrl_t { + typedef enum { + type_none = 0x00, ///< No controller defined + type_channelaftertouch = 0x2f, ///< Channel Key Pressure + type_velocity = 0xff, ///< Key Velocity + type_controlchange = 0xfe ///< Ordinary MIDI control change controller, see field 'controller_number' + } type_t; + + type_t type; ///< Controller type + uint controller_number; ///< MIDI controller number if this controller is a control change controller, 0 otherwise + }; + + /** + * Defines controller influencing attenuation. + * + * @see leverage_ctrl_t + */ + typedef leverage_ctrl_t attenuation_ctrl_t; + + /** + * Defines controller influencing envelope generator 1. + * + * @see leverage_ctrl_t + */ + typedef leverage_ctrl_t eg1_ctrl_t; + + /** + * Defines controller influencing envelope generator 2. + * + * @see leverage_ctrl_t + */ + typedef leverage_ctrl_t eg2_ctrl_t; /** * Defines the type of dimension, that is how the dimension zones (and @@ -212,7 +219,7 @@ dimension_velocity = 0x82, ///< Key Velocity (this is the only dimension where the ranges can exactly be defined). dimension_channelaftertouch = 0x83, ///< Channel Key Pressure dimension_releasetrigger = 0x84, ///< Special dimension for triggering samples on releasing a key. - dimension_keyboard = 0x85, ///< Key Position + dimension_keyboard = 0x85, ///< Dimension for keyswitching dimension_modwheel = 0x01, ///< Modulation Wheel (MIDI Controller 1) dimension_breath = 0x02, ///< Breath Controller (Coarse, MIDI Controller 2) dimension_foot = 0x04, ///< Foot Pedal (Coarse, MIDI Controller 4) @@ -267,18 +274,24 @@ vcf_type_bandreject = 0x03 } vcf_type_t; - /** Defines the envelope of a crossfade. */ + /** + * Defines the envelope of a crossfade. + * + * Note: The default value for crossfade points is 0,0,0,0. Layers with + * such a default value should be treated as if they would not have a + * crossfade. + */ struct crossfade_t { #if WORDS_BIGENDIAN - uint8_t in_start; ///< Start position of fade in. - uint8_t in_end; ///< End position of fade in. - uint8_t out_start; ///< Start position of fade out. - uint8_t out_end; ///< End postition of fade out. - #else // little endian uint8_t out_end; ///< End postition of fade out. uint8_t out_start; ///< Start position of fade out. uint8_t in_end; ///< End position of fade in. uint8_t in_start; ///< Start position of fade in. + #else // little endian + uint8_t in_start; ///< Start position of fade in. + uint8_t in_end; ///< End position of fade in. + uint8_t out_start; ///< Start position of fade out. + uint8_t out_end; ///< End postition of fade out. #endif // WORDS_BIGENDIAN }; @@ -293,6 +306,7 @@ class File; class Instrument; class Sample; + class Region; /** Encapsulates articulation information of a dimension region. * @@ -321,9 +335,9 @@ bool EG1Hold; ///< If true, Decay1 stage should be postponed until the sample reached the sample loop start. eg1_ctrl_t EG1Controller; ///< MIDI Controller which has influence on sample amplitude EG parameters (attack, decay, release). bool EG1ControllerInvert; ///< Invert values coming from defined EG1 controller. - uint8_t EG1ControllerAttackInfluence; ///< Amount EG1 Controller has influence on the EG1 Attack time. - uint8_t EG1ControllerDecayInfluence; ///< Amount EG1 Controller has influence on the EG1 Decay time. - uint8_t EG1ControllerReleaseInfluence; ///< Amount EG1 Controller has influence on the EG1 Release time. + uint8_t EG1ControllerAttackInfluence; ///< Amount EG1 Controller has influence on the EG1 Attack time (0 - 3, where 0 means off). + uint8_t EG1ControllerDecayInfluence; ///< Amount EG1 Controller has influence on the EG1 Decay time (0 - 3, where 0 means off). + uint8_t EG1ControllerReleaseInfluence; ///< Amount EG1 Controller has influence on the EG1 Release time (0 - 3, where 0 means off). double LFO1Frequency; ///< Frequency of the sample amplitude LFO (0.10 - 10.00 Hz). uint16_t LFO1InternalDepth; ///< Firm pitch of the sample amplitude LFO (0 - 1200 cents). uint16_t LFO1ControlDepth; ///< Controller depth influencing sample amplitude LFO pitch (0 - 1200 cents). @@ -340,9 +354,9 @@ double EG2Release; ///< Release time of the filter cutoff EG (0.000 - 60.000s). eg2_ctrl_t EG2Controller; ///< MIDI Controller which has influence on filter cutoff EG parameters (attack, decay, release). bool EG2ControllerInvert; ///< Invert values coming from defined EG2 controller. - uint8_t EG2ControllerAttackInfluence; ///< Amount EG2 Controller has influence on the EG2 Attack time. - uint8_t EG2ControllerDecayInfluence; ///< Amount EG2 Controller has influence on the EG2 Decay time. - uint8_t EG2ControllerReleaseInfluence; ///< Amount EG2 Controller has influence on the EG2 Release time. + uint8_t EG2ControllerAttackInfluence; ///< Amount EG2 Controller has influence on the EG2 Attack time (0 - 3, where 0 means off). + uint8_t EG2ControllerDecayInfluence; ///< Amount EG2 Controller has influence on the EG2 Decay time (0 - 3, where 0 means off). + uint8_t EG2ControllerReleaseInfluence; ///< Amount EG2 Controller has influence on the EG2 Release time (0 - 3, where 0 means off). double LFO2Frequency; ///< Frequency of the filter cutoff LFO (0.10 - 10.00 Hz). uint16_t LFO2InternalDepth; ///< Firm pitch of the filter cutoff LFO (0 - 1200 cents). uint16_t LFO2ControlDepth; ///< Controller depth influencing filter cutoff LFO pitch (0 - 1200). @@ -371,9 +385,9 @@ bool VCFKeyboardTracking; ///< If true: VCF cutoff frequence will be dependend to the note key position relative to the defined breakpoint value. uint8_t VCFKeyboardTrackingBreakpoint; ///< See VCFKeyboardTracking (0 - 127). // Key Velocity Transformations - curve_type_t VelocityResponseCurve; ///< Defines a transformation curve to the incoming velocity values affecting amplitude. - uint8_t VelocityResponseDepth; ///< Dynamic range of velocity affecting amplitude (0 - 4). - uint8_t VelocityResponseCurveScaling; ///< 0 - 127 + curve_type_t VelocityResponseCurve; ///< Defines a transformation curve to the incoming velocity values affecting amplitude (usually you don't have to interpret this parameter, use GetVelocityAttenuation() instead). + uint8_t VelocityResponseDepth; ///< Dynamic range of velocity affecting amplitude (0 - 4) (usually you don't have to interpret this parameter, use GetVelocityAttenuation() instead). + uint8_t VelocityResponseCurveScaling; ///< 0 - 127 (usually you don't have to interpret this parameter, use GetVelocityAttenuation() instead) curve_type_t ReleaseVelocityResponseCurve; ///< Defines a transformation curve to the incoming release veloctiy values affecting envelope times. uint8_t ReleaseVelocityResponseDepth; ///< Dynamic range of release velocity affecting envelope time (0 - 4). uint8_t ReleaseTriggerDecay; ///< 0 - 8 @@ -383,9 +397,9 @@ dim_bypass_ctrl_t DimensionBypass; ///< If defined, the MIDI controller can switch on/off the dimension in realtime. int8_t Pan; ///< Panorama / Balance (-64..0..63 <-> left..middle..right) bool SelfMask; ///< If true: high velocity notes will stop low velocity notes at the same note, with that you can save voices that wouldn't be audible anyway. - attenuation_ctrl_t AttenuationControl; ///< MIDI Controller which has influence on the volume level of the sample (or entire sample group). - bool InvertAttenuationControl; ///< Inverts the values coming from the defined Attenuation Controller. - uint8_t AttenuationControlTreshold; ///< 0-127 + attenuation_ctrl_t AttenuationController; ///< MIDI Controller which has influence on the volume level of the sample (or entire sample group). + bool InvertAttenuationController; ///< Inverts the values coming from the defined Attenuation Controller. + uint8_t AttenuationControllerThreshold;///< 0-127 uint8_t ChannelOffset; ///< Audio output where the audio signal of the dimension region should be routed to (0 - 9). bool SustainDefeat; ///< If true: Sustain pedal will not hold a note. bool MSDecode; ///< Gigastudio flag: defines if Mid Side Recordings should be decoded. @@ -404,11 +418,42 @@ ~DimensionRegion(); friend class Region; private: + typedef enum { ///< Used to decode attenuation, EG1 and EG2 controller + _lev_ctrl_none = 0x00, + _lev_ctrl_modwheel = 0x03, ///< Modulation Wheel (MIDI Controller 1) + _lev_ctrl_breath = 0x05, ///< Breath Controller (Coarse, MIDI Controller 2) + _lev_ctrl_foot = 0x07, ///< Foot Pedal (Coarse, MIDI Controller 4) + _lev_ctrl_effect1 = 0x0d, ///< Effect Controller 1 (Coarse, MIDI Controller 12) + _lev_ctrl_effect2 = 0x0f, ///< Effect Controller 2 (Coarse, MIDI Controller 13) + _lev_ctrl_genpurpose1 = 0x11, ///< General Purpose Controller 1 (Slider, MIDI Controller 16) + _lev_ctrl_genpurpose2 = 0x13, ///< General Purpose Controller 2 (Slider, MIDI Controller 17) + _lev_ctrl_genpurpose3 = 0x15, ///< General Purpose Controller 3 (Slider, MIDI Controller 18) + _lev_ctrl_genpurpose4 = 0x17, ///< General Purpose Controller 4 (Slider, MIDI Controller 19) + _lev_ctrl_portamentotime = 0x0b, ///< Portamento Time (Coarse, MIDI Controller 5) + _lev_ctrl_sustainpedal = 0x01, ///< Sustain Pedal (MIDI Controller 64) + _lev_ctrl_portamento = 0x19, ///< Portamento (MIDI Controller 65) + _lev_ctrl_sostenutopedal = 0x1b, ///< Sostenuto Pedal (MIDI Controller 66) + _lev_ctrl_softpedal = 0x09, ///< Soft Pedal (MIDI Controller 67) + _lev_ctrl_genpurpose5 = 0x1d, ///< General Purpose Controller 5 (Button, MIDI Controller 80) + _lev_ctrl_genpurpose6 = 0x1f, ///< General Purpose Controller 6 (Button, MIDI Controller 81) + _lev_ctrl_genpurpose7 = 0x21, ///< General Purpose Controller 7 (Button, MIDI Controller 82) + _lev_ctrl_genpurpose8 = 0x23, ///< General Purpose Controller 8 (Button, MIDI Controller 83) + _lev_ctrl_effect1depth = 0x25, ///< Effect 1 Depth (MIDI Controller 91) + _lev_ctrl_effect2depth = 0x27, ///< Effect 2 Depth (MIDI Controller 92) + _lev_ctrl_effect3depth = 0x29, ///< Effect 3 Depth (MIDI Controller 93) + _lev_ctrl_effect4depth = 0x2b, ///< Effect 4 Depth (MIDI Controller 94) + _lev_ctrl_effect5depth = 0x2d, ///< Effect 5 Depth (MIDI Controller 95) + _lev_ctrl_channelaftertouch = 0x2f, ///< Channel Key Pressure + _lev_ctrl_velocity = 0xff ///< Key Velocity + } _lev_ctrl_t; typedef std::map VelocityTableMap; static uint Instances; ///< Number of DimensionRegion instances. static VelocityTableMap* pVelocityTables; ///< Contains the tables corresponding to the various velocity parameters (VelocityResponseCurve and VelocityResponseDepth). double* pVelocityAttenuationTable; ///< Points to the velocity table corresponding to the velocity parameters of this DimensionRegion. + + leverage_ctrl_t DecodeLeverageController(_lev_ctrl_t EncodedController); + double* CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling); }; /** Encapsulates sample waves used for playback. */ @@ -447,10 +492,13 @@ protected: static unsigned int Instances; ///< Number of instances of class Sample. static unsigned long DecompressionBufferSize; ///< Current size of the decompression buffer. - static void* pDecompressionBuffer; ///< Small buffer used for decompression only. + static unsigned char* pDecompressionBuffer; ///< Small buffer used for decompression only. unsigned long FrameOffset; ///< Current offset (sample points) in current sample frame (for decompression only). unsigned long* FrameTable; ///< For positioning within compressed samples only: stores the offset values for each frame. unsigned long SamplePos; ///< For compressed samples only: stores the current position (in sample points). + unsigned long SamplesInLastFrame; ///< For compressed samples only: length of the last sample frame. + unsigned long WorstCaseFrameSize; ///< For compressed samples only: size (in bytes) of the largest possible sample frame. + unsigned long SamplesPerFrame; ///< For compressed samples only: number of samples in a full sample frame. buffer_t RAMCache; ///< Buffers samples (already uncompressed) in RAM. Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset); @@ -515,6 +563,22 @@ return (A > B) ? B : A; } inline long Abs(long val) { return (val > 0) ? val : -val; } + + // Guess size (in bytes) of a compressed sample + inline unsigned long GuessSize(unsigned long samples) { + // 16 bit: assume all frames are compressed - 1 byte + // per sample and 5 bytes header per 2048 samples + + // 24 bit: assume next best compression rate - 1.5 + // bytes per sample and 13 bytes header per 256 + // samples + const unsigned long size = + BitDepth == 24 ? samples + (samples >> 1) + (samples >> 8) * 13 + : samples + (samples >> 10) * 5; + // Double for stereo and add one worst case sample + // frame + return (Channels == 2 ? size << 1 : size) + WorstCaseFrameSize; + } private: void ScanCompressedSample(); friend class File; @@ -526,12 +590,13 @@ class Region : public DLS::Region { public: unsigned int Dimensions; ///< Number of defined dimensions. - dimension_def_t pDimensionDefinitions[5]; ///< Defines the five possible dimensions (the dimension's controller and number of bits/splits). + dimension_def_t pDimensionDefinitions[8]; ///< Defines the five (gig2) or eight (gig3) possible dimensions (the dimension's controller and number of bits/splits). uint32_t DimensionRegions; ///< Total number of DimensionRegions this Region contains. - DimensionRegion* pDimensionRegions[32]; ///< Pointer array to the 32 possible dimension regions (reflects NULL for dimension regions not in use). Avoid to access the array directly and better use GetDimensionRegionByValue() instead, but of course in some cases it makes sense to use the array (e.g. iterating through all DimensionRegions). + DimensionRegion* pDimensionRegions[256]; ///< Pointer array to the 32 (gig2) or 256 (gig3) possible dimension regions (reflects NULL for dimension regions not in use). Avoid to access the array directly and better use GetDimensionRegionByValue() instead, but of course in some cases it makes sense to use the array (e.g. iterating through all DimensionRegions). + unsigned int Layers; ///< Amount of defined layers (1 - 32). A value of 1 actually means no layering, a value > 1 means there is Layer dimension. The same information can of course also be obtained by accessing pDimensionDefinitions. - DimensionRegion* GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val); - DimensionRegion* GetDimensionRegionByBit(uint8_t Dim4Bit, uint8_t Dim3Bit, uint8_t Dim2Bit, uint8_t Dim1Bit, uint8_t Dim0Bit); + DimensionRegion* GetDimensionRegionByValue(const uint DimValues[8]); + DimensionRegion* GetDimensionRegionByBit(const uint8_t DimBits[8]); Sample* GetSample(); protected: uint8_t VelocityTable[128]; ///< For velocity dimensions with custom defined zone ranges only: used for fast converting from velocity MIDI value to dimension bit number. @@ -602,7 +667,7 @@ Instrument* GetFirstInstrument(); ///< Returns a pointer to the first Instrument object of the file, NULL otherwise. Instrument* GetNextInstrument(); ///< Returns a pointer to the next Instrument object of the file, NULL otherwise. Instrument* GetInstrument(uint index); - ~File() {}; + ~File(); protected: typedef std::list SampleList; typedef std::list InstrumentList;