--- libgig/trunk/src/gig.h 2005/01/23 20:47:18 347 +++ libgig/trunk/src/gig.h 2005/05/07 20:19:10 515 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003, 2004 by Christian Schoenebeck * - * * + * Copyright (C) 2003-2005 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 * @@ -80,6 +80,11 @@ void* pStart; ///< Points to the beginning of the buffer. unsigned long Size; ///< Size of the actual data in the buffer in bytes. unsigned long NullExtensionSize; ///< The buffer might be bigger than the actual data, if that's the case that unused space at the end of the buffer is filled with NULLs and NullExtensionSize reflects that unused buffer space in bytes. Those NULL extensions are mandatory for differential algorithms that have to take the following data words into account, thus have to access past the buffer's boundary. If you don't know what I'm talking about, just forget this variable. :) + buffer_t() { + pStart = NULL; + Size = 0; + NullExtensionSize = 0; + } }; /** Standard types of sample loops. */ @@ -219,7 +224,9 @@ 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_roundrobin = 0x86, ///< Different samples triggered each time a note is played, dimension regions selected in sequence + dimension_random = 0x87, ///< Different samples triggered each time a note is played, random order 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) @@ -279,8 +286,7 @@ * * 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, that is the crossfade volume factor should always - * be 1.0f for such layers. + * crossfade. */ struct crossfade_t { #if WORDS_BIGENDIAN @@ -303,6 +309,21 @@ unsigned long loop_cycles_left; ///< How many times the loop has still to be passed, this value will be decremented with each loop cycle. }; + /** + * @brief Used for indicating the progress of a certain task. + * + * The function pointer argument has to be supplied with a valid + * function of the given signature which will then be called on + * progress changes. The float argument of the callback function will + * then reflect the current progress as a value between 0.0 and 1.0. + */ + struct progress_t { + void (*callback)(float); ///< Callback function pointer which has to be assigned to a function for progress notification. + float __range_min; + float __range_max; + progress_t(); + }; + // just symbol prototyping class File; class Instrument; @@ -405,6 +426,8 @@ bool SustainDefeat; ///< If true: Sustain pedal will not hold a note. bool MSDecode; ///< Gigastudio flag: defines if Mid Side Recordings should be decoded. uint16_t SampleStartOffset; ///< Number of samples the sample start should be moved (0 - 2000). + double SampleAttenuation; ///< Sample volume (calculated from DLS::Sampler::Gain) + // derived attributes from DLS::Sampler DLS::Sampler::UnityNote; DLS::Sampler::FineTune; @@ -477,6 +500,8 @@ 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). + uint32_t TruncatedBits; ///< For 24-bit compressed samples only: number of bits truncated during compression (0, 4 or 6) + bool Dithered; ///< For 24-bit compressed samples only: if dithering was used during compression with bit reduction // own methods buffer_t LoadSampleData(); @@ -484,19 +509,24 @@ buffer_t LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount); buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount); buffer_t GetCache(); + // own static methods + static buffer_t CreateDecompressionBuffer(unsigned long MaxReadSize); + static void DestroyDecompressionBuffer(buffer_t& DecompressionBuffer); // overridden methods void ReleaseSampleData(); 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); + unsigned long Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer = NULL); + unsigned long ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer = NULL); 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 buffer_t InternalDecompressionBuffer; ///< Buffer used for decompression as well as for truncation of 24 Bit -> 16 Bit samples. 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); @@ -561,6 +591,28 @@ 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; + } + + // Worst case amount of sample points that can be read with the + // given decompression buffer. + inline unsigned long WorstCaseMaxSamples(buffer_t* pDecompressionBuffer) { + return (unsigned long) ((float)pDecompressionBuffer->Size / (float)WorstCaseFrameSize * (float)SamplesPerFrame); + } private: void ScanCompressedSample(); friend class File; @@ -585,7 +637,7 @@ Region(Instrument* pInstrument, RIFF::List* rgnList); void LoadDimensionRegions(RIFF::List* rgn); - Sample* GetSampleFromWavePool(unsigned int WavePoolTableIndex); + Sample* GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress = NULL); ~Region(); friend class Instrument; }; @@ -624,7 +676,7 @@ Region* RegionKeyTable[128]; ///< fast lookup for the corresponding Region of a MIDI key int RegionIndex; - Instrument(File* pFile, RIFF::List* insList); + Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress = NULL); ~Instrument(); friend class File; }; @@ -644,12 +696,12 @@ DLS::Resource::GetParent; // overridden methods File(RIFF::File* pRIFF); - Sample* GetFirstSample(); ///< Returns a pointer to the first Sample object of the file, NULL otherwise. + Sample* GetFirstSample(progress_t* pProgress = NULL); ///< Returns a pointer to the first Sample object of the file, NULL otherwise. 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() {}; + Instrument* GetInstrument(uint index, progress_t* pProgress = NULL); + ~File(); protected: typedef std::list SampleList; typedef std::list InstrumentList; @@ -659,8 +711,8 @@ InstrumentList* pInstruments; InstrumentList::iterator InstrumentsIterator; - void LoadSamples(); - void LoadInstruments(); + void LoadSamples(progress_t* pProgress = NULL); + void LoadInstruments(progress_t* pProgress = NULL); friend class Region; };