Playing audio in the background while running another function

조회 수: 13 (최근 30일)
Erik J
Erik J 2023년 5월 3일
댓글: Francis Tiong 2024년 9월 23일
I want to play audio using AudioFileReader and while that audio is playing, I want to run a separate loop that asks for user input. The loop asking for user input is not tied to the iterations of the loop playing the audio. Thus, the best way to do this seemed to try to run them in parallel using pareval, but I cannot get it to work.
The code to play audio:
function audioPlayback(filename)
% call audio device
playRec = audioPlayerRecorder(48000);
playRec.Device = 'MOTU Pro Audio';
% PRESENT
afr = dsp.AudioFileReader(filename);
adw = playRec;
while ~isDone(afr)
audio = afr();
adw(audio);
end
release(afr);
release(adw);
The code for the for loop:
function [score] = TestProgram(keywords, sentences)
for ii = 1:6
list = keywords(ii,:); % get keywords by stimuli
prompt = sentences(ii,:); % get full sentence
% prompt for selection of keywords
[indx, ~] = listdlg('PromptString', prompt,'ListString',list,'SelectionMode','multiple', 'ListSize',[300,100], 'CancelString','None');
% fill zeros for missed keywords
num_zeros = zeros(1,(5 - numel(indx)));
pad_indx = horzcat(indx, num_zeros);
% save words correct
scoreSave(ii,:) = pad_indx;
end
When I try to run in parallel, they run simultaneously:
fun1 = @audioPlayback;
fun2 = @ScoreQuickSIN;
playbackThread = parfeval(fun1, 0, audioList);
scoreSave_1 = parfeval(fun2, 0, keywords, sentences);
Am I doing something wrong? Is there a better way to do this?
I don't want to use audioplayer because the output sound level seems to depend on the output level I have set for my computer, which I don't want (e.g., I don't want changing the speaker volume on the computer to change the output level).

채택된 답변

Francis Tiong
Francis Tiong 2023년 5월 4일
The AudioFileReader and other audio interface functions cannot be run in the background. In addition, the user interface functions cannot be run in the background also. You can run the signal processing tasks or string processing tasks in the background. You can run the processing inside that audio loop and for each data frame push/check for processing going to and back from the other threads.
  댓글 수: 7
Michael Burke
Michael Burke 2024년 9월 23일
I wanted to do the same thing with audio recordings, I guess thats not possible here either.
Francis Tiong
Francis Tiong 2024년 9월 23일
In the latest version, 24b there is now a function called audioStreamer. This function now allows asynchronous operations with callbacks. So now you can (play or record) in the background while processing some other function in the foreground.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by