📄 tsphere.c
字号:
SP_FILE *sp; SP_INTEGER bps, nc=MC_CHAN, sc=MC_BUFS, sr=8000, r_nc, r_bps, r_sc; int c,s,cc,failure,rtn; char *out_file="testing.wav"; double factor; int ntypes=5, cur_type; union{ short **b_short; int **b_int; long **b_long; float **b_float; double **b_double; } buf; union{ short *b_short; int *b_int; long *b_long; float *b_float; double *b_double; } time_samp; char *types[5]; types[0] = "short"; types[1] = "int"; types[2] = "long"; types[3] = "float"; types[4] = "double"; fprintf(spfp,"-- Write of a multichannel data with varying types\n"); for (cur_type=0; cur_type<ntypes; cur_type++){ switch (cur_type){ case 0: bps=sizeof(short); break; case 1: bps=sizeof(int); break; case 2: bps=sizeof(long); break; case 3: bps=sizeof(float); break; case 4: bps=sizeof(double); break; } fprintf(spfp,"---- using type '%s', %d bytes ",types[cur_type],bps); fprintf(spfp,"per sample, %d channels, %d samples\n",nc,sc); /* set the byte size and allocate memory for the buffers */ switch (cur_type){ case 0: alloc_2dimarr(buf.b_short,nc,sc,short); alloc_singarr(time_samp.b_short,nc,short); break; case 1: alloc_2dimarr(buf.b_int,nc,sc,int); alloc_singarr(time_samp.b_int,nc,int); break; case 2: alloc_2dimarr(buf.b_long,nc,sc,long); alloc_singarr(time_samp.b_long,nc,long); break; case 3: alloc_2dimarr(buf.b_float,nc,sc,float); alloc_singarr(time_samp.b_float,nc,float); break; case 4: alloc_2dimarr(buf.b_double,nc,sc,double); alloc_singarr(time_samp.b_double,nc,double); break; } /* load the values in the buffer */ for (c=0; c<MC_CHAN; c++){ factor = 1.0 / ((c+1) * 10.0 ); switch (cur_type){ case 0: for (s=0; s<MC_BUFS; s++) buf.b_short[c][s] = (short)((c+1) * 10 * cos( M_PI * 2.0 * s * factor)); break; case 1: for (s=0; s<MC_BUFS; s++) buf.b_int[c][s] = (int)((c+1) * 10 * cos( M_PI * 2.0 * s * factor)); break; case 2: for (s=0; s<MC_BUFS; s++) buf.b_long[c][s] = (long)((c+1) * 10 * cos( M_PI * 2.0 * s * factor)); break; case 3: for (s=0; s<MC_BUFS; s++) buf.b_float[c][s] = (float)((c+1) * 10 * cos( M_PI * 2.0 * s * factor)); break; case 4: for (s=0; s<MC_BUFS; s++) buf.b_double[c][s] = (double)((c+1) * 10 * cos( M_PI * 2.0 * s * factor)); break; } } /* open a sphere file to write out to */ if ((sp=sp_open(out_file,"w")) == SPNULL) { fprintf(spfp," sp_open failed for file '%s' opened for write\n", out_file); sp_print_return_status(spfp); exit(-1); } /* set up the header */ /* set the channel count */ if (sp_h_set_field(sp,CHANNEL_COUNT_FIELD,T_INTEGER,(void *)&nc) != 0){ fprintf(spfp," sp_h_set_fielcase d: failed on field '%s'\n", CHANNEL_COUNT_FIELD); sp_print_return_status(spfp); exit(-1); } /* set the sample_n_bytes */ if (sp_h_set_field(sp,SAMPLE_N_BYTES_FIELD,T_INTEGER,(void *)&bps)!=0){ fprintf(spfp," sp_h_set_field: failed on field '%s'\n", SAMPLE_N_BYTES_FIELD); sp_print_return_status(spfp); exit(-1); } /* set the sample_count */ if (sp_h_set_field(sp,SAMPLE_COUNT_FIELD,T_INTEGER,(void *)&sc) != 0){ fprintf(spfp," sp_h_set_field: failed on field '%s'\n", SAMPLE_COUNT_FIELD); sp_print_return_status(spfp); exit(-1); } /* set the sample_rate */ if (sp_h_set_field(sp,SAMPLE_RATE_FIELD,T_INTEGER,(void *)&sr) != 0){ fprintf(spfp," sp_h_set_field: failed on field '%s'\n", SAMPLE_RATE_FIELD); sp_print_return_status(spfp); exit(-1); } /* set the sample_coding */ if (sp_h_set_field(sp,SAMPLE_CODING_FIELD,T_STRING,(void *)"raw") !=0){ fprintf(spfp," sp_h_set_field: failed on field '%s'\n", SAMPLE_CODING_FIELD); sp_print_return_status(spfp); exit(-1); } /* write the data to the file, INTERLEAVED!!!!! */ for (s=0; s<MC_BUFS; s++){ switch (cur_type){ case 0: for (c=0; c<MC_CHAN; c++) time_samp.b_short[c] = buf.b_short[c][s]; rtn = sp_write_data((void *)time_samp.b_short,bps,1,sp); break; case 1: for (c=0; c<MC_CHAN; c++) time_samp.b_int[c] = buf.b_int[c][s]; rtn = sp_write_data((void *)time_samp.b_int,bps,1,sp); break; case 2: for (c=0; c<MC_CHAN; c++) time_samp.b_long[c] = buf.b_long[c][s]; rtn = sp_write_data((void *)time_samp.b_long,bps,1,sp); break; case 3: for (c=0; c<MC_CHAN; c++) time_samp.b_float[c] = buf.b_float[c][s]; rtn = sp_write_data((void *)time_samp.b_float,bps,1,sp); break; case 4: for (c=0; c<MC_CHAN; c++) time_samp.b_double[c] = buf.b_double[c][s]; rtn = sp_write_data((void *)time_samp.b_double,bps,1,sp); break; default: rtn = 0; } if (rtn != 1){ fprintf(spfp, "Error: Multichannel write failed on time sample %d\n", s); sp_print_return_status(spfp); exit(-1); } } /* Close the output file */ if (sp_close(sp) != 0){ fprintf(spfp,"Erro: Close of multichannel file failed\n"); sp_print_return_status(spfp); exit(-1); } /*** NOW Read the file back in, comparing it to the form in memory ***/ if ((sp=sp_open(out_file,"rv")) == SPNULL) { fprintf(spfp," sp_open failed for file '%s' opened for read\n", out_file); sp_print_return_status(spfp); exit(-1); } /* get the channel count */ if (sp_h_get_field(sp,CHANNEL_COUNT_FIELD,T_INTEGER,(void *)&r_nc)!=0){ fprintf(spfp," sp_h_get_field: failed on field '%s'\n", CHANNEL_COUNT_FIELD); sp_print_return_status(spfp); exit(-1); } /* get the sample_n_bytes */ if (sp_h_get_field(sp,SAMPLE_N_BYTES_FIELD,T_INTEGER,(void *)&r_bps) != 0){ fprintf(spfp," sp_h_get_field: failed on field '%s'\n", SAMPLE_N_BYTES_FIELD); sp_print_return_status(spfp); exit(-1); } /* get the sample_count */ if (sp_h_get_field(sp,SAMPLE_COUNT_FIELD,T_INTEGER,(void *)&r_sc)!= 0){ fprintf(spfp," sp_h_get_field: failed on field '%s'\n", SAMPLE_COUNT_FIELD); sp_print_return_status(spfp); exit(-1); } /* verify the header fields */ if (nc != r_nc){ fprintf(spfp,"Error: read channel_count (%d) != expected (%d)\n", r_nc,nc); exit(-1); } if (bps != r_bps){ fprintf(spfp,"Error: read sample_n_bytes (%d) != expected (%d)\n" ,r_nc,nc); exit(-1); } if (sc != r_sc){ fprintf(spfp,"Error: read sample_count (%d) != expected (%d)\n", r_nc,nc); exit(-1); } if (0) switch (cur_type){ case 0: buf.b_short[1][2] = 32; break; case 1: buf.b_int[1][2] = 32; break; case 2: buf.b_long[1][2] = 32; break; case 3: buf.b_float[1][2] = 32; break; case 4: buf.b_double[1][2] = 32; break; default: fprintf(spfp, "type %d not defined\n",cur_type); exit(-1); } /* loop through reading the data, verifying it against the memory */ /* version */ for (s=0; s<MC_BUFS; s++){ char *mem, *file; switch (cur_type){ case 0: file=(char *)(time_samp.b_short); break; case 1: file=(char *)(time_samp.b_int); break; case 2: file=(char *)(time_samp.b_long); break; case 3: file=(char *)(time_samp.b_float); break; case 4: file=(char *)(time_samp.b_double); break; default: fprintf(spfp, "type %d not defined\n",cur_type); exit(-1); } if (sp_read_data((void *)file,r_bps,1,sp) != 1){ fprintf(spfp, "Error: Multichannel read failed on time sample %d\n", s); sp_print_return_status(spfp); exit(-1); } for (c=0, failure=0; c<MC_CHAN; c++){ switch (cur_type){ case 0: mem = (char *)&(buf.b_short[c][s]); file=(char *)&(time_samp.b_short[c]); break; case 1: mem = (char *)&(buf.b_int[c][s]); file=(char *)&(time_samp.b_int[c]); break; case 2: mem = (char *)&(buf.b_long[c][s]); file=(char *)&(time_samp.b_long[c]); break; case 3: mem = (char *)&(buf.b_float[c][s]); file=(char *)&(time_samp.b_float[c]); break; case 4: mem = (char *)&(buf.b_double[c][s]); file=(char *)&(time_samp.b_double[c]); break; default: fprintf(spfp, "type %d not defined\n",cur_type); exit(-1); } if (memcmp(mem,file,bps)) failure = 1; } if (failure){ fprintf(spfp,"Error Multichannel failed to match"); fprintf(spfp,"memory version on sample %d\n",s); fprintf(spfp,"Memory: "); switch (cur_type){ case 0: file=(char *)(time_samp.b_short); break; case 1: file=(char *)(time_samp.b_int); break; case 2: file=(char *)(time_samp.b_long); break; case 3: file=(char *)(time_samp.b_float); break; case 4: file=(char *)(time_samp.b_double); break; default: fprintf(spfp, "type %d not defined\n",cur_type); exit(-1); } for (c=0; c<MC_CHAN; c++){ switch (cur_type){ case 0: mem = (char *)&(buf.b_short[c][s]); break; case 1: mem = (char *)&(buf.b_int[c][s]); break; case 2: mem = (char *)&(buf.b_long[c][s]); break; case 3: mem = (char *)&(buf.b_float[c][s]); break; case 4: mem = (char *)&(buf.b_double[c][s]); break; default: fprintf(spfp,"type %d not defined\n",cur_type); exit(-1); } for (cc=0; cc<bps; cc++) fprintf(spfp," %2x", *((unsigned char *)( mem + cc ))); fprintf(spfp," |"); } fprintf(spfp,"\nFile: "); for (c=0; c<MC_CHAN; c++){ for (cc=0; cc<bps; cc++) fprintf(spfp," %2x", *((unsigned char *)( file + c*bps + cc ))); fprintf(spfp," |"); } fprintf(spfp,"\n"); /* exit(-1); */ } } /* free the memory associated with the type */ switch (cur_type){ case 0: free_2dimarr(buf.b_short,nc,short); free_singarr(time_samp.b_short,short); break; case 1: free_2dimarr(buf.b_int,nc,int); free_singarr(time_samp.b_int,int); break; case 2: free_2dimarr(buf.b_long,nc,long); free_singarr(time_samp.b_long,long); break; case 3: free_2dimarr(buf.b_float,nc,float); free_singarr(time_samp.b_float,float); break; case 4: free_2dimarr(buf.b_double,nc,double); free_singarr(time_samp.b_double,double); break; } if (sp != SPNULL) sp_close(sp); } fprintf(spfp,"\n");}/*****************************************************************************/void header_test(void){ SP_FILE *sp; int rtn; char *str; long lint; double real; fprintf(spfp,"-- Write Mode header operations:\n"); system("rm -f testing.wav"); if ((sp=sp_open("testing.wav","w")) == SPNULL) { fprintf(spfp," sp_open: Valid write open failed\n"); sp_print_return_status(spfp); exit(-1); } fprintf(spfp,"---- Testing the file header io:\n"); fprintf(spfp,"------ Field creation:\n"); if (sp_h_set_field(SPNULL,"field1",T_STRING,(void *)"char string 1") == 0) fprintf(spfp," sp_h_set_field: Null SPFILE pointer failed\n"); if (sp_h_set_field(sp,CNULL,T_STRING,(void *)"char string 1") == 0) fprintf(spfp," sp_h_set_field: Null field name failed\n"); if (sp_h_set_field(sp,"field1",4930,(void *)"char string 1") == 0) fprintf(spfp," sp_h_set_field: Invalid field type failed\n"); if (sp_h_set_field(sp,"field1",T_STRING,(void *)CNULL) == 0) fprintf(spfp," sp_h_set_field: Null value failed\n"); if (sp_h_set_field(sp,"field1",T_STRING,(void *)"string value1") != 0){ fprintf(spfp," sp_h_set_field: valid STRING command failed\n"); sp_print_return_status(spfp); } lint=1; if (sp_h_set_field(sp,"field2",T_INTEGER,(void *)&lint) != 0){ fprintf(spfp," sp_h_set_field: valid INTEGER command failed\n"); sp_print_return_status(spfp); exit(-1); } real=2.0; if (sp_h_set_field(sp,"field3",T_REAL,(void *)&real) != 0){ fprintf(spfp," sp_h_set_field: valid REAL command failed\n"); sp_print_return_status(spfp); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -