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