annotate util/SMALL_midiGenerate.m @ 9:28f2b5fe3483

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