function txt = Voice2Txt(Time,varargin)
%语音 短语音识别
Fs =16000;
nBits = 16 ;
nChannels = 2 ;
switch nargin
case 1
recObj = audiorecorder(Fs,nBits,nChannels);
disp('Start speaking.')
recordblocking(recObj, Time);
disp('End of Recording.');
Voice = getaudiodata(recObj);
case 2
[filename,pathname] = uigetfile({'*.wav';'*.mp3';'*.flac';'*.*'},...
'File Selector');
[y,Fs] = audioread([pathname filename]);
[P,Q] = rat(16000/Fs);
Voice = resample(y,P,Q);
end
global url_use
if isempty(url_use)==1
api_id ='n6xinle0ggjqo2s3pGh4xvx'; % Replace the api key
secret_key = 'Gx45o66V11mpsyns2si5yNu9OLdftwg'; % Replace the secret key
url_use = 'http://24.81e62f9169e9bd60c1d48ce193b4e971.2592000.1636640373.282335-24966080';
end
url= 'http://vop.baidu.com/server_api';
wavFilename = 'TemporalFile.wav';
audiowrite(wavFilename,Voice(:,1),Fs);
fid = fopen(wavFilename,'rb');
bytes = fread(fid);
fclose(fid);
base64string_len = size(bytes,1);
base64string = matlab.net.base64encode(bytes);
options = weboptions('RequestMethod', 'post','HeaderFields',{ 'Content-Type','application/json'});
options.Timeout =20;
m = struct;
m.format = 'wav';%Format
m.lan = 'zh';%Language
m.token = url_use;
m.len = base64string_len;
m.rate = 16000;
m.speech = base64string;
m.cuid = '2a:c6:3f:c419:44';
m.channel = 1;
Content = webwrite(url,m,options);
if isfield(Content,'result')
txt = Content.result{:};
else
txt = '';
end
if contains(txt,'关闭程序')
exit
错误提示:
未定义变量 "matlab" 或类 "matlab.net.base64encode"。
出错 Voice2Txt (line 34)
base64string = matlab.net.base64encode(bytes);