Reputation: 2069
I want to play a pure tone and record from the default microphone simultaneously using Matlab.
%% Main code
frequency = 500;
time = 5;
tic
parfor ii = 1:2
if ii==1
recording = test_record(time);
elseif ii==2
test_play(frequency,time);
end
end
toc
size(recording)
This all works fine except that recording variable is not available after the parfor loop ends and I need it for further analysis. I'd appreciate suggestions of how to make the recorded sample available after the parforloop or even better - I'll be happy to learn if a better design is preferable.
Thanks!
Here are the two additional functions used for running the above code :
%% Play the given frequency
function test_play(frequency,recording_time)
fs = 8000; % Samples per second
t = 0:(1/fs):recording_time;
y = sin(2*pi*frequency*t);
sound(y, fs);
end
%% Record from the microphone for given number of seconds
function recording = test_record(recording_time)
recObj = audiorecorder;
recordblocking(recObj, recording_time);
recording = getaudiodata(recObj);
end
Note - A related question's answer redirect to some program (Sox) that seem to do what I search for, however I want to write the Matlab code myself.
Upvotes: 1
Views: 910
Reputation: 36710
In your case, recording is a local variable which remains on the worker. This code creates a sliced cell array which should fix it:
%% Main code
frequency = 500;
time = 5;
recording=cell(2,1)
parfor ii = 1:2
if ii==1
recording{ii} = test_record(time);
elseif ii==2
test_play(frequency,time);
end
end
Obviously, your data is recording{1}
Upvotes: 2