Running an AudioToolbox function in background
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone, i'm using Parallel Computing Toolbox, Deep Learing Toolbox and Audio Toolbox.
I have a main script which gives instructions and trajectories to a robotic arm: while the robot is working, i would like to stop him immediatly using my voice (recorded from my laptop microphone) like some kind of an emergency stop. To do that, i was considering using a script with a function that runs in background (while robot is working and the main script is proceeding) which always listen and detect if the word 'stop' is pronounced. Then if 'stop' is detected, the main script is stopped and the robotic arm is stopped too.
The following code represent the function that i'm trying to run in background, which uses a neural network and some commands from Audio Toolbox to detect and classify my voice. At the end, the function give YMode that contains the words that have been detected, including 'unknown' and 'background' (according to the words that neural network was trained for).
To run in background using the last script below, i've tried to use a backgroundPool with the function parfeval and fetchNext but it returns the following error:
Error using parallel.FevalFuture/fetchNext.
The function evaluation completed with an error.
Error using audioDeviceReader
The specified superclass 'matlab.system.SystemInterface' contains a parse error, cannot be found on
MATLAB's search path, or is shadowed by another file with the same name.
I dont' know how to solve the problem. Is it possible that some functions from Audio Toolbox can't be executed in background while the main script is proceeding? If this is the case, and in any other cases, how can i came up with the problem?
Thanks for your time.
function [YMode] = STOPGOdetect()
load ('ReteAllenata_stopgo_aug.mat') % This is the neural network
% I know that load command doesn't work in background, but
% i left it here for simplicity's sake
classificationRate = 20;
adr = audioDeviceReader('SampleRate',fs,'SamplesPerFrame',floor(fs/classificationRate));
audioBuffer = dsp.AsyncBuffer(fs);
labels = ReteAllenata_stopgo_aug.Layers(end).Classes;
YBuffer(1:classificationRate/2) = categorical("background");
probBuffer = zeros([numel(labels),classificationRate/2]);
countThreshold = ceil(classificationRate*0.2);
probThreshold = 0.75;
timeLimit = inf;
h = figure('Units','normalized','Position',[0.3 0.3 0.5 0.5]);
tic
while toc < timeLimit
x = adr();
write(audioBuffer,x);
y = read(audioBuffer,fs,fs-adr.SamplesPerFrame);
spec = helperExtractAuditoryFeatures(y,fs);
[YPredicted,probs] = classify(ReteAllenata_stopgo_aug,spec,'ExecutionEnvironment','cpu');
YBuffer = [YBuffer(2:end),YPredicted];
probBuffer = [probBuffer(:,2:end),probs(:)];
subplot(2,1,1)
plot(y)
axis tight
ylim([-1,1])
subplot(2,1,2)
pcolor(spec')
caxis([-4 2.6445])
shading flat
[YMode,count] = mode(YBuffer);
maxProb = max(probBuffer(labels == YMode,:));
subplot(2,1,1)
if YMode == "background" || count < countThreshold || maxProb < probThreshold
title(" ")
else
title(string(YMode),'FontSize',20)
end
drawnow
end
end
%%----------------------------------- script for background ----------------------------------------------
delete(gcp('nocreate'))
bp = backgroundPool;
disp ('backgroundpool created')
f = parfeval(bp, @STOPGOdetect, 1);
[index,out] = fetchNext(f);
1 个评论
Francis Tiong
2023-7-6
Audio device reader cannot be run in the background. Here is a related discussion:
https://www.mathworks.com/support/search.html/answers/1957579-playing-audio-in-the-background-while-running-another-function.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:audio/audio-io-and-waveform-generation&page=1
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Background Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!