%% Apply Pitch-Shifting to Frequency-Domain Audio
% Read in an audio file and listen to it.

[audioIn,fs] = audioread("SpeechDFT-16-8-mono-5secs.wav");
sound(audioIn,fs)

%
% Convert the audio signal to a time-frequency representation using stft. Use a 512-point kbdwin with 75% overlap.

win = kbdwin(512);
overlapLength = 0.75*numel(win);

S = stft(audioIn, ...
    "Window",win, ...
    "OverlapLength",overlapLength, ...
    "Centered",false);

%
% Increase the pitch by 8 semitones and listen to the result. Specify the window and overlap length you used to compute the STFT.

nsemitones = 8;
lockPhase = false;
audioOut = shiftPitch(S,nsemitones, ...
                     "Window",win, ...
                     "OverlapLength",overlapLength, ...
                     "LockPhase",lockPhase);

sound(audioOut,fs)

%
% Decrease the pitch of the original audio by 8 semitones and listen to the result. Specify the window and overlap length you used to compute the STFT.

nsemitones = -8;
lockPhase = false;
audioOut = shiftPitch(S,nsemitones, ...
                     "Window",win, ...
                     "OverlapLength",overlapLength, ...
                     "LockPhase",lockPhase);

sound(audioOut,fs)

% Copyright 2019 The MathWorks, Inc.