running two processes concurrently and kbcheck problems

Hi
My problem is that the I need the code to take care of two processes concurrently:
1. create beeps sounds in different frequency
2. saving subject responses (key board presses)

So far i tried pulling the subject responses using kbcheck,
but kbcheck response is longer then the gap between two beeps....

%%%%%%%%%
while (k <= NumBeeps)
PsychPortAudio('Start', pahandle,1,0,0);
beepList = [beepList (GetSecs - start)];
CycleStart = GetSecs;
while ((GetSecs - CycleStart) < Period)
[keyIsDown,secs,keyCode] = KbCheck;

while ((GetSecs - CycleStart) < Period)
if keyIsDown
RT = [RT (GetSecs - start)];
while KbCheck && ((GetSecs - CycleStart) < Period) ; end %%% this part lasts too long, and each key press recorder more then once....
end
end
end
k = k +1;
end


%%%%%%%%%%%
and even if ill maximize this, i don't thing it is the ideal way of running concordant processes.
I was thinking of using a callback/ interrupt mechanism .

but i cant find a way to create an on screen/ window object.
do you have any idea how can I do that?

Thank you so much
I have no clue what you mean with "inconsistent dleay while the pahandle is on, between the beeps".

But you would expect some small delay between trials - a pause while audio playback is stopped. Intermixing calls to sound() with calls to PsychPortAudio() sounds like begging for trouble. Find out about proper use of the screen('flip') and psychportaudio('start') timestamps if you want to measure RT's (with respect to what? sound? visuals? which ones? or sound and visual onset sync'ed?) the current code will not measure rt wrt. to either visual or sound onset, cfe. PsychPortAudioTimingTest for sync'ed visual and audio onset and use of timestamps.

If you use psychportaudio('stop') to wait for sound to stop at the end of a trial, you can get rid of the weird if s.Active code and move all the 'addtoschedule' calls to the beginning of a trial. Finally the ...Period(iTrialNum)-beepWavDuration... part looks suspicious to me, like a good way to mess up timing if you don't really know what you're doing. Other than that, your use of sound schedules is correct.

I think you never explained your exact paradigm, so nobody can guess from the code what its intended behaviour was or what is going wrong.

Timing of sound and visuals can be sub-millisecond accurate if done right, with the right hardware and OS (Either OS/X or Linux or Window with a ASIO sound card -- timing with non-ASIO cards on Windows is just horrible).

-mario

--- In psychtoolbox@yahoogroups.com, lilac Adato <lilacadato@...> wrote:
>
> Thank you so much!
> I tried and tried to make it work, and by now,
> there is some kind of an inconsistent delay while the pahandle is on,
> between the beeps.
>
> why is it happening? how accurate it is? How can i produce bees exactly in
> the wanted rhythm?
>
>
>
> j=1;
> for i = 1:NumBeepsMaxVal
> buffer(j) = PsychPortAudio('CreateBuffer', [], audiodata);
> buffer(j+1) = PsychPortAudio('CreateBuffer', [],
> audiodatas);
> j=j+2;
> end
>
> pahandle = PsychPortAudio('Open', [], [], 1, infreq,
> nrchannels);
> runMode = 1;
> PsychPortAudio('RunMode', pahandle, runMode);
>
> PsychPortAudio('UseSchedule', pahandle, 1);
>
> RT=[];
> ListenChar(2);
> j=1;
>
> for iblocks=1:numBlocks
> for iTrialNum = 1:n
> picnum=(iblocks-1)*(n) +iTrialNum;
> if picnum>30
> picnum=picnum-30;
> end
> Screen('DrawTexture',wPtr,texArray.startTex(picnum));
> Screen(wPtr, 'Flip');
> sound(VoiceArr{picnum,1}, VoiceArr{picnum,2});
> F=0;
> ts=getsecs;
> while getsecs-ts<=5
> if F==0
> s = PsychPortAudio('GetStatus', pahandle);
> if s.Active == 0
> F=1;
> PsychPortAudio('UseSchedule', pahandle, 2);
> j=1;
> for i=1:NumBeeps(iTrialNum)
> PsychPortAudio('AddToSchedule',
> pahandle, buffer(j), 1, 0.0, [], 1);
> PsychPortAudio('AddToSchedule',
> pahandle, buffer(j+1), MultSilence(iTrialNum), 0.0,
> Period(iTrialNum)-beepWavDuration, 1);
> j=j+2;
> end
> end
> end
> end
>
> Screen('FillRect',wPtr,black);
> Screen(wPtr, 'Flip');
>
> CycleStart=getsecs;
> PsychPortAudio('Start', pahandle, [], 0, 1);
>
> while (GetSecs - CycleStart) < (Duration)
> [keyIsDown,secs,keyCode] = KbCheck;
> if keyIsDown
> RT(j,1)= (GetSecs - CycleStart);
> RT(j,2)= iTrialNum;
> RT(j,3)=iblocks;
> j=j+1;
> while KbCheck && (GetSecs - CycleStart) <
> Duration ; end
> end
> end
> waitsecs(breakDuration);
>
> end
> end
> ListenChar(1);
>
> On Mon, Jan 25, 2010 at 8:27 AM, Mario <mario.kleiner@...>wrote:
>
> >
> >
> > There's no such thing as callbacks in Matlab - at least none that would be
> > useful if timing or real parallelism is of any concern.
> >
> > BasicSoundScheduleDemo shows you how to program arbitrary sequences of
> > sounds that are played automatically and parallel to your Matlab code - you
> > can prepare audio buffers with different sounds and then create a "playlist"
> > that defines which buffers should be played in which order. You can
> > dynamically add to the playlist while it is playing.
> >
> > On OS/X, KbQueueDemo shows some functions for async keyboard checks, on
> > Windows or Linux no such thing supported yet.
> >
> > As it seems you try to measure RT's:
> >
> > a) Make sure you understand the timestamps returned by PsychPortAudio and
> > make use of them, e.g., PsychPortAudioTimingTest. This...
> >
> > > PsychPortAudio('Start', pahandle,1,0,0);
> > > beepList = [beepList (GetSecs - start)];
> >
> > ...is almost as good as flipping a coin, as your GetSecs() query will be
> > only loosely related to the onset of the sound.
> >
> > b) Make sure you have a proper soundcard and configuration as described on
> > the wiki and in posts on the forum, otherwise sound playback timing will be
> > inaccurate.
> >
> > c) Keyboards are inaccurate for RT measurements. Also read help KbCheck to
> > understand the returned 'secs' argument.
> >
> > -mario
> >
> > --- In psychtoolbox@yahoogroups.com <psychtoolbox%40yahoogroups.com>,
> > "lilaca@" <lilacadato@> wrote:
> > >
> > > Hi
> > > My problem is that the I need the code to take care of two processes
> > concurrently:
> > > 1. create beeps sounds in different frequency
> > > 2. saving subject responses (key board presses)
> > >
> > > So far i tried pulling the subject responses using kbcheck,
> > > but kbcheck response is longer then the gap between two beeps....
> > >
> > > %%%%%%%%%
> > > while (k <= NumBeeps)
> > > PsychPortAudio('Start', pahandle,1,0,0);
> > > beepList = [beepList (GetSecs - start)];
> > > CycleStart = GetSecs;
> > > while ((GetSecs - CycleStart) < Period)
> > > [keyIsDown,secs,keyCode] = KbCheck;
> > >
> > > while ((GetSecs - CycleStart) < Period)
> > > if keyIsDown
> > > RT = [RT (GetSecs - start)];
> > > while KbCheck && ((GetSecs - CycleStart) < Period) ; end %%% this part
> > lasts too long, and each key press recorder more then once....
> > > end
> > > end
> > > end
> > > k = k +1;
> > > end
> > >
> > >
> > > %%%%%%%%%%%
> > > and even if ill maximize this, i don't thing it is the ideal way of
> > running concordant processes.
> > > I was thinking of using a callback/ interrupt mechanism .
> > >
> > > but i cant find a way to create an on screen/ window object.
> > > do you have any idea how can I do that?
> > >
> > > Thank you so much
> > >
> >
> >
> >
>
>
>
> --
> Lilac Adato
> cell: 734-604-1248
>
Thank you so much again!
I'll try to explain the paradigm:
the basic thing is to produce beep sounds in different frequencies and the subject soppose to follow the beeps by
hitting the keyboard...
before each and every trail, we show the subject a picture (screen flip) and telling him a story (sound()- shiould I use PsychPortAudio as well?)...
to keep him/her interested...

right now- there is a dellay (one delley) in 2 Hz frequency after 17 seconds (i tried too 30 sec).
I dont know why...

if you have any more good suggestions I'll be more then happy too hear...


Thank you again.


On Thu, Feb 11, 2010 at 10:14 AM, Mario <mario.kleiner@...> wrote:



I have no clue what you mean with "inconsistent dleay while the pahandle is on, between the beeps".

But you would expect some small delay between trials - a pause while audio playback is stopped. Intermixing calls to sound() with calls to PsychPortAudio() sounds like begging for trouble. Find out about proper use of the screen('flip') and psychportaudio('start') timestamps if you want to measure RT's (with respect to what? sound? visuals? which ones? or sound and visual onset sync'ed?) the current code will not measure rt wrt. to either visual or sound onset, cfe. PsychPortAudioTimingTest for sync'ed visual and audio onset and use of timestamps.

If you use psychportaudio('stop') to wait for sound to stop at the end of a trial, you can get rid of the weird if s.Active code and move all the 'addtoschedule' calls to the beginning of a trial. Finally the ...Period(iTrialNum)-beepWavDuration... part looks suspicious to me, like a good way to mess up timing if you don't really know what you're doing. Other than that, your use of sound schedules is correct.

I think you never explained your exact paradigm, so nobody can guess from the code what its intended behaviour was or what is going wrong.

Timing of sound and visuals can be sub-millisecond accurate if done right, with the right hardware and OS (Either OS/X or Linux or Window with a ASIO sound card -- timing with non-ASIO cards on Windows is just horrible).

-mario



--- In psychtoolbox@yahoogroups.com, lilac Adato <lilacadato@...> wrote:
>
> Thank you so much!
> I tried and tried to make it work, and by now,
> there is some kind of an inconsistent delay while the pahandle is on,
> between the beeps.
>
> why is it happening? how accurate it is? How can i produce bees exactly in
> the wanted rhythm?
>
>
>
> j=1;
> for i = 1:NumBeepsMaxVal
> buffer(j) = PsychPortAudio('CreateBuffer', [], audiodata);
> buffer(j+1) = PsychPortAudio('CreateBuffer', [],
> audiodatas);
> j=j+2;
> end
>
> pahandle = PsychPortAudio('Open', [], [], 1, infreq,
> nrchannels);
> runMode = 1;
> PsychPortAudio('RunMode', pahandle, runMode);
>
> PsychPortAudio('UseSchedule', pahandle, 1);
>
> RT=[];
> ListenChar(2);
> j=1;
>
> for iblocks=1:numBlocks
> for iTrialNum = 1:n
> picnum=(iblocks-1)*(n) +iTrialNum;
> if picnum>30
> picnum=picnum-30;
> end
> Screen('DrawTexture',wPtr,texArray.startTex(picnum));
> Screen(wPtr, 'Flip');
> sound(VoiceArr{picnum,1}, VoiceArr{picnum,2});
> F=0;
> ts=getsecs;
> while getsecs-ts<=5
> if F==0
> s = PsychPortAudio('GetStatus', pahandle);
> if s.Active == 0
> F=1;
> PsychPortAudio('UseSchedule', pahandle, 2);
> j=1;
> for i=1:NumBeeps(iTrialNum)
> PsychPortAudio('AddToSchedule',
> pahandle, buffer(j), 1, 0.0, [], 1);
> PsychPortAudio('AddToSchedule',
> pahandle, buffer(j+1), MultSilence(iTrialNum), 0.0,
> Period(iTrialNum)-beepWavDuration, 1);
> j=j+2;
> end
> end
> end
> end
>
> Screen('FillRect',wPtr,black);
> Screen(wPtr, 'Flip');
>
> CycleStart=getsecs;
> PsychPortAudio('Start', pahandle, [], 0, 1);
>
> while (GetSecs - CycleStart) < (Duration)
> [keyIsDown,secs,keyCode] = KbCheck;
> if keyIsDown
> RT(j,1)= (GetSecs - CycleStart);
> RT(j,2)= iTrialNum;
> RT(j,3)=iblocks;
> j=j+1;
> while KbCheck && (GetSecs - CycleStart) <
> Duration ; end
> end
> end
> waitsecs(breakDuration);
>
> end
> end
> ListenChar(1);
>
> On Mon, Jan 25, 2010 at 8:27 AM, Mario <mario.kleiner@...>wrote:

>
> >
> >
> > There's no such thing as callbacks in Matlab - at least none that would be
> > useful if timing or real parallelism is of any concern.
> >
> > BasicSoundScheduleDemo shows you how to program arbitrary sequences of
> > sounds that are played automatically and parallel to your Matlab code - you
> > can prepare audio buffers with different sounds and then create a "playlist"
> > that defines which buffers should be played in which order. You can
> > dynamically add to the playlist while it is playing.
> >
> > On OS/X, KbQueueDemo shows some functions for async keyboard checks, on
> > Windows or Linux no such thing supported yet.
> >
> > As it seems you try to measure RT's:
> >
> > a) Make sure you understand the timestamps returned by PsychPortAudio and
> > make use of them, e.g., PsychPortAudioTimingTest. This...
> >
> > > PsychPortAudio('Start', pahandle,1,0,0);
> > > beepList = [beepList (GetSecs - start)];
> >
> > ...is almost as good as flipping a coin, as your GetSecs() query will be
> > only loosely related to the onset of the sound.
> >
> > b) Make sure you have a proper soundcard and configuration as described on
> > the wiki and in posts on the forum, otherwise sound playback timing will be
> > inaccurate.
> >
> > c) Keyboards are inaccurate for RT measurements. Also read help KbCheck to
> > understand the returned 'secs' argument.
> >
> > -mario
> >
> > --- In psychtoolbox@yahoogroups.com <psychtoolbox%40yahoogroups.com>,

> > "lilaca@" <lilacadato@> wrote:
> > >
> > > Hi
> > > My problem is that the I need the code to take care of two processes
> > concurrently:
> > > 1. create beeps sounds in different frequency
> > > 2. saving subject responses (key board presses)
> > >
> > > So far i tried pulling the subject responses using kbcheck,
> > > but kbcheck response is longer then the gap between two beeps....
> > >
> > > %%%%%%%%%
> > > while (k <= NumBeeps)
> > > PsychPortAudio('Start', pahandle,1,0,0);
> > > beepList = [beepList (GetSecs - start)];
> > > CycleStart = GetSecs;
> > > while ((GetSecs - CycleStart) < Period)
> > > [keyIsDown,secs,keyCode] = KbCheck;
> > >
> > > while ((GetSecs - CycleStart) < Period)
> > > if keyIsDown
> > > RT = [RT (GetSecs - start)];
> > > while KbCheck && ((GetSecs - CycleStart) < Period) ; end %%% this part
> > lasts too long, and each key press recorder more then once....
> > > end
> > > end
> > > end
> > > k = k +1;
> > > end
> > >
> > >
> > > %%%%%%%%%%%
> > > and even if ill maximize this, i don't thing it is the ideal way of
> > running concordant processes.
> > > I was thinking of using a callback/ interrupt mechanism .
> > >
> > > but i cant find a way to create an on screen/ window object.
> > > do you have any idea how can I do that?
> > >
> > > Thank you so much
> > >
> >
> >
> >
>
>
>
> --
> Lilac Adato
> cell: 734-604-1248
>




--
Lilac Adato
cell: 734-604-1248