Home > asr > cmRead.m

cmRead

PURPOSE ^

cmRead: Read the CM (confidence measure) file generated from Viterbi forced alignment or from human labeling

SYNOPSIS ^

function cmObj=cmRead(cmFile, deleteSil)

DESCRIPTION ^

 cmRead: Read the CM (confidence measure) file generated from Viterbi forced alignment or from human labeling
    Usage: cmObj=cmRead(cmFile, deleteSil)

    For example:
        wavFile='但使龍城飛將在.wav';
        cmFile='但使龍城飛將在.cm';
        deleteSil=0;
        cmObj=cmRead(cmFile, deleteSil);
        word=cmObj.word;
        [y, fs, nbits]=wavread(wavFile);
        for i=1:length(word)
            index=round(word(i).time/10000000*fs);            % Time unit is 100 ns (= 1/10000000 sec)
            index(index==0)=1; index(index>length(y))=length(y);    % Correct bounds if necessary
            fprintf('Hit return to play "%s" (text=%s, score=%f)...\n', word(i).name, word(i).text, word(i).score); pause
            sound(y(index(1):index(2)), fs);
        end

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function cmObj=cmRead(cmFile, deleteSil)
0002 % cmRead: Read the CM (confidence measure) file generated from Viterbi forced alignment or from human labeling
0003 %    Usage: cmObj=cmRead(cmFile, deleteSil)
0004 %
0005 %    For example:
0006 %        wavFile='但使龍城飛將在.wav';
0007 %        cmFile='但使龍城飛將在.cm';
0008 %        deleteSil=0;
0009 %        cmObj=cmRead(cmFile, deleteSil);
0010 %        word=cmObj.word;
0011 %        [y, fs, nbits]=wavread(wavFile);
0012 %        for i=1:length(word)
0013 %            index=round(word(i).time/10000000*fs);            % Time unit is 100 ns (= 1/10000000 sec)
0014 %            index(index==0)=1; index(index>length(y))=length(y);    % Correct bounds if necessary
0015 %            fprintf('Hit return to play "%s" (text=%s, score=%f)...\n', word(i).name, word(i).text, word(i).score); pause
0016 %            sound(y(index(1):index(2)), fs);
0017 %        end
0018 
0019 %    Roger Jang, 20070117
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');    % 讀入第一列:「% score=83.1444」
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');    % 1900000    2700000    d+a#82.13    -1537.86    dan#但#66.3123#110_147_127_149_804_3597_6101_5870_5912_6556_6237_5417_5040_5174_4792_4728...
0037     items=split(phoneNameScore{1}, '#'); phoneName=items{1}; phoneScore=eval(items{2});
0038     if ~isempty(wordNameScore)    % Found a new word
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;        % A new phone
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 % Record the duration of each word
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 % Get rid of 'sil' and 'sp' (for English)
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 % Get rid of 'sil+*' (for Chinese that uses CGU pinyin)
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    % 刪除這個 phone
0080                 word(i).time(1)=word(i).phone(2).time(1);    % 修正 word 的時間
0081                 word(i).phone(1)=[];
0082             else    % 只有一個 phone,紀錄 index,準備砍 word
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);            % Time unit is 100 ns (= 1/10000000 sec)
0101     index(index==0)=1; index(index>length(y))=length(y);    % Correct bounds if necessary
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

Generated on Tue 01-Jun-2010 09:50:19 by m2html © 2003