📄 tsphere.c
字号:
if (sp_h_set_field(spp, "sample_n_bytes", T_INTEGER, (void *) &nbyte)){ fprintf(spfp,"Error: unable to set sample_n_bytes field\n"); sp_print_return_status(spfp); exit(-1); } if (sp_h_set_field(spp, "sample_byte_format", T_STRING, (void *)"10")){ fprintf(spfp,"Error: unable to set sample_byte_format field\n"); sp_print_return_status(spfp); exit(-1); } if (sp_h_set_field(spp, "sample_coding", T_STRING, (void *)"pcm")){ fprintf(spfp,"Error: unable to set sample_coding field\n"); sp_print_return_status(spfp); exit(-1); } lint = 1; if (sp_h_set_field(spp, "channel_count", T_INTEGER, (void *)&lint)){ fprintf(spfp,"Error: unable to set channel_count field\n"); sp_print_return_status(spfp); exit(-1); } if (sp_set_data_mode(spp,"SE-PCM-2") != 0){ fprintf(spfp,"Error: sp_set_data_mode failed\n"); sp_print_return_status(spfp); exit(-1); } fprintf(spfp,"---- Allocating a waveform buffer\n"); if ((wavbuf=(short *)sp_data_alloc(spp,buf_nsamp)) == (short *)0){ fprintf(spfp,"Unable to malloc memory for wav buffer\n"); exit(-1); } factor = 1.0 / 100.0 ; for (i=0; i<buf_nsamp; i++) wavbuf[i] = (short)(1000 * cos( M_PI * 2.0 * i * factor)); fprintf(spfp,"---- Writing the waveform\n"); while (samps_written < size){ stow = (samps_written + buf_nsamp) < size ? buf_nsamp : size - samps_written; status = sp_write_data((void *)wavbuf, sizeof(short), stow, spp); if (status != stow){ fprintf(spfp,"Couldn't write NIST waveform file: %s\n", name); sp_print_return_status(spfp); status = sp_error(spp); sp_print_return_status(spfp); sp_close(spp); (void) mtrf_free((char *)wavbuf); exit(-1); } samps_written += stow; } fprintf(spfp,"---- Closing the file\n"); sp_data_free(spp,wavbuf); if (sp_close(spp) != 0) { fprintf(spfp,"SP_close failed\n"); sp_print_return_status(spfp); exit(-1); } fprintf(spfp,"\n"); unlink(name); }void ulaw_test(void){ fprintf(spfp,"-- Ulaw Test conversions\n"); fprintf(spfp,"---- Test creation from the original\n"); waveform_update(EX1_10,"SE-ULAW:SBF-1",EX4_ULAW); waveform_update(EX1_01,"SE-ULAW:SBF-1",EX4_ULAW); fprintf(spfp,"---- Write as 2 Byte PCM file\n"); waveform_update(EX4_ULAW,"SE-PCM-2:SBF-10",EX4_ULAW_10); waveform_update(EX4_ULAW,"SE-PCM-2:SBF-01",EX4_ULAW_01); waveform_update(EX4_ULAW,"SE-SHORTEN",EX4_ULAW_SHORTEN); waveform_update(EX4_ULAW,"SE-WAVPACK",EX4_ULAW_WAVPACK); waveform_update(EX4_ULAW_WAVPACK,"SE-ULAW",EX4_ULAW); waveform_update(EX4_ULAW_SHORTEN,"SE-ULAW",EX4_ULAW); waveform_update(EX4_ULAW_WAVPACK,"SE-PCM-2:SBF-10",EX4_ULAW_10); waveform_update(EX4_ULAW_SHORTEN,"SE-PCM-2:SBF-01",EX4_ULAW_01); fprintf(spfp,"---- Read as 2 Byte PCM file\n"); converted_read_check(EX4_ULAW,EX4_ULAW_10,"SE-PCM-2:SBF-10"); converted_read_check(EX4_ULAW,EX4_ULAW_10,"SE-PCM-2:SBF-01"); converted_read_check(EX4_ULAW,EX4_ULAW_01,"SE-PCM-2:SBF-10"); converted_read_check(EX4_ULAW,EX4_ULAW_01,"SE-PCM-2:SBF-01"); fprintf(spfp,"---- Read as 1 Byte ULAW file\n"); converted_read_check(EX1_10,EX4_ULAW,"SE-ULAW:SBF-1"); converted_read_check(EX1_01,EX4_ULAW,"SE-ULAW:SBF-1"); fprintf(spfp,"\n");}void converted_read_check(char *file1, char *file2, char *conv){ fprintf(spfp,"------ File: %s converted by %s, compared to %s\n", file1,conv,file2); if (diff_waveforms(file1,file2,conv,conv,1,spfp) > 0) exit(-1);}void two_channel_test(void){ fprintf(spfp,"-- Two Channel READ test:\n"); fprintf(spfp,"---- PCM stereo files compared to both single channels:\n"); if (perform_2_channel_read_test(EX5_2CHAN_PCM,"", EX5_CHAN1_PCM,EX5_CHAN2_PCM) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_PCM_WAVPACK,"", EX5_CHAN1_PCM,EX5_CHAN2_PCM) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_PCM_SHORTEN,"", EX5_CHAN1_PCM,EX5_CHAN2_PCM) > 0) exit(-1); fprintf(spfp,"---- ULAW stereo files compared to both single channels:\n"); if (perform_2_channel_read_test(EX5_2CHAN,"", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_SHORTEN,"", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_WAVPACK,"", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); fprintf(spfp,"---- ULAW stereo files converted to PCM, "); fprintf(spfp,"compared to both single channels:\n"); if (perform_2_channel_read_test(EX5_2CHAN,"SE-PCM",EX5_CHAN1_PCM, EX5_CHAN2_PCM) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_SHORTEN,"SE-PCM", EX5_CHAN1_PCM,EX5_CHAN2_PCM) > 0) exit(-1); if (perform_2_channel_read_test(EX5_2CHAN_WAVPACK,"SE-PCM", EX5_CHAN1_PCM,EX5_CHAN2_PCM) > 0) exit(-1); fprintf(spfp,"---- ULAW stereo files converted to PCM stereo files:\n"); converted_read_check(EX5_2CHAN,EX5_2CHAN_PCM,"SE-PCM:SBF-10"); converted_read_check(EX5_2CHAN_WAVPACK,EX5_2CHAN_PCM,"SE-PCM:SBF-10"); converted_read_check(EX5_2CHAN_SHORTEN,EX5_2CHAN_PCM,"SE-PCM:SBF-10"); fprintf(spfp,"-- Two Channel WRITE test\n"); fprintf(spfp,"---- 2 ULAW single channel files, creating a stereo file\n"); if (perform_2_channel_write_test(EX5_2CHAN,"", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); if (perform_2_channel_write_test(EX5_2CHAN_SHORTEN,"SE-SHORTEN", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); if (perform_2_channel_write_test(EX5_2CHAN_WAVPACK,"SE-WAVPACK", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); fprintf(spfp,"---- 2 PCM single channel files, creating a stereo file\n"); if (perform_2_channel_write_test(EX5_2CHAN_PCM,"",EX5_CHAN1_PCM, EX5_CHAN2_PCM) > 0) exit(-1); if (perform_2_channel_write_test(EX5_2CHAN_PCM_SHORTEN,"SE-SHORTEN", EX5_CHAN1_PCM,EX5_CHAN2_PCM) >0) exit(-1); if (perform_2_channel_write_test(EX5_2CHAN_PCM_WAVPACK,"SE-WAVPACK", EX5_CHAN1_PCM,EX5_CHAN2_PCM) >0) exit(-1); fprintf(spfp, "---- 2 ULAW single channel files, creating a PCM stereo file\n"); if (perform_2_channel_write_test(EX5_2CHAN_PCM,"SE-PCM", EX5_CHAN1,EX5_CHAN2) > 0) exit(-1); fprintf(spfp,"\n");}int perform_2_channel_write_test(char *combined_wav, char *conversion, char *chan1_wav, char *chan2_wav){ SP_FILE *new_sp=SPNULL, *chan1_sp=SPNULL, *chan2_sp=SPNULL; char *new_buf=CNULL, *new_ptr, *chan1_buf=CNULL, *chan2_buf=CNULL; char *new_wav="output.wav"; SP_INTEGER lint, chan1_snb, chan2_snb, new_snb; size_t s, i; size_t frame_size=160; int return_value; fprintf(spfp,"------ Creating stereo file from '%s' and ,",chan1_wav); fprintf(spfp,"'%s' conversions '%s', verifying to file '%s'\n", chan2_wav, conversion ,combined_wav); /******** Channel 1 SP_FILE ****************/ if ((chan1_sp = sp_open(chan1_wav, "rv")) == SPNULL) { fprintf(spfp,"Couldn't open for read-verified single "); fprintf(spfp,"channel file: %s", chan1_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if ((chan1_buf = (char *)sp_data_alloc(chan1_sp,frame_size)) == CNULL){ fprintf(spfp,"Unable to allocate waveform data for "); fprintf(spfp,"file '%s'\n",chan1_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if (sp_h_get_field(chan1_sp,SAMPLE_N_BYTES_FIELD,T_INTEGER, (void *)&chan1_snb) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", SAMPLE_N_BYTES_FIELD,chan1_wav); goto FATAL_QUIT; } /******** Channel 2 SP_FILE ****************/ if ((chan2_sp = sp_open(chan2_wav, "rv")) == SPNULL) { fprintf(spfp,"Couldn't open for read-verified single "); fprintf(spfp,"channel file: %s", chan2_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if ((chan2_buf = (char *)sp_data_alloc(chan2_sp,frame_size)) == CNULL){ fprintf(spfp,"Unable to allocate waveform data for "); fprintf(spfp,"file '%s'\n",chan2_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if (sp_h_get_field(chan2_sp,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&chan2_snb) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", SAMPLE_N_BYTES_FIELD,chan1_wav); goto FATAL_QUIT; } if ((new_sp = sp_open(new_wav, "wv")) == SPNULL) { fprintf(spfp,"Couldn't open for write-verified 2 channel "); fprintf(spfp,"file: %s", new_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } /* fill the header with fields from channel 1 */ if (sp_copy_header(chan1_sp, new_sp) > 0){ fprintf(spfp,"Couldn't duplicate the header in file "); fprintf(spfp,"'%s' for file '%s'\n",chan1_wav,new_wav); sp_print_return_status(spfp); } if (sp_h_get_field(new_sp,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&new_snb) != 0){ fprintf(spfp,"Unable to retieve %s",SAMPLE_N_BYTES_FIELD); fprintf(spfp," field from file '%s'\n",new_wav); goto FATAL_QUIT; } /* now reset the appropriate fields */ /* set the channel count to 2 */ lint = 2; if (sp_h_set_field(new_sp,CHANNEL_COUNT_FIELD, T_INTEGER,(void *)&lint) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", CHANNEL_COUNT_FIELD,new_wav); goto FATAL_QUIT; } sp_h_delete_field(new_sp,SAMPLE_CHECKSUM_FIELD); if ((new_buf = (char *)sp_data_alloc(new_sp,frame_size)) == CNULL){ fprintf(spfp,"Unable to allocate waveform data for file '%s'\n", new_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if (! strsame(conversion,"") ) if (sp_set_data_mode(new_sp,conversion) != 0){ fprintf(spfp,"Error: sp_set_data_mode to "); fprintf(spfp,"'%s' failed on file '%s'\n",conversion,new_wav); sp_print_return_status(spfp); } /* now read through the 2 channel files, interleaving them and writing */ /* them to the new_sp just cycle through the data checking to make */ /* sure the interleaving is correct */ do { s = sp_read_data(chan1_buf,chan1_snb,frame_size,chan1_sp); if ((sp_get_return_status() > 0) && (s == 0)) { fprintf(spfp,"Unable to read samples from channel 1 file\n"); sp_print_return_status(spfp); goto FATAL_QUIT; } if (sp_read_data(chan2_buf,chan2_snb,s,chan2_sp) != s){ fprintf(spfp,"Unable to read %d samples from file %s\n", s,chan2_wav); goto FATAL_QUIT; } new_ptr = new_buf; /* interleave the samples */ for (i=0; i<s; i++) { memcpy(new_ptr + i*2*chan1_snb, chan1_buf + i*chan1_snb, chan1_snb); memcpy(new_ptr + i*2*chan2_snb + chan2_snb, chan2_buf + i*chan2_snb, chan2_snb); } /* write the buffer out */ if (sp_write_data(new_buf,new_snb,s,new_sp) != s){ fprintf(spfp,"Error: Unable to write %d samples to file '%s'\n", s,new_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } } while (s > 0); /* everything is ok */ return_value = 0; goto CLEAN_UP; FATAL_QUIT: /* Failed routine */ return_value = 100; CLEAN_UP: if (new_sp != SPNULL) { if (new_buf != CNULL) sp_data_free(new_sp,new_buf); if (sp_close(new_sp) != 0){ fprintf(spfp,"Error: Unable to close stereo file '%s'\n", new_wav); sp_print_return_status(spfp); return_value = 200; } } if (chan1_sp != SPNULL) { if (chan1_buf != CNULL) sp_data_free(chan1_sp,chan1_buf); sp_close(chan1_sp); } if (chan2_sp != SPNULL) { if (chan2_buf != CNULL) sp_data_free(chan2_sp,chan2_buf); sp_close(chan2_sp); } if (return_value == 0){ int rtn; if (diff_waveforms(combined_wav,new_wav,CNULL,CNULL,1,spfp) != 0){ fprintf(spfp,"Successful stereo file creation BUT failed "); fprintf(spfp,"waveform verification\n"); exit(-1); } if ((rtn = diff_data(combined_wav,new_wav,1,spfp)) != 0){ fprintf(spfp,"Warning: files '%s' and '%s' decode to the", combined_wav,new_wav); if (rtn == 100) fprintf(spfp," same form, but are not identical on disk\n"); else fprintf(spfp," same form, but are byte-swapped on disk\n"); warning++; } unlink(new_wav); } return(return_value);}int perform_2_channel_read_test(char *combined_wav, char *conversion, char *chan1_wav, char *chan2_wav){ SP_FILE *comb_sp=SPNULL, *chan1_sp=SPNULL, *chan2_sp=SPNULL; char *comb_buf=CNULL, *chan1_buf=CNULL, *chan2_buf=CNULL; SP_INTEGER chan1_snb, chan2_snb, comb_snb; size_t s, i; int frame_size=160; int return_value; fprintf(spfp, "------ File '%s', converted by '%s' ",combined_wav,conversion); fprintf(spfp,"compared to channel 1 '%s' and channel 2 '%s'\n", chan1_wav, chan2_wav); if ((comb_sp = sp_open(combined_wav, "rv")) == SPNULL) { fprintf(spfp,"Couldn't open for read-verified 2 channel "); fprintf(spfp,"file: %s", combined_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } /* convert the input format of the stereo file */ if (! strsame(conversion,"") ) if (sp_set_data_mode(comb_sp,conversion) != 0){ fprintf(spfp,"Error: sp_set_data_mode to "); fprintf(spfp,"'%s' failed on file '%s'\n", conversion,combined_wav); sp_print_return_status(spfp); } if ((comb_buf = (char *)sp_data_alloc(comb_sp,frame_size)) == CNULL){ fprintf(spfp,"Unable to allocate memory for "); fprintf(spfp,"file '%s'\n",combined_wav); sp_print_return_status(spfp); goto FATAL_QUIT; } if (sp_h_get_field(comb_sp,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&comb_snb) != 0){ fp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -