📄 spwrite.c
字号:
/* First, un-interleave the data if neccessary */ if (spstat->user_data_fmt == SP_df_array){ /* convert the data from it's array 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 *out_dat; if (sp_verbose > 15) fprintf(spfp,"Proc %s: UN-Interleaving the data\n",proc); arr_buff = (char **)buffer; for (c=0; c<chcnt; c++){ out_dat = (char *)spwav->interleave_buffer + (snb * c) ; arr = arr_buff[c] + (samples_written * snb); for (s=0; s<out_samples; s++,arr+=snb,out_dat+=recsize){ memcpy(out_dat,arr,snb); /*if (s < 7) fprintf(spfp," %6d",*((short *)out_dat));*/ } /* fprintf(spfp,"\n"); */ } out_inter_block = (void *)spwav->interleave_buffer; } else { /* use the buffer passed to the function */ if (out_inter_block == (void *)0) out_inter_block = (void *)buffer; else out_inter_block = end_of_input_block; end_of_input_block = (void *)((char *)out_inter_block + (out_samples * spifr->status->user_sample_n_bytes * spifr->status->user_channel_count)); } /* Next do the sample encoding conversions if necessary */ if ((spstat->user_encoding == SP_se_ulaw) && (spstat->file_encoding == SP_se_pcm2)) { if (sp_verbose > 15) fprintf(spfp,"Proc %s: converting ULAW to PCM\n",proc); sh_data = (short *)spwav->converted_buffer; ulaw_data = (unsigned char *)out_inter_block; /*** This was a bug, it used to compare to sp_sbf_10 ***/ if (spstat->file_sbf != get_natural_sbf(2)) for (o=0; o < out_samples*spstat->user_channel_count; o++) { *sh_data = ulaw2linear_01_sbf_lut[*ulaw_data]; sh_data ++; ulaw_data ++; } else for (o=0; o < out_samples*spstat->user_channel_count; o++) { *sh_data = ulaw2linear_10_sbf_lut[*ulaw_data]; sh_data ++; ulaw_data ++; } out_block = (void *)spwav->converted_buffer; } else if ((spstat->user_encoding == SP_se_pcm2) && (spstat->file_encoding == SP_se_ulaw)) { if (sp_verbose > 15) fprintf(spfp,"Proc %s: converting ULAW to PCM\n",proc); ulaw_data = (unsigned char *)spwav->converted_buffer; sh_data = (short *)out_inter_block ; if (spstat->user_sbf != spifr->status->natural_sbf){ if (sp_verbose > 16) fprintf(spfp, "Proc %s: Swap bytes before conversion to ULAW\n", proc); for (o=0; o < out_samples*spstat->user_channel_count; o++) { /* Pre swap the bytes */ char *p , temp; short odata; odata = *sh_data; p = (char *) &odata; temp = *p; *p = *(p + 1); *(p + 1) = temp; *ulaw_data = linear2ulaw(odata); sh_data ++; ulaw_data ++; } } else for (o=0; o < out_samples*spstat->user_channel_count; o++) { *ulaw_data = linear2ulaw(*sh_data);/* if(o<30){ fprintf(spfp," conv %6d-%4x %4d-%2x", *sh_data,*sh_data, *ulaw_data, *ulaw_data); if ((o+1) % 4 == 0) fprintf(spfp,"\n"); }*/ sh_data ++; ulaw_data ++; } out_block = (void *)spwav->converted_buffer; } else { /* use the buffer passed to the function */ out_block = out_inter_block; } /*****************************************************/ /**** INVARIANT ASSERTION: ****/ /**** out_block points to the file_encoded data ****/ /**** but the same channel count as the user's ****/ /**** input. ****/ /*****************************************************/ /* do the channel modifications */ if (spstat->channels != CHANNELSNULL){ int in_recsize, out_recsize, in_chancnt, out_chancnt, out_snb; int oc, s; void *eod; if (sp_verbose > 15) fprintf(spfp,"Proc %s: Converting channels\n",proc); in_chancnt = spstat->user_channel_count; in_recsize = spstat->file_sample_n_bytes * in_chancnt; out_chancnt = spstat->file_channel_count; out_snb = spstat->file_sample_n_bytes; out_recsize = spstat->file_sample_n_bytes * out_chancnt; /* process each requested channel */ 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 = (char *)out_block + ((spstat->channels->ochan[oc].orig_channel[0]-1)* spstat->file_sample_n_bytes); out_ptr = (char *)spwav->file_data_buffer + (oc * spifr->status->file_sample_n_bytes); /* fprintf(spfp, " %x %x %x %x\n",in_ptr,in_conversion_block, out_ptr , out_block); fprintf(spfp," %d %d\n",in_recsize, out_recsize); */ for (s=0; s<out_samples; s++){ memcpy(out_ptr,in_ptr,out_snb); in_ptr += in_recsize; out_ptr += out_recsize; } in_ptr-=((spstat->channels->ochan[oc].orig_channel[0]-1)* spstat->file_sample_n_bytes); eod = (void *)in_ptr; } else { /* Channels need to be added together */ if (spstat->file_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 *)out_block; out_ptr = (short *)spwav->file_data_buffer; ochn = &(spstat->channels->ochan[oc]); if (spstat->file_sbf== get_natural_sbf(2)){ for (s=0; s<out_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; } eod = in_ptr; } 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<out_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; } eod = in_ptr; } } else if (spifr->status->file_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 *)out_block; out_ptr = (unsigned char *)spwav->file_data_buffer; ochn = &(spifr->status->channels->ochan[oc]); for (s=0; s<out_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; } eod = in_ptr; } } /**** in_ptr MUST point to the next input block of data ***/ out_block = spwav->file_data_buffer; } ret = fob_fwrite(out_block, spstat->file_sample_n_bytes* spstat->file_channel_count, out_samples, spwav->sp_fob); if (ret != out_samples){ return_err(proc,306,0, rsprintf("Unable to write data %d written, not %d", ret,out_samples)); } samples_written += ret; /* Perform the checksum computation */ switch (spstat->file_encoding){ case SP_se_pcm2: /* if the user data is byte swapped */ /* or if the translated data is byte swapped */ if (((spstat->file_encoding == spstat->user_encoding) && (spstat->user_sbf != get_natural_sbf(2))) || ((spstat->file_encoding != spstat->user_encoding) && (spstat->file_sbf != get_natural_sbf(2))) ){ if (sp_verbose > 16) fprintf(spfp,"Proc %s: Swapping for checksum\n",proc); checksum = sp_compute_short_checksum(out_block, out_samples* spstat->file_channel_count, TRUE); } else { if (sp_verbose > 16) fprintf(spfp, "Proc %s: Not Swapping for checksum\n",proc); checksum = sp_compute_short_checksum(out_block, out_samples* spstat->file_channel_count, FALSE); } if (sp_verbose > 16) fprintf(spfp, "Proc %s: short checksum = %d Swap is %s old Swap is %s\n", proc,checksum, t_or_f(((spstat->file_encoding == spstat->user_encoding) && (spstat->natural_sbf != spstat->user_sbf)) || ((spstat->file_encoding != spstat->user_encoding) && (spstat->file_sbf != SP_sbf_01))), t_or_f(spstat->natural_sbf != spstat->user_sbf)); spwav->checksum = sp_add_checksum(spwav->checksum,checksum); spwav->samples_written += ret; break; default: { int n; n = out_samples * spstat->file_channel_count * spstat->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(out_block,n); spwav->checksum = sp_add_checksum(spwav->checksum,checksum); spwav->samples_written += ret; } } if (sp_verbose > 11) fprintf(spfp,"Proc %s: Block %d: Requested %d samples, written %d total %d, Checksum %d (BC %d)\n", proc,block++,num_sample,ret, spwav->samples_written, checksum,spwav->checksum); } return_success(proc,0,samples_written,"ok"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -