How to simply the code to reduce elapsed time

2 ビュー (過去 30 日間)
steamrice
steamrice 2020 年 2 月 20 日
コメント済み: steamrice 2020 年 2 月 21 日
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?

採用された回答

Walter Roberson
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
Walter Roberson 2020 年 2 月 21 日
It is more difficult to advise you about how to improve the code when you have deleted the code :(
steamrice
steamrice 2020 年 2 月 21 日
:(

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeUsing audio files についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by