function shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
% shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
% 
% converts midi pitch to shift values in constant-Q vector.
% shifts for pitches that below the lowest or above the highest CQT
% frequency are set to a value of -1

numMidiPitches = length(midiPitches);
numFreqsCQT    = length(binFreqsCQT);

pitchF0s = midiPitch2Freq(midiPitches, tuningFreqInHz);
outOfBoundsPitches = (pitchF0s < binFreqsCQT(1) | pitchF0s > binFreqsCQT(end));
if any(outOfBoundsPitches)
    warning('F0s of some midi pitches lie outside the constant-Q spectrogram frequency range');
end
   
freqRatios = repmat(binFreqsCQT, 1, numMidiPitches) ./ repmat(pitchF0s', numFreqsCQT, 1);
freqRatios(freqRatios < 1) = 1 ./ freqRatios(freqRatios < 1);

[dummy shifts] = min(abs(freqRatios), [], 1);
shifts = shifts - 1; % 'no shift' is defined as 0
shifts(outOfBoundsPitches) = -1;
shifts = shifts';