Playing audio in the background while running another function
조회 수: 13 (최근 30일)
이전 댓글 표시
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).
댓글 수: 0
채택된 답변
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
2024년 9월 23일
I wanted to do the same thing with audio recordings, I guess thats not possible here either.
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 Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!