comparison util/SMALL_midiGenerate.m @ 8:33850553b702

(none)
author idamnjanovic
date Mon, 22 Mar 2010 10:56:54 +0000
parents
children fc395272d53e
comparison
equal deleted inserted replaced
7:0151f1ea080d 8:33850553b702
1 function reconstructed=SMALL_midiGenerate(V, Problem)
2 %%% Reconstraction of midi file from representation in the given dictionary
3 % Ivan Damnjanovic 2009
4 %
5 % SMALL_midiGenerate is a part of SMALLbox and can be use to reconstruct
6 % a midi file given representation of the training set (V) in the
7 % dictionary Problem.A.
8 % Output is reconstructed structure with two fields:
9 % - reconstructed.notes - matrix with transcribed notes
10 % - reconstructed.midi - midi representation of transcription
11
12
13 U=Problem.A; % Dictionary used for representation
14 fs=Problem.fs; % Sampling rate
15 f=Problem.f; % vector of frequencies at wihch spectrogram is computed
16
17 ts=(Problem.windowSize*Problem.overlap)/fs; %size of an analysis frame in seconds
18
19 %%
20 % Components pitch estimation using modified SWIPE algorithm by Arthuro
21 % Camacho
22 %
23 % Columns of matrix U are spectrograms of the notes learned from the
24 % training set. We are estimating pitches of these notes by also
25 % restricting pitch values to the one of the 88 piano notes.
26
27 pitch=zeros(size(U,2),1);
28
29 for i=1:size(U,2)
30
31 pitch(i) = SMALL_swipe(U(:,i),fs, f, [27.50 8192], 1/12);
32
33 end
34
35 %%
36 % If some of columns of U have the same pitch, their contribution to the
37 % score (matrix V) is summed.
38
39 [Ps,idx]=sort(pitch);
40 ndp=1;
41 Pd(ndp)=Ps(1);
42 Vnew(ndp,:)=V(idx(1),:);
43 for i=2:88
44 if Ps(i)> Ps(i-1)
45
46 ndp=ndp+1;
47 Vnew(ndp,:)=V(idx(i),:);
48 Pd(ndp)=Ps(i);
49
50 else
51 Vnew(ndp,:)=Vnew(ndp,:)+V(idx(i),:);
52 end
53 end
54 %%
55 % Generate midi matrix
56
57 midx=0;
58 for i=1:ndp
59
60 % Threshold for finding onsets and offsets of notes
61
62 thr=mean(Vnew(i,:));%+std(Vnew(i,:));
63
64 if(Pd(i)~=0)
65 for j=1:size(Vnew,2)
66 if Vnew(i,j)<thr
67 Vnew(i,j)=0;
68 if(j>1)
69 if (Vnew(i,j-1)==1)
70 try
71 M(midx,6)=(j-1)*ts;
72 if (M(midx,6)-M(midx,5))<2*ts
73 midx=midx-1;
74 end
75 catch
76 pause;
77 end
78 end
79 end
80 else
81 Vnew(i,j)=1;
82 if(j>1)
83 if (Vnew(i,j-1)==0)
84 midx=midx+1;
85 M(midx,1)=1;
86 M(midx,2)=1;
87 M(midx,3)=69 +round( 12 *log2(Pd(i)/440));
88 M(midx,4)=80;
89 M(midx,5)=(j-1)*ts;
90 end
91 else
92 midx=midx+1;
93 M(midx,1)=1;
94 M(midx,2)=1;
95 M(midx,3)=69 + round(12 *log2(Pd(i)/440));
96 M(midx,4)=80;
97 M(midx,5)=0;
98 end
99 end
100 end
101 if M(midx,6)==0
102 M(midx,6)=(j-1)*ts;
103 end
104 end
105 end
106
107 M=sortrows(M,5);
108 reconstructed.notes=M;
109 reconstructed.midi = matrix2midi(M);