// file: $isip/util/demo/isip_comm_server_speech_recognition/isip_comm_speech_recognition.h
// version: $Id: isip_comm_speech_recognition.h 10403 2006-01-27 19:53:24Z stanley $
//

/*-----------------------------------------------------------------------*
 *                                                                       *
 *                   ISIP Communicator Recognition Server                * 
 *									 *
 *-----------------------------------------------------------------------*/

extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <strings.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>

// isip include files (from communicator directory)
//
#include <galaxy/galaxy_all.h>
#define SERVER_FUNCTIONS_INCLUDE "isip_comm_speech_recognition_server.h"
#include <galaxy/server_functions.h>

};

#include <trace_projector.h>
#include <extract_feature.h>
#include <String.h>
#include <Filename.h>
#include <Float.h>
#include <Vector.h>
#include <VectorFloat.h>
#include <FrontEnd.h>
#include <Sdb.h>

// some global variables. this is from the data recorder
//

// define constants 
//
#define SERVER_NAME "speech_recognizer"
#define DEBUG 1
#define RECORDED "This utterance is recorded."
#define MAX_UTT_DURATION (float)20    // 20 seconds
#define MAX_LENGTH 256                // max string length
#define NORMALIZATION_VALUE 32767     // 2**15
#define USE_SERVER_DATA
#define BLOCK_MODE 1

// declare some global variables for this server
//
char *sys_id, sys_id_value[MAX_LENGTH];
float sample_rate_d = 8000;
float frame_duration_d = 0.010;
int num_channels_d = 1;
float cpu_seconds_elapsed_d;
float audio_duration_sec_d;
char audio_logdir_d[MAX_LENGTH]="./logs/";
char client_logdir_d[MAX_LENGTH]="./logs/";
int utt_counter_d = 0;
char filename_d[MAX_LENGTH];
char extension_d[MAX_LENGTH];
boolean file_open = false;
int total_samples_d = 0;
char acknowledgement_d[MAX_LENGTH];
char server_name_d[MAX_LENGTH];
FILE* log_file_d;
FILE* client_log_file_d;

// declare static variables
//
long fr;
char *hyp;
int   server_active, total_samples, server_enabled;
float audio_duration_sec, cpu_seconds_elapsed;
clock_t time1, time2;

// declare the energy parameters
//
double LOG10_E = (double)0.4342944819;
short *window_buffer_d = NULL;
int capacity_d = 0;
float *energy_d = NULL;
short min_signal = 32767;
short max_signal = 0;
float min_energy = 100000;
float max_energy = 0;

// declare the vad parameters
//
int is_vad_d = 0;
int in_silence_d = 0;

// audio log parameters
//
char audio_logdir[200]="./logs/";
int utt_counter = 0;
char filename[200];

// define a variable to remember server state
//
int server_state_d;

// the frontend object
//
Extract_feature frontend;
Trace_projector tp;
char_1* hypotheses = NULL;
char_1* alignment = NULL;
Sdb sdb_params_d;
Vector<FrontEnd> vector_fe_d;
Vector<String> vector_param_d;

/*-----------------------------------------------------------------------*
 * Usage Information 							 *
 *-----------------------------------------------------------------------*/

static char *oas[] = {
   "-frontend filename", "use a configuration file for ISIP Frontend", NULL,
   "-decoder filename", "use a configuration file for ISIP Recognizer", NULL,
   "-sysid string", "value of sysid key to represent decoder", "si",
   NULL
};

/*-----------------------------------------------------------------------*
 * Define state
 ments for receiving audio from the MIT/MITRE audio server * 
 *-----------------------------------------------------------------------*/

#define AUDIO_SERVER_NEW_UTT "new_utt"
#define AUDIO_SERVER_SAMPLES_DONE "done"
#define AUDIO_SERVER_SAMPLES_ABORT "cancel"
#define AUDIO_CLIENT_PLAY_SAMPLES_DONE "AUDIO_CLIENT_PLAY_SAMPLES_DONE"
#define AUDIO_CLIENT_PLAY_SAMPLES_START "AUDIO_CLIENT_PLAY_START"

// define constants
//
#define MAX_HYPO_LENGTH 50000         // 50000 characters
#define GET_PROCESS_ID 0
#define WAIT_AUDIO_READY 5
#define AUDIO_READY_ACK 10
#define START_RECORDING 20
#define START_RECORDING_ACK 30
#define DATA_PROCESSING 40
#define DATA_ACK 50
#define UTTERANCE_END 60

// define constants
//
#define NBEST_HEAD "<pause1>"
#define NBEST_TAIL "<pause2>"

// define functions
//
void process_frame(Gal_Frame frame_a, void *server_data_a);
void state_get_process_id(Gal_Frame frame_a, void *server_data_a);
void state_audio_ready(Gal_Frame frame_a, void *server_data_a);
void state_start_recording_ack(Gal_Frame frame_a, void *server_data_a);
void state_process_data(Gal_Frame frame_a, void *server_data_a);
int uttproc_result(char** hypo);
int uttproc_alignment(char** align);
int return_recognized_speech(void *server_data_a, char *hypo, float total_time, float real_time);
