How to simply the code to reduce elapsed time

3 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 20 Feb 2020
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 Commenti
Walter Roberson
Walter Roberson il 21 Feb 2020
It is more difficult to advise you about how to improve the code when you have deleted the code :(

Accedi per commentare.

Più risposte (0)

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by