How can we start different sessions simultaneously?
Show older comments
Dear all,
I want to play two different stereo audio (one using my internal laptop audiocard, and the other uaing an external audiocard).
I defined two sessions:
d=daq.getDevices;
dev=d(6);
dev2=d(5);
s1 = daq.createSession('directsound');
noutchan1 = 2;
addAudioOutputChannel(s1, dev.ID, 1:noutchan1);
s2=daq.createSession('directsound');
noutchan2=2;
addAudioOutputChannel(s2, dev2.ID, 1:noutchan2);
after defining my stereo sound like ([audio1L,audio1R] and [audio2L,audio2R]) I run both of them in foreground like below:
queueOutputData(s1,[audio1L,audio1R]);
queueOutputData(s2,[audio2L,audio2R]);
startForeground(s1);
startForeground(s2);
when I run both of them using "startForeground", the first session plays primarily and then the second sound plays afterward.
Is there a way to start two different sessions simultaneously?
Thanks
Answers (2)
PIYUSH AGGARWAL
on 17 Jun 2019
0 votes
try enclosing the section of the code wfere you are running the foreground in a parfor (Parallel For loop) loop.This automatically assigns workers to your code & runs the section enclosed in the parfor parallely.
You can further refer to the documentation page & see examples.
4 Comments
Mehrdad Bahadori
on 18 Jun 2019
Edited: Mehrdad Bahadori
on 18 Jun 2019
PIYUSH AGGARWAL
on 18 Jun 2019
Hi Mehrdad, In the above code provided by you I don't see any difference in the statement getting executed inside the parfor, I mean there's no difference. So first of all I think it should be like this :-
And as far as the error message you got is concerned, That is probably because somewhere in your code you are trying to access any field of an object which is not a valid struct. Just try debugging using the debugger tool of editor & see the root cause of that problem.
If anything moore, do reach out. We at MathWorks are Happy to help!
queueOutputData(s1,[channel1,channel2]);
queueOutputData(s2,[channel3,channel4]);
startForeground(s1);
startForeground(s2);
parfor k=1:2
if k ==1
startForeground(s1);
else
startForeground(s2);
end
end
Mehrdad Bahadori
on 18 Jun 2019
PIYUSH AGGARWAL
on 19 Jun 2019
Hi Mehrdad,
Then I guess you need to share the entire code with me so that I can help you in debugging it. Have you tried debugging at your end? If not, just try to do it by adding breakpoints in the code & see which exact line is causing the issue. I'm positive that it has to do with something related to invalid dot accessing of some illegal data type. Also please share the error message.
Regards,
Piyush
Walter Roberson
on 19 Jun 2019
0 votes
"Is there a way to start two different sessions simultaneously?"
No. And parfor or spmd will not help for this purpose. Starting a session takes a number of software steps and communications with hardware, so even if you succesfully issued two startForeground at the exact same time, the data recording would probably not start at the same time.
There are two approaches that can be followed:
- Do not bother being overly concerned about starting two sessions at the same time. But do use recording mechanisms that record timestamps for the samples, and then have your MATLAB code synchronize the streams according to the timestamps; or
- Do not bother being overly concerned about starting two sessions at the same time. However, configure a common trigger (probably a hardware trigger) for both of them so that even though the sessions might not start at the same time, the hardware starts recording data at the same time.
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!