# TImestretch in realtime with FMOD\_DSP\_TYPE\_PITCHSHIFT

**URL:** <https://qa.fmod.com/t/timestretch-in-realtime-with-fmod-dsp-type-pitchshift/13731>\
**Category:** FMOD Engine\
**Created:** [March 1, 2018, 2:56pm UTC](https://qa.fmod.com/t/timestretch-in-realtime-with-fmod-dsp-type-pitchshift/13731 "2018-03-01T14:56:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkolesnik](https://avatars.discourse-cdn.com/v4/letter/p/858c86/32.png) [@pkolesnik](https://qa.fmod.com/u/pkolesnik)\
**Post date:** [March 1, 2018, 2:56pm UTC](https://qa.fmod.com/t/timestretch-in-realtime-with-fmod-dsp-type-pitchshift/13731/1 "2018-03-01T14:56:27Z")

</div>

Hello,

I would like to timestretch a sound in realtime with FMOD, using a combination of FMOD\_DSP\_TYPE\_PITCHSHIFT and changing channel frequency, as described in earlier posts. The stretch ratio would be adjusted in realtime based on user input.

The timestretch is working, and the overall quality is acceptable. However, at the time of every change of stretch ratio, there is a short audible audio glitch in the sound - it sounds like it goes slightly out of pitch for a moment.

I tried a number of things, such as:  
-pausing sound before calling the DSP parameter change and channel frequency change, then unpausing it right after  
-reducing the DSP buffer size  
-applying the DSP to Master channel group instead of individual channel  
-gradual ramp-up to the new stretch ratio

The audible glitch is still there, every time the speed of sound change gets called.

I would be very grateful for any ideas or suggestions. My last resort would be to start exploring third-party timestretch libraries, through DSP callback with FMOD – however, I wanted to check if I am not missing something obvious here, before going down that route.

## Extracts from code are below:

FMOD::System\* mpSystem;  
FMOD::DSP \*myDSP;  
FMOD::Channel \*mChannel;

//initialize system  
FMOD::System\_Create(&mpSystem);  
mpSystem-\>setDSPBufferSize(256, 8) );  
mpSystem-\>setSoftwareFormat(0, FMOD\_SPEAKERMODE\_STEREO, 0);  
mpSystem-\>init(32, FMOD\_INIT\_NORMAL, extraDriverData);

//Create sound  
mpSystem-\>createSound(strSoundName.c\_str(), FMOD\_2D | FMOD\_LOOP\_OFF | FMOD\_CREATESAMPLE, nullptr, &mSound);

//Play sound  
mpSystem-\>playSound(sgpImplementation-\>mSound, nullptr, true, &mChannel));  
if (mChannel)  
{  
mChannel-\>setVolume(1.0));  
mChannel-\>setPaused(false));  
}

…

//Set up the pitch shift dsp  
mpSystem-\>createDSPByType(FMOD\_DSP\_TYPE\_PITCHSHIFT, &myDSP));  
myDSP-\>setParameterFloat(0, 1); //set pitch to 1  
myDSP-\>setParameterFloat(1, 4096); // set FFT buffer size to 4096  
mChannel-\>setMode(FMOD\_2D);  
mChannel-\>addDSP(0, myDSP);

…

//Do the timestretch - stretchval in the range of 0.5 to 2.0  
mChannel-\>setFrequency(stretchVal \* 44100.0);  
myDSP-\>setParameterFloat(0, 1.0/stretchVal);

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 5, 2018, 12:43am UTC](https://qa.fmod.com/t/timestretch-in-realtime-with-fmod-dsp-type-pitchshift/13731/2 "2018-03-05T00:43:30Z")

</div>

This appears to be caused by the two calls, setFrequency and setParameterFloat, not occurring at the exact same time. To do this you can use System::lockDSP and System::unlockDSP around the set calls.

[https://fmod.com/resources/documentation-api?page=content/generated/FMOD\_System\_LockDSP.html#/](https://fmod.com/resources/documentation-api?page=content/generated/FMOD_System_LockDSP.html#/)
