//////////////////////////////server udp////////////////////////////////////
#include "unp.h"
#include
#include
#include
#include
#include
#include
#include
#include
float LENGTH = 0.1; // how many seconds of speech to store
int BUFSIZE = 16404;
typedef struct // structure for format chunk of size 16 bytes
{
short audioFormat;
short numOfChannels;
int sampleRate;
int byteRate;
short blockAlign;
short bitsPerSample;
} FmtBlock;
typedef struct sockaddr SA;
void bg_echo(int, SA*, socklen_t,char* );
unsigned char *buf = 0;
struct soundSet {
float length;
int bitsPerSample;
int numOfChannels;
int sampleRate;
int noOfIteration;
}set;
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
printf("server program\n");
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
// bg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),argv[1]);
bg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),"/root/wav/utpal/ekl.wav");
}
void bg_echo(int sockfd, SA *pcliaddr, socklen_t clilen,char *argv)
{
int n, connect;
socklen_t len;
char mseg[MAXLINE];
unsigned char *buf = 0; // this buffer holds the digitized audio
int check;
int bufSize;
int flag;
int fd, fds; // sound device file descriptor
int arg; // argument for ioctl calls
int status; // return status of system calls
char fileDescriptor[5]; // wav file description
int fileSize; // wav file size
char fileFormat[5]; // wav file format
char ChunkId[5]; // fmt chunk id
unsigned int ChunkSize; // fmt chunk size
FmtBlock fmtBlock1; // fmt block of size 16
unsigned int iteration; // no of iterations required to play the data block
int i; // used as counter in for loops
int temp; // temporary variable
char *filePath = 0;
int foundFormatChunk = 0;
for ( ; ; )
{
// three way handshake ...
printf("ready to recv\n");
len = clilen;
printf("waiting for connection to be established ...\n");
n = recvfrom(sockfd, &connect , 4, 0, pcliaddr, &len);
if (connect != 1) exit(0);
printf("acknowledging connection ...\n\n\n");
sendto(sockfd, &connect,sizeof(connect), 0, pcliaddr, len);
// finally confirmed connection !!!
filePath = argv;
printf( "Reading file %s ...\n", filePath);
/* fd = open ("/dev/dsp", O_WRONLY ); // opening the device file ...
if ( fd < 0 ) {
perror ( "open of /dev/dsp failed\n" );
exit(1);
}*/
fds = open ( filePath , O_RDONLY ); // opening the file to play ...
if ( fds < 0 ) {
perror ( "open of the specified input file failed\n" );
exit(1);
}
//
// reading wav file information ...
//
read(fds,fileDescriptor,4); // will always b RIFF
fileDescriptor[4] = 0;
if ( strcmp("RIFF", fileFormat) == 0 ) {
printf("cant play file, this is not a RIFF\n");
close(fd);
close(fds);
exit(0);
}
printf("File Descriptor : \"%s\"\n", fileDescriptor );
read(fds,&fileSize,4); //total file size - 8
printf("File Size : %10d bytes\n", fileSize + 8);
read(fds,fileFormat, 4 ); //will always be WAVE
fileFormat[4] = 0;
printf("File Format : \"%s\"\n\n", fileFormat);
if ( strcmp("WAVE", fileFormat) != 0 ) {
printf("cant play file, this is not a WAV file\n");
close(fd);
close(fds);
exit(0);
}
fileSize -= 4;
while(fileSize > 0)
{
read(fds,ChunkId,4); // reading ChunkId
ChunkId[4] = 0;
read(fds,&ChunkSize,4); // reading ChunkSize
fileSize = fileSize - ChunkSize;
if (strcmp("fmt ", ChunkId) != 0 && strcmp("data",ChunkId) != 0 )
{
printf("Chunk Id : %s\n\n",ChunkId);
lseek(fds, ChunkSize , SEEK_CUR); // skip if not format or data chunk
continue;
}
else if ( strcmp("fmt ",ChunkId) == 0 ) //format chunk
{
printf("Fmt Chunk Id : \"%s\"\n", ChunkId);
printf("Fmt Chunk Size : %10d bytes\n", ChunkSize);
read(fds,&fmtBlock1,16); // read the format chunk
printf("Audio Format : %10d", fmtBlock1.audioFormat);
if ( fmtBlock1.audioFormat == 1 )
printf (" \"PCM\" - \"Linear Quantization\"\n");
else {
printf ("\nNot PCM File, cannot play file\n");
exit(1);
}
printf("Num Of Channels : %10d", fmtBlock1.numOfChannels);
if ( fmtBlock1.numOfChannels == 1 )
printf ( " \"mono\"\n");
else if ( fmtBlock1.numOfChannels == 2 )
printf ( " \"stereo\"\n");
else
printf ( " \"multi channel\"\n" );
printf("Sample Rate : %10d samples per sec\n", fmtBlock1.sampleRate);
printf("Byte Rate : %10d bytes per sec\n", fmtBlock1.byteRate);
printf("Block Align : %10d\n", fmtBlock1.blockAlign);
printf("Bits Per Sample : %10d bits per sample\n", fmtBlock1.bitsPerSample);
if ( ChunkSize > 16 )
lseek(fds,(ChunkSize-16),SEEK_CUR); // skip rest of the format chunk
/* // set sampling parameters
arg = fmtBlock1.bitsPerSample; // sample size
status = ioctl ( fd, SOUND_PCM_WRITE_BITS, &arg );
if ( status == -1 ) {
perror("SOUND_PCM_WRITE_BITS ioctl failed\n");
exit(1);
}
if ( arg != fmtBlock1.bitsPerSample ) {
perror( "unable to set sample size\n" );
exit(1);
}
// set channel parameters
arg = fmtBlock1.numOfChannels; // mono or stereo
status = ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &arg );
if ( status == -1 ) {
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed\n");
exit(1);
}
if ( arg != fmtBlock1.numOfChannels ) {
perror("unable to set number of channels\n");
exit(1);
}
// set sampling parameters
arg = fmtBlock1.sampleRate; // sampling rate
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1) {
perror("SOUND_PCM_WRITE_WRITE ioctl failed\n");
exit(1);
}
if ( arg != fmtBlock1.sampleRate ) {
perror("unable to set the sampling rate\n");
exit(1);
}*/
// bufSize = LENGTH * fmtBlock1.sampleRate * fmtBlock1.bitsPerSample * fmtBlock1.numOfChannels / 8;
bufSize = BUFSIZE;
printf("Buffer Size : %10d bytes\n\n", bufSize);
if ( buf )
free ( buf );
buf = (unsigned char*) malloc ( bufSize );
if ( buf == 0 ) {
perror ( "Cannot allocate memory for buffer\n");
close(fd);
exit(1);
}
foundFormatChunk = 1;
}
else if ( foundFormatChunk == 1 ) // if Data Chunk whose format has already been found
{
printf("Data Chunk Id : \"%s\"\n", ChunkId);
printf("Data Chunk Size : %10u bytes\n", ChunkSize);
iteration = (int) ( ceil ( ((float)ChunkSize) / bufSize ) );
printf("No Of Iteration : %10d\n", iteration );
printf("KBPS : %10d\n\n",fmtBlock1.sampleRate *
fmtBlock1.bitsPerSample * fmtBlock1.numOfChannels / 1000);
// building the sound settings data ...
set.length=LENGTH;
set.sampleRate=fmtBlock1.sampleRate;
set.bitsPerSample=fmtBlock1.bitsPerSample;
set.numOfChannels=fmtBlock1.numOfChannels;
set.noOfIteration=iteration;
// sending sound settings data ...
printf("waiting for sound information request ...\n");
n = recvfrom ( sockfd, &n, sizeof(n), 0 , pcliaddr, &len);
printf("request recieved, sending sound information ...\n");
sendto(sockfd, &set, sizeof(set), 0, pcliaddr, len);
printf("Waiting for request for sound data ...\n");
n = recvfrom ( sockfd, &n, sizeof(n), 0 , pcliaddr, &len);
printf("Now sending sound data ...\n\n");
for( i = 0 ; i < iteration -1 ; i++ )
{
// printf("Reading %d ...\n",i);
status = read ( fds, buf, bufSize );
// printf("Data read %d ...\n",i);
if ( status != bufSize ) {
perror ( "read wrong number of bytes form the input file\n");
exit(1);
}
//sending data to client
// printf("Sending %d ...\n",i);
check = sendto(sockfd, buf, bufSize, 0, pcliaddr, len);
if ( check == -1 )
{
perror ( "cannot send data " );
exit(1);
}
// printf("Data sent %d ...\n",i);
// printf("Waiting for Ack %d ...\n",i);
n = recvfrom(sockfd, &connect , 4, 0, pcliaddr, &len);
// printf("Ack recieved for %d ...\n\n",connect);
/* status = write ( fd , buf, bufSize );
if ( status != bufSize ) {
perror ( "wrote wrong number of bytes into the audio device\n" );
exit(1);
}
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if (status == -1) {
perror( "SOUND_PCM_SYNC ioctl failed\n" );
exit(1);
}*/
}
// printf("Reading %d ...\n",i);
status = read ( fds, buf, bufSize );
// printf("Data read %d ...\n",i);
// printf("Writing %d ...\n",i);
check = sendto(sockfd, buf, bufSize, 0, pcliaddr, len);
if ( check == -1 )
{
perror ( "cannot send data ");
exit(1);
}
/* temp = write ( fd , buf, status );
if ( temp != status ) {
perror ( "wrote wrong number of bytes into the audio device\n" );
exit(1);
}
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if ( status == -1 ) {
perror ( "SOUND_PCM_SYNC ioctl failed\n" );
exit(1);
}*/
printf("Done !!\n\n");
foundFormatChunk = 0;
}
}
close(fd);
// close(fds);
exit(0);
//sending sound setting
}
}