How to simply the code to reduce elapsed time
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a script that is running (a for loop) : to get path of audio, saving the audio, play the audio(that is a function). However, I realized that using tic and toc the elapsed time is about 3 seconds for a trial to run. I am thinking that it is the play audio section that slow down the program. I am wondering is there a way to keep what I have but reduce the elapsed time?
0 个评论
采纳的回答
Walter Roberson
2020-2-20
Creating an audiorecorder() object is expensive. You should avoid doing that each cycle. Create the object outside the loop and pass it in to the functions. You can call recordblocking() on the same audiorecorder object multiple times.
You also have the challenge that when you sound() to play the sound, that will not be at all well synchronized with the recordblocking. sound() is non-blocking, but there would be a variable amount of time for sound() to start going, and variable amount of time for the function to return and for recordblocking() to start.
As timing is important for stimulus experiments, I would also recommend that you read all of the audio files in ahead of time and store them numerically, and then play the appropriate ones.
I would aslo generally recommend that you look at the Audio System Toolbox functions, and consider creating a single recorder object and a single player object that you activate as needed or send samples to as needed for the playing function.
If you have not done so already, I would suggest you look at the facilities provided by Psychtoolbox, http://psychtoolbox.org/
Psychtoolbox interfaces between Matlab or Octave and the computer hardware. The PTB core routines provide access to the display frame buffer and color lookup table, reliably synchronize with the vertical screen retrace, support sub-millisecond timing, expose raw OpenGL commands, support video playback and capture as well as low-latency audio, and facilitate the collection of observer responses.
7 个评论
Walter Roberson
2020-2-21
It is more difficult to advise you about how to improve the code when you have deleted the code :(
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Using audio files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!