Welcome! Log In Create A New Profile

Advanced

mgladdsound but save ID to play later

Posted by lyt4 
mgladdsound but save ID to play later
December 20, 2019 02:30PM
Hi,
I am writing an adaptor and would like to load sounds when creating the object and save the object ID, so that I can play them later, during analyze(). However when I create the object and call obj.SoundID = mgladdsound(filename) it immediately plays. Moreover, mglplaysound(obj.SoundID) and mglactivatesound(obj.SoundID, true) both don't produce the sound. As a workaround I decided to call mgladdsound(filename) every frame in which that is desired - but maybe that is not the best strategy? Would appreciate any advice.
Thanks!
Re: mgladdsound but save ID to play later
December 20, 2019 03:54PM
Have you looked at AudioSound, if it suits for your purpose?
https://monkeylogic.nimh.nih.gov/docs_RuntimeFunctions.html#AudioSound

You don't see mglplaysound or mglpresent used in any adapter. It is because you should let NIMH ML handle stimuli to keep precise timing. You can create sounds, but you shouldn't play them yourself (unless you do it on purpose).

MGL objects are active, by default, when they are created. You should deactivate them as soon as you create so that they are not presented.
mglsetproperty(id,'active',false);

When you want to present them in adapter codes, what you need to do is just to activate them. Then they will be presented next time when the screen is updated. For sounds, NIMH ML deactivates them automatically once they start playing so that they may not restart in the next frame.

You must destroy every MGL object you create. Otherwise, MATLAB will crash eventually due to memory leaks.
Re: mgladdsound but save ID to play later
December 21, 2019 11:31PM
Hi Jaewon,
that works beautifully - thank you!
Lucas

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.