--- libgig/trunk/src/gig.h 2015/05/02 11:32:27 2762 +++ libgig/trunk/src/gig.h 2017/07/20 22:09:54 3323 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2015 by Christian Schoenebeck * + * Copyright (C) 2003-2017 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -27,6 +27,22 @@ #include "DLS.h" #include +#ifndef __has_feature +# define __has_feature(x) 0 +#endif +#ifndef HAVE_RTTI +# if __GXX_RTTI || __has_feature(cxx_rtti) || _CPPRTTI +# define HAVE_RTTI 1 +# else +# define HAVE_RTTI 0 +# endif +#endif +#if HAVE_RTTI +# include +#else +# warning No RTTI available! +#endif + #if WORDS_BIGENDIAN # define LIST_TYPE_3PRG 0x33707267 # define LIST_TYPE_3EWL 0x3365776C @@ -45,6 +61,7 @@ # define CHUNK_ID_SCRI 0x53637269 // own gig format extension # define CHUNK_ID_LSNM 0x4c534e4d // own gig format extension # define CHUNK_ID_SCSL 0x5343534c // own gig format extension +# define CHUNK_ID_LSDE 0x4c534445 // own gig format extension #else // little endian # define LIST_TYPE_3PRG 0x67727033 # define LIST_TYPE_3EWL 0x6C776533 @@ -63,13 +80,22 @@ # define CHUNK_ID_SCRI 0x69726353 // own gig format extension # define CHUNK_ID_LSNM 0x4d4e534c // own gig format extension # define CHUNK_ID_SCSL 0x4c534353 // own gig format extension +# define CHUNK_ID_LSDE 0x4544534c // own gig format extension #endif // WORDS_BIGENDIAN +#ifndef GIG_DECLARE_ENUM +# define GIG_DECLARE_ENUM(type, ...) enum type { __VA_ARGS__ } +#endif + +// just symbol prototyping (since Serialization.h not included by default here) +namespace Serialization { class Archive; } + /** Gigasampler/GigaStudio specific classes and definitions */ namespace gig { typedef std::string String; typedef RIFF::progress_t progress_t; + typedef RIFF::file_offset_t file_offset_t; /** Lower and upper limit of a range. */ struct range_t { @@ -80,8 +106,8 @@ /** Pointer address and size of a buffer. */ struct buffer_t { 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. :) + file_offset_t Size; ///< Size of the actual data in the buffer in bytes. + file_offset_t 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; @@ -89,66 +115,90 @@ } }; - /** Standard types of sample loops. */ - typedef enum { + /** Standard types of sample loops. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(loop_type_t, loop_type_normal = 0x00000000, ///< Loop forward (normal) loop_type_bidirectional = 0x00000001, ///< Alternating loop (forward/backward, also known as Ping Pong) loop_type_backward = 0x00000002 ///< Loop backward (reverse) - } loop_type_t; + ); - /** Society of Motion Pictures and Television E time format. */ - typedef enum { + /** Society of Motion Pictures and Television E time format. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(smpte_format_t, smpte_format_no_offset = 0x00000000, ///< no SMPTE offset smpte_format_24_frames = 0x00000018, ///< 24 frames per second smpte_format_25_frames = 0x00000019, ///< 25 frames per second smpte_format_30_frames_dropping = 0x0000001D, ///< 30 frames per second with frame dropping (30 drop) smpte_format_30_frames = 0x0000001E ///< 30 frames per second - } smpte_format_t; + ); - /** Defines the shape of a function graph. */ - typedef enum { + /** Defines the shape of a function graph. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(curve_type_t, curve_type_nonlinear = 0, curve_type_linear = 1, curve_type_special = 2, curve_type_unknown = 0xffffffff - } curve_type_t; + ); - /** Dimensions allow to bypass one of the following controllers. */ - typedef enum { + /** Dimensions allow to bypass one of the following controllers. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(dim_bypass_ctrl_t, dim_bypass_ctrl_none, dim_bypass_ctrl_94, ///< Effect 4 Depth (MIDI Controller 94) dim_bypass_ctrl_95 ///< Effect 5 Depth (MIDI Controller 95) - } dim_bypass_ctrl_t; + ); - /** Defines how LFO3 is controlled by. */ - typedef enum { + /** Defines how LFO3 is controlled by. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(lfo3_ctrl_t, lfo3_ctrl_internal = 0x00, ///< Only internally controlled. lfo3_ctrl_modwheel = 0x01, ///< Only controlled by external modulation wheel. lfo3_ctrl_aftertouch = 0x02, ///< Only controlled by aftertouch controller. lfo3_ctrl_internal_modwheel = 0x03, ///< Controlled internally and by external modulation wheel. lfo3_ctrl_internal_aftertouch = 0x04 ///< Controlled internally and by aftertouch controller. - } lfo3_ctrl_t; + ); - /** Defines how LFO2 is controlled by. */ - typedef enum { + /** Defines how LFO2 is controlled by. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(lfo2_ctrl_t, lfo2_ctrl_internal = 0x00, ///< Only internally controlled. lfo2_ctrl_modwheel = 0x01, ///< Only controlled by external modulation wheel. lfo2_ctrl_foot = 0x02, ///< Only controlled by external foot controller. lfo2_ctrl_internal_modwheel = 0x03, ///< Controlled internally and by external modulation wheel. lfo2_ctrl_internal_foot = 0x04 ///< Controlled internally and by external foot controller. - } lfo2_ctrl_t; + ); - /** Defines how LFO1 is controlled by. */ - typedef enum { + /** Defines how LFO1 is controlled by. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(lfo1_ctrl_t, lfo1_ctrl_internal = 0x00, ///< Only internally controlled. lfo1_ctrl_modwheel = 0x01, ///< Only controlled by external modulation wheel. lfo1_ctrl_breath = 0x02, ///< Only controlled by external breath controller. lfo1_ctrl_internal_modwheel = 0x03, ///< Controlled internally and by external modulation wheel. lfo1_ctrl_internal_breath = 0x04 ///< Controlled internally and by external breath controller. - } lfo1_ctrl_t; + ); - /** Defines how the filter cutoff frequency is controlled by. */ - typedef enum { + /** Defines how the filter cutoff frequency is controlled by. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(vcf_cutoff_ctrl_t, vcf_cutoff_ctrl_none = 0x00, vcf_cutoff_ctrl_none2 = 0x01, ///< The difference between none and none2 is unknown vcf_cutoff_ctrl_modwheel = 0x81, ///< Modulation Wheel (MIDI Controller 1) @@ -161,16 +211,19 @@ vcf_cutoff_ctrl_genpurpose7 = 0xd2, ///< General Purpose Controller 7 (Button, MIDI Controller 82) vcf_cutoff_ctrl_genpurpose8 = 0xd3, ///< General Purpose Controller 8 (Button, MIDI Controller 83) vcf_cutoff_ctrl_aftertouch = 0x80 ///< Key Pressure - } vcf_cutoff_ctrl_t; + ); - /** Defines how the filter resonance is controlled by. */ - typedef enum { + /** Defines how the filter resonance is controlled by. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(vcf_res_ctrl_t, vcf_res_ctrl_none = 0xffffffff, vcf_res_ctrl_genpurpose3 = 0, ///< General Purpose Controller 3 (Slider, MIDI Controller 18) vcf_res_ctrl_genpurpose4 = 1, ///< General Purpose Controller 4 (Slider, MIDI Controller 19) vcf_res_ctrl_genpurpose5 = 2, ///< General Purpose Controller 5 (Button, MIDI Controller 80) vcf_res_ctrl_genpurpose6 = 3 ///< General Purpose Controller 6 (Button, MIDI Controller 81) - } vcf_res_ctrl_t; + ); /** * Defines a controller that has a certain contrained influence on a @@ -181,15 +234,21 @@ * attenuation_ctrl_t, eg1_ctrl_t or eg2_ctrl_t) in your code! */ struct leverage_ctrl_t { - typedef enum { + /** Defines possible controllers. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(type_t, 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 + + void serialize(Serialization::Archive* archive); }; /** @@ -219,8 +278,10 @@ * dimension zones is always a power of two. All dimensions can have up * to 32 zones (except the layer dimension with only up to 8 zones and * the samplechannel dimension which currently allows only 2 zones). + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() */ - typedef enum { + GIG_DECLARE_ENUM(dimension_t, dimension_none = 0x00, ///< Dimension not in use. dimension_samplechannel = 0x80, ///< If used sample has more than one channel (thus is not mono). dimension_layer = 0x81, ///< For layering of up to 8 instruments (and eventually crossfading of 2 or 4 layers). @@ -255,16 +316,18 @@ dimension_effect3depth = 0x5d, ///< Effect 3 Depth (MIDI Controller 93) dimension_effect4depth = 0x5e, ///< Effect 4 Depth (MIDI Controller 94) dimension_effect5depth = 0x5f ///< Effect 5 Depth (MIDI Controller 95) - } dimension_t; + ); /** * Intended for internal usage: will be used to convert a dimension value * into the corresponding dimension bit number. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() */ - typedef enum { + GIG_DECLARE_ENUM(split_type_t, split_type_normal, ///< dimension value between 0-127 split_type_bit ///< dimension values are already the sought bit number - } split_type_t; + ); /** General dimension definition. */ struct dimension_def_t { @@ -275,14 +338,17 @@ float zone_size; ///< Intended for internal usage: reflects the size of each zone (128/zones) for normal split types only, 0 otherwise. }; - /** Defines which frequencies are filtered by the VCF. */ - typedef enum { + /** Defines which frequencies are filtered by the VCF. + * + * @see enumCount(), enumKey(), enumKeys(), enumValue() + */ + GIG_DECLARE_ENUM(vcf_type_t, vcf_type_lowpass = 0x00, vcf_type_lowpassturbo = 0xff, ///< More poles than normal lowpass vcf_type_bandpass = 0x01, vcf_type_highpass = 0x02, vcf_type_bandreject = 0x03 - } vcf_type_t; + ); /** * Defines the envelope of a crossfade. @@ -303,13 +369,48 @@ uint8_t out_start; ///< Start position of fade out. uint8_t out_end; ///< End postition of fade out. #endif // WORDS_BIGENDIAN + + void serialize(Serialization::Archive* archive); }; /** Reflects the current playback state for a sample. */ struct playback_state_t { - unsigned long position; ///< Current position within the sample. + file_offset_t 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. + file_offset_t loop_cycles_left; ///< How many times the loop has still to be passed, this value will be decremented with each loop cycle. + }; + + /** + * Defines behavior options for envelope generators. + * + * These options allow to override the precise default behavior of the + * envelope generators' state machines. + * + * @b Note: These EG options are an extension to the original gig file + * format, so these options are not available with the original + * Gigasampler/GigaStudio software! Currently only LinuxSampler and gigedit + * support these EG options! + * + * Adding these options to the original gig file format was necessary, + * because the precise state machine behavior of envelope generators of the + * gig format (and thus the default EG behavior if not explicitly overridden + * here) deviates from common, expected behavior of envelope generators in + * general, if i.e. compared with EGs of hardware synthesizers. For example + * with the gig format, the attack and decay stages will be aborted as soon + * as a note-off is received. Most other EG implementations in the industry + * however always run the attack and decay stages to their full duration, + * even if an early note-off arrives. The latter behavior is intentionally + * implemented in most other products, because it is required to resemble + * percussive sounds in a realistic manner. + */ + struct eg_opt_t { + bool AttackCancel; ///< Whether the "attack" stage is cancelled when receiving a note-off (default: @c true). + bool AttackHoldCancel; ///< Whether the "attack hold" stage is cancelled when receiving a note-off (default: @c true). + bool DecayCancel; ///< Whether the "decay" stage is cancelled when receiving a note-off (default: @c true). + bool ReleaseCancel; ///< Whether the "release" stage is cancelled when receiving a note-on (default: @c true). + + eg_opt_t(); + void serialize(Serialization::Archive* archive); }; // just symbol prototyping @@ -450,6 +551,7 @@ uint16_t SampleStartOffset; ///< Number of samples the sample start should be moved (0 - 2000). double SampleAttenuation; ///< Sample volume (calculated from DLS::Sampler::Gain) uint8_t DimensionUpperLimits[8]; ///< gig3: defines the upper limit of the dimension values for this dimension region. In case you wondered why this is defined on DimensionRegion level and not on Region level: the zone sizes (upper limits) of the velocity dimension can indeed differ in the individual dimension regions, depending on which zones of the other dimension types are currently selected. So this is exceptional for the velocity dimension only. All other dimension types have the same dimension zone sizes for every single DimensionRegion (of the sample Region). + eg_opt_t EGOptions; ///< [gig extension]: Behavior options which should be used for all 3 envelope generators. // derived attributes from DLS::Sampler using DLS::Sampler::UnityNote; @@ -485,7 +587,9 @@ DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src); ~DimensionRegion(); void CopyAssign(const DimensionRegion* orig, const std::map* mSamples); + void serialize(Serialization::Archive* archive); friend class Region; + friend class Serialization::Archive; private: typedef enum { ///< Used to decode attenuation, EG1 and EG2 controller // official leverage controllers as they were defined in the original Gigasampler/GigaStudio format: @@ -588,7 +692,7 @@ } _lev_ctrl_t; typedef std::map VelocityTableMap; - static uint Instances; ///< Number of DimensionRegion instances. + static size_t 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. double* pVelocityReleaseTable; ///< Points to the velocity table corresponding to the release velocity parameters of this DimensionRegion @@ -651,53 +755,56 @@ // own methods buffer_t LoadSampleData(); - buffer_t LoadSampleData(unsigned long SampleCount); + buffer_t LoadSampleData(file_offset_t SampleCount); buffer_t LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount); - buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount); + buffer_t LoadSampleDataWithNullSamplesExtension(file_offset_t SampleCount, uint NullSamplesCount); buffer_t GetCache(); // own static methods - static buffer_t CreateDecompressionBuffer(unsigned long MaxReadSize); + static buffer_t CreateDecompressionBuffer(file_offset_t MaxReadSize); static void DestroyDecompressionBuffer(buffer_t& DecompressionBuffer); // overridden methods void ReleaseSampleData(); - void Resize(int iNewSize); - unsigned long SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence = RIFF::stream_start); - unsigned long GetPos() const; - unsigned long Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer = NULL); - unsigned long ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer = NULL); - unsigned long Write(void* pBuffer, unsigned long SampleCount); + void Resize(file_offset_t NewSize); + file_offset_t SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence = RIFF::stream_start); + file_offset_t GetPos() const; + file_offset_t Read(void* pBuffer, file_offset_t SampleCount, buffer_t* pExternalDecompressionBuffer = NULL); + file_offset_t ReadAndLoop(void* pBuffer, file_offset_t SampleCount, playback_state_t* pPlaybackState, DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer = NULL); + file_offset_t Write(void* pBuffer, file_offset_t SampleCount); Group* GetGroup() const; virtual void UpdateChunks(progress_t* pProgress); void CopyAssignMeta(const Sample* orig); void CopyAssignWave(const Sample* orig); + uint32_t GetWaveDataCRC32Checksum(); + bool VerifyWaveData(uint32_t* pActually = NULL); protected: - static unsigned int Instances; ///< Number of instances of class Sample. + static size_t Instances; ///< Number of instances of class Sample. static buffer_t InternalDecompressionBuffer; ///< Buffer used for decompression as well as for truncation of 24 Bit -> 16 Bit samples. Group* pGroup; ///< pointer to the Group this sample belongs to (always not-NULL) - 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. + file_offset_t FrameOffset; ///< Current offset (sample points) in current sample frame (for decompression only). + file_offset_t* FrameTable; ///< For positioning within compressed samples only: stores the offset values for each frame. + file_offset_t SamplePos; ///< For compressed samples only: stores the current position (in sample points). + file_offset_t SamplesInLastFrame; ///< For compressed samples only: length of the last sample frame. + file_offset_t WorstCaseFrameSize; ///< For compressed samples only: size (in bytes) of the largest possible sample frame. + file_offset_t SamplesPerFrame; ///< For compressed samples only: number of samples in a full sample frame. buffer_t RAMCache; ///< Buffers samples (already uncompressed) in RAM. unsigned long FileNo; ///< File number (> 0 when sample is stored in an extension file, 0 when it's in the gig) RIFF::Chunk* pCk3gix; RIFF::Chunk* pCkSmpl; - uint32_t crc; ///< CRC-32 checksum of the raw sample data + uint32_t crc; ///< Reflects CRC-32 checksum of the raw sample data at the last time when the sample's raw wave form data has been modified consciously by the user by calling Write(). - Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo = 0); + Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset, unsigned long fileNo = 0, int index = -1); ~Sample(); + uint32_t CalculateWaveDataChecksum(); // Guess size (in bytes) of a compressed sample - inline unsigned long GuessSize(unsigned long samples) { + inline file_offset_t GuessSize(file_offset_t 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 = + const file_offset_t size = BitDepth == 24 ? samples + (samples >> 1) + (samples >> 8) * 13 : samples + (samples >> 10) * 5; // Double for stereo and add one worst case sample @@ -707,8 +814,8 @@ // 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); + inline file_offset_t WorstCaseMaxSamples(buffer_t* pDecompressionBuffer) { + return (file_offset_t) ((float)pDecompressionBuffer->Size / (float)WorstCaseFrameSize * (float)SamplesPerFrame); } private: void ScanCompressedSample(); @@ -1010,6 +1117,7 @@ void SetScriptAsText(const String& text); void SetGroup(ScriptGroup* pGroup); ScriptGroup* GetGroup() const; + void CopyAssign(const Script* orig); protected: Script(ScriptGroup* group, RIFF::Chunk* ckScri); virtual ~Script(); @@ -1266,6 +1374,11 @@ virtual void LoadInstruments(progress_t* pProgress); virtual void LoadScriptGroups(); void SetSampleChecksum(Sample* pSample, uint32_t crc); + uint32_t GetSampleChecksum(Sample* pSample); + uint32_t GetSampleChecksumByIndex(int index); + bool VerifySampleChecksumTable(); + bool RebuildSampleChecksumTable(); + int GetWaveTableIndexOf(gig::Sample* pSample); friend class Region; friend class Sample; friend class Instrument; @@ -1288,10 +1401,25 @@ */ class Exception : public DLS::Exception { public: - Exception(String Message); + Exception(String format, ...); + Exception(String format, va_list arg); void PrintMessage(); + protected: + Exception(); }; +#if HAVE_RTTI + size_t enumCount(const std::type_info& type); + const char* enumKey(const std::type_info& type, size_t value); + bool enumKey(const std::type_info& type, String key); + const char** enumKeys(const std::type_info& type); +#endif // HAVE_RTTI + size_t enumCount(String typeName); + const char* enumKey(String typeName, size_t value); + bool enumKey(String typeName, String key); + const char** enumKeys(String typeName); + size_t enumValue(String key); + String libraryName(); String libraryVersion();