How to simply the code to reduce elapsed time

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?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Feb. 2020

1 Stimme

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 Kommentare

steamrice
steamrice am 20 Feb. 2020
Thanks! I see! Would you mind giving me a little me directions on how to modfiy the code?
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.
Do you mean I should just use recordblocking() as a function and replace sound()?
I have look at Psychtoolbox. Is it possbile to use PsychPortAudio to replace what I have? I have looked at the BasicSoundInputDemo. It does not allow me to play the file I want.
steamrice
steamrice am 20 Feb. 2020
:(
steamrice
steamrice am 20 Feb. 2020
anyone?!
I do not always notice responses immediately. The notifications can take up to two hours to synchronize, and then I have to look for them, which I tend not to do when I am asleep or occupied with other tasks.
With the port audio tools you can use the usual MATLAB functions to read a file into memory, and then you can tell the tools to fill the buffer, and then you can issue a start, including a time in the future.
I have not seen very many people using the port audio so I am not as familiar with them as I am with the graphics part (I have not used those myself either but enough posts about the graphics have come through over the years that I understand some parts of those.)
It is more difficult to advise you about how to improve the code when you have deleted the code :(
steamrice
steamrice am 21 Feb. 2020
:(

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by