📄 sdatamod.c
字号:
#include <stdio.h>#define SPHERE_LIBRARY_CODE#include <sp/sphere.h>#include <string.h>int parse_channel_selection(char *, SP_FILE *sp);struct header_t *global_header;/* * * set_data_mode * * */int sp_set_data_mode(SP_FILE *sp, char *mode){ char *mode_str, *mstr, *proc="sp_set_data_mode " SPHERE_VERSION_STR; int se_set=0, sbf_set=0, df_set=0; enum SP_sample_encoding new_encoding, old_encoding = SP_se_null; enum SP_waveform_comp new_compress, old_compress = SP_wc_null; enum SP_sample_byte_fmt new_sbf , old_sbf = SP_sbf_null; enum SP_data_format new_data_fmt, old_data_fmt = SP_df_null; if (sp_verbose > 10) fprintf(spfp,"Proc %s: \n",proc); if (sp_verbose > 15) fprintf(spfp,"Proc %s: mode string '%s'\n",proc,mode); if (sp == SPNULL) return_err(proc,100,100,"Null SPFILE"); if (mode == CNULL) return_err(proc,101,101,"Null mode string"); /* Set up the old file status. */ switch (sp->open_mode){ case SP_mode_read: if (sp_verbose > 15) fprintf(spfp,"Proc %s: read filename %s\n",proc, sp->read_spifr->status->external_filename); if (sp->read_spifr->status->read_occured_flag) return_err(proc,1000,1000, "Call executed after READ occured\n"); old_encoding = sp->read_spifr->status->file_encoding ; old_compress = sp->read_spifr->status->file_compress ; old_sbf = sp->read_spifr->status->file_sbf ; old_data_fmt = sp->read_spifr->status->user_data_fmt ; /* set up the default operation modes */ new_compress = SP_wc_none; new_sbf = SP_sbf_N; new_encoding = old_encoding; new_data_fmt = old_data_fmt; break; case SP_mode_update: if (old_encoding != sp->write_spifr->status->user_encoding){ /* change the data mode write, then delete the sample checksum, */ /* from the header because it's no longer valid */ if (sp_verbose > 15){ fprintf(spfp,"Proc %s: Sample encoding changed, ",proc); fprintf(spfp,"re-generating a checksum\n"); } sp->open_mode = SP_mode_write; sp_h_delete_field(sp,SAMPLE_CHECKSUM_FIELD); sp->open_mode = SP_mode_update; } /* There should be NO 'break' command here, it must fail through */ /* to the SP_mode_write clause */ case SP_mode_write: if (sp_verbose > 15) fprintf(spfp,"Proc %s: write/update filename %s\n",proc, sp->write_spifr->status->external_filename); if (sp->write_spifr->status->write_occured_flag) return_err(proc,1001,1001,"Call executed after WRITE occured\n"); old_encoding = sp->write_spifr->status->user_encoding ; old_compress = sp->write_spifr->status->user_compress ; old_sbf = sp->write_spifr->status->user_sbf ; old_data_fmt = sp->write_spifr->status->user_data_fmt ; /* set up the default operation modes */ new_compress = old_compress; new_sbf = SP_sbf_N; new_encoding = old_encoding; new_data_fmt = old_data_fmt; break; default: return_err(proc,106,106, "Unknown file open mode in SPFILE structure"); } if (sp_verbose > 15){ fprintf(spfp,"Proc %s: Before mode parsing: ",proc); fprintf(spfp,"old_encoding %10s new_encoding %10s\n", enum_str_SP_sample_encoding(old_encoding), enum_str_SP_sample_encoding(new_encoding)); fprintf(spfp,"Proc %s: Before mode parsing: ",proc); fprintf(spfp,"old_compress %10s new_compress %10s\n", enum_str_SP_waveform_comp(old_compress), enum_str_SP_waveform_comp(new_compress)); fprintf(spfp,"Proc %s: Before mode parsing: ",proc); fprintf(spfp,"old_sbf %10s new_sbf %10s\n", enum_str_SP_sample_byte_fmt(old_sbf), enum_str_SP_sample_byte_fmt(new_sbf)); fprintf(spfp,"Proc %s: Before mode parsing: ",proc); fprintf(spfp,"old_data_fmt %10s new_data_fmt %10s\n", enum_str_SP_data_format(old_data_fmt), enum_str_SP_data_format(new_data_fmt)); } /* the tokenization method is data destructive , so */ /* make a local copy */ mode_str=mtrf_strdup(mode); /* begin parsing each token */ mstr = strtok(mode_str,":"); while (mstr != CNULL){ if (sp_verbose > 15) fprintf(spfp,"Proc %s: token found = %s\n",proc,mstr); if ((strsame(mstr,"SE-PCM")) || (strsame(mstr,"SE-PCM-2"))) { new_encoding = SP_se_pcm2; new_compress = SP_wc_none; se_set++; } else if (strsame(mstr,"SE-PCM-1")) { new_encoding = SP_se_pcm1; new_compress = SP_wc_none; se_set++; } else if (strsame(mstr,"SE-RAW")) { new_encoding = SP_se_raw; new_compress = SP_wc_none; se_set++; } else if (strsame(mstr,"SE-ULAW")) { new_encoding = SP_se_ulaw; new_compress = SP_wc_none; se_set++; } else if (strsame(mstr,"SE-SHORTEN")){ new_encoding = old_encoding; new_compress = SP_wc_shorten; se_set++; } else if (strsame(mstr,"SE-WAVPACK")){ new_encoding = old_encoding; new_compress = SP_wc_wavpack; se_set++; } else if (strsame(mstr,"SE-SHORTPACK")){ new_encoding = old_encoding; new_compress = SP_wc_shortpack; se_set++; } else if (strsame(mstr,"SE-ORIG")){ new_encoding = old_encoding; new_compress = old_compress; se_set++; } else if (strsame(mstr,"SBF-01")) { new_sbf = SP_sbf_01; sbf_set++; } else if (strsame(mstr,"SBF-10")) { new_sbf = SP_sbf_10; sbf_set++; } else if (strsame(mstr,"SBF-1")) { new_sbf = SP_sbf_1; sbf_set++; } else if (strsame(mstr,"SBF-N")) { new_sbf = SP_sbf_N; sbf_set++; } else if (strsame(mstr,"SBF-ORIG")) { new_sbf = old_sbf; sbf_set++; } else if (strsame(mstr,"DF-RAW")) { new_data_fmt = SP_df_raw; df_set++; } else if (strsame(mstr,"DF-ARRAY")) { new_data_fmt = SP_df_array; df_set++; } else if (strncmp(mstr,"CH-",3) == 0) { if (parse_channel_selection(mstr+3,sp) != 0) return_err(proc,105,105, rsprintf("Parse channel selection string '%s'", get_return_status_message())); } else { mtrf_free(mode_str); return_err(proc,102,102, rsprintf("Illegal token '%s' in mode string '%s'", mstr,mode)); } mstr = strtok(CNULL,":"); } mtrf_free(mode_str); if (se_set > 1) return_err(proc,103,103, rsprintf("Too many sample_encoding options%s'%s'", " used in mode string ",mode)); if (sbf_set > 1) return_err(proc,104,104, rsprintf("Too many sample_byte_format options%s'%s'", " used in mode string ",mode)); /* convert the natural byte order to the proper value */ if (new_sbf == SP_sbf_N){ int conv_size; if (new_encoding == SP_se_pcm2) conv_size = 2; else if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_pcm1)) conv_size = 1; else conv_size = (sp->open_mode == SP_mode_read) ? sp->read_spifr->status->file_sample_n_bytes : sp->write_spifr->status->file_sample_n_bytes; new_sbf = get_natural_sbf(conv_size); } if (sp_verbose > 15){ fprintf(spfp,"Proc %s: After mode parsing ",proc); fprintf(spfp,"old_encoding %10s new_encoding %10s\n", enum_str_SP_sample_encoding(old_encoding), enum_str_SP_sample_encoding(new_encoding)); fprintf(spfp,"Proc %s: After mode parsing ",proc); fprintf(spfp,"old_compress %10s new_compress %10s\n", enum_str_SP_waveform_comp(old_compress), enum_str_SP_waveform_comp(new_compress)); fprintf(spfp,"Proc %s: After mode parsing ",proc); fprintf(spfp,"old_sbf %10s new_sbf %10s\n", enum_str_SP_sample_byte_fmt(old_sbf), enum_str_SP_sample_byte_fmt(new_sbf)); fprintf(spfp,"Proc %s: After mode parsing ",proc); fprintf(spfp,"old_data_fmt %10s new_data_fmt %10s\n", enum_str_SP_data_format(old_data_fmt), enum_str_SP_data_format(new_data_fmt)); } /* Test the supported file conversions dependent on the file's open mode */ switch (sp->open_mode){ case SP_mode_read: switch (old_encoding) { case SP_se_pcm2: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_raw) || (new_encoding == SP_se_pcm2)) ; else return_err(proc,108,108, "Illegal read transformation from PCM2"); break; case SP_se_pcm1: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_raw) || (new_encoding == SP_se_pcm2) || (new_encoding == SP_se_pcm1)) ; else return_err(proc,109,109, "Illegal read transformation from PCM1"); break; case SP_se_ulaw: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_raw) || (new_encoding == SP_se_pcm2) || (new_encoding == SP_se_pcm1)) ; else return_err(proc,110,110, "Illegal read transformation from ULAW"); break; case SP_se_raw: if (new_encoding == SP_se_raw) ; else return_err(proc,111,111, "Illegal read transformation from RAW"); break; default: if (old_encoding != new_encoding) return_err(proc,112,112, "Internal function error for file opened for reading"); } switch (old_compress){ case SP_wc_shortpack: case SP_wc_wavpack: case SP_wc_shorten: if (new_compress != SP_wc_none) return_err(proc,113,113, rsprintf("Illegal read transformation to %s", "compress a compressed file")); if (new_encoding == SP_se_null) return_err(proc,114,114, "Internal function error for file opened for read"); break; case SP_wc_none: if (new_compress != SP_wc_none) return_err(proc,115,115, "Unable to read an uncompressed file as compressed"); break; default: return_err(proc,115,115, "Internal function error for file opened for read"); } switch (old_sbf){ case SP_sbf_10: case SP_sbf_01: case SP_sbf_N: if (new_sbf == SP_sbf_1) if ((new_encoding == SP_se_pcm1) || (new_encoding == SP_se_ulaw)) ; else return_err(proc,116,116, "Unable to convert to a one-byte per sample format"); break; case SP_sbf_1: if (new_sbf != SP_sbf_1) if (new_encoding == SP_se_pcm2) ; else return_err(proc,117,117, "Unable to convert to a 2-byte per sample format"); break; default: if (new_sbf != old_sbf) return_err(proc,118,118, rsprintf("%s for read sbf converted from %s to %s", "Internal function error for file opened", enum_str_SP_sample_byte_fmt(old_sbf), enum_str_SP_sample_byte_fmt(new_sbf))); break; } break; /*********************************************************************/ /** Write and Update Mode Checking */ case SP_mode_update: case SP_mode_write: switch (old_encoding) { case SP_se_pcm2: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_pcm1) || (new_encoding == SP_se_pcm2)) ; else return_err(proc,119,119, "Illegal write transformation from PCM2"); break; case SP_se_pcm1: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_pcm1) || (new_encoding == SP_se_pcm2) || (new_encoding == SP_se_pcm1)) ; else return_err(proc,120,120, "Illegal write transformation from PCM1"); break; case SP_se_ulaw: if ((new_encoding == SP_se_ulaw) || (new_encoding == SP_se_pcm1) || (new_encoding == SP_se_pcm2)) ; else return_err(proc,121,121, "Illegal write transformation from ULAW"); break; case SP_se_raw: if (new_encoding == SP_se_raw) ; else return_err(proc,122,122, "Illegal write transformation from RAW"); break; default: return_err(proc,123,123, "Internal function error: old sample_encoding not set"); } switch (old_compress){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -