/* This program is to test play and record features of Linux OS */ /* Also its a multithreaded program to test the DUPLEX capability */ # include # include # include # include # include # include # include # include # include # include /* Function Prototypes */ void* play(void*); void* record(void*); void sig_hnd(); /* Device Name */ # define DEVICE_NAME "/dev/dsp" /* Thread Ids for PLay & Record */ pthread_t play_tid,record_tid; /* Buffer & File Descriptors */ # define BUFF_SIZE 4096 int audio_fd,io_fd; unsigned char audio_buffer[BUFF_SIZE]; void sig_hnd(int signo) { printf("\n Completed Recording..."); pthread_exit(0); close(io_fd); close(audio_fd); } void main(void) { /* Thread Status */ void **status; /* Thread Attribute Declarations */ pthread_attr_t attr; /* Opening the audio device for playing and recording (DUPLEX) */ if((audio_fd = open(DEVICE_NAME,O_RDWR,0)) == -1 ) { /* Opening of device failed */ perror(DEVICE_NAME); exit(errno); } pthread_attr_init(&attr); pthread_create(&play_tid,&attr,play,NULL); /* Play Thread Created */ pthread_create(&record_tid,&attr,record,NULL); /* Record Thread Created */ signal(SIGINT,sig_hnd); pthread_join(record_tid,status); } void* play(void* arg) { int len; /* Opening the input File */ if((io_fd = open("RAJEN.WAV",O_RDONLY,0644)) == -1){ /* Opening of input file failed */ perror("RAJEN.WAV"); exit(errno); } printf("\n Playing.....\n"); while(read(io_fd,audio_buffer,512)){ /* Reading from input file */ if((len = write(audio_fd,audio_buffer,512)) == -1){ /* Audio Device Output error */ perror("Audio Output"); exit(errno); } }/* End of while */ close(audio_fd); } void* record(void* arg) { int len; /* Opening the output File */ if((io_fd = open("RAJ.WAV",O_APPEND|O_TRUNC|O_WRONLY|O_CREAT,0644)) == -1){ /* Opening of output file failed */ perror("RAJ.WAV"); exit(errno); } printf("\n Recording...."); printf("\n Press Ctrl-C To End Recording..."); while(1){ /* Reading from audio Device */ if((len = read(audio_fd,audio_buffer,512)) == -1){ /* Audio Device Read error */ perror("Audio Read"); exit(errno); } if((len = write(io_fd,audio_buffer,512)) == -1){ /* File Write Error */ perror("File Write"); exit(errno); } }/* End of while */ }