0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 function [cKey] = ComputeKey (x, f_s, afWindow, iBlockLength, iHopLength)
0012
0013
0014 if (nargin < 5)
0015 iHopLength = 2048;
0016 end
0017 if (nargin < 4)
0018 iBlockLength = 4096;
0019 end
0020
0021 if (nargin < 3 || isempty(afWindow))
0022 afWindow = hann(iBlockLength, 'periodic');
0023 end
0024
0025
0026 cMajor = char ('C Maj','C# Maj','D Maj','D# Maj','E Maj','F Maj',...
0027 'F# Maj','G Maj','G# Maj','A Maj','A# Maj','B Maj');
0028 cMinor = char ('c min','c# min','d min','d# min','e min','f min',...
0029 'f# min','g min','g# min','a min','a# min','b min');
0030
0031
0032 t_pc = [6.35 2.23 3.48 2.33 4.38 4.09 2.52 5.19 2.39 3.66 2.29 2.88
0033 6.33 2.68 3.52 5.38 2.60 3.53 2.54 4.75 3.98 2.69 3.34 3.17];
0034 t_pc = diag(1 ./ sum(t_pc, 2)) * t_pc;
0035
0036
0037 if (length(afWindow) ~= iBlockLength)
0038 error('window length mismatch');
0039 end
0040
0041
0042 [v_pc, t] = computeFeature("SpectralPitchChroma", x, f_s, afWindow, iBlockLength, iHopLength);
0043
0044
0045 v_pc = mean(v_pc, 2);
0046
0047
0048 d = zeros(2,12);
0049 for (i = 0:11)
0050 d(:,i+1)= sum(abs(repmat(v_pc', 2, 1)-circshift(t_pc, [0 i])), 2);
0051 end
0052 [dist,iKeyIdx] = min(d,[],2);
0053 if (dist(1) < dist(2))
0054 cKey = deblank(cMajor(iKeyIdx(1), :));
0055 else
0056 cKey = deblank(cMinor(iKeyIdx(2), :));
0057 end
0058 end