short sp_htons(short t){ static enum SP_sample_byte_fmt sbf = SP_sbf_null; if (sbf == SP_sbf_null) sbf = get_natural_sbf(2); if (sbf == SP_sbf_10) return(t); swap_bytes(t); return(t); }
long sp_ntohl(long t){ static enum SP_sample_byte_fmt sbf = SP_sbf_null; if (sbf == SP_sbf_null) sbf = get_natural_sbf(4); if (sbf == SP_sbf_3210) return(t); if (sbf == SP_sbf_1032){ swap_short(t); return(t); } fprintf(stderr, "Error: sp_ntohl(%lx) Failed, Contact the SPHERE maintainer\n", t); exit(-1); }
static int read_data_in(void *buffer, size_t num_sample, SP_FILE *sp){ SPIFR *spifr; char *proc=" read_data_in " SPHERE_VERSION_STR; int samples_read = 0; int in_samples=0, o, oc, s, c, ret; int block=0; char *in_block=(char *)0, *out_block=(char *)0; char *current_data_location = (char *)0; char *out_conversion_block=(char *)0,*in_conversion_block=(char *)0; char *next_out_block=(char *)0; short *sh_data=(short *)0; unsigned char *ulaw_data = (unsigned char *)0; SP_CHECKSUM checksum=0, block_checksum=0; int file_record_size; char **arr_buff = (char **)0, *arr; spifr = sp->read_spifr; file_record_size = spifr->status->file_sample_n_bytes * spifr->status->file_channel_count; while ((samples_read < num_sample) && (! fob_feof(spifr->waveform->sp_fob))){ if (sp_verbose > 16) fprintf(spfp,"Proc %s: Beginning block %d\n",proc,block); /* read in either a block or the whole chunk if data */ if (spifr->waveform->file_data_buffer != CNULL) { if (sp_verbose > 15) fprintf(spfp, "Proc %s: reading a block into temporary storage\n", proc); in_block = (void *)spifr->waveform->file_data_buffer; current_data_location = in_block; if ((in_samples = (num_sample - samples_read))> TRANSLATION_LEN) in_samples = TRANSLATION_LEN; ret = fob_fread(in_block, file_record_size, in_samples, spifr->waveform->sp_fob); if (ret < 0) return_err(proc,105,0,"Unable to read data"); } else { /* there was no change in the sample coding so just read */ /* in the data */ if (sp_verbose > 15) fprintf(spfp, "Proc %s: read a block WITHOUT coding conversions\n", proc); ret = fob_fread(buffer, file_record_size, num_sample, spifr->waveform->sp_fob); if (ret < 0) return_err(proc,107,0,"Unable to read data"); in_block = (void *)buffer; current_data_location = in_block; } if (sp_verbose > 16) fprintf(spfp, "Proc %s: block read of %d Samples, expected %d\n", proc,ret,num_sample); in_samples = ret; /**** ret is the number of samples per time period read in from */ /**** the file */ /**** Do the checksum computation before format changes occur ***/ switch (spifr->status->file_encoding){ case SP_se_pcm2: /* DON'T SWAP if the coding type hasn't changed, and the */ /* output SBF == natural_sbf */ /* OR if the coding changed and the file SBF */ /* is == natural SBF */ if (((spifr->status->user_encoding == SP_se_pcm2) && (spifr->status->natural_sbf == spifr->status->user_sbf)) || ((spifr->status->user_encoding != SP_se_pcm2) && (spifr->status->natural_sbf == spifr->status->file_sbf))){ if (sp_verbose > 16) fprintf(spfp, "Proc %s: Not Swapping for checksum\n",proc); checksum = sp_compute_short_checksum((void *)in_block,in_samples* spifr->status->file_channel_count, FALSE); } else { if (sp_verbose > 16) fprintf(spfp,"Proc %s: Swapping for checksum\n",proc); checksum = sp_compute_short_checksum((void *)in_block, in_samples*spifr->status->file_channel_count, TRUE); } break; default: { int n; n = in_samples * spifr->status->file_channel_count * spifr->status->file_sample_n_bytes; if (sp_verbose > 16) fprintf(spfp,"Proc %s: Computing char checksum %d bytes\n", proc,n); checksum = sp_compute_char_checksum(in_block, n); break; } } spifr->waveform->checksum = sp_add_checksum(spifr->waveform->checksum,checksum); block_checksum = checksum; /*** FINISHED WITH THE CHECKSUMS ***/ samples_read += in_samples; spifr->waveform->samples_read += in_samples; if (sp_eof(sp)) /* only check this if the user_sample count is real */ /* Added June 22, 1994 by JGF */ if (spifr->status->user_sample_count != 999999999){ if (spifr->waveform->samples_read != spifr->status->user_sample_count){ spifr->waveform->read_premature_eof = TRUE; return_err(proc,500,0, rsprintf("Premature End-of-File %d read != %d", spifr->waveform->samples_read, spifr->status->user_sample_count)); } } if (spifr->waveform->samples_read == spifr->status->user_sample_count){ if ((! spifr->status->ignore_checksum) && (spifr->waveform->checksum != spifr->status->file_checksum)){ spifr->waveform->failed_checksum = TRUE; return_err(proc,1000,0, rsprintf("%sComputed %d != Expected %d", "Checksum Test Failed ", spifr->waveform->checksum, spifr->status->file_checksum)); } } /**** Do the sample coding conversions ****/ if (((spifr->status->file_encoding == SP_se_ulaw) && (spifr->status->user_encoding == SP_se_pcm2)) || ((spifr->status->file_encoding == SP_se_pcm2) && (spifr->status->user_encoding == SP_se_ulaw))) { int samples_to_change = in_samples*spifr->status->file_channel_count; if ((spifr->status->channels != CHANNELSNULL) || (spifr->status->user_data_fmt == SP_df_array)){ out_conversion_block = next_out_block = (char *)spifr->waveform->converted_buffer; if (sp_verbose > 16) fprintf(spfp,"Proc %s: using converted buffer output\n", proc); } else { out_conversion_block = (char *)buffer; if (sp_verbose > 16) fprintf(spfp,"Proc %s: using buffer output argument\n", proc); if (next_out_block == (char *)0) next_out_block = out_conversion_block; } if (sp_verbose > 16) fprintf(spfp, "Proc %s: converting %d (%d*%d) samples\n",proc, samples_to_change,in_samples, spifr->status->file_channel_count); if ((spifr->status->file_encoding == SP_se_ulaw) && (spifr->status->user_encoding == SP_se_pcm2)) { /* now convert the block into the buffer */ if (sp_verbose > 16) fprintf(spfp,"Proc %s: Converting ULAW to PCM", proc); /* set up some pointers */ ulaw_data = (unsigned char *)spifr->waveform->file_data_buffer; sh_data = (short *)next_out_block ; /*** This was a bug, it used to compare to sp_sbf_10 ***/ if (spifr->status->user_sbf != get_natural_sbf(2)){ if (sp_verbose > 16) fprintf(spfp," outputting 01 format bytes\n"); for (o=0; o < samples_to_change; o++) { *sh_data = ulaw2linear_01_sbf_lut[*ulaw_data]; sh_data ++; ulaw_data ++; } } else { if (sp_verbose > 16) fprintf(spfp," outputting 10 format bytes\n"); for (o=0; o < samples_to_change; o++) { *sh_data = ulaw2linear_10_sbf_lut[*ulaw_data]; sh_data ++; ulaw_data ++; } } next_out_block = (char *)sh_data; } else if ((spifr->status->file_encoding == SP_se_pcm2) && (spifr->status->user_encoding == SP_se_ulaw)) { if (sp_verbose > 16) fprintf(spfp,"Proc %s: Performing read translation%s\n", proc," from PCM2 to ULAW"); /* set up some pointers */ sh_data = (short *)spifr->waveform->file_data_buffer; ulaw_data = (unsigned char *)next_out_block; if (spifr->status->file_sbf != spifr->status->natural_sbf){ char *p , temp; short odata; if (sp_verbose > 16) fprintf(spfp, "Proc %s: Swapping input shorts\n", proc); for (o=0; o < samples_to_change; o++) { /* Pre swap the bytes */ odata = *sh_data; p = (char *) &odata; temp = *p; *p = *(p + 1); *(p + 1) = temp; /* fprintf(spfp, " %d -> %x -> %x\n",o,*sh_data,odata); */ *ulaw_data = linear2ulaw(odata); sh_data ++; ulaw_data ++; } }else for (o=0; o < samples_to_change; o++) { *ulaw_data =linear2ulaw(*sh_data); sh_data ++; ulaw_data ++; } next_out_block = (char *)ulaw_data; } else return_err(proc,109,0,"Internal Error"); current_data_location = out_conversion_block; } /* Do the channel conversions */ if (spifr->status->channels != CHANNELSNULL){ int in_recsize, out_recsize, in_chancnt, out_chancnt; if (sp_verbose > 15) fprintf(spfp,"Proc %s: Converting channels\n",proc); if (spifr->status->user_data_fmt == SP_df_array) out_block = spifr->waveform->interleave_buffer; else if (out_block == (void *)0) out_block = (void *)buffer; current_data_location = out_block; if (spifr->status->file_encoding != spifr->status->user_encoding) in_conversion_block = (void *)spifr->waveform->converted_buffer; else if (in_conversion_block == (void *)0) in_conversion_block = (void *)spifr->waveform->file_data_buffer; in_chancnt = spifr->status->file_channel_count; in_recsize = spifr->status->user_sample_n_bytes * in_chancnt; out_chancnt = spifr->status->user_channel_count; out_recsize = spifr->status->user_sample_n_bytes * out_chancnt; for (oc=0; oc<spifr->status->channels->num_chan; oc++) if (spifr->status->channels->ochan[oc].num_origin == 1){ char *in_ptr, *out_ptr; in_ptr = in_conversion_block + ((spifr->status->channels->ochan[oc].orig_channel[0]-1) * spifr->status->user_sample_n_bytes); out_ptr = out_block + (oc * spifr->status->user_sample_n_bytes); for (s=0; s<in_samples; s++){ memcpy(out_ptr,in_ptr, spifr->status->user_sample_n_bytes); in_ptr += in_recsize; out_ptr += out_recsize; } } else { if (spifr->status->user_encoding == SP_se_pcm2) { /* do a straight add */ ORIGINATION_CHAN *ochn; short *in_ptr, *out_ptr; int sum, ch; if (sp_verbose > 15) fprintf(spfp, "Proc %s: Adding PCM data %s %d\n", proc, "channel",oc); in_ptr = (short *)in_conversion_block; out_ptr = (short *)out_block; ochn = &(spifr->status->channels->ochan[oc]); if (spifr->status->user_sbf== get_natural_sbf(2)){ for (s=0; s<in_samples; s++){ for (ch=0, sum=0; ch < ochn->num_origin; ch++){ sum +=*(in_ptr+ochn->orig_channel[ch]-1); } if (sum > 32767) sum = 32767; else if (sum < -32768) sum = -32768; *(out_ptr + oc) = (short)sum; in_ptr += in_chancnt; out_ptr += out_chancnt; } } else { short x; if (sp_verbose > 15) fprintf(spfp,"Proc %s: %s %d\n", proc, "Adding Swapped PCM data channel",oc); for (s=0; s<in_samples; s++){ /* fprintf(spfp,"sample %d\n",s); */ for (ch=0, sum=0; ch < ochn->num_origin; ch++){ x = *(in_ptr+ochn->orig_channel[ch]-1); swap_bytes(x); sum += x; /* fprintf(spfp," %d = %d\n",x,sum); */ } if (sum > 32767) x = 32767; else if (sum < -32768) x = -32768; else x = sum; swap_bytes(x); *(out_ptr + oc) = (short)x; in_ptr += in_chancnt; out_ptr += out_chancnt; } } } else if (spifr->status->user_encoding == SP_se_ulaw){ /* do an add on ulaw data */ ORIGINATION_CHAN *ochn; unsigned char *in_ptr, *out_ptr; int sum, ch; if (sp_verbose > 15) fprintf(spfp, "Proc %s: Adding ULAW data %s %d\n", proc, "channel",oc); in_ptr = (unsigned char *)in_conversion_block; out_ptr = (unsigned char *)out_block; ochn = &(spifr->status->channels->ochan[oc]); for (s=0; s<in_samples; s++){ for (ch=0, sum=0; ch < ochn->num_origin; ch++){ sum += ulaw2linear_10_sbf_lut[ *(in_ptr+ochn->orig_channel[ch]-1)]; } if (sum > 32767) sum = 32767; else if (sum < -32768) sum = -32768; *(out_ptr + oc) = linear2ulaw(sum); in_ptr += in_chancnt; out_ptr += out_chancnt; } } } out_block += out_recsize * in_samples; } if (spifr->status->user_data_fmt == SP_df_array){ /* convert the data from it's interleaved form */ /* to an array format */ int snb = spifr->status->user_sample_n_bytes; int chcnt = spifr->status->user_channel_count; int recsize = snb * chcnt; char *in_dat; if (sp_verbose > 15) fprintf(spfp,"Proc %s: Converting data to an array\n", proc); if (arr_buff == (char **)0) arr_buff = (char **)buffer; for (c=0; c<chcnt; c++){ in_dat = current_data_location + (snb * c) ; arr = arr_buff[c] + ((samples_read-in_samples) * snb); for (s=0; s<in_samples; s++,arr+=snb,in_dat+=recsize) memcpy(arr,in_dat,snb); } } if (sp_verbose > 11){ fprintf(spfp,"Proc %s: Block %d: Requested %d samples", proc,block++,num_sample); fprintf(spfp," read %d total %d, Checksum %d (BC %d)\n", ret,spifr->waveform->samples_read, block_checksum, spifr->waveform->checksum); } } return_success(proc,0,samples_read,"ok"); }
int sp_set_default_operations(SP_FILE *sp) { SP_INTEGER l_int; char *str, *proc="sp_set_default_operations " SPHERE_VERSION_STR; SPIFR *spifr; int ret; if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc); if (sp == SPNULL) return_err(proc,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,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,(void *)&l_int) != 0){ /* ---------- OLD CODE --------------------- return_err(proc,101,101,rsprintf("Missing '%s' header field", SAMPLE_COUNT_FIELD)); spifr->status->user_sample_count = 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"); }
SPIFR *alloc_SPIFR() { SPIFR *tspifr; if ((tspifr=(SPIFR *)mtrf_malloc(sizeof(SPIFR))) == SPIFRNULL) return(SPIFRNULL); /* init the sphere_t structure */ if ((tspifr->status=(struct spfile_status_t *) mtrf_malloc(sizeof(struct spfile_status_t))) == (struct spfile_status_t *)0){ mtrf_free(tspifr); return(SPIFRNULL); } if ((tspifr->waveform=(struct waveform_t *) mtrf_malloc(sizeof(struct waveform_t))) == (struct waveform_t *)0){ mtrf_free(tspifr->status); mtrf_free(tspifr); return(SPIFRNULL); } tspifr->header = HDRNULL; /* init the waveform structure */ tspifr->waveform->sp_fp = (FILE *)0; tspifr->waveform->sp_fob = FOBPNULL; tspifr->waveform->samples_read = 0; tspifr->waveform->samples_written = 0; tspifr->waveform->checksum = 0; tspifr->waveform->header_data_size = -1; tspifr->waveform->read_premature_eof = FALSE; tspifr->waveform->failed_checksum = FALSE; /* init the status structure */ tspifr->status->external_filename = CNULL; tspifr->status->file_header = HDRNULL; tspifr->status->extra_checksum_verify = FALSE; tspifr->status->is_disk_file = FALSE; tspifr->status->is_temp_file = FALSE; tspifr->status->temp_filename = CNULL; tspifr->status->user_channel_count = 0; tspifr->status->user_sample_count = 0; tspifr->status->user_sample_rate = 0; tspifr->status->user_sample_n_bytes = 0; tspifr->status->file_channel_count = 0; tspifr->status->file_sample_count = 0; tspifr->status->file_sample_rate = 0; tspifr->status->file_sample_n_bytes = 0; tspifr->status->file_checksum = (-1); tspifr->status->ignore_checksum = FALSE; tspifr->status->file_header = HDRNULL; tspifr->status->user_encoding = tspifr->status->file_encoding = SP_se_null; tspifr->status->user_compress = tspifr->status->file_compress = SP_wc_null; tspifr->status->user_sbf = tspifr->status->file_sbf = SP_sbf_null; tspifr->status->natural_sbf = get_natural_sbf(2); tspifr->status->write_occured_flag = FALSE; tspifr->status->read_occured_flag = FALSE; tspifr->status->field_set_occured_flag = FALSE; tspifr->status->set_data_mode_occured_flag = FALSE; return(tspifr); }