Blocking and Non-Blocking Mode
Previous Topic  Next Topic 

All of the above functions can be called in both blocking or non-blocking mode as specified by a call to SMmode(vox_chan,nonblocking_flag).      Under normal circumstances any call to the above blocking functions will cause the Telecom Engine task to block until the function is interrupted by one of the terminating events.       However it is sometimes useful to allow program execution to continue while a SMplay() or other function continues in the background.


In order to allow this the voice channel can be put into non-blocking mode using the SMmode(vox_chan,nonblocking_flag) function.     If the nonblocking_flag argument is set to a non zero value then any calls to the above speech functions will return immediately whilst the play, record, play tone etc proceeds in the background.        


In this case it is up to the application to ensure that the current speech function has finished before attempting to call one of the other blocking speech functions.     To do this there is a function SMstate(vox_chan) which returns the current function that running on the channel at the present time or 0 if there are no speech functions currently running.


NOTE: if nonblocking_flag is set to 1 then the SMmode() call will only apply to the next blocking speech function call.   Once that function has completed then the channel mode will be set back to blocking mode.      If a non-zero value  other than 1 is given then the channel will stay in non-blocking mode until a call to SMmode() is made again with nonblocking_flag set to 0 to put the channel back into non-blocking mode.


There are two constants defined in ACULAB.INC for this purpose as shown below:


const MODE_BLOCKING                                =0;

const MODE_NONBLOCKING_ONCEONLY        =1;

const MODE_NONBLOCKNG                        =2;



For example, the following code extract will play some music in the background whilst a database look-up occurs.      Once the database lookup has completed the application will abort the music and wait for the channel to return to idle.


       // Prevent DTMF tones from interrupting playback   

       SMtoneint(vox_chan,0);

      

       // Play "Please wait while we look up the information"   

       SMplay(vox_chan,"PLSWAIT.VOX");

       // Change the mode to play in the background (non-blocking) for the next speech function only (non-blocking_flag=1)

       SMmode(vox_chan,MODE_NONBLOCKING_ONCEONLY);

       // Play music in the background while information is retrieved

       SMplay(vox_chan,"MUSIC.VOX");

       // Do the data retrieval whilst music is playing…

       data_retrieval_func();


       // Abort the music

       SMabort(vox_chan);

       // Loop waiting for chan state to return to 0 (should only take milliseconds..)

       while(SMstate(vox_chan))

               ;

       endwhile


       //  Allow DTMF tones to interrupt SMplay() etc again..

       SMtoneint(vox_chan,1);

       …


       onsignal

               // Check if hangup received during music playback (or other  non-blocking operation

               if(SMstate(vox_chan))

                       SMabort(vox_chan);        

                       // Loop waiting for chan state to return to 0 (should only take milliseconds..)

                       while(SMstate(vox_chan))

                               ;

                       endwhile

               endif


       ..

       endonsignal