0001 function [mid, ti] = note2mid(note, midTimeUnit, duration, plotOpt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 if nargin<1, selfdemo; return; end
0016 if nargin<2, midTimeUnit=1/16; end
0017 if nargin<3, duration=8; end
0018 if nargin<4, plotOpt=0; end
0019
0020 note=double(note);
0021 smtn=note(1:2:end);
0022 time=note(2:2:end)/64;
0023
0024 noteIndex=1;
0025 cumTime=time(noteIndex);
0026 mid=[];
0027 t=0;
0028 while t<duration
0029 t=t+midTimeUnit;
0030 if t>cumTime
0031 noteIndex=noteIndex+1;
0032 if noteIndex>length(smtn)
0033 break;
0034 end
0035 cumTime=cumTime+time(noteIndex);
0036 end
0037 mid=[mid, smtn(noteIndex)];
0038 end
0039 ti=midTimeUnit*(1:length(mid));
0040
0041 if plotOpt
0042 temp=mid;
0043 temp(temp==0)=nan;
0044 plot(ti, temp, 'g.');
0045 hold on
0046 notePlot(note);
0047 hold off
0048 end
0049
0050
0051 function selfdemo
0052
0053 note = [55 23 55 23 55 23 55 23 57 23 55 35 0 9 57 23 60 69 0 18 64 69 0 18 62 23 62 23 62 23 62 12 60 12 64 23 60 35 0 9 57 12 55 12 55 127];
0054 midTimeUnit=1/16;
0055 duration=9;
0056 mid = feval(mfilename, note, midTimeUnit, duration, 1);
0057 title(['Green dots is the output ', mfilename]);
0058 fprintf('按下任意鍵播放 note...\n'); pause
0059 notePlay(note, 1/64);
0060 fprintf('按下任意鍵播放 mid...\n'); pause
0061 pvPlay(mid, midTimeUnit);