int main(int argc, char ** argv) { (void)argc; long n = atol(argv[1]); int low,high; long i; double t; if (!pow2check(n)) { fprintf(stderr, "error : n (%ld) not a power of two\n", n); exit(1); } sample_t * buf = calloc(sizeof(sample_t), n); complex double * X = calloc(sizeof(complex double), n); complex double * Y = calloc(sizeof(complex double), n); low = atoi(argv[2]); high = atoi(argv[3]); while (1) { /* 標準入力からn個標本を読む */ ssize_t m = read_n(0, n * sizeof(sample_t), buf); if (m == 0) break; /* 複素数の配列に変換 */ sample_to_complex(buf, X, n); /* FFT -> Y */ fft(X, Y, n); // Yのバンド外を0にする // t = (double) n / SAMPLING_FREQEUENCY; // for (i = 0; i < n; ++i){ // if(i/t<low || i/t>high){ // Y[i] = 0; // } // } /* IFFT -> Z */ ifft(Y, X, n); /* 標本の配列に変換 */ complex_to_sample(Y, buf, n); /* 標準出力へ出力 */ write_n(1, m, buf); } return 0; }
int main(int argc, char *argv[]) { calculate_sample_count(); node_type_t type = 0; int port; int ss; struct sockaddr_in addr; data_t d; int s; int status = 0; pthread_t th; complex double *X = (complex double *)malloc(sizeof(complex double) * SAMPLE); complex double *Y = (complex double *)malloc(sizeof(complex double) * SAMPLE); complex short *data = (complex short *)malloc(sizeof(complex short) * N); sample_t *buf = (sample_t *)malloc(sizeof(sample_t) * SAMPLE); sample_t *prev_data = (sample_t *)malloc(sizeof(sample_t) * SAMPLE / 2); char response_send, response_recv; int m, n; if (argc == 2) { type = SERVER; } else if (argc == 3) { type = CLIENT; } else { fprintf(stderr, "Usage: %s [ip address] <port>", argv[1]); exit(1); } port = atoi(argv[argc - 1]); ss = socket(PF_INET, SOCK_STREAM, 0); if (ss == -1) die("socket"); addr.sin_family = AF_INET; addr.sin_port = htons(port); d.socket_number = &ss; d.s = &s; d.status = &status; switch (type) { case SERVER: addr.sin_addr.s_addr = INADDR_ANY; if (bind(ss, (struct sockaddr *)&addr, sizeof(addr)) == -1) die("bind"); if (listen(ss, 10) == -1) die("listen"); if (pthread_create(&th, NULL, accept_connection, (void *)&d) != 0) die("pthread_create"); fprintf(stderr, "Waiting...\n"); break; case CLIENT: if (inet_aton(argv[1], &addr.sin_addr) == 0) die("inet_aton"); if (connect(ss, (struct sockaddr *)&addr, sizeof(addr)) == -1) die("connect"); s = ss; status = 1; fprintf(stderr, "Connected to %s:%d.\n", argv[1], port); break; default: break; } memset(prev_data, 0, sizeof(sample_t) * SAMPLE / 2); while (1) { m = read_n(0, sizeof(sample_t) * SAMPLE / 2, buf + SAMPLE / 2); if (m == 0) break; if (status == 1) { memcpy(buf, prev_data, sizeof(sample_t) * SAMPLE / 2); memcpy(prev_data, buf + SAMPLE / 2, sizeof(sample_t) * SAMPLE / 2); hamming_window(buf, SAMPLE); sample_to_complex(buf, X, SAMPLE); fft(X, Y, SAMPLE); cut_off(data, Y, FREQ_MIN, FREQ_MAX, SAMPLE); set_response_with_data(&response_send, data, N); send(s, &response_send, sizeof(char), 0); switch (response_send) { case RES_OK: case RES_OK_TO_SI: write_n(s, sizeof(complex short) * N, data); // n = send(s, data, sizeof(complex short) * N, 0); break; case RES_SI: memset(prev_data, 0, sizeof(sample_t) * SAMPLE / 2); break; default: break; } // n = recv(s, &response_recv, sizeof(char), 0); n = read_n(s, sizeof(char), &response_recv); if (n == -1) die("recv"); if (n == 0) break; switch (response_recv) { case RES_OK: case RES_OK_TO_SI: n = read_n(s, sizeof(complex short) * N, data); // n = recv(s, data, sizeof(complex short) * N, 0); if (n == -1) die("recv"); if (n == 0) break; re_cut_off(data, Y, FREQ_MIN, FREQ_MAX, SAMPLE); ifft(Y, X, SAMPLE); complex_to_sample(X, buf, SAMPLE); re_hamming_window(buf, SAMPLE); write_n(1, sizeof(sample_t) * SAMPLE / 2, buf + SAMPLE / 4); break; case RES_SI: break; default: break; } } } pthread_join(th, NULL); close(ss); return 0; }
int main(int argc, char *argv[]){ int n,m,n_recv; int s,ss; struct sockaddr_in addr; // addres information for bin struct sockaddr_in client_addr; if(argc==2){ // server ss = socket(PF_INET, SOCK_STREAM, 0); addr.sin_family = AF_INET; // IPv4 addr.sin_port = htons(atoi(argv[1])); // port number to wait on addr.sin_addr.s_addr = INADDR_ANY; // any IP address can be connected if(bind(ss, (struct sockaddr *)&addr, sizeof(addr)) == -1){ die("bind"); } if(listen(ss,10) == -1){ die("listen"); } socklen_t len = sizeof(struct sockaddr_in); s = accept(ss, (struct sockaddr *)&client_addr, &len); if(s==-1){ die("accept"); } if(close(ss)==-1){ die("close"); } }else if(argc==3){ // client s = socket(PF_INET, SOCK_STREAM, 0); addr.sin_family = AF_INET; // IPv4 addr.sin_addr.s_addr = inet_addr(argv[1]); // IP address addr.sin_port = htons(atoi(argv[2])); // port number int ret = connect(s, (struct sockaddr *)&addr, sizeof(addr)); if(ret == -1){ //connect die("connect"); } } FILE *fp_rec; FILE *fp_play; if ( (fp_rec=popen("rec -q -t raw -b 16 -c 1 -e s -r 44100 - 2> /dev/null","r")) ==NULL) { die("popen:rec"); } if ( (fp_play=popen("play -t raw -b 16 -c 1 -e s -r 44100 - 2> /dev/null ","w")) ==NULL) { die("popen:play"); } // sample_t *rec_data, *play_data; int cut_low=300, cut_high=5000; int send_len = (cut_high-cut_low)*N/SAMPLING_FREQEUENCY; sample_t * rec_data = malloc(sizeof(sample_t)*N); double * send_data = malloc(sizeof(double)*send_len*2); double * recv_data = malloc(sizeof(double)*send_len*2); sample_t * play_data = malloc(sizeof(sample_t)*N); complex double * X = calloc(sizeof(complex double), N); complex double * Y = calloc(sizeof(complex double), N); complex double * Z = calloc(sizeof(complex double), N); complex double * W = calloc(sizeof(complex double), N); int re=0, r; while(1){ // ssize_t m = fread_n(*fp_rec, n * sizeof(sample_t), rec_data); // 必ずNバイト読む re = 0; while(re<N){ r=fread(rec_data+re,sizeof(sample_t),N/sizeof(sample_t)-re,fp_rec); if(r==-1) die("fread"); if(r==0) break; re += r; } // n=fread(rec_data,sizeof(sample_t),N/sizeof(sample_t),fp_rec); // re = 0; // re = fread(rec_data,sizeof(sample_t), N-re, fp_rec); memset(rec_data+re,0,N-re); // 複素数の配列に変換 sample_to_complex(rec_data, X, N); // /* FFT -> Y */ fft(X, Y, N); // Yの一部を送る int i; for(i=cut_low*N/SAMPLING_FREQEUENCY;i<cut_low*N/SAMPLING_FREQEUENCY+send_len;i++){ send_data[2*i]=creal(Y[i]); send_data[2*i+1]=cimag(Y[i]); } // if(send_all(s,(char *)send_data,sizeof(long)*send_len*2)==-1){ // die("send"); // } memset(W,0+0*I,N*sizeof(complex double)); memset(Z,0+0*I,N*sizeof(complex double)); // memset(play_data,0,sizeof(long)*send_len*2); // if(recv_all(s,(char *)recv_data,sizeof(long)*send_len*2)==-1){ // die("recv"); // } for(i=cut_low*N/SAMPLING_FREQEUENCY;i<cut_low*N/SAMPLING_FREQEUENCY+send_len;i++){ W[i]=(double)send_data[2*i]+(double)send_data[2*i+1]*I; } // /* IFFT -> Z */ ifft(W, Z, N); // // 標本の配列に変換 complex_to_sample(Z, play_data, N); // /* 標準出力へ出力 */ // write_n(1, N, send_data); write_n(1, N, play_data); // fwrite(play_data,sizeof(sample_t),N/sizeof(sample_t),fp_play); // write(1,recv_data,n_recv); } close(s); }