0001 function mlf=mlfRead(mlfFile, keepSil)
0002
0003
0004
0005
0006
0007
0008 if nargin<1, selfdemo; return; end
0009 if nargin<2, keepSil=0; end
0010
0011 content = textread(mlfFile, '%s', 'delimiter', '\n', 'whitespace', '');
0012 if length(content)<=3
0013 mlf.word=[];
0014 return;
0015 end
0016
0017 items=split(content{2}, '#');
0018 mlf.score=eval(items{2});
0019 content(1:2)=[];
0020 content(end)=[];
0021
0022 wordIndex=0;
0023 for i=1:length(content)
0024 line=content{i};
0025
0026 items=split(line, 9);
0027 sTime=eval(items{1});
0028 eTime=eval(items{2});
0029 phoneNameScore=items{3};
0030 temp=split(phoneNameScore, '#'); phoneName=temp{1}; phoneScore=eval(temp{2});
0031 logProb=eval(items{4});
0032 if length(items)>4
0033 wordNameScore=items{5};
0034 temp=split(wordNameScore, '#'); wordName=temp{1}; wordScore=eval(temp{2});
0035 wordIndex=wordIndex+1;
0036 word(wordIndex).name=wordName;
0037 word(wordIndex).score=wordScore;
0038 phoneIndex=1;
0039 end
0040 word(wordIndex).phone(phoneIndex).name=phoneName;
0041 word(wordIndex).phone(phoneIndex).score=phoneScore;
0042 word(wordIndex).phone(phoneIndex).time=[sTime; eTime];
0043 word(wordIndex).phone(phoneIndex).logProb=logProb;
0044 phoneIndex=phoneIndex+1;
0045 end
0046
0047
0048 for i=1:length(word)
0049 word(i).time=[word(i).phone(1).time(1); word(i).phone(end).time(2)];
0050 end
0051
0052
0053 if ~keepSil
0054 silIndex=[];
0055 for i=1:length(word)
0056 if strcmp(word(i).name, 'sil') | strcmp(word(i).name, 'sp')
0057 silIndex=[silIndex, i];
0058 end
0059 end
0060 word(silIndex)=[];
0061 end
0062
0063 if ~keepSil
0064 deleteIndex=[];
0065 for i=1:length(word);
0066 nameLen=length(word(i).phone(1).name);
0067 if strcmp(word(i).phone(1).name(1:min(nameLen,4)), 'sil+')
0068 if length(word(i).phone)>1
0069 word(i).time(1)=word(i).phone(2).time(1);
0070 word(i).phone(1)=[];
0071 else
0072 deleteIndex=[deleteIndex, i];
0073 end
0074 end
0075 end
0076 word(deleteIndex)=[];
0077 end
0078 mlf.word=word;
0079
0080
0081 function selfdemo
0082 wavFile='但使龍城飛將在.wav';
0083 mlfFile='但使龍城飛將在.mlf';
0084 mlf=feval(mfilename, mlfFile);
0085 [y, fs, nbits]=wavread(wavFile);
0086 for i=1:length(mlf.word)
0087 index=ceil(mlf.word(i).time/10000000*fs);
0088 fprintf('Hit return to play "%s"...\n', mlf.word(i).name); pause
0089
0090 sound(y(index(1):index(2)), fs);
0091 end