使用matlab自带的VAD以及SCOPE函数进行伪实时的VAD判断,为什么说是伪实时的呢?因为本程序实际上还是使用录音-处理-录音-处理的节奏进行VAD的处理,为了平衡VAD的耗时以及录音时长的冲突,将时间块分的很大(1s),也就是说在更新VAD结果的频率上选择的是不高于1Hz。
废话不多说,实现看以下代码:
clc
clear all
close all
time = 10;
timeDiv = time / 10 * 0.5 ;
timeDiv = max(timeDiv,1);
audioRecorder = audiorecorder( 8000, 16 ,1);
theSound = zeros(7937,time);
theSoundFinal = zeros(1,7936*time)';
dataStart = 1;
dataStop = 0;
timeCost = 0;
%% 对示波器进行初始化
scope = dsp.TimeScope(2,...
'SampleRate', [8000/80 8000], ...
'BufferLength', time * 8000, ...
'YLimits', [-0.3 1.1], ...
'TimeSpan',time,...
'ShowGrid', true, ...
'Title','Decision speech and speech data', ...
'TimeSpanOverrunAction','Scroll',...
'ReduceUpdates',false);
%% 对VAD的配置进行初始化
VAD_cst_param = vadInitCstParams;
%% 开始录音并处理数据
for recordTime = 1:ceil(time/timeDiv)
% 获取当前录音机的状态,如果是关闭的则打开,如果已经打开就直接进行下一步
ifRunning = get(audioRecorder,'Running');
status = strncmp(ifRunning,'on',2);
if(status == 0)
record(audioRecorder);
tic;
end
timeCost = toc;
timeCost = min(timeCost,timeDiv);
pause(timeDiv - timeCost)
stop(audioRecorder);
audioDiv = getaudiodata(audioRecorder)/2;
record(audioRecorder); % 重新打开录音机,并开始计时
tic;
audioSource = dsp.SignalSource(audioDiv,80);
dataLen = length(audioDiv);
numTSteps =ceil( dataLen/8000 * 100);
while(numTSteps)
% 从audioSource()中提取10ms的数据
speech = audioSource();
% 调用VAD算法
decision = vadG729(speech, VAD_cst_param);
% 绘制音频数据波形以及VAD判断结果
scope(decision, speech);
numTSteps = numTSteps - 1;
end
dataStop = dataStop + dataLen;
theSoundFinal(dataStart:dataStop,1) = audioDiv;
dataStart = dataStop + 1;
recordTime;
end
plot(theSoundFinal(:,1));
ylim([-1 1]);
sound(theSoundFinal,8000)
pause(time);
运行结果如下: