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!