/*---------------------------------------------------------------------------*\

    FILE....: filedtmf.cpp
    TYPE....: C++ program
    AUTHOR..: David Rowe
    DATE....: 22/11/01

    Test program for decoding DTMFs from a file, using the DSP based DTMF
    decoder.

\*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*\

         Voicetronix Voice Processing Board (VPB) Software

         Copyright (C) 1999-2001 Voicetronix www.voicetronix.com.au

         This library is free software; you can redistribute it and/or
         modify it under the terms of the GNU Lesser General Public
         License as published by the Free Software Foundation; either
         version 2.1 of the License, or (at your option) any later version.

         This library is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
         Lesser General Public License for more details.

         You should have received a copy of the GNU Lesser General Public
         License along with this library; if not, write to the Free Software
         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
	 USA

\*---------------------------------------------------------------------------*/

#include "mess.h"
#include "comm.h"
#include "config.h"
#include "dspfifo.h"
#include "timer.h"
#include "timer.h"
#include "wobbly.h"
#include "vpbapi.h"
#include "mapdev.h"
#include "translate.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int kbhit();

#define WAIT		1	// time out delay 
#define	BOARD		0	// board number
#define	N	        160	// size of processing frame
#define LOG_FRAMES      200     // number of frames to log

word offhook[] = {
	PC_LCODEC_OFFHK,
	PC_CODEC_OFFHK,
	0			// channel
};

word onhook[] = {
	PC_LCODEC_ONHK,
	PC_CODEC_ONHK,
	0			// channel
};

void set_codec_reg(int    chdev,
	           unsigned short addr,
          	   unsigned short data,
		   Comm   *c
		  );

int main(int argc, char *argv[])
{
	long		i;
	VPBREG		*v;
	word		upmess[COMM_MAX_MESSPCDSP];
	char		s[MAX_STR];
	WFILE		*win;
	int		rd;
	short		bufwr[N];
	int		frames;

	if (argc != 2) {
		printf("usage: %s InputDTMFWaveFile\n",argv[0]);
		exit(0);
	}

	// initialise 

	try {

		Comm comm;
		v = comm.vpbreg(BOARD);

		// configure VPB signal processing --------------------------

		config_create_object(&comm,BOARD,FIFO_DOWN, 0, 0,v->sztxdf[0]);
		config_create_object(&comm,BOARD,DTMF     , 1, 0, 0);

		config_create_wire(&comm, BOARD, 0, 1);

		vpb_wave_open_read(&win, argv[1]);

		// start main processing loop ------------------------------

		mess_mprintf_on();
		config_run(&comm, BOARD);
		i = 0;
		frames = 0;
		do {
			// check for events from VPB

			while(comm.GetMessageVPB(BOARD, upmess) == VPB_OK) {
				translate(BOARD, s, upmess);
				mprintf(s);
			}

			rd = vpb_wave_read(win,(char*)bufwr,sizeof(short)*N);
			while(v->txdf[0]->Write((word*)bufwr, N) != Fifo::OK);
			//	if (v->ddmodel == DD_PCI)
			//		dynamic_cast<V4PCI_DSP*>(v->hostdsp)->CheckForAssert(BOARD);

		} while(rd == sizeof(short)*N);

		// send clear config message -------------------------------

		mess_mprintf_off();
		config_stop(&comm, BOARD);
		config_clear(&comm, BOARD);

		vpb_wave_close_read(win);

	}	// try ...

	catch (Wobbly w) {
		char	s[MAX_STR];

		w.translate(s);

		if (w.file[0] != 0)
			printf("exception caught: %s, %s, line = %d\n",s,
			       w.file, w.line);
		else
			printf("exception caught: %s\n",s);

		printf("Press any key to exit....\n");
	}
}

