diff --git a/Source/Shifter.cpp b/Source/Shifter.cpp index b440533..683d29e 100644 --- a/Source/Shifter.cpp +++ b/Source/Shifter.cpp @@ -137,6 +137,39 @@ float Shifter::GetOutputEnvelopePeriodFreeze(int freeze_voice) { } int Shifter::GetPeakIndex() { + static float current_index = 0; + float smooth = 1 / 200.0f; + int diff = in_playhead - current_index; + if (diff < 0) + diff += BUFFER_SIZE; + if (diff < in_period * 2) { + diff = last_max_index - current_index; + if (diff < -(BUFFER_SIZE / 2)) + diff += BUFFER_SIZE; + if (diff > BUFFER_SIZE / 2) + diff -= BUFFER_SIZE; + + float adjust = (float)diff * smooth; + if (adjust > 1 || adjust < -1) { + int dummy = 0; + } + current_index += adjust; + if ((int)current_index > BUFFER_SIZE) { + current_index -= BUFFER_SIZE; + } + if((int)current_index < 0) { + current_index += BUFFER_SIZE; + } + return (int)current_index; + } + else { + current_index += in_period; + if (current_index > BUFFER_SIZE) { + current_index -= BUFFER_SIZE; + } + } + + float threshold = .0075f; int index = in_playhead - in_period * 2; if (index < 0) index += BUFFER_SIZE; @@ -148,8 +181,17 @@ int Shifter::GetPeakIndex() { { //float val = fabs(in_buffer[index]); float val = in_buffer[index]; + int period_difference = index - last_max_index; + if (period_difference < 0) + period_difference += BUFFER_SIZE; + //prefer peaks near last peak or near next peak + /*if(!((period_difference > in_period * (0-threshold) && period_difference < in_period* (0+threshold)) || (period_difference > in_period * (1-threshold) && period_difference < in_period * (1+threshold)))) { + val *= 0.75f; + }*/ + if (val > max_value) { + max_index = index; max_value = val; } @@ -158,7 +200,8 @@ int Shifter::GetPeakIndex() { index -= BUFFER_SIZE; } } - return max_index; + last_max_index = max_index; + return (int)current_index; } diff --git a/Source/Shifter.h b/Source/Shifter.h index 5dcd871..e598165 100644 --- a/Source/Shifter.h +++ b/Source/Shifter.h @@ -235,6 +235,7 @@ private: bool enable_autotune = false; float freeze_period; float freeze_volume = 1; + int last_max_index = 0; MidiPitchSmoother out_midi_smoother; }; #endif \ No newline at end of file