mersenne31
mersenne31

Reputation: 51

Sound command 'playsnd' not executing because of longer loop time

This is my first post on StackOverflow so I apologize in advance if it's not the way it should be. What I'm trying to do: I run the while(time > 0) loop below and am checking for a button press, when the button is pressed a tone is played using the playsnd command in MATLAB which is in the "sound.m" function. "Sound.m" is called by playsound which is called in the while(value(1) == 1) loop which checks for a button press. My problem: When TIME is set 8s or higher, and when 50% of the time has elapsed, and the button is pressed, playsnd doesn't execute instantaneously! Weird part is that, for the first 50% it executes instantaneously when the button is pressed. And for TIME below 8s, playsnd executes instantaneously as well when the button is pressed! Another weird part is that the error only happens 80% of the time and it makes no logical sense to me at this moment. What should the loop time have to do with playsnd not executing?

Would really appreciate some of your wisdom on this problem :) Thanks in advance!

while(time > 0)
                value = getvalue(portCH);

                if(value(1) == 1)
                    display('Time refreshed');
                    time = TIME;
                    time_cumulative = time_cumulative +LOOP_TIME;
                    playsound(TONE);
                    while(value(1) ~= 0)
                    value = getvalue(portCH);
                    end
                end
                 time = time - LOOP_TIME;
end

Upvotes: 1

Views: 516

Answers (1)

Mikhail Poda
Mikhail Poda

Reputation: 5832

I suppose it is because of EDT (Java Event Dispatch Thread) which is responsible for IO - see this post. Just put drawnow() in your loop and MATLAB will make sure that all jobs in the IO queue will be executed befor proceeding.

Upvotes: 1

Related Questions