Home > melodyRecognition > note2mid.m

note2mid

PURPOSE ^

note2mid: note to mid conversion

SYNOPSIS ^

function [mid, ti] = note2mid(note, midTimeUnit, duration, plotOpt)

DESCRIPTION ^

note2mid: note to mid conversion
    Usage:
    [mid, ti] = note2mid(note, midTimeUnit, duration, plotOpt)
        note: 格式是[音高, 音長, 音高, 音長, ...],其中音長的時間單位是 1/64 秒
        midTimeUnit: 音高向量的每點所代表時間
        duration:音高向量的總長度,以秒為單位(若是 inf,代表取整首)
        mid: 音高向量
        ti:對應於音高向量的時間點

    Type note2mid for a self demo.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [mid, ti] = note2mid(note, midTimeUnit, duration, plotOpt)
0002 %note2mid: note to mid conversion
0003 %    Usage:
0004 %    [mid, ti] = note2mid(note, midTimeUnit, duration, plotOpt)
0005 %        note: 格式是[音高, 音長, 音高, 音長, ...],其中音長的時間單位是 1/64 秒
0006 %        midTimeUnit: 音高向量的每點所代表時間
0007 %        duration:音高向量的總長度,以秒為單位(若是 inf,代表取整首)
0008 %        mid: 音高向量
0009 %        ti:對應於音高向量的時間點
0010 %
0011 %    Type note2mid for a self demo.
0012 
0013 %    Roger Jang, 20040612
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);        % Semitone
0022 time=note(2:2:end)/64;        % Time duration, in the unit of 1/64 second
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 % ====== Self demo
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);

Generated on Tue 01-Jun-2010 09:49:37 by m2html © 2003