--- libgig/trunk/src/gig.h 2003/10/25 20:15:04 2 +++ libgig/trunk/src/gig.h 2003/12/26 16:15:31 24 @@ -36,7 +36,7 @@ /// reallocated which is time consuming and unefficient. #define INITIAL_SAMPLE_BUFFER_SIZE 512000 // 512 kB -#if 0 //WORDS_BIGENDIAN +#if WORDS_BIGENDIAN # define LIST_TYPE_3PRG 0x33707267 # define LIST_TYPE_3EWL 0x3365776C # define CHUNK_ID_SMPL 0x736D706C @@ -57,12 +57,16 @@ #endif // WORDS_BIGENDIAN /** (so far) every exponential paramater in the gig format has a basis of 1.000000008813822 */ -#define GIG_EXP_DECODE(x) (powl(1.000000008813822, x)) -#define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01)) -#define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03) -#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) +#define GIG_EXP_DECODE(x) (pow(1.000000008813822, x)) +#define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01)) +#define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03) +#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 { @@ -278,6 +282,13 @@ #endif // WORDS_BIGENDIAN }; + /** Reflects the current playback state for a sample. */ + struct playback_state_t { + unsigned long position; ///< Current position within the sample. + bool reverse; ///< If playback direction is currently backwards (in case there is a pingpong or reverse loop defined). + unsigned long loop_cycles_left; ///< How many times the loop has still to be passed, this value will be decremented with each loop cycle. + }; + // just symbol prototyping class File; class Instrument; @@ -362,7 +373,7 @@ // 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; + uint8_t VelocityResponseCurveScaling; ///< 0 - 127 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 @@ -386,7 +397,18 @@ DLS::Sampler::SampleLoops; DLS::Sampler::pSampleLoops; + // Methods + double GetVelocityAttenuation(uint8_t MIDIKeyVelocity); + protected: DimensionRegion(RIFF::List* _3ewl); + ~DimensionRegion(); + friend class Region; + private: + 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. }; /** Encapsulates sample waves used for playback. */ @@ -397,14 +419,15 @@ uint32_t Product; ///< Specifies the MIDI model ID defined by the manufacturer corresponding to the Manufacturer field. If no particular manufacturer's product is to be specified, a value of 0 should be used. uint32_t SamplePeriod; ///< Specifies the duration of time that passes during the playback of one sample in nanoseconds (normally equal to 1 / Samplers Per Second, where Samples Per Second is the value found in the format chunk). uint32_t MIDIUnityNote; ///< Specifies the musical note at which the sample will be played at it's original sample rate. - uint32_t MIDIPitchFraction; ///< Specifies the fraction of a semitone up from the specified MIDI unity note field. A value of 0x80000000 means 1/2 semitone (50 cents) and a value of 0x00000000 means no fine tuning between semitones. + uint32_t FineTune; ///< Specifies the fraction of a semitone up from the specified MIDI unity note field. A value of 0x80000000 means 1/2 semitone (50 cents) and a value of 0x00000000 means no fine tuning between semitones. smpte_format_t SMPTEFormat; ///< Specifies the Society of Motion Pictures and Television E time format used in the following SMPTEOffset field. If a value of 0 is set, SMPTEOffset should also be set to 0. uint32_t SMPTEOffset; ///< The SMPTE Offset value specifies the time offset to be used for the synchronization / calibration to the first sample in the waveform. This value uses a format of 0xhhmmssff where hh is a signed value that specifies the number of hours (-23 to 23), mm is an unsigned value that specifies the number of minutes (0 to 59), ss is an unsigned value that specifies the number of seconds (0 to 59) and ff is an unsigned value that specifies the number of frames (0 to -1). uint32_t Loops; ///< Number of defined sample loops (so far only seen single loops in gig files - please report me if you encounter more!). - uint32_t LoopID; ///< Specifies the unique ID that corresponds to one of the defined cue points in the cue point list (only if Loops > 0). + uint32_t LoopID; ///< Specifies the unique ID that corresponds to one of the defined cue points in the cue point list (only if Loops > 0), as the Gigasampler format only allows one loop definition at the moment, this attribute isn't really useful for anything. loop_type_t LoopType; ///< The type field defines how the waveform samples will be looped (only if Loops > 0). - uint32_t LoopStart; ///< The start value specifies the byte offset into the waveform data of the first sample to be played in the loop (only if Loops > 0). - uint32_t LoopEnd; ///< The end value specifies the byte offset into the waveform data of the last sample to be played in the loop (only if Loops > 0). + uint32_t LoopStart; ///< The start value specifies the offset (in sample points) in the waveform data of the first sample to be played in the loop (only if Loops > 0). + uint32_t LoopEnd; ///< The end value specifies the offset (in sample points) in the waveform data which represents the end of the loop (only if Loops > 0). + uint32_t LoopSize; ///< Length of the looping area (in sample points) which is equivalent to LoopEnd - LoopStart. uint32_t LoopFraction; ///< The fractional value specifies a fraction of a sample at which to loop (only if Loops > 0). This allows a loop to be fine tuned at a resolution greater than one sample. A value of 0 means no fraction, a value of 0x80000000 means 1/2 of a sample length. 0xFFFFFFFF is the smallest fraction of a sample that can be represented. uint32_t LoopPlayCount; ///< Number of times the loop should be played (only if Loops > 0, a value of 0 = infinite). bool Compressed; ///< If the sample wave is compressed (probably just interesting for instrument and sample editors, as this library already handles the decompression in it's sample access methods anyway). @@ -420,6 +443,7 @@ unsigned long SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence = RIFF::stream_start); unsigned long GetPos(); unsigned long Read(void* pBuffer, unsigned long SampleCount); + unsigned long ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState); protected: static unsigned int Instances; ///< Number of instances of class Sample. static unsigned long DecompressionBufferSize; ///< Current size of the decompression buffer. @@ -431,6 +455,66 @@ Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset); ~Sample(); + /** + * Swaps the order of the data words in the given memory area + * with a granularity given by \a WordSize. + * + * @param pData - pointer to the memory area to be swapped + * @param AreaSize - size of the memory area to be swapped (in bytes) + * @param WordSize - size of the data words (in bytes) + */ + inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) { + switch (WordSize) { // TODO: unefficient + case 1: { + uint8_t* pDst = (uint8_t*) pData; + uint8_t cache; + unsigned long lo = 0, hi = AreaSize - 1; + for (; lo < hi; hi--, lo++) { + cache = pDst[lo]; + pDst[lo] = pDst[hi]; + pDst[hi] = cache; + } + break; + } + case 2: { + uint16_t* pDst = (uint16_t*) pData; + uint16_t cache; + unsigned long lo = 0, hi = (AreaSize >> 1) - 1; + for (; lo < hi; hi--, lo++) { + cache = pDst[lo]; + pDst[lo] = pDst[hi]; + pDst[hi] = cache; + } + break; + } + case 4: { + uint32_t* pDst = (uint32_t*) pData; + uint32_t cache; + unsigned long lo = 0, hi = (AreaSize >> 2) - 1; + for (; lo < hi; hi--, lo++) { + cache = pDst[lo]; + pDst[lo] = pDst[hi]; + pDst[hi] = cache; + } + break; + } + default: { + uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient + unsigned long lo = 0, hi = AreaSize - WordSize; + for (; lo < hi; hi -= WordSize, lo += WordSize) { + memcpy(pCache, (uint8_t*) pData + lo, WordSize); + memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize); + memcpy((uint8_t*) pData + hi, pCache, WordSize); + } + delete[] pCache; + break; + } + } + } + inline long Min(long A, long B) { + return (A > B) ? B : A; + } + inline long Abs(long val) { return (val > 0) ? val : -val; } private: void ScanCompressedSample(); friend class File; @@ -517,6 +601,7 @@ Sample* GetNextSample(); ///< Returns a pointer to the next Sample object of the file, NULL otherwise. 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() {}; protected: typedef std::list SampleList;