#! @TCL@ -f
#
# file: dec_next_0.tcl
#

# procedure to run the decoder
#
proc run_proc {} {

    # declare globals
    #
    global p
    
    global stop_flag
    global mfcc_format
    global current_frame
    global num_frames
    global num_step

    # reset if necessary
    #
    if {$current_frame == $num_frames} {
	reset_proc
    }
    
    # reset stop flag if necessary
    #
    set stop_flag 0
    $p.stop configure -state normal
    
    # run the program from the current point
    #
    puts stdout "curr frame : $current_frame"
    flush stdout
    while {$current_frame < $num_frames} {
	if {$stop_flag == 0} {
	    $p.stop configure -state normal
	    if { $current_frame == -1 } {
		message_proc "Wait while loading data"
	    } else {
		message_proc "You can press \"Stop\" to pause the \
			continuous run."
	    }
	    next_frame_proc

	} else {
	    set stop_flag 0
	    $p.stop configure -state disabled
	    message_proc "You can press \"Run\" to start the \
		    continuous run."
	    after 5000 {message_proc ""}
	    return
	}
    }

    
    # reset the pipe flag
    #
    set current_frame $num_frames
    set pipe_flag 0

    message_proc ""
}

# procedure to advance the decoder frame by frame
#
proc next_frame_proc {} {

    # define globals
    #
    global decoder_pipe
    global decoder_exe
    global pipe_flag

    global current_frame
    global num_frames
    global num_step

    global num_n_best
    global ph_context_size
    global gram_size
    global beam

    global params_file
    global tmp_latfile
    global tmp_lmfile
    global tmp_infile
    global tmp_outfile

    global st_beam
    global ph_beam
    global wd_beam
    global mapmi

    global wordlist_file
    global hyp_list
    global pause_time

    global p

    if {$pipe_flag == 0} {
	reset_proc
    }

    # check if need to restart
    #
    restart_proc

    # set the wait mode
    #
    $p config -cursor watch
    update

    # if this is the initialization, just start the decoder and advance
    # one frame
    #
    global file_time
    global old_time
    
    if {$current_frame == -1} {
	
	# make the temporary files
	#
	make_tmp_proc
	if {[file exists $wordlist_file] == 1} {
	    exec rm $wordlist_file
	}
	
	# write the paramters into file
	#
	write_proc $params_file
		
	# set the decoder execution
	#
	set decoder_run "$decoder_exe -p $params_file -demo"
	
	# start a pipe for the decoder
	#
	set decoder_pipe [open |$decoder_run "w"]
	flush $decoder_pipe

	# set the pipe flag
	#
	set pipe_flag 1
	
        # wait for the output file to be created
        #
        while {1} {
	    
	    after $pause_time	    
	    if {[file exists $wordlist_file] == 1} {
		set file_time [file mtime $wordlist_file]
		set old_time $file_time
		break
	    }
	}

	# increment current_frame
	#
	after $pause_time
	incr current_frame
	
    } elseif {$current_frame < $num_frames} {
	
	# evaluate the frame and output the n-best wordlist here
	#
	flush $decoder_pipe
	puts $decoder_pipe "$num_step\n"
        flush $decoder_pipe

	# wait for the output file to be updated
        #
        while {1} {
	    after $pause_time
	    set file_time [file mtime $wordlist_file]
	    #if {$file_time > $old_time} {
		#set old_time $file_time
	    break
	#}
	 
	}
	
	# increment current_frame
	#
	after $pause_time
	if {$num_step > 0} {
	    incr current_frame $num_step
	} else {
	    incr current_frame
	}
	
	# reset flag if this is the last frame
	#
	if {$current_frame >= $num_frames} {
	    set current_frame $num_frames
	    set pipe_flag 0
	}
	
        # plot data
	#
	flush $decoder_pipe
	plot_data_proc
	
	if {$num_step < 0} {
	    
	    # reset 
	    #
	    set num_step 1
	    
	    # close pipe
	    #
	    set pipe_flag 0
	}	
    } 

    # update the display
    #
    $p config -cursor top_left_arrow
    update
}

proc set_beam_proc {beam} {
    
    # reset the app
    #
    reset_proc
}

# end of file
#
