diff --git a/src/libtinysoundfont/tsf.h b/src/libtinysoundfont/tsf.h index 0dbdb59..2a8147c 100644 --- a/src/libtinysoundfont/tsf.h +++ b/src/libtinysoundfont/tsf.h @@ -1,4 +1,4 @@ -/* TinySoundFont - v0.7 - SoundFont2 synthesizer - https://github.com/schellingb/TinySoundFont +/* TinySoundFont - v0.8 - SoundFont2 synthesizer - https://github.com/schellingb/TinySoundFont no warranty implied; use at your own risk Do this: #define TSF_IMPLEMENTATION @@ -15,14 +15,13 @@ [OPTIONAL] #define TSF_POW, TSF_POWF, TSF_EXPF, TSF_LOG, TSF_TAN, TSF_LOG10, TSF_SQRT to avoid math.h NOT YET IMPLEMENTED - - Lower level voice interface to render single voices/presets - Support for ChorusEffectsSend and ReverbEffectsSend generators - Better low-pass filter without lowering performance too much - Support for modulators LICENSE (MIT) - Copyright (C) 2017 Bernhard Schelling + Copyright (C) 2017, 2018 Bernhard Schelling Based on SFZero, Copyright (C) 2012 Steve Folta (https://github.com/stevefolta/SFZero) Permission is hereby granted, free of charge, to any person obtaining a copy of this @@ -80,13 +79,13 @@ struct tsf_stream // Custom data given to the functions as the first parameter void* data; - // Function pointer will be called to read 'size' bytes into ptr + // Function pointer will be called to read 'size' bytes into ptr (returns number of read bytes) int (*read)(void* data, void* ptr, unsigned int size); // Function pointer will be called to skip ahead over 'count' bytes int (*tell)(void* data); - // Function pointer will be called to skip ahead over 'count' bytes + // Function pointer will be called to skip ahead over 'count' bytes (returns 1 on success, 0 on error) int (*skip)(void* data, unsigned int count); // Function pointer will be called to seek to absolute position @@ -105,11 +104,20 @@ TSFDEF tsf* tsf_load(struct tsf_stream* stream); // Free the memory related to this tsf instance TSFDEF void tsf_close(tsf* f); +// Stop all playing notes immediatly and reset all channel parameters +TSFDEF void tsf_reset(tsf* f); + +// Returns the preset index from a bank and preset number, or -1 if it does not exist in the loaded SoundFont +TSFDEF int tsf_get_presetindex(const tsf* f, int bank, int preset_number); + // Returns the number of presets in the loaded SoundFont -TSFDEF int tsf_get_presetcount(tsf* f); +TSFDEF int tsf_get_presetcount(const tsf* f); // Returns the name of a preset index >= 0 and < tsf_get_presetcount() -TSFDEF const char* tsf_get_presetname(tsf* f, int preset); +TSFDEF const char* tsf_get_presetname(const tsf* f, int preset_index); + +// Returns the name of a preset by bank and preset number +TSFDEF const char* tsf_bank_get_presetname(const tsf* f, int bank, int preset_number); // Supported output modes by the render methods enum TSFOutputMode @@ -131,17 +139,29 @@ enum TSFOutputMode // Setup the parameters for the voice render methods // outputmode: if mono or stereo and how stereo channel data is ordered // samplerate: the number of samples per second (output frequency) -// globalgaindb: volume gain in decibels (>0 means higher, <0 means lower) -TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float globalgaindb CPP_DEFAULT0); +// global_gain_db: volume gain in decibels (>0 means higher, <0 means lower) +TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float global_gain_db CPP_DEFAULT0); // Start playing a note -// preset: preset index >= 0 and < tsf_get_presetcount() +// preset_index: preset index >= 0 and < tsf_get_presetcount() // key: note value between 0 and 127 (60 being middle C) // vel: velocity as a float between 0.0 (equal to note off) and 1.0 (full) -TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel); +// bank: instrument bank number (alternative to preset_index) +// preset_number: preset number (alternative to preset_index) +// (bank_note_on returns 0 if preset does not exist, otherwise 1) +TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel); +TSFDEF int tsf_bank_note_on(tsf* f, int bank, int preset_number, int key, float vel); // Stop playing a note -TSFDEF void tsf_note_off(tsf* f, int preset, int key); +// (bank_note_off returns 0 if preset does not exist, otherwise 1) +TSFDEF void tsf_note_off(tsf* f, int preset_index, int key); +TSFDEF int tsf_bank_note_off(tsf* f, int bank, int preset_number, int key); + +// Stop playing all notes (end with sustain and release) +TSFDEF void tsf_note_off_all(tsf* f); + +// Returns the number of active voices +TSFDEF int tsf_active_voice_count(tsf* f); // Render output samples into a buffer // You can either render as signed 16-bit values (tsf_render_short) or @@ -152,6 +172,50 @@ TSFDEF void tsf_note_off(tsf* f, int preset, int key); TSFDEF void tsf_render_short(tsf* f, short* buffer, int samples, int flag_mixing CPP_DEFAULT0); TSFDEF void tsf_render_float(tsf* f, float* buffer, int samples, int flag_mixing CPP_DEFAULT0); +// Higher level channel based functions, set up channel parameters +// channel: channel number +// preset_index: preset index >= 0 and < tsf_get_presetcount() +// preset_number: preset number (alternative to preset_index) +// flag_mididrums: 0 for normal channels, otherwise apply MIDI drum channel rules +// bank: instrument bank number (alternative to preset_index) +// pan: stereo panning value from 0.0 (left) to 1.0 (right) (default 0.5 center) +// volume: linear volume scale factor (default 1.0 full) +// pitch_wheel: pitch wheel position 0 to 16383 (default 8192 unpitched) +// pitch_range: range of the pitch wheel in semitones (default 2.0, total +/- 2 semitones) +// tuning: tuning of all playing voices in semitones (default 0.0, standard (A440) tuning) +// (set_preset_number and set_bank_preset return 0 if preset does not exist, otherwise 1) +TSFDEF void tsf_channel_set_presetindex(tsf* f, int channel, int preset_index); +TSFDEF int tsf_channel_set_presetnumber(tsf* f, int channel, int preset_number, int flag_mididrums CPP_DEFAULT0); +TSFDEF void tsf_channel_set_bank(tsf* f, int channel, int bank); +TSFDEF int tsf_channel_set_bank_preset(tsf* f, int channel, int bank, int preset_number); +TSFDEF void tsf_channel_set_pan(tsf* f, int channel, float pan); +TSFDEF void tsf_channel_set_volume(tsf* f, int channel, float volume); +TSFDEF void tsf_channel_set_pitchwheel(tsf* f, int channel, int pitch_wheel); +TSFDEF void tsf_channel_set_pitchrange(tsf* f, int channel, float pitch_range); +TSFDEF void tsf_channel_set_tuning(tsf* f, int channel, float tuning); + +// Start or stop playing notes on a channel (needs channel preset to be set) +// channel: channel number +// key: note value between 0 and 127 (60 being middle C) +// vel: velocity as a float between 0.0 (equal to note off) and 1.0 (full) +TSFDEF void tsf_channel_note_on(tsf* f, int channel, int key, float vel); +TSFDEF void tsf_channel_note_off(tsf* f, int channel, int key); +TSFDEF void tsf_channel_note_off_all(tsf* f, int channel); //end with sustain and release +TSFDEF void tsf_channel_sounds_off_all(tsf* f, int channel); //end immediatly + +// Apply a MIDI control change to the channel (not all controllers are supported!) +TSFDEF void tsf_channel_midi_control(tsf* f, int channel, int controller, int control_value); + +// Get current values set on the channels +TSFDEF int tsf_channel_get_preset_index(tsf* f, int channel); +TSFDEF int tsf_channel_get_preset_bank(tsf* f, int channel); +TSFDEF int tsf_channel_get_preset_number(tsf* f, int channel); +TSFDEF float tsf_channel_get_pan(tsf* f, int channel); +TSFDEF float tsf_channel_get_volume(tsf* f, int channel); +TSFDEF int tsf_channel_get_pitchwheel(tsf* f, int channel); +TSFDEF float tsf_channel_get_pitchrange(tsf* f, int channel); +TSFDEF float tsf_channel_get_tuning(tsf* f, int channel); + #ifdef __cplusplus # undef CPP_DEFAULT0 } @@ -187,9 +251,10 @@ TSFDEF void tsf_render_float(tsf* f, float* buffer, int samples, int flag_mixing #if !defined(TSF_POW) || !defined(TSF_POWF) || !defined(TSF_EXPF) || !defined(TSF_LOG) || !defined(TSF_TAN) || !defined(TSF_LOG10) || !defined(TSF_SQRT) # include -# if !defined(__cplusplus) && !defined(NAN) && !defined(powf) && !defined(expf) -# define powf (float)pow // deal with old math.h files that -# define expf (float)exp // come without powf and expf +# if !defined(__cplusplus) && !defined(NAN) && !defined(powf) && !defined(expf) && !defined(sqrtf) +# define powf (float)pow // deal with old math.h +# define expf (float)exp // files that come without +# define sqrtf (float)sqrt // powf, expf and sqrtf # endif # define TSF_POW pow # define TSF_POWF powf @@ -197,7 +262,7 @@ TSFDEF void tsf_render_float(tsf* f, float* buffer, int samples, int flag_mixing # define TSF_LOG log # define TSF_TAN tan # define TSF_LOG10 log10 -# define TSF_SQRT sqrt +# define TSF_SQRTF sqrtf #endif #ifndef TSF_NO_STDIO @@ -231,20 +296,21 @@ typedef char tsf_char20[20]; struct tsf { struct tsf_preset* presets; - int presetNum; + struct tsf_voice* voices; int fontSamplesOffset; int fontSampleCount; - - struct tsf_voice *voices; - int voiceNum; - - float outSampleRate; - enum TSFOutputMode outputmode; - float globalGainDB; - + struct tsf_channels* channels; float* outputSamples; + + int presetNum; + int voiceNum; int outputSampleSize; + unsigned int voicePlayIndex; + + enum TSFOutputMode outputmode; + float outSampleRate; + float globalGainDB; struct tsf_hydra *hydra; @@ -501,8 +567,8 @@ typedef int32_t fixed8p24; struct tsf_riffchunk { tsf_fourcc id; tsf_u32 size; }; -struct tsf_envelope { float delay, start, attack, hold, decay, sustain, release, keynumToHold, keynumToDecay; }; -struct tsf_voice_envelope { float level, slope; int samplesUntilNextSegment; int segment; struct tsf_envelope parameters; TSF_BOOL segmentIsExponential, exponentialDecay; }; +struct tsf_envelope { float delay, attack, hold, decay, sustain, release, keynumToHold, keynumToDecay; }; +struct tsf_voice_envelope { float level, slope; int samplesUntilNextSegment; short segment, midiVelocity; struct tsf_envelope parameters; TSF_BOOL segmentIsExponential, isAmpEnv; }; struct tsf_voice_lowpass { double QInv, a0, a1, b1, b2, z1, z2; TSF_BOOL active; }; struct tsf_voice_lfo { int samplesUntil; float level, delta; }; @@ -513,7 +579,7 @@ struct tsf_region unsigned char lokey, hikey, lovel, hivel; unsigned int group, offset, end, loop_start, loop_end; int transpose, tune, pitch_keycenter, pitch_keytrack; - float volume, pan; + float attenuation, pan; struct tsf_envelope ampenv, modenv; int initialFilterQ, initialFilterFc; int modEnvToPitch, modEnvToFilterFc, modLfoToFilterFc, modLfoToVolume; @@ -533,22 +599,36 @@ struct tsf_preset struct tsf_voice { - int playingPreset, playingKey, curPitchWheel; + int playingPreset, playingKey, playingChannel; struct tsf_region* region; double pitchInputTimecents, pitchOutputFactor; double sourceSamplePosition; fixed32p32 sourceSamplePositionF32P32; float noteGainDB, panFactorLeft, panFactorRight; - unsigned int sampleEnd, loopStart, loopEnd; + unsigned int playIndex, loopStart, loopEnd; struct tsf_voice_envelope ampenv, modenv; struct tsf_voice_lowpass lowpass; struct tsf_voice_lfo modlfo, viblfo; }; +struct tsf_channel +{ + unsigned short presetIndex, bank, pitchWheel, midiPan, midiVolume, midiExpression, midiRPN, midiData; + float panOffset, gainDB, pitchRange, tuning; +}; + +struct tsf_channels +{ + void (*setupVoice)(tsf* f, struct tsf_voice* voice); + struct tsf_channel* channels; + int channelNum, activeChannel; +}; + static double tsf_timecents2Secsd(double timecents) { return TSF_POW(2.0, timecents / 1200.0); } static float tsf_timecents2Secsf(float timecents) { return TSF_POWF(2.0f, timecents / 1200.0f); } static float tsf_cents2Hertz(float cents) { return 8.176f * TSF_POWF(2.0f, cents / 1200.0f); } static float tsf_decibelsToGain(float db) { return (db > -100.f ? TSF_POWF(10.0f, db * 0.05f) : 0); } +static float tsf_gainToDecibels(float gain) { return (gain <= .00001f ? -100.f : (float)(20.0 * TSF_LOG10(gain))); } static TSF_BOOL tsf_riffchunk_read(struct tsf_riffchunk* parent, struct tsf_riffchunk* chunk, struct tsf_stream* stream) { @@ -587,68 +667,172 @@ static void tsf_region_clear(struct tsf_region* i, TSF_BOOL for_relative) i->delayVibLFO = -12000.0f; } -static void tsf_region_operator(struct tsf_region* region, tsf_u16 genOper, union tsf_hydra_genamount* amount) +static void tsf_region_operator(struct tsf_region* region, tsf_u16 genOper, union tsf_hydra_genamount* amount, struct tsf_region* merge_region) { enum { - StartAddrsOffset, EndAddrsOffset, StartloopAddrsOffset, EndloopAddrsOffset, StartAddrsCoarseOffset, ModLfoToPitch, VibLfoToPitch, ModEnvToPitch, - InitialFilterFc, InitialFilterQ, ModLfoToFilterFc, ModEnvToFilterFc, EndAddrsCoarseOffset, ModLfoToVolume, Unused1, ChorusEffectsSend, - ReverbEffectsSend, Pan, Unused2, Unused3, Unused4, DelayModLFO, FreqModLFO, DelayVibLFO, FreqVibLFO, DelayModEnv, AttackModEnv, HoldModEnv, - DecayModEnv, SustainModEnv, ReleaseModEnv, KeynumToModEnvHold, KeynumToModEnvDecay, DelayVolEnv, AttackVolEnv, HoldVolEnv, DecayVolEnv, - SustainVolEnv, ReleaseVolEnv, KeynumToVolEnvHold, KeynumToVolEnvDecay, Instrument, Reserved1, KeyRange, VelRange, StartloopAddrsCoarseOffset, - Keynum, Velocity, InitialAttenuation, Reserved2, EndloopAddrsCoarseOffset, CoarseTune, FineTune, SampleID, SampleModes, Reserved3, ScaleTuning, - ExclusiveClass, OverridingRootKey, Unused5, EndOper + _GEN_TYPE_MASK = 0x0F, + GEN_FLOAT = 0x01, + GEN_INT = 0x02, + GEN_UINT_ADD = 0x03, + GEN_UINT_ADD15 = 0x04, + GEN_KEYRANGE = 0x05, + GEN_VELRANGE = 0x06, + GEN_LOOPMODE = 0x07, + GEN_GROUP = 0x08, + GEN_KEYCENTER = 0x09, + + _GEN_LIMIT_MASK = 0xF0, + GEN_INT_LIMIT12K = 0x10, //min -12000, max 12000 + GEN_INT_LIMITFC = 0x20, //min 1500, max 13500 + GEN_INT_LIMITQ = 0x30, //min 0, max 960 + GEN_INT_LIMIT960 = 0x40, //min -960, max 960 + GEN_INT_LIMIT16K4500 = 0x50, //min -16000, max 4500 + GEN_FLOAT_LIMIT12K5K = 0x60, //min -12000, max 5000 + GEN_FLOAT_LIMIT12K8K = 0x70, //min -12000, max 8000 + GEN_FLOAT_LIMIT1200 = 0x80, //min -1200, max 1200 + GEN_FLOAT_LIMITPAN = 0x90, //* .001f, min -.5f, max .5f, + GEN_FLOAT_LIMITATTN = 0xA0, //* .1f, min 0, max 144.0 + GEN_FLOAT_MAX1000 = 0xB0, //min 0, max 1000 + GEN_FLOAT_MAX1440 = 0xC0, //min 0, max 1440 + + _GEN_MAX = 59, }; - switch (genOper) + #define _TSFREGIONOFFSET(TYPE, FIELD) (unsigned char)(((TYPE*)&((struct tsf_region*)0)->FIELD) - (TYPE*)0) + #define _TSFREGIONENVOFFSET(TYPE, ENV, FIELD) (unsigned char)(((TYPE*)&((&(((struct tsf_region*)0)->ENV))->FIELD)) - (TYPE*)0) + static const struct { unsigned char mode, offset; } genMetas[_GEN_MAX] = { - case StartAddrsOffset: region->offset += amount->shortAmount; break; - case EndAddrsOffset: region->end += amount->shortAmount; break; - case StartloopAddrsOffset: region->loop_start += amount->shortAmount; break; - case EndloopAddrsOffset: region->loop_end += amount->shortAmount; break; - case StartAddrsCoarseOffset: region->offset += amount->shortAmount * 32768; break; - case ModLfoToPitch: region->modLfoToPitch = amount->shortAmount; break; - case VibLfoToPitch: region->vibLfoToPitch = amount->shortAmount; break; - case ModEnvToPitch: region->modEnvToPitch = amount->shortAmount; break; - case InitialFilterFc: region->initialFilterFc = amount->shortAmount; break; - case InitialFilterQ: region->initialFilterQ = amount->shortAmount; break; - case ModLfoToFilterFc: region->modLfoToFilterFc = amount->shortAmount; break; - case ModEnvToFilterFc: region->modEnvToFilterFc = amount->shortAmount; break; - case EndAddrsCoarseOffset: region->end += amount->shortAmount * 32768; break; - case ModLfoToVolume: region->modLfoToVolume = amount->shortAmount; break; - case Pan: region->pan = amount->shortAmount * (2.0f / 10.0f); break; - case DelayModLFO: region->delayModLFO = amount->shortAmount; break; - case FreqModLFO: region->freqModLFO = amount->shortAmount; break; - case DelayVibLFO: region->delayVibLFO = amount->shortAmount; break; - case FreqVibLFO: region->freqVibLFO = amount->shortAmount; break; - case DelayModEnv: region->modenv.delay = amount->shortAmount; break; - case AttackModEnv: region->modenv.attack = amount->shortAmount; break; - case HoldModEnv: region->modenv.hold = amount->shortAmount; break; - case DecayModEnv: region->modenv.decay = amount->shortAmount; break; - case SustainModEnv: region->modenv.sustain = amount->shortAmount; break; - case ReleaseModEnv: region->modenv.release = amount->shortAmount; break; - case KeynumToModEnvHold: region->modenv.keynumToHold = amount->shortAmount; break; - case KeynumToModEnvDecay: region->modenv.keynumToDecay = amount->shortAmount; break; - case DelayVolEnv: region->ampenv.delay = amount->shortAmount; break; - case AttackVolEnv: region->ampenv.attack = amount->shortAmount; break; - case HoldVolEnv: region->ampenv.hold = amount->shortAmount; break; - case DecayVolEnv: region->ampenv.decay = amount->shortAmount; break; - case SustainVolEnv: region->ampenv.sustain = amount->shortAmount; break; - case ReleaseVolEnv: region->ampenv.release = amount->shortAmount; break; - case KeynumToVolEnvHold: region->ampenv.keynumToHold = amount->shortAmount; break; - case KeynumToVolEnvDecay: region->ampenv.keynumToDecay = amount->shortAmount; break; - case KeyRange: region->lokey = amount->range.lo; region->hikey = amount->range.hi; break; - case VelRange: region->lovel = amount->range.lo; region->hivel = amount->range.hi; break; - case StartloopAddrsCoarseOffset: region->loop_start += amount->shortAmount * 32768; break; - case InitialAttenuation: region->volume += -amount->shortAmount / 100.0f; break; - case EndloopAddrsCoarseOffset: region->loop_end += amount->shortAmount * 32768; break; - case CoarseTune: region->transpose += amount->shortAmount; break; - case FineTune: region->tune += amount->shortAmount; break; - case SampleModes: region->loop_mode = ((amount->wordAmount&3) == 3 ? TSF_LOOPMODE_SUSTAIN : ((amount->wordAmount&3) == 1 ? TSF_LOOPMODE_CONTINUOUS : TSF_LOOPMODE_NONE)); break; - case ScaleTuning: region->pitch_keytrack = amount->shortAmount; break; - case ExclusiveClass: region->group = amount->wordAmount; break; - case OverridingRootKey: region->pitch_keycenter = amount->shortAmount; break; - //case gen_endOper: break; // Ignore. - //default: addUnsupportedOpcode(generator_name); + { GEN_UINT_ADD , _TSFREGIONOFFSET(unsigned int, offset ) }, // 0 StartAddrsOffset + { GEN_UINT_ADD , _TSFREGIONOFFSET(unsigned int, end ) }, // 1 EndAddrsOffset + { GEN_UINT_ADD , _TSFREGIONOFFSET(unsigned int, loop_start ) }, // 2 StartloopAddrsOffset + { GEN_UINT_ADD , _TSFREGIONOFFSET(unsigned int, loop_end ) }, // 3 EndloopAddrsOffset + { GEN_UINT_ADD15 , _TSFREGIONOFFSET(unsigned int, offset ) }, // 4 StartAddrsCoarseOffset + { GEN_INT | GEN_INT_LIMIT12K , _TSFREGIONOFFSET( int, modLfoToPitch ) }, // 5 ModLfoToPitch + { GEN_INT | GEN_INT_LIMIT12K , _TSFREGIONOFFSET( int, vibLfoToPitch ) }, // 6 VibLfoToPitch + { GEN_INT | GEN_INT_LIMIT12K , _TSFREGIONOFFSET( int, modEnvToPitch ) }, // 7 ModEnvToPitch + { GEN_INT | GEN_INT_LIMITFC , _TSFREGIONOFFSET( int, initialFilterFc ) }, // 8 InitialFilterFc + { GEN_INT | GEN_INT_LIMITQ , _TSFREGIONOFFSET( int, initialFilterQ ) }, // 9 InitialFilterQ + { GEN_INT | GEN_INT_LIMIT12K , _TSFREGIONOFFSET( int, modLfoToFilterFc ) }, //10 ModLfoToFilterFc + { GEN_INT | GEN_INT_LIMIT12K , _TSFREGIONOFFSET( int, modEnvToFilterFc ) }, //11 ModEnvToFilterFc + { GEN_UINT_ADD15 , _TSFREGIONOFFSET(unsigned int, end ) }, //12 EndAddrsCoarseOffset + { GEN_INT | GEN_INT_LIMIT960 , _TSFREGIONOFFSET( int, modLfoToVolume ) }, //13 ModLfoToVolume + { 0 , (0 ) }, // Unused + { 0 , (0 ) }, //15 ChorusEffectsSend (unsupported) + { 0 , (0 ) }, //16 ReverbEffectsSend (unsupported) + { GEN_FLOAT | GEN_FLOAT_LIMITPAN , _TSFREGIONOFFSET( float, pan ) }, //17 Pan + { 0 , (0 ) }, // Unused + { 0 , (0 ) }, // Unused + { 0 , (0 ) }, // Unused + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONOFFSET( float, delayModLFO ) }, //21 DelayModLFO + { GEN_INT | GEN_INT_LIMIT16K4500 , _TSFREGIONOFFSET( int, freqModLFO ) }, //22 FreqModLFO + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONOFFSET( float, delayVibLFO ) }, //23 DelayVibLFO + { GEN_INT | GEN_INT_LIMIT16K4500 , _TSFREGIONOFFSET( int, freqVibLFO ) }, //24 FreqVibLFO + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONENVOFFSET( float, modenv, delay ) }, //25 DelayModEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, modenv, attack ) }, //26 AttackModEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONENVOFFSET( float, modenv, hold ) }, //27 HoldModEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, modenv, decay ) }, //28 DecayModEnv + { GEN_FLOAT | GEN_FLOAT_MAX1000 , _TSFREGIONENVOFFSET( float, modenv, sustain ) }, //29 SustainModEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, modenv, release ) }, //30 ReleaseModEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT1200 , _TSFREGIONENVOFFSET( float, modenv, keynumToHold ) }, //31 KeynumToModEnvHold + { GEN_FLOAT | GEN_FLOAT_LIMIT1200 , _TSFREGIONENVOFFSET( float, modenv, keynumToDecay) }, //32 KeynumToModEnvDecay + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONENVOFFSET( float, ampenv, delay ) }, //33 DelayVolEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, ampenv, attack ) }, //34 AttackVolEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K5K , _TSFREGIONENVOFFSET( float, ampenv, hold ) }, //35 HoldVolEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, ampenv, decay ) }, //36 DecayVolEnv + { GEN_FLOAT | GEN_FLOAT_MAX1440 , _TSFREGIONENVOFFSET( float, ampenv, sustain ) }, //37 SustainVolEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT12K8K , _TSFREGIONENVOFFSET( float, ampenv, release ) }, //38 ReleaseVolEnv + { GEN_FLOAT | GEN_FLOAT_LIMIT1200 , _TSFREGIONENVOFFSET( float, ampenv, keynumToHold ) }, //39 KeynumToVolEnvHold + { GEN_FLOAT | GEN_FLOAT_LIMIT1200 , _TSFREGIONENVOFFSET( float, ampenv, keynumToDecay) }, //40 KeynumToVolEnvDecay + { 0 , (0 ) }, // Instrument (special) + { 0 , (0 ) }, // Reserved + { GEN_KEYRANGE , (0 ) }, //43 KeyRange + { GEN_VELRANGE , (0 ) }, //44 VelRange + { GEN_UINT_ADD15 , _TSFREGIONOFFSET(unsigned int, loop_start ) }, //45 StartloopAddrsCoarseOffset + { 0 , (0 ) }, //46 Keynum (special) + { 0 , (0 ) }, //47 Velocity (special) + { GEN_FLOAT | GEN_FLOAT_LIMITATTN , _TSFREGIONOFFSET( float, attenuation ) }, //48 InitialAttenuation + { 0 , (0 ) }, // Reserved + { GEN_UINT_ADD15 , _TSFREGIONOFFSET(unsigned int, loop_end ) }, //50 EndloopAddrsCoarseOffset + { GEN_INT , _TSFREGIONOFFSET( int, transpose ) }, //51 CoarseTune + { GEN_INT , _TSFREGIONOFFSET( int, tune ) }, //52 FineTune + { 0 , (0 ) }, // SampleID (special) + { GEN_LOOPMODE , _TSFREGIONOFFSET( int, loop_mode ) }, //54 SampleModes + { 0 , (0 ) }, // Reserved + { GEN_INT , _TSFREGIONOFFSET( int, pitch_keytrack ) }, //56 ScaleTuning + { GEN_GROUP , _TSFREGIONOFFSET(unsigned int, group ) }, //57 ExclusiveClass + { GEN_KEYCENTER , _TSFREGIONOFFSET( int, pitch_keycenter ) }, //58 OverridingRootKey + }; + #undef _TSFREGIONOFFSET + #undef _TSFREGIONENVOFFSET + if (amount) + { + int offset; + if (genOper >= _GEN_MAX) return; + offset = genMetas[genOper].offset; + switch (genMetas[genOper].mode & _GEN_TYPE_MASK) + { + case GEN_FLOAT: (( float*)region)[offset] = amount->shortAmount; return; + case GEN_INT: (( int*)region)[offset] = amount->shortAmount; return; + case GEN_UINT_ADD: ((unsigned int*)region)[offset] += amount->shortAmount; return; + case GEN_UINT_ADD15: ((unsigned int*)region)[offset] += amount->shortAmount<<15; return; + case GEN_KEYRANGE: region->lokey = amount->range.lo; region->hikey = amount->range.hi; return; + case GEN_VELRANGE: region->lovel = amount->range.lo; region->hivel = amount->range.hi; return; + case GEN_LOOPMODE: region->loop_mode = ((amount->wordAmount&3) == 3 ? TSF_LOOPMODE_SUSTAIN : ((amount->wordAmount&3) == 1 ? TSF_LOOPMODE_CONTINUOUS : TSF_LOOPMODE_NONE)); return; + case GEN_GROUP: region->group = amount->wordAmount; return; + case GEN_KEYCENTER: region->pitch_keycenter = amount->shortAmount; return; + } + } + else //merge regions and clamp values + { + for (genOper = 0; genOper != _GEN_MAX; genOper++) + { + int offset = genMetas[genOper].offset; + switch (genMetas[genOper].mode & _GEN_TYPE_MASK) + { + case GEN_FLOAT: + { + float *val = &((float*)region)[offset], vfactor, vmin, vmax; + *val += ((float*)merge_region)[offset]; + switch (genMetas[genOper].mode & _GEN_LIMIT_MASK) + { + case GEN_FLOAT_LIMIT12K5K: vfactor = 1.0f; vmin = -12000.0f; vmax = 5000.0f; break; + case GEN_FLOAT_LIMIT12K8K: vfactor = 1.0f; vmin = -12000.0f; vmax = 8000.0f; break; + case GEN_FLOAT_LIMIT1200: vfactor = 1.0f; vmin = -1200.0f; vmax = 1200.0f; break; + case GEN_FLOAT_LIMITPAN: vfactor = 0.001f; vmin = -0.5f; vmax = 0.5f; break; + case GEN_FLOAT_LIMITATTN: vfactor = 0.1f; vmin = 0.0f; vmax = 144.0f; break; + case GEN_FLOAT_MAX1000: vfactor = 1.0f; vmin = 0.0f; vmax = 1000.0f; break; + case GEN_FLOAT_MAX1440: vfactor = 1.0f; vmin = 0.0f; vmax = 1440.0f; break; + default: continue; + } + *val *= vfactor; + if (*val < vmin) *val = vmin; + else if (*val > vmax) *val = vmax; + continue; + } + case GEN_INT: + { + int *val = &((int*)region)[offset], vmin, vmax; + *val += ((int*)merge_region)[offset]; + switch (genMetas[genOper].mode & _GEN_LIMIT_MASK) + { + case GEN_INT_LIMIT12K: vmin = -12000; vmax = 12000; break; + case GEN_INT_LIMITFC: vmin = 1500; vmax = 13500; break; + case GEN_INT_LIMITQ: vmin = 0; vmax = 960; break; + case GEN_INT_LIMIT960: vmin = -960; vmax = 960; break; + case GEN_INT_LIMIT16K4500: vmin = -16000; vmax = 4500; break; + default: continue; + } + if (*val < vmin) *val = vmin; + else if (*val > vmax) *val = vmax; + continue; + } + case GEN_UINT_ADD: + { + ((unsigned int*)region)[offset] += ((unsigned int*)merge_region)[offset]; + continue; + } + } + } } } @@ -667,13 +851,13 @@ static void tsf_region_envtosecs(struct tsf_envelope* p, TSF_BOOL sustainIsGain) if (!p->keynumToDecay) p->decay = (p->decay < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->decay)); if (p->sustain < 0.0f) p->sustain = 0.0f; - else if (sustainIsGain) p->sustain = 100.0f * tsf_decibelsToGain(-p->sustain / 10.0f); - else p->sustain = p->sustain / 10.0f; + else if (sustainIsGain) p->sustain = tsf_decibelsToGain(-p->sustain / 10.0f); + else p->sustain = 1.0f - (p->sustain / 1000.0f); } -static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) +static void tsf_load_preset(const tsf* res, struct tsf_hydra *hydra, int presetToLoad) { - enum { GenInstrument = 41, GenSampleID = 53 }; + enum { GenInstrument = 41, GenKeyRange = 43, GenVelRange = 44, GenSampleID = 53 }; // Read each preset. struct tsf_hydra_phdr phdr; int phdrIdx, phdrMaxIdx; @@ -683,6 +867,7 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) struct tsf_hydra_phdr otherPhdr; int otherPhdrIdx; struct tsf_preset* preset; + struct tsf_region globalRegion; for (otherPhdrIdx = 0, get_phdr(hydra, otherPhdrIdx, &otherPhdr); otherPhdrIdx != phdrMaxIdx; otherPhdrIdx++, get_phdr(hydra, otherPhdrIdx, &otherPhdr)) { if (otherPhdrIdx == phdrIdx || otherPhdr.bank > phdr.bank) continue; @@ -708,11 +893,14 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) for (pbagIdx = phdr.presetBagNdx, get_pbag(hydra, pbagIdx, &pbag), pbagEndIdx = phdrNext.presetBagNdx; pbagIdx != pbagEndIdx; pbagIdx++, get_pbag(hydra, pbagIdx, &pbag)) { struct tsf_hydra_pbag pbagNext; + unsigned char plokey = 0, phikey = 127, plovel = 0, phivel = 127; get_pbag(hydra, pbagIdx + 1, &pbagNext); struct tsf_hydra_pgen pgen; int pgenIdx, pgenEndIdx; for (pgenIdx = pbag.genNdx, get_pgen(hydra, pgenIdx, &pgen), pgenEndIdx = pbagNext.genNdx; pgenIdx != pgenEndIdx; pgenIdx++, get_pgen(hydra, pgenIdx, &pgen)) { + if (pgen.genOper == GenKeyRange) { plokey = pgen.genAmount.range.lo; phikey = pgen.genAmount.range.hi; continue; } + if (pgen.genOper == GenVelRange) { plovel = pgen.genAmount.range.lo; phivel = pgen.genAmount.range.hi; continue; } if (pgen.genOper != GenInstrument) continue; if (pgen.genAmount.wordAmount >= hydra->instNum) continue; struct tsf_hydra_inst inst, instNext; @@ -726,21 +914,25 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) get_ibag(hydra, ibagIdx + 1, &ibagNext); struct tsf_hydra_igen igen; int igenIdx, igenEndIdx; + unsigned char ilokey = 0, ihikey = 127, ilovel = 0, ihivel = 127; for (igenIdx = ibag.instGenNdx, get_igen(hydra, igenIdx, &igen), igenEndIdx = ibagNext.instGenNdx; igenIdx != igenEndIdx; igenIdx++, get_igen(hydra, igenIdx, &igen)) - if (igen.genOper == GenSampleID) - preset->regionNum++; + { + if (igen.genOper == GenKeyRange) { ilokey = igen.genAmount.range.lo; ihikey = igen.genAmount.range.hi; continue; } + if (igen.genOper == GenVelRange) { ilovel = igen.genAmount.range.lo; ihivel = igen.genAmount.range.hi; continue; } + if (igen.genOper == GenSampleID && ihikey >= plokey && ilokey <= phikey && ihivel >= plovel && ilovel <= phivel) preset->regionNum++; + } } } } preset->regions = (struct tsf_region*)TSF_MALLOC(preset->regionNum * sizeof(struct tsf_region)); + tsf_region_clear(&globalRegion, TSF_TRUE); // Zones. - //*** TODO: Handle global zone (modulators only). for (pbagIdx = phdr.presetBagNdx, get_pbag(hydra, pbagIdx, &pbag), pbagEndIdx = phdrNext.presetBagNdx; pbagIdx != pbagEndIdx; pbagIdx++, get_pbag(hydra, pbagIdx, &pbag)) { - struct tsf_region presetRegion; - tsf_region_clear(&presetRegion, TSF_TRUE); + struct tsf_region presetRegion = globalRegion; + int hadGenInstrument = 0; struct tsf_hydra_pbag pbagNext; get_pbag(hydra, pbagIdx + 1, &pbagNext); struct tsf_hydra_pgen pgen; @@ -757,15 +949,6 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) if (whichInst >= hydra->instNum) continue; tsf_region_clear(&instRegion, TSF_FALSE); - // Preset generators are supposed to be "relative" modifications of - // the instrument settings, but that makes no sense for ranges. - // For those, we'll have the instrument's generator take - // precedence, though that may not be correct. - instRegion.lokey = presetRegion.lokey; - instRegion.hikey = presetRegion.hikey; - instRegion.lovel = presetRegion.lovel; - instRegion.hivel = presetRegion.hivel; - struct tsf_hydra_inst inst, instNext; get_inst(hydra, whichInst, &inst); get_inst(hydra, whichInst + 1, &instNext); @@ -787,40 +970,16 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) struct tsf_hydra_shdr shdr; get_shdr(hydra, igen.genAmount.wordAmount, &shdr); + //preset region key and vel ranges are a filter for the zone regions + if (zoneRegion.hikey < presetRegion.lokey || zoneRegion.lokey > presetRegion.hikey) continue; + if (zoneRegion.hivel < presetRegion.lovel || zoneRegion.lovel > presetRegion.hivel) continue; + if (presetRegion.lokey > zoneRegion.lokey) zoneRegion.lokey = presetRegion.lokey; + if (presetRegion.hikey < zoneRegion.hikey) zoneRegion.hikey = presetRegion.hikey; + if (presetRegion.lovel > zoneRegion.lovel) zoneRegion.lovel = presetRegion.lovel; + if (presetRegion.hivel < zoneRegion.hivel) zoneRegion.hivel = presetRegion.hivel; + //sum regions - zoneRegion.offset += presetRegion.offset; - zoneRegion.end += presetRegion.end; - zoneRegion.loop_start += presetRegion.loop_start; - zoneRegion.loop_end += presetRegion.loop_end; - zoneRegion.transpose += presetRegion.transpose; - zoneRegion.tune += presetRegion.tune; - zoneRegion.pitch_keytrack += presetRegion.pitch_keytrack; - zoneRegion.volume += presetRegion.volume; - zoneRegion.pan += presetRegion.pan; - zoneRegion.ampenv.delay += presetRegion.ampenv.delay; - zoneRegion.ampenv.attack += presetRegion.ampenv.attack; - zoneRegion.ampenv.hold += presetRegion.ampenv.hold; - zoneRegion.ampenv.decay += presetRegion.ampenv.decay; - zoneRegion.ampenv.sustain += presetRegion.ampenv.sustain; - zoneRegion.ampenv.release += presetRegion.ampenv.release; - zoneRegion.modenv.delay += presetRegion.modenv.delay; - zoneRegion.modenv.attack += presetRegion.modenv.attack; - zoneRegion.modenv.hold += presetRegion.modenv.hold; - zoneRegion.modenv.decay += presetRegion.modenv.decay; - zoneRegion.modenv.sustain += presetRegion.modenv.sustain; - zoneRegion.modenv.release += presetRegion.modenv.release; - zoneRegion.initialFilterQ += presetRegion.initialFilterQ; - zoneRegion.initialFilterFc += presetRegion.initialFilterFc; - zoneRegion.modEnvToPitch += presetRegion.modEnvToPitch; - zoneRegion.modEnvToFilterFc += presetRegion.modEnvToFilterFc; - zoneRegion.delayModLFO += presetRegion.delayModLFO; - zoneRegion.freqModLFO += presetRegion.freqModLFO; - zoneRegion.modLfoToPitch += presetRegion.modLfoToPitch; - zoneRegion.modLfoToFilterFc += presetRegion.modLfoToFilterFc; - zoneRegion.modLfoToVolume += presetRegion.modLfoToVolume; - zoneRegion.delayVibLFO += presetRegion.delayVibLFO; - zoneRegion.freqVibLFO += presetRegion.freqVibLFO; - zoneRegion.vibLfoToPitch += presetRegion.vibLfoToPitch; + tsf_region_operator(&zoneRegion, 0, TSF_NULL, &presetRegion); // EG times need to be converted from timecents to seconds. tsf_region_envtosecs(&zoneRegion.ampenv, TSF_TRUE); @@ -830,11 +989,9 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) zoneRegion.delayModLFO = (zoneRegion.delayModLFO < -11950.0f ? 0.0f : tsf_timecents2Secsf(zoneRegion.delayModLFO)); zoneRegion.delayVibLFO = (zoneRegion.delayVibLFO < -11950.0f ? 0.0f : tsf_timecents2Secsf(zoneRegion.delayVibLFO)); - // Pin values to their ranges. - if (zoneRegion.pan < -100.0f) zoneRegion.pan = -100.0f; - else if (zoneRegion.pan > 100.0f) zoneRegion.pan = 100.0f; - if (zoneRegion.initialFilterQ < 1500 || zoneRegion.initialFilterQ > 13500) zoneRegion.initialFilterQ = 0; + // Fixup sample positions + get_shdr(hydra, igen.genAmount.wordAmount, &shdr); zoneRegion.offset += shdr.start; zoneRegion.end += shdr.end; zoneRegion.loop_start += shdr.startLoop; @@ -842,20 +999,15 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) if (shdr.endLoop > 0) zoneRegion.loop_end -= 1; if (zoneRegion.pitch_keycenter == -1) zoneRegion.pitch_keycenter = shdr.originalPitch; zoneRegion.tune += shdr.pitchCorrection; - - // Pin initialAttenuation to max +6dB. - if (zoneRegion.volume > 6.0f) - { - zoneRegion.volume = 6.0f; - //addUnsupportedOpcode("extreme gain in initialAttenuation"); - } + zoneRegion.sample_rate = shdr.sampleRate; + if (zoneRegion.end && zoneRegion.end < res->fontSampleCount) zoneRegion.end++; + else zoneRegion.end = res->fontSampleCount; preset->regions[region_index] = zoneRegion; - preset->regions[region_index].sample_rate = shdr.sampleRate; region_index++; hadSampleID = 1; } - else tsf_region_operator(&zoneRegion, igen.genOper, &igen.genAmount); + else tsf_region_operator(&zoneRegion, igen.genOper, &igen.genAmount, TSF_NULL); } // Handle instrument's global zone. @@ -865,17 +1017,22 @@ static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) // Modulators (TODO) //if (ibag->instModNdx < ibag[1].instModNdx) addUnsupportedOpcode("any modulator"); } + hadGenInstrument = 1; } - else tsf_region_operator(&presetRegion, pgen.genOper, &pgen.genAmount); + else tsf_region_operator(&presetRegion, pgen.genOper, &pgen.genAmount, TSF_NULL); } // Modulators (TODO) //if (pbag->modNdx < pbag[1].modNdx) addUnsupportedOpcode("any modulator"); + + // Handle preset's global zone. + if (pbagIdx == phdr.presetBagNdx && !hadGenInstrument) + globalRegion = presetRegion; } } } -static void tsf_load_samples(int *fontSamplesOffset, int* fontSampleCount, struct tsf_riffchunk *chunkSmpl, struct tsf_stream* stream) +static void tsf_load_samples(int *fontSamplesOffset, unsigned int* fontSampleCount, struct tsf_riffchunk *chunkSmpl, struct tsf_stream* stream) { // Read sample data into float format buffer. unsigned int samplesLeft; @@ -884,7 +1041,7 @@ static void tsf_load_samples(int *fontSamplesOffset, int* fontSampleCount, struc stream->skip(stream->data, samplesLeft * sizeof(short)); } -static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int active_segment, float outSampleRate) +static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, short active_segment, float outSampleRate) { switch (active_segment) { @@ -898,33 +1055,41 @@ static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int act e->slope = 0.0; return; } + /* fall through */ case TSF_SEGMENT_DELAY: e->samplesUntilNextSegment = (int)(e->parameters.attack * outSampleRate); if (e->samplesUntilNextSegment > 0) { + if (!e->isAmpEnv) + { + //mod env attack duration scales with velocity (velocity of 1 is full duration, max velocity is 0.125 times duration) + e->samplesUntilNextSegment = (int)(e->parameters.attack * ((145 - e->midiVelocity) / 144.0f) * outSampleRate); + } e->segment = TSF_SEGMENT_ATTACK; e->segmentIsExponential = TSF_FALSE; - e->level = e->parameters.start / 100.0f; + e->level = 0.0f; e->slope = 1.0f / e->samplesUntilNextSegment; return; } + /* fall through */ case TSF_SEGMENT_ATTACK: e->samplesUntilNextSegment = (int)(e->parameters.hold * outSampleRate); if (e->samplesUntilNextSegment > 0) { e->segment = TSF_SEGMENT_HOLD; e->segmentIsExponential = TSF_FALSE; - e->level = 1.0; - e->slope = 0.0; + e->level = 1.0f; + e->slope = 0.0f; return; } + /* fall through */ case TSF_SEGMENT_HOLD: e->samplesUntilNextSegment = (int)(e->parameters.decay * outSampleRate); if (e->samplesUntilNextSegment > 0) { e->segment = TSF_SEGMENT_DECAY; - e->level = 1.0; - if (e->exponentialDecay) + e->level = 1.0f; + if (e->isAmpEnv) { // I don't truly understand this; just following what LinuxSampler does. float mysterySlope = -9.226f / e->samplesUntilNextSegment; @@ -937,19 +1102,21 @@ static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int act // get to zero, not to the sustain level. The SFZ spec is not that // specific about what "decay" means, so perhaps it's really supposed // to specify the time to reach the sustain level. - e->samplesUntilNextSegment = (int)(TSF_LOG((e->parameters.sustain / 100.0) / e->level) / mysterySlope); + e->samplesUntilNextSegment = (int)(TSF_LOG(e->parameters.sustain) / mysterySlope); } } else { - e->slope = (e->parameters.sustain / 100.0f - 1.0f) / e->samplesUntilNextSegment; + e->slope = -1.0f / e->samplesUntilNextSegment; + e->samplesUntilNextSegment = (int)(e->parameters.decay * (1.0f - e->parameters.sustain) * outSampleRate); e->segmentIsExponential = TSF_FALSE; } return; } + /* fall through */ case TSF_SEGMENT_DECAY: e->segment = TSF_SEGMENT_SUSTAIN; - e->level = e->parameters.sustain / 100.0f; + e->level = e->parameters.sustain; e->slope = 0.0f; e->samplesUntilNextSegment = 0x7FFFFFFF; e->segmentIsExponential = TSF_FALSE; @@ -957,7 +1124,7 @@ static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int act case TSF_SEGMENT_SUSTAIN: e->segment = TSF_SEGMENT_RELEASE; e->samplesUntilNextSegment = (int)((e->parameters.release <= 0 ? TSF_FASTRELEASETIME : e->parameters.release) * outSampleRate); - if (e->exponentialDecay) + if (e->isAmpEnv) { // I don't truly understand this; just following what LinuxSampler does. float mysterySlope = -9.226f / e->samplesUntilNextSegment; @@ -974,12 +1141,12 @@ static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int act default: e->segment = TSF_SEGMENT_DONE; e->segmentIsExponential = TSF_FALSE; - e->level = e->slope = 0; + e->level = e->slope = 0.0f; e->samplesUntilNextSegment = 0x7FFFFFF; } } -static void tsf_voice_envelope_setup(struct tsf_voice_envelope* e, struct tsf_envelope* new_parameters, int midiNoteNumber, TSF_BOOL setExponentialDecay, float outSampleRate) +static void tsf_voice_envelope_setup(struct tsf_voice_envelope* e, struct tsf_envelope* new_parameters, int midiNoteNumber, short midiVelocity, TSF_BOOL isAmpEnv, float outSampleRate) { e->parameters = *new_parameters; if (e->parameters.keynumToHold) @@ -992,7 +1159,8 @@ static void tsf_voice_envelope_setup(struct tsf_voice_envelope* e, struct tsf_en e->parameters.decay += e->parameters.keynumToDecay * (60.0f - midiNoteNumber); e->parameters.decay = (e->parameters.decay < -10000.0f ? 0.0f : tsf_timecents2Secsf(e->parameters.decay)); } - e->exponentialDecay = setExponentialDecay; + e->midiVelocity = midiVelocity; + e->isAmpEnv = isAmpEnv; tsf_voice_envelope_nextsegment(e, TSF_SEGMENT_NONE, outSampleRate); } @@ -1040,7 +1208,6 @@ static void tsf_voice_lfo_process(struct tsf_voice_lfo* e, int blockSamples) static void tsf_voice_kill(struct tsf_voice* v) { - v->region = TSF_NULL; v->playingPreset = -1; } @@ -1061,15 +1228,11 @@ static void tsf_voice_endquick(struct tsf_voice* v, float outSampleRate) v->modenv.parameters.release = 0.0f; tsf_voice_envelope_nextsegment(&v->modenv, TSF_SEGMENT_SUSTAIN, outSampleRate); } -static void tsf_voice_calcpitchratio(struct tsf_voice* v, float outSampleRate) +static void tsf_voice_calcpitchratio(struct tsf_voice* v, float pitchShift, float outSampleRate) { - double note = v->playingKey, adjustedPitch; - note += v->region->transpose; - note += v->region->tune / 100.0; - - adjustedPitch = v->region->pitch_keycenter + (note - v->region->pitch_keycenter) * (v->region->pitch_keytrack / 100.0); - if (v->curPitchWheel != 8192) adjustedPitch += ((4.0 * v->curPitchWheel / 16383.0) - 2.0); - + double note = v->playingKey + v->region->transpose + v->region->tune / 100.0; + double adjustedPitch = v->region->pitch_keycenter + (note - v->region->pitch_keycenter) * (v->region->pitch_keytrack / 100.0); + if (pitchShift) adjustedPitch += pitchShift; v->pitchInputTimecents = adjustedPitch * 100.0; v->pitchOutputFactor = v->region->sample_rate / (tsf_timecents2Secsd(v->region->pitch_keycenter * 100.0) * outSampleRate); } @@ -1120,27 +1283,27 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i TSF_BOOL updateVibLFO = (v->viblfo.delta && (region->vibLfoToPitch)); TSF_BOOL isLooping = (v->loopStart < v->loopEnd); unsigned int tmpLoopStart = v->loopStart, tmpLoopEnd = v->loopEnd; - double tmpSampleEndDbl = (double)v->sampleEnd, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0; + double tmpSampleEndDbl = (double)region->end, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0; double tmpSourceSamplePosition = v->sourceSamplePosition; struct tsf_voice_lowpass tmpLowpass = v->lowpass; TSF_BOOL dynamicLowpass = (region->modLfoToFilterFc || region->modEnvToFilterFc); - float tmpSampleRate, tmpInitialFilterFc, tmpModLfoToFilterFc, tmpModEnvToFilterFc; + float tmpSampleRate = f->outSampleRate, tmpInitialFilterFc, tmpModLfoToFilterFc, tmpModEnvToFilterFc; TSF_BOOL dynamicPitchRatio = (region->modLfoToPitch || region->modEnvToPitch || region->vibLfoToPitch); double pitchRatio; float tmpModLfoToPitch, tmpVibLfoToPitch, tmpModEnvToPitch; TSF_BOOL dynamicGain = (region->modLfoToVolume != 0); - float noteGain, tmpModLfoToVolume; + float noteGain = 0, tmpModLfoToVolume; - if (dynamicLowpass) tmpSampleRate = f->outSampleRate, tmpInitialFilterFc = (float)region->initialFilterFc, tmpModLfoToFilterFc = (float)region->modLfoToFilterFc, tmpModEnvToFilterFc = (float)region->modEnvToFilterFc; - else tmpSampleRate = 0, tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0; + if (dynamicLowpass) tmpInitialFilterFc = (float)region->initialFilterFc, tmpModLfoToFilterFc = (float)region->modLfoToFilterFc, tmpModEnvToFilterFc = (float)region->modEnvToFilterFc; + else tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0; if (dynamicPitchRatio) pitchRatio = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch; else pitchRatio = tsf_timecents2Secsd(v->pitchInputTimecents) * v->pitchOutputFactor, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0; - if (dynamicGain) noteGain = 0, tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f; + if (dynamicGain) tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f; else noteGain = tsf_decibelsToGain(v->noteGainDB), tmpModLfoToVolume = 0; while (numSamples) @@ -1152,8 +1315,9 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i if (dynamicLowpass) { float fres = tmpInitialFilterFc + v->modlfo.level * tmpModLfoToFilterFc + v->modenv.level * tmpModEnvToFilterFc; - tmpLowpass.active = (fres <= 13500.0f); - if (tmpLowpass.active) tsf_voice_lowpass_setup(&tmpLowpass, tsf_cents2Hertz(fres) / tmpSampleRate); + float lowpassFc = (fres <= 13500 ? tsf_cents2Hertz(fres) / tmpSampleRate : 1.0f); + tmpLowpass.active = (lowpassFc < 0.499f); + if (tmpLowpass.active) tsf_voice_lowpass_setup(&tmpLowpass, lowpassFc); } if (dynamicPitchRatio) @@ -1165,8 +1329,8 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i gainMono = noteGain * v->ampenv.level; // Update EG. - tsf_voice_envelope_process(&v->ampenv, blockSamples, f->outSampleRate); - if (updateModEnv) tsf_voice_envelope_process(&v->modenv, blockSamples, f->outSampleRate); + tsf_voice_envelope_process(&v->ampenv, blockSamples, tmpSampleRate); + if (updateModEnv) tsf_voice_envelope_process(&v->modenv, blockSamples, tmpSampleRate); // Update LFOs. if (updateModLFO) tsf_voice_lfo_process(&v->modlfo, blockSamples); @@ -1273,7 +1437,7 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff unsigned int tmpLoopStart = v->loopStart, tmpLoopEnd = v->loopEnd; //double tmpSampleEndDbl = (double)v->sampleEnd, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0; //double tmpSourceSamplePosition = v->sourceSamplePosition; - fixed32p32 tmpSampleEndF32P32 = ((fixed32p32)(v->sampleEnd)) << 32; + fixed32p32 tmpSampleEndF32P32 = ((fixed32p32)(region->end)) << 32; fixed32p32 tmpLoopEndF32P32 = ((fixed32p32)(tmpLoopEnd + 1)) << 32; fixed32p32 tmpSourceSamplePositionF32P32 = v->sourceSamplePositionF32P32; struct tsf_voice_lowpass tmpLowpass = v->lowpass; @@ -1370,7 +1534,7 @@ TSFDEF tsf* tsf_load(struct tsf_stream* stream) struct tsf_riffchunk chunkList; struct tsf_hydra hydra; int fontSamplesOffset = 0; - int fontSampleCount = 0; + unsigned int fontSampleCount = 0; if (!tsf_riffchunk_read(TSF_NULL, &chunkHead, stream) || !TSF_FourCCEquals(chunkHead.id, "sfbk")) { @@ -1437,7 +1601,7 @@ TSFDEF tsf* tsf_load(struct tsf_stream* stream) res->presets = (struct tsf_preset*)TSF_MALLOC(res->presetNum * sizeof(struct tsf_preset)); TSF_MEMSET(res->presets, 0, res->presetNum * sizeof(struct tsf_preset)); res->fontSamplesOffset = fontSamplesOffset; - res->fontSampleCount = fontSampleCount; + res->fontSampleCount = fontSampleCount; res->outSampleRate = 44100.0f; res->hydra = (struct tsf_hydra*)TSF_MALLOC(sizeof(struct tsf_hydra)); TSF_MEMCPY(res->hydra, &hydra, sizeof(*res->hydra)); @@ -1463,6 +1627,7 @@ TSFDEF void tsf_close(tsf* f) TSF_FREE(preset->regions); TSF_FREE(f->presets); TSF_FREE(f->voices); + if (f->channels) { TSF_FREE(f->channels->channels); TSF_FREE(f->channels); } TSF_FREE(f->outputSamples); f->hydra->stream->close(f->hydra->stream->data); TSF_FREE(f->hydra->stream); @@ -1471,53 +1636,74 @@ TSFDEF void tsf_close(tsf* f) TSF_FREE(f); } -TSFDEF int tsf_get_presetcount(tsf* f) +TSFDEF void tsf_reset(tsf* f) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + for (; v != vEnd; v++) + if (v->playingPreset != -1 && (v->ampenv.segment < TSF_SEGMENT_RELEASE || v->ampenv.parameters.release)) + tsf_voice_endquick(v, f->outSampleRate); + if (f->channels) { TSF_FREE(f->channels->channels); TSF_FREE(f->channels); f->channels = TSF_NULL; } +} + +TSFDEF int tsf_get_presetindex(const tsf* f, int bank, int preset_number) +{ + const struct tsf_preset *presets; + int i, iMax; + for (presets = f->presets, i = 0, iMax = f->presetNum; i < iMax; i++) + if (presets[i].preset == preset_number && presets[i].bank == bank) + return i; + return -1; +} + +TSFDEF int tsf_get_presetcount(const tsf* f) { return f->presetNum; } -TSFDEF const char* tsf_get_presetname(tsf* f, int preset) +TSFDEF const char* tsf_get_presetname(const tsf* f, int preset) { if (f->presets[preset].regions == NULL && preset >= 0 && preset < f->presetNum) tsf_load_preset(f, f->hydra, preset); return (preset < 0 || preset >= f->presetNum ? TSF_NULL : f->presets[preset].presetName); } -TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float globalgaindb) +TSFDEF const char* tsf_bank_get_presetname(const tsf* f, int bank, int preset_number) { - f->outSampleRate = (float)(samplerate >= 1 ? samplerate : 44100.0f); - f->outputmode = outputmode; - f->globalGainDB = globalgaindb; + return tsf_get_presetname(f, tsf_get_presetindex(f, bank, preset_number)); } -TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel) +TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float global_gain_db) { - int midiVelocity = (int)(vel * 127); - TSF_BOOL haveGroupedNotesPlaying = TSF_FALSE; - struct tsf_voice *v, *vEnd; struct tsf_region *region, *regionEnd; + f->outputmode = outputmode; + f->outSampleRate = (float)(samplerate >= 1 ? samplerate : 44100.0f); + f->globalGainDB = global_gain_db; +} - if (preset < 0 || preset >= f->presetNum) return; - if (f->presets[preset].regions == NULL) tsf_load_preset(f, f->hydra, preset); +TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel) +{ + short midiVelocity = (short)(vel * 127); + int voicePlayIndex; + struct tsf_region *region, *regionEnd; - // Are any grouped notes playing? (Needed for group stopping) Also stop any voices still playing this note. - for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) - { - if (v->playingPreset != preset) continue; - if (v->playingKey == key) tsf_voice_endquick(v, f->outSampleRate); - if (v->region->group) haveGroupedNotesPlaying = TSF_TRUE; - } + if (preset_index < 0 || preset_index >= f->presetNum) return; + if (vel <= 0.0f) { tsf_note_off(f, preset_index, key); return; } + if (f->presets[preset_index].regions == NULL) tsf_load_preset(f, f->hydra, preset_index); // Play all matching regions. - for (region = f->presets[preset].regions, regionEnd = region + f->presets[preset].regionNum; region != regionEnd; region++) + voicePlayIndex = f->voicePlayIndex++; + for (region = f->presets[preset_index].regions, regionEnd = region + f->presets[preset_index].regionNum; region != regionEnd; region++) { - struct tsf_voice* voice = TSF_NULL; double adjustedPan; TSF_BOOL doLoop; float filterQDB; + struct tsf_voice *voice, *v, *vEnd; TSF_BOOL doLoop; float lowpassFilterQDB, lowpassFc; if (key < region->lokey || key > region->hikey || midiVelocity < region->lovel || midiVelocity > region->hivel) continue; - if (haveGroupedNotesPlaying && region->group) - for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) - if (v->playingPreset == preset && v->region->group == region->group) - tsf_voice_endquick(v, f->outSampleRate); + voice = TSF_NULL, v = f->voices, vEnd = v + f->voiceNum; + if (region->group) + { + for (; v != vEnd; v++) + if (v->playingPreset == preset_index && v->region->group == region->group) tsf_voice_endquick(v, f->outSampleRate); + else if (v->playingPreset == -1 && !voice) voice = v; + } + else for (; v != vEnd; v++) if (v->playingPreset == -1) { voice = v; break; } - for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) if (v->playingPreset == -1) { voice = v; break; } if (!voice) { f->voiceNum += 4; @@ -1533,27 +1719,26 @@ TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel) } voice->region = region; - voice->playingPreset = preset; + voice->playingPreset = preset_index; voice->playingKey = key; + voice->playIndex = voicePlayIndex; + voice->noteGainDB = f->globalGainDB - region->attenuation - tsf_gainToDecibels(1.0f / vel); - // Pitch. - voice->curPitchWheel = 8192; - tsf_voice_calcpitchratio(voice, f->outSampleRate); - - // Gain. - voice->noteGainDB = f->globalGainDB + region->volume; - // Thanks to for explaining the velocity curve in a way that I could understand, although they mean "log10" when they say "log". - voice->noteGainDB += (float)(-20.0 * TSF_LOG10(1.0 / vel)); - // The SFZ spec is silent about the pan curve, but a 3dB pan law seems common. This sqrt() curve matches what Dimension LE does; Alchemy Free seems closer to sin(adjustedPan * pi/2). - adjustedPan = (region->pan + 100.0) / 200.0; - voice->panFactorLeft = (float)TSF_SQRT(1.0 - adjustedPan); - voice->panFactorRight = (float)TSF_SQRT(adjustedPan); + if (f->channels) + { + f->channels->setupVoice(f, voice); + } + else + { + tsf_voice_calcpitchratio(voice, 0, f->outSampleRate); + // The SFZ spec is silent about the pan curve, but a 3dB pan law seems common. This sqrt() curve matches what Dimension LE does; Alchemy Free seems closer to sin(adjustedPan * pi/2). + voice->panFactorLeft = TSF_SQRTF(0.5f - region->pan); + voice->panFactorRight = TSF_SQRTF(0.5f + region->pan); + } // Offset/end. voice->sourceSamplePosition = region->offset; - voice->sourceSamplePositionF32P32 = ((int64_t)region->offset)<< 32; - voice->sampleEnd = f->fontSampleCount; - if (region->end > 0 && region->end < voice->sampleEnd) voice->sampleEnd = region->end + 1; + voice->sourceSamplePositionF32P32 = ((int64_t)region->offset)<< 32; // Loop. doLoop = (region->loop_mode != TSF_LOOPMODE_NONE && region->loop_start < region->loop_end); @@ -1561,15 +1746,16 @@ TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel) voice->loopEnd = (doLoop ? region->loop_end : 0); // Setup envelopes. - tsf_voice_envelope_setup(&voice->ampenv, ®ion->ampenv, key, TSF_TRUE, f->outSampleRate); - tsf_voice_envelope_setup(&voice->modenv, ®ion->modenv, key, TSF_FALSE, f->outSampleRate); + tsf_voice_envelope_setup(&voice->ampenv, ®ion->ampenv, key, midiVelocity, TSF_TRUE, f->outSampleRate); + tsf_voice_envelope_setup(&voice->modenv, ®ion->modenv, key, midiVelocity, TSF_FALSE, f->outSampleRate); // Setup lowpass filter. - filterQDB = region->initialFilterQ / 10.0f; - voice->lowpass.QInv = 1.0 / TSF_POW(10.0, (filterQDB / 20.0)); + lowpassFc = (region->initialFilterFc <= 13500 ? tsf_cents2Hertz((float)region->initialFilterFc) / f->outSampleRate : 1.0f); + lowpassFilterQDB = region->initialFilterQ / 10.0f; + voice->lowpass.QInv = 1.0 / TSF_POW(10.0, (lowpassFilterQDB / 20.0)); voice->lowpass.z1 = voice->lowpass.z2 = 0; - voice->lowpass.active = (region->initialFilterFc <= 13500); - if (voice->lowpass.active) tsf_voice_lowpass_setup(&voice->lowpass, tsf_cents2Hertz((float)region->initialFilterFc) / f->outSampleRate); + voice->lowpass.active = (lowpassFc < 0.499f); + if (voice->lowpass.active) tsf_voice_lowpass_setup(&voice->lowpass, lowpassFc); // Setup LFO filters. tsf_voice_lfo_setup(&voice->modlfo, region->delayModLFO, region->freqModLFO, f->outSampleRate); @@ -1577,12 +1763,55 @@ TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel) } } -TSFDEF void tsf_note_off(tsf* f, int preset, int key) +TSFDEF int tsf_bank_note_on(tsf* f, int bank, int preset_number, int key, float vel) +{ + int preset_index = tsf_get_presetindex(f, bank, preset_number); + if (preset_index == -1) return 0; + tsf_note_on(f, preset_index, key, vel); + return 1; +} + +TSFDEF void tsf_note_off(tsf* f, int preset_index, int key) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum, *vMatchFirst = TSF_NULL, *vMatchLast = TSF_NULL; + for (; v != vEnd; v++) + { + //Find the first and last entry in the voices list with matching preset, key and look up the smallest play index + if (v->playingPreset != preset_index || v->playingKey != key || v->ampenv.segment >= TSF_SEGMENT_RELEASE) continue; + else if (!vMatchFirst || v->playIndex < vMatchFirst->playIndex) vMatchFirst = vMatchLast = v; + else if (v->playIndex == vMatchFirst->playIndex) vMatchLast = v; + } + if (!vMatchFirst) return; + for (v = vMatchFirst; v <= vMatchLast; v++) + { + //Stop all voices with matching preset, key and the smallest play index which was enumerated above + if (v != vMatchFirst && v != vMatchLast && + (v->playIndex != vMatchFirst->playIndex || v->playingPreset != preset_index || v->playingKey != key || v->ampenv.segment >= TSF_SEGMENT_RELEASE)) continue; + tsf_voice_end(v, f->outSampleRate); + } +} + +TSFDEF int tsf_bank_note_off(tsf* f, int bank, int preset_number, int key) +{ + int preset_index = tsf_get_presetindex(f, bank, preset_number); + if (preset_index == -1) return 0; + tsf_note_off(f, preset_index, key); + return 1; +} + +TSFDEF void tsf_note_off_all(tsf* f) { struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; - for (; v != vEnd; v++) - if (v->playingPreset == preset && v->playingKey == key) - tsf_voice_end(v, f->outSampleRate); + for (; v != vEnd; v++) if (v->playingPreset != -1 && v->ampenv.segment < TSF_SEGMENT_RELEASE) + tsf_voice_end(v, f->outSampleRate); +} + +TSFDEF int tsf_active_voice_count(tsf* f) +{ + int count = 0; + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + for (; v != vEnd; v++) if (v->playingPreset != -1) count++; + return count; } TSFDEF void tsf_render_short(tsf* f, short* buffer, int samples, int flag_mixing) @@ -1636,6 +1865,276 @@ TSFDEF void tsf_render_short_fast(tsf* f, short* buffer, int samples, int flag_m } +static void tsf_channel_setup_voice(tsf* f, struct tsf_voice* v) +{ + struct tsf_channel* c = &f->channels->channels[f->channels->activeChannel]; + float newpan = v->region->pan + c->panOffset; + v->playingChannel = f->channels->activeChannel; + v->noteGainDB += c->gainDB; + tsf_voice_calcpitchratio(v, (c->pitchWheel == 8192 ? c->tuning : ((c->pitchWheel / 16383.0f * c->pitchRange * 2.0f) - c->pitchRange + c->tuning)), f->outSampleRate); + if (newpan <= -0.5f) { v->panFactorLeft = 1.0f; v->panFactorRight = 0.0f; } + else if (newpan >= 0.5f) { v->panFactorLeft = 0.0f; v->panFactorRight = 1.0f; } + else { v->panFactorLeft = TSF_SQRTF(0.5f - newpan); v->panFactorRight = TSF_SQRTF(0.5f + newpan); } +} + +static struct tsf_channel* tsf_channel_init(tsf* f, int channel) +{ + int i; + if (f->channels && channel < f->channels->channelNum) return &f->channels->channels[channel]; + if (!f->channels) + { + f->channels = (struct tsf_channels*)TSF_MALLOC(sizeof(struct tsf_channels)); + f->channels->setupVoice = &tsf_channel_setup_voice; + f->channels->channels = NULL; + f->channels->channelNum = 0; + f->channels->activeChannel = 0; + } + i = f->channels->channelNum; + f->channels->channelNum = channel + 1; + f->channels->channels = (struct tsf_channel*)TSF_REALLOC(f->channels->channels, f->channels->channelNum * sizeof(struct tsf_channel)); + for (; i <= channel; i++) + { + struct tsf_channel* c = &f->channels->channels[i]; + c->presetIndex = c->bank = 0; + c->pitchWheel = c->midiPan = 8192; + c->midiVolume = c->midiExpression = 16383; + c->midiRPN = 0xFFFF; + c->midiData = 0; + c->panOffset = 0.0f; + c->gainDB = 0.0f; + c->pitchRange = 2.0f; + c->tuning = 0.0f; + } + return &f->channels->channels[channel]; +} + +static void tsf_channel_applypitch(tsf* f, int channel, struct tsf_channel* c) +{ + struct tsf_voice *v, *vEnd; + float pitchShift = (c->pitchWheel == 8192 ? c->tuning : ((c->pitchWheel / 16383.0f * c->pitchRange * 2.0f) - c->pitchRange + c->tuning)); + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) + if (v->playingChannel == channel && v->playingPreset != -1) + tsf_voice_calcpitchratio(v, pitchShift, f->outSampleRate); +} + +TSFDEF void tsf_channel_set_presetindex(tsf* f, int channel, int preset_index) +{ + tsf_channel_init(f, channel)->presetIndex = (unsigned short)preset_index; +} + +TSFDEF int tsf_channel_set_presetnumber(tsf* f, int channel, int preset_number, int flag_mididrums) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + int preset_index; + if (flag_mididrums) + { + preset_index = tsf_get_presetindex(f, 128 | (c->bank & 0x7FFF), preset_number); + if (preset_index == -1) preset_index = tsf_get_presetindex(f, 128, preset_number); + if (preset_index == -1) preset_index = tsf_get_presetindex(f, 128, 0); + if (preset_index == -1) preset_index = tsf_get_presetindex(f, (c->bank & 0x7FFF), preset_number); + } + else preset_index = tsf_get_presetindex(f, (c->bank & 0x7FFF), preset_number); + if (preset_index == -1) preset_index = tsf_get_presetindex(f, 0, preset_number); + if (preset_index != -1) + { + c->presetIndex = (unsigned short)preset_index; + return 1; + } + return 0; +} + +TSFDEF void tsf_channel_set_bank(tsf* f, int channel, int bank) +{ + tsf_channel_init(f, channel)->bank = (unsigned short)bank; +} + +TSFDEF int tsf_channel_set_bank_preset(tsf* f, int channel, int bank, int preset_number) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + int preset_index = tsf_get_presetindex(f, bank, preset_number); + if (preset_index == -1) return 0; + c->presetIndex = (unsigned short)preset_index; + c->bank = (unsigned short)bank; + return 1; +} + +TSFDEF void tsf_channel_set_pan(tsf* f, int channel, float pan) +{ + struct tsf_voice *v, *vEnd; + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) + if (v->playingChannel == channel && v->playingPreset != -1) + { + float newpan = v->region->pan + pan - 0.5f; + if (newpan <= -0.5f) { v->panFactorLeft = 1.0f; v->panFactorRight = 0.0f; } + else if (newpan >= 0.5f) { v->panFactorLeft = 0.0f; v->panFactorRight = 1.0f; } + else { v->panFactorLeft = TSF_SQRTF(0.5f - newpan); v->panFactorRight = TSF_SQRTF(0.5f + newpan); } + } + tsf_channel_init(f, channel)->panOffset = pan - 0.5f; +} + +TSFDEF void tsf_channel_set_volume(tsf* f, int channel, float volume) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + float gainDB = tsf_gainToDecibels(volume), gainDBChange = gainDB - c->gainDB; + struct tsf_voice *v, *vEnd; + if (gainDBChange == 0) return; + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) + if (v->playingChannel == channel && v->playingPreset != -1) + v->noteGainDB += gainDBChange; + c->gainDB = gainDB; +} + +TSFDEF void tsf_channel_set_pitchwheel(tsf* f, int channel, int pitch_wheel) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + if (c->pitchWheel == pitch_wheel) return; + c->pitchWheel = (unsigned short)pitch_wheel; + tsf_channel_applypitch(f, channel, c); +} + +TSFDEF void tsf_channel_set_pitchrange(tsf* f, int channel, float pitch_range) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + if (c->pitchRange == pitch_range) return; + c->pitchRange = pitch_range; + if (c->pitchWheel != 8192) tsf_channel_applypitch(f, channel, c); +} + +TSFDEF void tsf_channel_set_tuning(tsf* f, int channel, float tuning) +{ + struct tsf_channel *c = tsf_channel_init(f, channel); + if (c->tuning == tuning) return; + c->tuning = tuning; + tsf_channel_applypitch(f, channel, c); +} + +TSFDEF void tsf_channel_note_on(tsf* f, int channel, int key, float vel) +{ + if (!f->channels || channel >= f->channels->channelNum) return; + f->channels->activeChannel = channel; + tsf_note_on(f, f->channels->channels[channel].presetIndex, key, vel); +} + +TSFDEF void tsf_channel_note_off(tsf* f, int channel, int key) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum, *vMatchFirst = TSF_NULL, *vMatchLast = TSF_NULL; + for (; v != vEnd; v++) + { + //Find the first and last entry in the voices list with matching channel, key and look up the smallest play index + if (v->playingPreset == -1 || v->playingChannel != channel || v->playingKey != key || v->ampenv.segment >= TSF_SEGMENT_RELEASE) continue; + else if (!vMatchFirst || v->playIndex < vMatchFirst->playIndex) vMatchFirst = vMatchLast = v; + else if (v->playIndex == vMatchFirst->playIndex) vMatchLast = v; + } + if (!vMatchFirst) return; + for (v = vMatchFirst; v <= vMatchLast; v++) + { + //Stop all voices with matching channel, key and the smallest play index which was enumerated above + if (v != vMatchFirst && v != vMatchLast && + (v->playIndex != vMatchFirst->playIndex || v->playingPreset == -1 || v->playingChannel != channel || v->playingKey != key || v->ampenv.segment >= TSF_SEGMENT_RELEASE)) continue; + tsf_voice_end(v, f->outSampleRate); + } +} + +TSFDEF void tsf_channel_note_off_all(tsf* f, int channel) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + for (; v != vEnd; v++) + if (v->playingPreset != -1 && v->playingChannel == channel && v->ampenv.segment < TSF_SEGMENT_RELEASE) + tsf_voice_end(v, f->outSampleRate); +} + +TSFDEF void tsf_channel_sounds_off_all(tsf* f, int channel) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + for (; v != vEnd; v++) + if (v->playingPreset != -1 && v->playingChannel == channel && (v->ampenv.segment < TSF_SEGMENT_RELEASE || v->ampenv.parameters.release)) + tsf_voice_endquick(v, f->outSampleRate); +} + +TSFDEF void tsf_channel_midi_control(tsf* f, int channel, int controller, int control_value) +{ + struct tsf_channel* c = tsf_channel_init(f, channel); + switch (controller) + { + case 7 /*VOLUME_MSB*/ : c->midiVolume = (unsigned short)((c->midiVolume & 0x7F ) | (control_value << 7)); goto TCMC_SET_VOLUME; + case 39 /*VOLUME_LSB*/ : c->midiVolume = (unsigned short)((c->midiVolume & 0x3F80) | control_value); goto TCMC_SET_VOLUME; + case 11 /*EXPRESSION_MSB*/ : c->midiExpression = (unsigned short)((c->midiExpression & 0x7F ) | (control_value << 7)); goto TCMC_SET_VOLUME; + case 43 /*EXPRESSION_LSB*/ : c->midiExpression = (unsigned short)((c->midiExpression & 0x3F80) | control_value); goto TCMC_SET_VOLUME; + case 10 /*PAN_MSB*/ : c->midiPan = (unsigned short)((c->midiPan & 0x7F ) | (control_value << 7)); goto TCMC_SET_PAN; + case 42 /*PAN_LSB*/ : c->midiPan = (unsigned short)((c->midiPan & 0x3F80) | control_value); goto TCMC_SET_PAN; + case 6 /*DATA_ENTRY_MSB*/ : c->midiData = (unsigned short)((c->midiData & 0x7F) | (control_value << 7)); goto TCMC_SET_DATA; + case 38 /*DATA_ENTRY_LSB*/ : c->midiData = (unsigned short)((c->midiData & 0x3F80) | control_value); goto TCMC_SET_DATA; + case 0 /*BANK_SELECT_MSB*/ : c->bank = (unsigned short)(0x8000 | control_value); return; //bank select MSB alone acts like LSB + case 32 /*BANK_SELECT_LSB*/ : c->bank = (unsigned short)((c->bank & 0x8000 ? ((c->bank & 0x7F) << 7) : 0) | control_value); return; + case 101 /*RPN_MSB*/ : c->midiRPN = (unsigned short)(((c->midiRPN == 0xFFFF ? 0 : c->midiRPN) & 0x7F ) | (control_value << 7)); return; + case 100 /*RPN_LSB*/ : c->midiRPN = (unsigned short)(((c->midiRPN == 0xFFFF ? 0 : c->midiRPN) & 0x3F80) | control_value); return; + case 98 /*NRPN_LSB*/ : c->midiRPN = 0xFFFF; return; + case 99 /*NRPN_MSB*/ : c->midiRPN = 0xFFFF; return; + case 120 /*ALL_SOUND_OFF*/ : tsf_channel_sounds_off_all(f, channel); return; + case 123 /*ALL_NOTES_OFF*/ : tsf_channel_note_off_all(f, channel); return; + case 121 /*ALL_CTRL_OFF*/ : + c->midiVolume = c->midiExpression = 16383; + c->midiPan = 8192; + c->bank = 0; + tsf_channel_set_volume(f, channel, 1.0f); + tsf_channel_set_pan(f, channel, 0.5f); + tsf_channel_set_pitchrange(f, channel, 2.0f); + return; + } + return; +TCMC_SET_VOLUME: + //Raising to the power of 3 seems to result in a decent sounding volume curve for MIDI + tsf_channel_set_volume(f, channel, TSF_POWF((c->midiVolume / 16383.0f) * (c->midiExpression / 16383.0f), 3.0f)); + return; +TCMC_SET_PAN: + tsf_channel_set_pan(f, channel, c->midiPan / 16383.0f); + return; +TCMC_SET_DATA: + if (c->midiRPN == 0) tsf_channel_set_pitchrange(f, channel, (c->midiData >> 7) + 0.01f * (c->midiData & 0x7F)); + else if (c->midiRPN == 1) tsf_channel_set_tuning(f, channel, (int)c->tuning + ((float)c->midiData - 8192.0f) / 8192.0f); //fine tune + else if (c->midiRPN == 2 && controller == 6) tsf_channel_set_tuning(f, channel, ((float)control_value - 64.0f) + (c->tuning - (int)c->tuning)); //coarse tune + return; +} + +TSFDEF int tsf_channel_get_preset_index(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->channels->channels[channel].presetIndex : 0); +} + +TSFDEF int tsf_channel_get_preset_bank(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? (f->channels->channels[channel].bank & 0x7FFF) : 0); +} + +TSFDEF int tsf_channel_get_preset_number(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->presets[f->channels->channels[channel].presetIndex].preset : 0); +} + +TSFDEF float tsf_channel_get_pan(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->channels->channels[channel].panOffset - 0.5f : 0.5f); +} + +TSFDEF float tsf_channel_get_volume(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? tsf_decibelsToGain(f->channels->channels[channel].gainDB) : 1.0f); +} + +TSFDEF int tsf_channel_get_pitchwheel(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->channels->channels[channel].pitchWheel : 8192); +} + +TSFDEF float tsf_channel_get_pitchrange(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->channels->channels[channel].pitchRange : 2.0f); +} + +TSFDEF float tsf_channel_get_tuning(tsf* f, int channel) +{ + return (f->channels && channel < f->channels->channelNum ? f->channels->channels[channel].tuning : 0.0f); +} #ifdef __cplusplus }