Showing posts with label Send. Show all posts
Showing posts with label Send. Show all posts

Take microphone input and send to headphones

How to write a C Program Take microphone input and send to headphones in C Programming Language ?

Solution:

  1. /*****************************************************************************/ 
  2. /* DESCRIPTION                                                               */
  3. /*   TMS320C5505 USB Stick. Application 1. Getting started.                  */
  4. /*   Take microphone input and send to headphones.                           */
  5. /*                                                                           */
  6. /* REVISION                                                                  */
  7. /*   Revision: 1.00                                                              */
  8. /*   Author  : Richard Sikora                                                */
  9. /*
  10.  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  11.  *
  12.  *
  13.  *  Redistribution and use in source and binary forms, with or without
  14.  *  modification, are permitted provided that the following conditions
  15.  *  are met:
  16.  *
  17.  *    Redistributions of source code must retain the above copyright
  18.  *    notice, this list of conditions and the following disclaimer.
  19.  *
  20.  *    Redistributions in binary form must reproduce the above copyright
  21.  *    notice, this list of conditions and the following disclaimer in the
  22.  *    documentation and/or other materials provided with the  
  23.  *    distribution.
  24.  *
  25.  *    Neither the name of Texas Instruments Incorporated nor the names of
  26.  *    its contributors may be used to endorse or promote products derived
  27.  *    from this software without specific prior written permission.
  28.  *
  29.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32.  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33.  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35.  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36.  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37.  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39.  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.  *
  41. */
  42.  
  43. #include "stdio.h"
  44. #include "usbstk5505.h"
  45. #include "aic3204.h"
  46. #include "PLL.h"
  47. #include "stereo.h"
  48.  
  49. Int16 left_input;
  50. Int16 right_input;
  51. Int16 left_output;
  52. Int16 right_output;
  53. Int16 mono_input;
  54.  
  55.  
  56. #define SAMPLES_PER_SECOND 48000
  57.  
  58. unsigned long int i = 0;
  59.  
  60.  
  61. /* ------------------------------------------------------------------------ *
  62.  *                                                                          *
  63.  *  main( )                                                                 *
  64.  *                                                                          *
  65.  * ------------------------------------------------------------------------ */
  66. void main( void )
  67. {
  68.     /* Initialize BSL */
  69.     USBSTK5505_init( );
  70.     // inicjalizacja zewnÄ™trznego programatora, w której włączane sÄ… zegary dla peryferii, funkcja ta zwraca wartoÅ›c 0
  71.        
  72.         /* Initialize PLL */
  73.         pll_frequency_setup(100);
  74.         //ustawienie czÄ™stotliwoÅ›ci pÄ™tli synchronizacji fazowej, stosowanej do synchronizacji czÄ™stotliwoÅ›ci, na 100 MHz
  75.  
  76.  
  77.     aic3204_hardware_init();
  78.     aic3204_init();
  79.     /* inicjalizacja interfejsu sprzÄ™towego, kodeka audio - aic3204
  80.      W funkcji tej miÄ™dzy innymi zostaje ustawiony bit 5 - A20_MODE na stan 1, co oznacza ze wybrana zostaÅ‚a opcja wejÅ›cia/wyjÅ›cia GPIO26 (pin 26).
  81.     Zainicjalizowana zostaje również magistrala I2C, w której ustawiono miÄ™dzy innymi preskaler oraz czÄ™stotliwoÅ›ci zegara.*/
  82.    
  83.     printf("\n\nRunning Getting Started Project\n");
  84.     printf( "<-> Audio Loopback from Stereo IN --> to HP/Lineout\n" );
  85.     // WyÅ›wietlenie tekstu na wyÅ›wietlaczu
  86.        
  87.         /* Setup sampling frequency and 30dB gain for microphone */
  88.     set_sampling_frequency_and_gain(SAMPLES_PER_SECOND, 30);
  89.     /*Funkcja w której przy zadanych parametrach nastÄ™puje pomnożenie przez 2 argumentu ADCgain (30), co skutkuje zmianÄ… kroków z 1 dB na 0.5 dB.
  90.       NastÄ™pnie zostaje zostaje sprawdzona poprawność wyboru czÄ™stotliwoÅ›ci. Jeżeli wybrana zostaÅ‚a czÄ™stotliwość inna niż : 6857, 800, 9600, 12000, 16000, 24000, 48000,
  91.       domyÅ›lnie jej wartość zostaje ustawiona na 48000. */
  92.  
  93.     asm("bclr XF");
  94.     // ustawienie XF (zewnÄ™trznej flagi wyjÅ›cia) w stan niski, co podowuje wyłączenie znajdujÄ…cej siÄ™ na module diody
  95.  
  96.         for ( i = 0  ; i < SAMPLES_PER_SECOND * 600L  ;i++  )   /* Proces próbkowania sygnaÅ‚u wejÅ›ciowego. Argument 'i' jest typu long int, aby udaÅ‚o siÄ™ przetworzyć 48000*600 próbek.*/
  97.         {
  98.                 if("XF" == 0)
  99.                 {
  100.                         USBSTK5505_waitusec(500000);    //Jeżeli zewnÄ™trzna flaga wyjÅ›cia XF jest ustawiona na stan niski, to stan wysoki załączy siÄ™ po 0.5s
  101.                         asm(" bset XF");                                //Jeżeli natomiast na XF panuje stan wysoki, to stan niski zostanie włączony po 1s.
  102.                 }
  103.                 else
  104.                         {
  105.                         USBSTK5505_waitusec(1000000);
  106.                         asm(" bclr XF");
  107.                         }
  108.  
  109.      aic3204_codec_read(&left_input, &right_input); // funkcja, która zczytuje wartoÅ›ci sygnałów na wejÅ›ciu co przerwanie
  110.    
  111.      mono_input = stereo_to_mono(left_input, right_input);
  112.      // w funkcji tej sygnaÅ‚y wejÅ›ciowe z obu kanałów zostajÄ… dodane, po czym ich wartość jest podzielona przez 2, aby zapobiec przesterowaniu
  113.      left_output =  left_input*2;
  114.      // przypisanie sygnaÅ‚u z wejÅ›cia do wyjÅ›cia. W tym wypadku wzmocnienie lewego kanaÅ‚u w stosunku do wejÅ›cia bÄ™zie dwukrotnie wiÄ™ksze
  115.      right_output = right_input;
  116.    
  117.      aic3204_codec_write(left_output, right_output); // funkcja, która wpisuje wartość sygnaÅ‚u w kodek AIC3204 zaraz przed wyjÅ›ciem
  118.         }
  119.  
  120.    /* Disable I2S and put codec into reset */
  121.     aic3204_disable();                                                  // reset kodeka i wyłączenie magistrali I2S
  122.  
  123.     printf( "\n***Program has Terminated***\n" );
  124.     SW_BREAKPOINT;      // pÄ™tla nieskoÅ„czona while
  125. }
  126.  
  127. /* ------------------------------------------------------------------------ *
  128.  *                                                                          *
  129.  *  End of main.c                                                           *
  130.  *                                                                          *
  131.  * ------------------------------------------------------------------------ */

Client/Server C program to make client send the name of a file

Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.


Solution:

Client Side:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");
scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}

Server Side:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[2000],cc[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("erroe:no port no\n usage:\n/server port no\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
listen(sockfd,10);
len=sizeof(cli);
printf("serve:\nwaiting for connection\n");
newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);
bzero(buffer,255);
n=read(newsockfd,buffer,255);
printf("\nserver recv:%s\n",buffer);
if((fd=fopen(buffer,"r"))!=NULL)
{
printf("server:%s found\n opening and reading..\n",buffer);
printf("reading..\n..reading complete");
fgets(cc,2000,fd);
while(!feof(fd))
{
fgets(c,2000,fd);
strcat(cc,c);
}
n=write(newsockfd,cc,strlen(cc));
if(n<0)
printf("error writing to socket");
printf("\ntransfer complete\n");
}
else
{
printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}


Output Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.:

Server part output


[root@localhost ~]# ./a.out 4455
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#

client part output

[root@localhost ~]# ./a.out 4455
waiting for serverserver is online
client enter the path2.c
File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar