// file: $isip/util/demo/isip_comm_data_recorder/isip_data_comm_recorder.h
// version: $Id: isip_comm_data_recorder.h 10046 2005-05-06 20:57:15Z elliott $
//

/*-----------------------------------------------------------------------*
 *                                                                       *
 *                ISIP Communicator Audio Recorder Server                * 
 *									 *
 *-----------------------------------------------------------------------*/

extern "C" {

// system include files 
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <strings.h>
#include <time.h>
#include <math.h>

// this line must appear before the galaxy include files
//
#define SERVER_FUNCTIONS_INCLUDE "isip_comm_data_recorder_server.h"

// Galaxy include files
//
#include <galaxy/util.h>
#include <galaxy/galaxy_all.h>
#include <galaxy/server_functions.h>

};

// isip include files
//
#include <AudioFile.h>

AudioFile adf_d;

// define constants 
//
#define SERVER_NAME "data_recorder"
#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

// declare some global variables for this server
//
char *sys_id, sys_id_value[MAX_LENGTH];
float sample_rate_d = 8000;
int num_channels_d = 1;

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* client_log_file_d = NULL;


// Usage Information, defined by Galaxy Communicator
// architecture. Used to set more commandline options besides basic
// commandline options of the Galaxy Communicator.
//
static char *oas[] = {
  "-sysid string", "value of sysid key to represent decoder", "si",
  NULL
};

// define server state
//
// basically the server works like finite state machine right now
//
// GET_PROCESS_ID   = 0, the server waiting for the query process id
// WAIT_AUDIO_READY = 5, the server waiting for the audio_ready signal
// AUDIO_READY_ACK  = 10, the server send audio_ready signal acknowledgement
//                       signal
// START_RECORDING  = 20, the server send the start recording signal
// START_RECORDING_ACK = 30, the server waiting for start recording signal
//                           acknowledgement
// RAW_DATA = 40, the server waiting for raw data signal
// DATA_ACK = 50, the server send the data acknowledgement signal
// UTTERANCE_END = 60, the server send utterance end acknowledgement signal 

#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 RAW_DATA 40
#define DATA_ACK 50
#define UTTERANCE_END 60

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

// define the signal process method
//
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_raw_data(Gal_Frame frame_a, void *server_data_a);
void set_format(Gal_Frame frame_a, void *server_data_a);
