📄 spopen.c
字号:
spifr->status->file_sample_count = (int)l_int;*/ /* added a condition to permit a pipe to NOT have a sample_count */ /* field. ADDED June 22, 1993 */ if (spifr->status->is_disk_file){ return_err(proc,101,101,rsprintf("Missing '%s' header field", SAMPLE_COUNT_FIELD)); } else { /* Sample counts may be missing from piped files */ spifr->status->user_sample_count = spifr->status->file_sample_count = 999999999; } } else spifr->status->user_sample_count = spifr->status->file_sample_count = (int)l_int; if (l_int <= 0) return_err(proc,108,108, rsprintf("Field '%s' value out of range, %d <= 0", SAMPLE_COUNT_FIELD,l_int)); if (sp_h_get_field(sp,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&l_int) != 0) return_err(proc,104,104, rsprintf("Missing '%s' header field", SAMPLE_N_BYTES_FIELD)); spifr->status->user_sample_n_bytes = spifr->status->file_sample_n_bytes = (int)l_int; if (l_int <= 0) return_err(proc,108,108, rsprintf("Field '%s' value out of range, %d <= 0", SAMPLE_N_BYTES_FIELD,l_int)); if (sp_h_get_field(sp,CHANNEL_COUNT_FIELD,T_INTEGER,(void *)&l_int) != 0) return_err(proc,105,105, rsprintf("Missing '%s' header field", CHANNEL_COUNT_FIELD)); spifr->status->user_channel_count = spifr->status->file_channel_count = (int)l_int; if (l_int <= 0) return_err(proc,108,108, rsprintf("Field '%s' value out of range, > 0", CHANNEL_COUNT_FIELD,l_int)); /**********************************************************************/ /* 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,(void *)&str)) != 0){ if ((spifr->status->file_sbf = get_natural_sbf(spifr->status->user_sample_n_bytes)) == SP_sbf_null) return_err(proc,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)); 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(2)) != 0) || (h_set_field(spifr->status->file_header,SAMPLE_BF_FIELD, T_STRING,get_natural_byte_order(2)) != 0)){ sp_print_return_status(spfp); mtrf_free(str); return_err(proc,110,110, rsprintf("Unable to re-set sample byte format%s", "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(spfp); mtrf_free(str); return_err(proc,111,111, rsprintf("Unable to re-set sample coding field%s", "for a shortpacked file")); } } else if (ret != 0) { /* there really was an error */ mtrf_free(str); return_err(proc,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,(void *)&str) != 0) if (spifr->status->user_sample_n_bytes == 1) str = mtrf_strdup("ulaw"); else /* the default, since old Corpora are missing this field */ str = mtrf_strdup("pcm"); 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(spfp); return_err(proc,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,(void *)&l_int); switch (spifr->status->file_encoding) { case SP_se_pcm1: case SP_se_pcm2: case SP_se_ulaw: if (ret != 0) return_err(proc,102,102, rsprintf("Header field '%s' missing, but required%s", SAMPLE_RATE_FIELD," for waveform data")); spifr->status->user_sample_rate = spifr->status->file_sample_rate = (int)l_int; 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,(void *)&l_int)==0) spifr->status->file_checksum = l_int; 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,120,120, rsprintf("Ulaw encoding requires a 1 byte sample,%s %d", " however the header value is", spifr->status->file_sample_n_bytes)); if (spifr->status->file_encoding == SP_se_pcm1 && spifr->status->file_sample_n_bytes != 1) return_err(proc,120,120, rsprintf("PCM1 encoding requires a 1 byte sample, %s %d", "however the header value is", spifr->status->file_sample_n_bytes)); if (spifr->status->file_encoding == SP_se_pcm1 && spifr->status->file_sample_n_bytes != 2) return_err(proc,120,120, rsprintf("PCM2 encoding requires a 2 byte sample, %s %d", "however the header value is", 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(spfp); return_err(proc,110,110, rsprintf("Unable to set up default encodings %s", "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(spfp); return_err(proc,111,111, "Unable to set up default encodings on file opened for update"); } if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning 0\n",proc); return_success(proc,0,0,"ok");}int parse_sample_byte_format(char *str, enum SP_sample_byte_fmt *sbf){ char *proc="parse_sample_byte_format " SPHERE_VERSION_STR; if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc); if (str == CNULL) return_err(proc,100,100,"Null sample_byte_format_string"); if (sbf == (enum SP_sample_byte_fmt *)0) return_err(proc,101,101,"Null sbf pointer"); if (strsame(str,"01")) *sbf = SP_sbf_01; else if (strsame(str,"10")) *sbf = SP_sbf_10; else if (strsame(str,"1")) *sbf = SP_sbf_1; else if (strsame(str,"0123")) *sbf = SP_sbf_0123; else if (strsame(str,"1032")) *sbf = SP_sbf_1032; else if (strsame(str,"2301")) *sbf = SP_sbf_2301; else if (strsame(str,"3210")) *sbf = SP_sbf_3210; else if (strsame(str,"N")) *sbf = SP_sbf_N; if (strstr(str,"shortpack") != CNULL) { /* this return value must remain 1000, other functions depend on it*/ return_err(proc,1000,1000, rsprintf("Unknown sample_byte_format value '%s' in header",str)); } if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning 0\n",proc); return_success(proc,0,0,"ok");} int parse_sample_coding(char *str, int sample_n_bytes, enum SP_sample_encoding *sample_encoding, enum SP_waveform_comp *wav_compress){ int enc_set=FALSE, comp_set=FALSE; char *pstr, *str_mem; char *proc="parse_sample_coding " SPHERE_VERSION_STR; if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc); if (str == CNULL) return_err(proc,101,101,"Null coding string"); if (sample_n_bytes < 1 || sample_n_bytes > 2) { /*then we are assuming the field wasn't set yet, so use the default*/ sample_n_bytes = 0; } if (sample_encoding == (enum SP_sample_encoding *)0) return_err(proc,103,103,"Null sample encoding pointer"); if (wav_compress == (enum SP_waveform_comp *)0) return_err(proc,104,104,"Null waveform compress pointer"); *wav_compress = SP_wc_null; *sample_encoding = SP_se_null; if (sp_verbose > 16) fprintf(spfp,"%s: string IS %s\n",proc,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(spfp,"%s: token found = %s\n",proc,pstr); if (strsame(pstr,"pcm")){ if (enc_set){ mtrf_free(str_mem); return_err(proc,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") || strsame(pstr,"mu-law")) { if (enc_set){ mtrf_free(str_mem); return_err(proc,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,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,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,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,106,106, "Multiple waveform compressions in header field"); } *wav_compress = SP_wc_shortpack; comp_set = TRUE; } else { mtrf_free(str_mem); return_err(proc,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) fprintf(spfp,"Proc %s: Returning 0\n",proc); return_success(proc,0,0,"ok");}/* * sp_fd_open * Version of sp_open added by A. Parker at ERL. This version takes an * already open file descriptor, instead of a file name. * */SP_FILE *sp_fp_open(FILE *fp){ SP_FILE *tsp; char *errmsg, *proc="sp_fd_open " SPHERE_VERSION_STR, *fopen_mode; extern in_entropic; /* defined in spread.c */ enum SP_file_open_mode current_mode; if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc); if (fp == NULL) return_err(proc,101,SPNULL,"Null file stream pointer"); if ((tsp=sp_alloc_and_init_sphere_t()) == SPNULL) return_err(proc,102,SPNULL,"Unable to malloc SPFILE memory");/* added by Entropic, 7/29/96 */ in_entropic = 1; /* set the file open mode in the status structure */ current_mode = tsp->open_mode = SP_mode_read; tsp->read_spifr->status->extra_checksum_verify = FALSE; fopen_mode = READMODE; tsp->read_spifr->waveform->sp_fp = fp; tsp->read_spifr->status->is_disk_file = FALSE; tsp->read_spifr->status->external_filename = mtrf_strdup("ESPS"); /* now that the file is opened, load the header if it exist, */ /* otherwise alloc an empty header for the user */ /* read the header */ tsp->read_spifr->header = sp_open_header(tsp->read_spifr->waveform->sp_fp,TRUE,&errmsg); if ( tsp->read_spifr->header == HDRNULL ) { free_sphere_t(tsp); return_err(proc,104,SPNULL, rsprintf("Unable to open SPHERE header of file, %s", errmsg)); } /* get the size of the header */ if ((tsp->read_spifr->waveform->header_data_size = sp_header_size(tsp->read_spifr->header)) < 0){ free_sphere_t(tsp); return_err(proc,111,SPNULL, rsprintf("Unable to get SPHERE header size of file")); } /****** Remove the sample_count field if it's ******/ /****** value is 999999999 and the file is a stream ******/ /****** Added by JGF June 22, 1994 ******/ if (! tsp->read_spifr->status->is_disk_file){ int type, size; SP_INTEGER l_int; if (sp_get_field(tsp->read_spifr->header, SAMPLE_COUNT_FIELD,&type,&size) == 0){ if (sp_get_data(tsp->read_spifr->header,SAMPLE_COUNT_FIELD, (char *)&l_int,&size) == 0){ if (l_int == 999999999){ if (sp_delete_field(tsp->read_spifr->header, SAMPLE_COUNT_FIELD) < 0) return_err(proc,112,SPNULL, rsprintf("Unable to delete fake '%s' field", SAMPLE_COUNT_FIELD)); } } } } /****** Correct any out-of-date headers right NOW ******/ if (correct_out_of_date_headers(tsp) != 0){ fprintf(spfp,"Warning: correction of "); fprintf(spfp,"out-of-date headers failed\n"); sp_print_return_status(spfp); } /* duplicate the header for the file interface */ if ((tsp->read_spifr->status->file_header = sp_dup_header(tsp->read_spifr->header)) == HDRNULL){ fprintf(spfp,"Error: sp_open_header unable "); fprintf(spfp,"to dup header for file\n"); free_sphere_t(tsp); return_err(proc,106,SPNULL, rsprintf("Unable to duplicate the SPHERE header of file")); } /* set the default operation settings */ if (sp_set_default_operations(tsp) != 0){ print_return_status(spfp); return_err(proc,107,SPNULL, rsprintf("Unable to interpret the SPHERE header of file")); }/* sp_file_dump(tsp,spfp);*/ if (sp_verbose > 17) sp_file_dump(tsp,spfp); if (sp_verbose > 11) fprintf(spfp,"Proc %s: Returning Sphere-file pointer\n",proc); return_success(proc,0,tsp,"ok");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -