# Real-time pitchshifting performance

**URL:** <https://forum.juce.com/t/real-time-pitchshifting-performance/51939>\
**Category:** Android\
**Created:** [June 24, 2022, 11:13am UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939 "2022-06-24T11:13:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![reneeez](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@reneeez](https://forum.juce.com/u/reneeez)\
**Post date:** [June 24, 2022, 11:13am UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/1 "2022-06-24T11:13:45Z")

</div>

In my journey to use Juce in my music app (Android only) I am trying to see if real-time pitchshifting is feasible.

I’ve tried a few things but all of them result in the audio thread running on \> 70% with ‘just’ pitchshifting. Since I would like to add more audio effects in the future I think \>70% is too much.

Below is what I tried so far. It’s all working fine, except for it taking too much out of the CPU for my needs. Is this just the way it is because pitchshifting is such a heavy operation? Or am I missing something that would improve performance?

I’ve read about SoundTouch as well. Since that is running in the time-domain I expect it to be better for performance. I prefer the options listed below since they are in the frequency-domain, but maybe that’s just not an option?

I’m using `deviceManager.getCpuUsage()` to check CPU usage.

**[Audio-Effects/Pitch Shift at master · juandagilc/Audio-Effects · GitHub](https://github.com/juandagilc/Audio-Effects/tree/master/Pitch%20Shift)**  
I implemented this algorithm in my app and made sure Juce is using FFTW or PFFFT. No luck here.

**Rubberband**  
Below you can find the code used to run

```auto
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
    rubberBandStretcher = new RubberBand::RubberBandStretcher(sampleRate,
                                                              2,
                                                              RubberBand::RubberBandStretcher::OptionProcessRealTime,
                                                              1.0,
                                                              1.5);

    rubberBandStretcher->setMaxProcessSize(samplesPerBlockExpected);
}

void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
    rubberBandStretcher->process((const float* const*)bufferToFill.buffer->getArrayOfReadPointers(), bufferToFill.buffer->getNumSamples(), false);
    rubberBandStretcher->retrieve((float* const*)bufferToFill.buffer->getArrayOfWritePointers(), bufferToFill.buffer->getNumSamples());
}

```

The cmakelists.txt file used to build:

```auto
project(rubberband)
cmake_minimum_required(VERSION 2.8)

file(GLOB rubberband_src
    "src/audiocurves/*.*"
    "src/base/*.*"
    "src/dsp/*.*"
    "src/float_cast/*.*"
    "src/kissfft/*.*"
    "src/pommier/*.*"
    "src/speex/*.*"
    "src/system/*.*"
    "src/*.*"
)

add_library(rubberband ${rubberband_src})

target_include_directories(rubberband
        PRIVATE src
        PUBLIC include)

# Macro definitions
add_definitions(-DUSE_PTHREADS -DPROCESS_SAMPLE_TYPE=float)
add_definitions(-D_LIB -D_USE_MATH_DEFINES -DHAVE_KISSFFT -DUSE_SPEEX)

# CXX_FLAGS
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -DNO_THREAD_CHECKS -DNO_TIMING")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ffast-math -mfpmath=sse -msse -msse2 -O3 -ftree-vectorize")

install(TARGETS rubberband
        LIBRARY DESTINATION lib/${ANDROID_ABI}
        ARCHIVE DESTINATION lib/${ANDROID_ABI})

install(DIRECTORY include DESTINATION include)

```

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [June 24, 2022, 7:20pm UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/2 "2022-06-24T19:20:36Z")

</div>

The state of the art for realtime pitch shifting is PSOLA, which is an entirely time domain, granular-based approach.

Here’s some example code - this may not be production ready, it’s still under development - [Limes/analyzer.h at main · benthevining/Limes · GitHub](https://github.com/benthevining/Limes/blob/main/libs/limes_audio/psola/analyzer.h)

---

<div class="post-metadata">

**Author:** ![stenzel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stenzel/32/11737_2.png) [@stenzel](https://forum.juce.com/u/stenzel)\
**Post date:** [June 25, 2022, 8:07am UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/3 "2022-06-25T08:07:15Z")

</div>

PSOLA is good if a singular pitch exists, but for more complex signals frequency domain are superior.

---

<div class="post-metadata">

**Author:** ![reneeez](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@reneeez](https://forum.juce.com/u/reneeez)\
**Post date:** [June 25, 2022, 10:11am UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/4 "2022-06-25T10:11:08Z")

</div>

Thanks for the PSOLA suggestion! As @stenzel mentioned and from what I read about it it’s mainly focussed on speech pitch shifting.

In my app (a loopstation app) I intend to allow multiple audio effects (like pitch shifting, reverb, echo etc…) on self-recorded music. Therefor it seems PSOLA won’t be a good fit. If I misunderstand please let me know!

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [June 25, 2022, 3:30pm UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/5 "2022-06-25T15:30:44Z")

</div>

This is true. If you’re expecting polyphonic input, a resampling-based approach may be your best bet.

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [June 25, 2022, 10:10pm UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/6 "2022-06-25T22:10:34Z")

</div>

i wouldn’t worry about that too much. pitchshifting is creative work and these types of effects always come with compromises anyway. people would rather have a weird sound than weird limitations, i think

---

<div class="post-metadata">

**Author:** ![reneeez](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@reneeez](https://forum.juce.com/u/reneeez)\
**Post date:** [June 26, 2022, 10:13pm UTC](https://forum.juce.com/t/real-time-pitchshifting-performance/51939/7 "2022-06-26T22:13:36Z")

</div>

That’s true as well. I will check it out anyway, thanks for the responses!

In the mean time, if anyone knows about a high-performant frequency-domain pitchshifter (or any other tips) please let me know!
