/*

*/

#include "dv_echo.h"

unsigned char   NullVoice0[12] = {0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x55,0x2d,0x16};
unsigned char   NullVoice1[12] = {0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x16,0x29,0xf5};

void	audio_msg(char caller[], char repeater[], struct packet pkt)
{
	int	n;
	int	k;
	char	buf[150];
	char	voice_d[12];
	FILE	*voice;
	struct	packet	send_pkt;
	unsigned char	pkt_seq;

	logfile = fopen (LOGFILE, "a");
	now = time(NULL);
	fprintf (logfile, "%24.24s audio message %8.8s %8.8s\n", ctime(&now), caller, repeater);
	fclose (logfile);

	memcpy (&send_pkt, &pkt, sizeof(send_pkt));
	pkt_seq = 0x00;

	for (n = 0 ; n < 8 ; n++) 	/* dst call */
	{
		if (caller[n] == 0x20)
		{
			for (k = 0 ; k < 20 ; k++)
			{
				send_pkt.B_header.seq = pkt_seq;
				if (pkt_seq == 0)
				{
					memcpy (send_pkt.voice, NullVoice0, 12);
				}
				else
				{
					memcpy (send_pkt.voice, NullVoice1, 12);
				}
				fwrite (&send_pkt, 56, 1, dat);
				pkt_seq++;
				if (pkt_seq == 21) pkt_seq = 0;
			}
		}
		else
		{
			sprintf(buf,"%s%c.dsv", AudioDirectory, caller[n]);
			voice = fopen (buf, "rb");
			while (fread (send_pkt.voice, 12, 1, voice))
			{
				send_pkt.B_header.seq = pkt_seq;
				if (pkt_seq == 0)
				{
					pkt_seq = 0;
					memcpy (&send_pkt.voice[9], &NullVoice0[9], 3);
				}
				else
				{
					memcpy (&send_pkt.voice[9], &NullVoice1[9], 3);
				}
				fwrite (&send_pkt, 56, 1, dat);
				pkt_seq++;
				if (pkt_seq == 21) pkt_seq = 0;			
			}
			fclose (voice);
			
		}
	}

	/* message "THIS IS" */	
        sprintf(buf,"%sthisis.dsv", AudioDirectory);
        voice = fopen (buf, "rb");
	if (voice != NULL)
	{
        	while (fread (send_pkt.voice, 12, 1, voice))
        	{
        		send_pkt.B_header.seq = pkt_seq;
                	if (pkt_seq == 0)
                	{
                		pkt_seq = 0;
                        	memcpy (&send_pkt.voice[9], &NullVoice0[9], 3);
                	}
                	else
                	{
                		memcpy (&send_pkt.voice[9], &NullVoice1[9], 3);
                	}
                	fwrite (&send_pkt, 56, 1, dat);
                	pkt_seq++;
                	if (pkt_seq == 21) pkt_seq = 0;
		}
        	fclose (voice);
	}

	for (n = 0 ; n < 8 ; n++)	/* src call */
	{
		if (repeater[n] == 0x20)
		{
                        for (k = 0 ; k < 20 ; k++)
                        {
                                send_pkt.B_header.seq = pkt_seq;
                                if (pkt_seq == 0)
                                {
                                        memcpy (send_pkt.voice, NullVoice0, 12);
                                }
                                else
                                {
                                        memcpy (send_pkt.voice, NullVoice1, 12);
                                }
                                fwrite (&send_pkt, 56, 1, dat);
                                pkt_seq++;
                                if (pkt_seq == 21) pkt_seq = 0;
                        }
		}
		else
		{
			sprintf (buf, "%s%c.dsv", AudioDirectory, repeater[n]);
			voice = fopen (buf, "rb");
                        while (fread (send_pkt.voice, 12, 1, voice))
                        {
                                send_pkt.B_header.seq = pkt_seq;
                                if (pkt_seq == 0)
                                {
                                        memcpy (&send_pkt.voice[9], &NullVoice0[9], 3);
                                }
                                else
                                {
                                        memcpy (&send_pkt.voice[9], &NullVoice1[9], 3);
                                }
                                fwrite (&send_pkt, 56, 1, dat);
				pkt_seq++;
				if (pkt_seq == 21) pkt_seq = 0;
                        }
                        fclose (voice);
		}
	}

	/* Message */
        sprintf(buf,"%smessage.dsv", AudioDirectory);
        voice = fopen (buf, "rb");
        if (voice != NULL)
	{
		while (fread (send_pkt.voice, 12, 1, voice))
        	{
                	send_pkt.B_header.seq = pkt_seq;
                	if (pkt_seq == 0)
                	{
                        	pkt_seq = 0;
                        	memcpy (&send_pkt.voice[9], &NullVoice0[9], 3);
                	}
                	else
                	{
                        	memcpy (&send_pkt.voice[9], &NullVoice1[9], 3);
                	}
                	fwrite (&send_pkt, 56, 1, dat);
                	pkt_seq++;
                	if (pkt_seq == 21) pkt_seq = 0;
        	}
        	fclose (voice);
	}

        for ( ;  ; )	/* last padding */
        {
        	send_pkt.B_header.seq = pkt_seq;
                if (pkt_seq == 0)
                {
                	break;
                }
                else
                {
                	memcpy (send_pkt.voice, NullVoice1, 12);
                }
                fwrite (&send_pkt, 56, 1, dat);
                pkt_seq++;
                if (pkt_seq == 21) break;
	}
}
