📄 spopen.c
字号:
{ switch (id){ case SP_se_pcm2: return("SP_se_pcm2"); case SP_se_pcm1: return("SP_se_pcm1"); case SP_se_ulaw: return("SP_se_ulaw"); case SP_se_raw: return("SP_se_raw"); case SP_se_null: return("SP_se_null"); default: return("UNKNOWN"); }}char *enum_str_SP_waveform_comp(id)enum SP_waveform_comp id;{ switch (id){ case SP_wc_shorten: return("SP_wc_shorten"); case SP_wc_wavpack: return("SP_wc_wavpack"); case SP_wc_shortpack: return("SP_wc_shortpack"); case SP_wc_none: return("SP_wc_none"); case SP_wc_null: return("SP_wc_null"); default: return("UNKNOWN"); }}char *enum_str_SP_sample_byte_fmt(id)enum SP_sample_byte_fmt id;{ switch (id){ case SP_sbf_01: return("SP_sbf_01"); case SP_sbf_10: return("SP_sbf_10"); case SP_sbf_1: return("SP_sbf_1"); case SP_sbf_N: return("SP_sbf_N"); case SP_sbf_orig: return("SP_sbf_orig"); case SP_sbf_null: return("SP_sbf_null"); default: return("UNKNOWN"); }}sp_set_default_operations(sp)SP_FILE *sp;{ long lint; char *str, *proc_name="sp_set_default_operations"; SPIFR *spifr; struct header_t *header; int ret; if (sp_verbose > 10) printf("Proc %s:\n",proc_name); if (sp == SPNULL) return_err(proc_name,100,100,"Null SPFILE pointer"); if ((sp->open_mode == SP_mode_read) || (sp->open_mode == SP_mode_update)) spifr = sp->read_spifr; else if (sp->open_mode == SP_mode_write) spifr = sp->write_spifr; else return_err(proc_name,100,100,"Unknown File Mode"); /****************************************************************************/ /* The following fields are REQUIRED for Read operations, NO Exceptions */ if (sp_h_get_field(sp,SAMPLE_COUNT_FIELD,T_INTEGER,&lint) != 0) return_err(proc_name,101,101,rsprintf("Missing '%s' header field",SAMPLE_COUNT_FIELD)); spifr->status->user_sample_count = spifr->status->file_sample_count = (int)lint; if (lint <= 0) return_err(proc_name,108,108,rsprintf("Field '%s' value out of range, > 0",lint)); if (sp_h_get_field(sp,SAMPLE_N_BYTES_FIELD,T_INTEGER,&lint) != 0) return_err(proc_name,104,104,rsprintf("Missing '%s' header field",SAMPLE_N_BYTES_FIELD)); spifr->status->user_sample_n_bytes = spifr->status->file_sample_n_bytes = (int)lint; if (lint <= 0) return_err(proc_name,108,108,rsprintf("Field '%s' value out of range, > 0",lint)); if (sp_h_get_field(sp,CHANNEL_COUNT_FIELD,T_INTEGER,&lint) != 0) return_err(proc_name,105,105,rsprintf("Missing '%s' header field",CHANNEL_COUNT_FIELD)); spifr->status->user_channel_count = spifr->status->file_channel_count = (int)lint; if (lint <= 0) return_err(proc_name,108,108,rsprintf("Field '%s' value out of range, > 0",lint)); /****************************************************************************/ /* The following fields may exist, if they do not, there is a default */ /***** NOTE: only set the file_sbf, Sp_set_data_mode is called to set the */ /***** user_sbf */ if ((ret=sp_h_get_field(sp,SAMPLE_BF_FIELD,T_STRING,&str)) != 0){ if ((spifr->status->file_sbf = get_natural_sbf(spifr->status->user_sample_n_bytes)) == SP_sbf_null) return_err(proc_name,107,107,rsprintf("Unable to read sample sizes of %d bytes", spifr->status->user_sample_n_bytes)); } else { /* str holds the sample_byte_format_value */ ret = parse_sample_byte_format(str,&(spifr->status->file_sbf),spifr->status->user_sample_n_bytes); if (ret == 1000) { /* then the file was compressed using change_wav_format */ spifr->status->file_sbf = get_natural_sbf(spifr->status->user_sample_n_bytes); spifr->status->file_compress = SP_wc_shortpack; if ((h_set_field(spifr->header,SAMPLE_BF_FIELD,T_STRING,get_natural_byte_order()) != 0) || (h_set_field(spifr->status->file_header,SAMPLE_BF_FIELD,T_STRING,get_natural_byte_order()) != 0)){ sp_print_return_status(stdout); mtrf_free(str); return_err(proc_name,110,110,"Unable to re-set sample byte format field for a shortpacked file"); } if ((h_set_field(spifr->header,SAMPLE_CODING_FIELD,T_STRING,rsprintf("pcm,embedded-%s",str)) != 0) || (h_set_field(spifr->status->file_header,SAMPLE_CODING_FIELD,T_STRING,rsprintf("pcm,embedded-%s",str)) != 0)){ sp_print_return_status(stdout); mtrf_free(str); return_err(proc_name,111,111,"Unable to re-set sample coding field for a shortpacked file"); } } else if (ret != 0) { /* there really was an error */ mtrf_free(str); return_err(proc_name,106,106,"Unable to parse the 'sample_byte_format' field"); } mtrf_free(str); } /***** field break *****/ if (sp_h_get_field(sp,SAMPLE_CODING_FIELD,T_STRING,&str) != 0) str = mtrf_strdup("pcm"); /* the default, since old Corpora are missing this field */ if (parse_sample_coding(str,spifr->status->file_sample_n_bytes, &(spifr->status->file_encoding), &(spifr->status->file_compress)) != 0){ mtrf_free(str); print_return_status(stderr); return_err(proc_name,107,107, rsprintf("Unable to parse sample_coding value '%s' header field",str)); } mtrf_free(str); /****************************************************************************/ /* The following fields are conditionally required. */ ret = sp_h_get_field(sp,SAMPLE_RATE_FIELD,T_INTEGER,&lint); switch (spifr->status->file_encoding) { case SP_se_pcm1: case SP_se_pcm2: case SP_se_ulaw: if (ret != 0) return_err(proc_name,102,102,rsprintf("Header field '%s' missing, but required for waveform data", SAMPLE_RATE_FIELD)); spifr->status->user_sample_rate = spifr->status->file_sample_rate = (int)lint; break; case SP_se_raw: default: spifr->status->user_sample_rate = spifr->status->file_sample_rate = 0; } /****************************************************************************/ /* The following fields are OPTIONAL, but if they exist, there is a */ /* special purpose for them */ if (sp_h_get_field(sp,SAMPLE_CHECKSUM_FIELD,T_INTEGER,&lint) == 0) spifr->status->file_checksum = lint; else { spifr->status->ignore_checksum = TRUE; } /*********************/ /* Consitency checks */ /*********************/ if (spifr->status->file_encoding == SP_se_ulaw && spifr->status->file_sample_n_bytes != 1) return_err(proc_name,120,120,rsprintf("Ulaw encoding requires a 1 byte sample, however the header value is %d\n", spifr->status->file_sample_n_bytes)); if (spifr->status->file_encoding == SP_se_pcm1 && spifr->status->file_sample_n_bytes != 1) return_err(proc_name,120,120,rsprintf("PCM1 encoding requires a 1 byte sample, however the header value is %d\n", spifr->status->file_sample_n_bytes)); if (spifr->status->file_encoding == SP_se_pcm1 && spifr->status->file_sample_n_bytes != 2) return_err(proc_name,120,120,rsprintf("PCM2 encoding requires a 2 byte sample, however the header value is %d\n", spifr->status->file_sample_n_bytes)); /********************************/ /* set up the default decodings */ if (sp->open_mode == SP_mode_read) if (sp_set_data_mode(sp,"") != 0){ print_return_status(stderr); return_err(proc_name,110,110,"Unable to set up default encodings on file opened for read"); } else if (sp->open_mode == SP_mode_update) if (sp_set_data_mode(sp,"SE_ORIG:SBF_ORIG") != 0){ print_return_status(stderr); return_err(proc_name,111,111,"Unable to set up default encodings on file opened for update"); } if (sp_verbose > 11) printf("Proc %s: Returning 0\n",proc_name); return_success(proc_name,0,0,"ok");}int parse_sample_byte_format(str,sbf)char *str;enum SP_sample_byte_fmt *sbf;{ char *proc_name="parse_sample_byte_format"; if (sp_verbose > 10) printf("Proc %s:\n",proc_name); if (str == CNULL) return_err(proc_name,100,100,"Null sample_byte_format_string"); if (sbf == (enum SP_sample_byte_fmt *)0) return_err(proc_name,101,101,"Null sbf pointer"); if (strsame(str,"01")) *sbf = SP_sbf_01; if (strsame(str,"10")) *sbf = SP_sbf_10; if (strsame(str,"1")) *sbf = SP_sbf_1; if (strstr(str,"shortpack") != CNULL) { /* this return value must remain 1000, other functions depend on it */ return_err(proc_name,1000,1000, rsprintf("Unknown sample_byte_format value '%s' in header",str)); } if (sp_verbose > 11) printf("Proc %s: Returning 0\n",proc_name); return_success(proc_name,0,0,"ok");} int parse_sample_coding(str,sample_n_bytes,sample_encoding,wav_compress)char *str;int sample_n_bytes;enum SP_waveform_comp *wav_compress;enum SP_sample_encoding *sample_encoding; { int enc_set=FALSE, comp_set=FALSE; char *pstr, *str_mem; char *proc_name="parse_sample_coding"; if (sp_verbose > 10) printf("Proc %s:\n",proc_name); if (str == CNULL) return_err(proc_name,101,101,"Null coding string"); if (sample_n_bytes < 1 || sample_n_bytes > 2) return_err(proc_name,102,102,rsprintf("Sample_n_bytes %d out of range",sample_n_bytes)); if (sample_encoding == (enum SP_sample_encoding *)0) return_err(proc_name,103,103,"Null sample encoding pointer"); if (wav_compress == (enum SP_waveform_comp *)0) return_err(proc_name,104,104,"Null waveform compress pointer"); *wav_compress = SP_wc_null; *sample_encoding = SP_se_null; if (sp_verbose > 16) fprintf(stderr,"%s: string IS %s\n",proc_name,str); /* the algorithm to parse the sample encoding field is : */ /* 1: get a token before a ',' or NULL */ /* 2: set a flag to what it matches */ /* 3: move past the token */ /* 4: loop to (1) */ /* make a duplicate copy because strtok is destructive */ str_mem = mtrf_strdup(str); pstr = strtok(str_mem,","); while (pstr != CNULL){ if (sp_verbose > 16) fprintf(stderr,"%s: token found = %s\n",proc_name,pstr); if (strsame(pstr,"pcm")){ if (enc_set){ mtrf_free(str_mem); return_err(proc_name,105,105,"Multiple sample encodings in header field"); } if (sample_n_bytes == 1) *sample_encoding = SP_se_pcm1; else *sample_encoding = SP_se_pcm2; enc_set = TRUE; } else if (strsame(pstr,"ulaw")) { if (enc_set){ mtrf_free(str_mem); return_err(proc_name,105,105,"Multiple sample encodings in header field"); } *sample_encoding = SP_se_ulaw; enc_set = TRUE; } else if (strsame(pstr,"raw")){ if (enc_set){ mtrf_free(str_mem); return_err(proc_name,105,105,"Multiple sample encodings in header field"); } *sample_encoding = SP_se_raw; enc_set = TRUE; } else if (strstr(pstr,"embedded-shorten-v") != CNULL) { if (comp_set) { mtrf_free(str_mem); return_err(proc_name,106,106, "Multiple waveform compressions in header field"); } *wav_compress = SP_wc_shorten; comp_set = TRUE; } else if (strstr(pstr,"embedded-wavpack") != CNULL) { if (comp_set){ mtrf_free(str_mem); return_err(proc_name,106,106, "Multiple waveform compressions in header field"); } *wav_compress = SP_wc_wavpack; comp_set = TRUE; } else if (strstr(pstr,"embedded-shortpack-v") != CNULL) { if (comp_set){ mtrf_free(str_mem); return_err(proc_name,106,106, "Multiple waveform compressions in header field"); } *wav_compress = SP_wc_shortpack; comp_set = TRUE; } else { mtrf_free(str_mem); return_err(proc_name,107,107,"Unknown token in sample coding field"); } pstr = strtok(CNULL,","); } if (*wav_compress == SP_wc_null) *wav_compress = SP_wc_none; if (*sample_encoding == SP_se_null) *sample_encoding = SP_se_pcm2; mtrf_free(str_mem); if (sp_verbose > 11) printf("Proc %s: Returning 0\n",proc_name); return_success(proc_name,0,0,"ok");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -