Mercurial > hg > tipic
comparison matlab/MATLAB-Chroma-Toolbox_2.0/audio_to_pitch_via_FB.m @ 0:b54ee0a0be67
Import MATLAB Chroma Toolbox
author | Chris Cannam |
---|---|
date | Wed, 05 Aug 2015 21:08:56 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b54ee0a0be67 |
---|---|
1 function [f_pitch,sideinfo] = audio_to_pitch_via_FB(f_audio,parameter,sideinfo) | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % Name: audio_to_pitch_via_FB | |
4 % Date of Revision: 2011-03 | |
5 % Programmer: Meinard Mueller, Sebastian Ewert | |
6 % | |
7 % Description: | |
8 % Computing and saving of pitch features via a pre-designed filterbank. | |
9 % features. For each window length specified via parameter.winLenSTMSP | |
10 % the following is computed: | |
11 % - STMSP (short-time mean-square power) for each MIDI pitch between | |
12 % parameter.midiMin and parameter.midiMax | |
13 % - STMSP subbands are stored in f_pitch, where f_pitch(p,:) contains | |
14 % STMSP of subband of pitch p | |
15 % - sideinfo contains information of original pcm, which is saved along | |
16 % with f_pitch into a single mat-file | |
17 % - Information f_pitch and sideinfo is stored in mat-file: | |
18 % save(strcat(parameter.saveDir,parameter.saveFilename),'f_pitch','sideinfo'); | |
19 % | |
20 % Input: | |
21 % f_audio | |
22 % parameter.winLenSTMSP = 4410; | |
23 % parameter.shiftFB = 0; | |
24 % parameter.midiMin = 21; | |
25 % parameter.midiMax = 108; | |
26 % parameter.save = 0; | |
27 % parameter.saveDir = ''; | |
28 % parameter.saveFilename = ''; | |
29 % parameter.saveAsTuned = 0; | |
30 % parameter.fs = 22050; | |
31 % parameter.visualize = 0; | |
32 % | |
33 % Required files: | |
34 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25.mat' | |
35 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25_minusHalf.mat' | |
36 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25_minusQuarter.mat' | |
37 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25_minusThird.mat' | |
38 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25_minusThreeQuarters.mat' | |
39 % 'MIDI_FB_ellip_pitch_60_96_22050_Q25_minusTwoThird.mat' | |
40 % | |
41 % Output: | |
42 % f_pitch | |
43 % sideinfo | |
44 % | |
45 % | |
46 % License: | |
47 % This file is part of 'Chroma Toolbox'. | |
48 % | |
49 % 'Chroma Toolbox' is free software: you can redistribute it and/or modify | |
50 % it under the terms of the GNU General Public License as published by | |
51 % the Free Software Foundation, either version 2 of the License, or | |
52 % (at your option) any later version. | |
53 % | |
54 % 'Chroma Toolbox' is distributed in the hope that it will be useful, | |
55 % but WITHOUT ANY WARRANTY; without even the implied warranty of | |
56 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
57 % GNU General Public License for more details. | |
58 % | |
59 % You should have received a copy of the GNU General Public License | |
60 % along with 'Chroma Toolbox'. If not, see <http://www.gnu.org/licenses/>. | |
61 % | |
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
63 | |
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
65 % Check parameters | |
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
67 | |
68 if nargin<3 | |
69 sideinfo=[]; | |
70 end | |
71 | |
72 if nargin<2 | |
73 parameter=[]; | |
74 end | |
75 if isfield(parameter,'visualize')==0 | |
76 parameter.visualize = 0; | |
77 end | |
78 if isfield(parameter,'save')==0 | |
79 parameter.save = 0; | |
80 end | |
81 if isfield(parameter,'saveDir')==0 | |
82 parameter.saveDir = ''; | |
83 end | |
84 if isfield(parameter,'saveFilename')==0 | |
85 parameter.saveFilename = ''; | |
86 end | |
87 if isfield(parameter,'saveAsTuned')==0 | |
88 parameter.saveAsTuned = 0; | |
89 end | |
90 if isfield(parameter,'fs')==0 | |
91 parameter.fs = 22050; | |
92 else | |
93 if parameter.fs ~= 22050 | |
94 error('audio_to_pitch_via_FB not implemented yet for sample rates other than 22050.'); | |
95 end | |
96 end | |
97 if isfield(parameter,'midiMin')==0 | |
98 parameter.midiMin = 21; | |
99 end | |
100 if isfield(parameter,'midiMax')==0 | |
101 parameter.midiMax = 108; | |
102 end | |
103 if isfield(parameter,'winLenSTMSP')==0 | |
104 parameter.winLenSTMSP = 4410; | |
105 %parameter.winLenSTMSP = [882 4410]; | |
106 end | |
107 if isfield(parameter,'shiftFB')==0 | |
108 parameter.shiftFB = 0; | |
109 end | |
110 | |
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
112 % Main program | |
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
114 | |
115 if parameter.shiftFB == 0 | |
116 load MIDI_FB_ellip_pitch_60_96_22050_Q25.mat | |
117 elseif parameter.shiftFB == 1 | |
118 load MIDI_FB_ellip_pitch_60_96_22050_Q25_minusQuarter.mat | |
119 elseif parameter.shiftFB == 2 | |
120 load MIDI_FB_ellip_pitch_60_96_22050_Q25_minusThird.mat | |
121 elseif parameter.shiftFB == 3 | |
122 load MIDI_FB_ellip_pitch_60_96_22050_Q25_minusHalf.mat | |
123 elseif parameter.shiftFB == 4 | |
124 load MIDI_FB_ellip_pitch_60_96_22050_Q25_minusTwoThird.mat | |
125 elseif parameter.shiftFB == 5 | |
126 load MIDI_FB_ellip_pitch_60_96_22050_Q25_minusThreeQuarters.mat | |
127 else | |
128 error('Wrong shift parameter!') | |
129 end | |
130 | |
131 fs_pitch = zeros(1,128); | |
132 fs_index = zeros(1,128); | |
133 | |
134 fs_pitch(21:59) = 882; | |
135 fs_pitch(60:95) = 4410; | |
136 fs_pitch(96:120) = 22050; | |
137 | |
138 fs_index(21:59) = 3; | |
139 fs_index(60:95) = 2; | |
140 fs_index(96:120) = 1; | |
141 | |
142 pcm_ds = cell(3,1); | |
143 pcm_ds{1} = f_audio; | |
144 pcm_ds{2} = resample(pcm_ds{1},1,5,100); | |
145 pcm_ds{3} = resample(pcm_ds{2},1,5,100); | |
146 | |
147 fprintf('Computing subbands and STMSP for all pitches: (%i-%i): %4i',parameter.midiMin,parameter.midiMax,0); | |
148 | |
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
150 % Compute features for all pitches | |
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
152 | |
153 winLenSTMSP = parameter.winLenSTMSP; | |
154 winOvSTMSP = round(winLenSTMSP/2); | |
155 featureRate = parameter.fs./(winLenSTMSP-winOvSTMSP); %formerly win_res | |
156 wav_size = size(f_audio,1); | |
157 | |
158 num_window = length(winLenSTMSP); | |
159 f_pitch_energy = cell(num_window,1); | |
160 seg_pcm_num = cell(num_window,1); | |
161 seg_pcm_start = cell(num_window,1); | |
162 seg_pcm_stop = cell(num_window,1); | |
163 for w=1:num_window; | |
164 step_size = winLenSTMSP(w)-winOvSTMSP(w); | |
165 group_delay = round(winLenSTMSP(w)/2); | |
166 seg_pcm_start{w} = [1 1:step_size:wav_size]'; %group delay is adjusted | |
167 seg_pcm_stop{w} = min(seg_pcm_start{w}+winLenSTMSP(w),wav_size); | |
168 seg_pcm_stop{w}(1) = min(group_delay,wav_size); | |
169 seg_pcm_num{w} = size(seg_pcm_start{w},1); | |
170 f_pitch_energy{w} = zeros(120,seg_pcm_num{w}); | |
171 end | |
172 | |
173 | |
174 for p=parameter.midiMin:parameter.midiMax | |
175 fprintf('\b\b\b\b');fprintf('%4i',p); | |
176 index = fs_index(p); | |
177 f_filtfilt = filtfilt(h(p).b, h(p).a, pcm_ds{index}); | |
178 f_square = f_filtfilt.^2; | |
179 | |
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
181 % f_pitch_energy | |
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
183 for w=1:length(winLenSTMSP) | |
184 factor = (parameter.fs/fs_pitch(p)); %adjustment for sampling rate | |
185 for k=1:seg_pcm_num{w} | |
186 start = ceil((seg_pcm_start{w}(k)/parameter.fs)*fs_pitch(p)); | |
187 stop = floor((seg_pcm_stop{w}(k)/parameter.fs)*fs_pitch(p)); | |
188 f_pitch_energy{w}(p,k)=sum(f_square(start:stop))*factor; | |
189 end | |
190 end | |
191 end | |
192 fprintf('\n'); | |
193 | |
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
195 % Save f_pitch_energy for each window size separately as f_pitch | |
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
197 sideinfo.pitch.version = 1; | |
198 sideinfo.pitch.midiMin = parameter.midiMin; | |
199 sideinfo.pitch.midiMax = parameter.midiMax; | |
200 if parameter.save == 1 | |
201 for w=1:num_window; | |
202 f_pitch = f_pitch_energy{w}; | |
203 sideinfo.pitch.winLenSTMSP = winLenSTMSP(w); | |
204 sideinfo.pitch.winOvSTMSP = winOvSTMSP(w); | |
205 sideinfo.pitch.featureRate = featureRate(w); | |
206 sideinfo.pitch.shiftFB = parameter.shiftFB; | |
207 sideinfo.pitch.featuresAreTuned = 0; | |
208 if parameter.saveAsTuned | |
209 sideinfo.pitch.featuresAreTuned = 1; | |
210 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w))); | |
211 else | |
212 switch(parameter.shiftFB) | |
213 case 0 | |
214 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w))); | |
215 case 1 | |
216 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w)),'_minusQuarter'); | |
217 case 2 | |
218 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w)),'_minusThird'); | |
219 case 3 | |
220 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w)),'_minusHalf'); | |
221 case 4 | |
222 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w)),'_minusTwoThird'); | |
223 case 5 | |
224 filename = strcat(parameter.saveFilename,'_pitch_',num2str(winLenSTMSP(w)),'_minusThreeQuarter'); | |
225 end | |
226 end | |
227 save(strcat(parameter.saveDir,filename),'f_pitch','sideinfo'); | |
228 end | |
229 else | |
230 f_pitch = f_pitch_energy{num_window}; | |
231 sideinfo.pitch.winLenSTMSP = winLenSTMSP(num_window); | |
232 sideinfo.pitch.winOvSTMSP = winOvSTMSP(num_window); | |
233 sideinfo.pitch.featureRate = featureRate(num_window); | |
234 sideinfo.pitch.shiftFB = parameter.shiftFB; | |
235 end | |
236 | |
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
238 % Visualization | |
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
240 if parameter.visualize == 1 | |
241 for w=1:num_window; | |
242 parameterVis.featureRate = featureRate(w); | |
243 visualizePitch(f_pitch_energy{w},parameterVis); | |
244 end | |
245 end | |
246 | |
247 end | |
248 | |
249 |