diff options
author | Wohlstand <admin@wohlnet.ru> | 2020-09-11 10:44:07 +0300 |
---|---|---|
committer | Wohlstand <admin@wohlnet.ru> | 2020-09-11 10:44:07 +0300 |
commit | e6d73ae7e996301523d0c425bd07158f7c1d597e (patch) | |
tree | d391d2dedc4503d2c720ba3dcd9e4e3cceb203f5 /src/adlmidi_midiplay.cpp | |
parent | ffa7b0e6b53278eb5186991d4a17278b836067ea (diff) | |
download | libADLMIDI-e6d73ae7e996301523d0c425bd07158f7c1d597e.tar.gz libADLMIDI-e6d73ae7e996301523d0c425bd07158f7c1d597e.tar.bz2 libADLMIDI-e6d73ae7e996301523d0c425bd07158f7c1d597e.zip |
Fixed an assert in the HMI SOS pitch bend calculator
Diffstat (limited to 'src/adlmidi_midiplay.cpp')
-rw-r--r-- | src/adlmidi_midiplay.cpp | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/adlmidi_midiplay.cpp b/src/adlmidi_midiplay.cpp index 870d629..683ab1d 100644 --- a/src/adlmidi_midiplay.cpp +++ b/src/adlmidi_midiplay.cpp @@ -328,17 +328,27 @@ static uint_fast32_t s_hmi_freqtable[s_hmi_freqtable_size] = }; const size_t s_hmi_bendtable_size = 12; -static uint32_t s_hmi_bendtable[s_hmi_bendtable_size] = +static uint_fast32_t s_hmi_bendtable[s_hmi_bendtable_size] = { 0x144, 0x132, 0x121, 0x110, 0x101, 0xf8, 0xe5, 0xd8, 0xcc, 0xc1, 0xb6, 0xac }; -#define hmi_range_assert(formula, maxVal) assert((formula) >= 0 && (formula) < maxVal) +#define hmi_range_fix(formula, maxVal) \ + ( \ + (formula) < 0 ? \ + 0 : \ + ( \ + (formula) >= (int32_t)maxVal ? \ + (int32_t)maxVal : \ + (formula) \ + )\ + ) -static uint32_t s_hmi_bend_calc(uint32_t bend, uint32_t note) +static uint_fast32_t s_hmi_bend_calc(uint_fast32_t bend, int_fast32_t note) { - const uint32_t midi_bend_range = 1; - uint32_t noteMod12, bendFactor, outFreq, fmOctave, fmFreq, newFreq; + const int_fast32_t midi_bend_range = 1; + uint_fast32_t bendFactor, outFreq, fmOctave, fmFreq, newFreq, idx; + int_fast32_t noteMod12; note -= 12; // while(doNote >= 12) // ugly way to MOD 12 @@ -354,15 +364,15 @@ static uint32_t s_hmi_bend_calc(uint32_t bend, uint32_t note) { bendFactor = ((63 - bend) * 1000) >> 6; - hmi_range_assert(note - midi_bend_range, s_hmi_freqtable_size); - hmi_range_assert(midi_bend_range - 1, s_hmi_bendtable_size); + idx = hmi_range_fix(note - midi_bend_range, s_hmi_freqtable_size); + newFreq = outFreq - s_hmi_freqtable[idx]; - newFreq = outFreq - s_hmi_freqtable[note - midi_bend_range]; if(newFreq > 719) { newFreq = fmFreq - s_hmi_bendtable[midi_bend_range - 1]; newFreq &= 0x3ff; } + newFreq = (newFreq * bendFactor) / 1000; outFreq -= newFreq; } @@ -370,24 +380,26 @@ static uint32_t s_hmi_bend_calc(uint32_t bend, uint32_t note) { bendFactor = ((bend - 64) * 1000) >> 6; - hmi_range_assert(note + midi_bend_range, s_hmi_freqtable_size); - hmi_range_assert(11 - noteMod12, s_hmi_bendtable_size); + idx = hmi_range_fix(note + midi_bend_range, s_hmi_freqtable_size); + newFreq = s_hmi_freqtable[idx] - outFreq; - newFreq = s_hmi_freqtable[note + midi_bend_range] - outFreq; if(newFreq > 719) { - fmFreq = s_hmi_bendtable[11 - noteMod12]; + idx = hmi_range_fix(11 - noteMod12, s_hmi_bendtable_size); + fmFreq = s_hmi_bendtable[idx]; outFreq = (fmOctave + 1024) | fmFreq; - newFreq = s_hmi_freqtable[note + midi_bend_range] - outFreq; + + idx = hmi_range_fix(note + midi_bend_range, s_hmi_freqtable_size); + newFreq = s_hmi_freqtable[idx] - outFreq; } + newFreq = (newFreq * bendFactor) / 1000; outFreq += newFreq; } return outFreq; } - -#undef hmi_range_assert +#undef hmi_range_fix static inline double s_hmiFreq(double noteD, double bendD) { |