⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spclose.c

📁 speech signal process tools
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>#define SPHERE_LIBRARY_CODE#include <sp/sphere.h>#include <string.h>#include <setjmp.h>extern jmp_buf	exitenv;int verify_file_checksum(char *filename) ;int sp_close(SP_FILE *sp){    char *proc="sp_close " SPHERE_VERSION_STR;    char *write_name;    char *read_name;    SP_INTEGER lint=0, header_size=0, data_size=0;    int header_changed=FALSE;    SPIFR *spifr;    SPSTATUS *w_spstat, *r_spstat;    int ret, verify_checksum=FALSE;    if (sp_verbose > 10) fprintf(spfp,"Proc %s:\n",proc);    if (sp == SPNULL)	return_err(proc,100,100,"Null SPFILE pointer");    w_spstat = sp->write_spifr->status;    r_spstat = sp->read_spifr->status;    write_name = (w_spstat->external_filename == CNULL) ? CNULL :	mtrf_strdup(sp->write_spifr->status->external_filename);    read_name =  (r_spstat->external_filename == CNULL) ? CNULL : 	mtrf_strdup(r_spstat->external_filename);    if (sp->open_mode == SP_mode_update) {	if (sp_verbose > 10) fprintf(spfp,"Proc %s:  Mode SP_update\n",proc);	if (w_spstat->write_occured_flag) {	    /* if there has been a spwrite, then the waveform is written     */	    /* as if it were a file opened for write */	    /* Step 1: recursively call sp_close, changing the mode to write */	    /* Step 2: delete the previous file                              */	    /* Step 3: rename the temporary file                             */	    if (sp_verbose > 15)		fprintf(spfp,"Proc %s: Overwriting the original waveform\n",		       proc);	    sp->open_mode = SP_mode_write;	    if ((ret=sp_close(sp)) != 0){		unlink(write_name);		if (write_name != CNULL) mtrf_free(write_name);		if (read_name != CNULL) mtrf_free(read_name);		return_child(proc,int,ret);	    }	    unlink(read_name);	    rename(write_name,read_name);	    if (write_name != CNULL) mtrf_free(write_name);	    if (read_name != CNULL) mtrf_free(read_name);	    return_success(proc,0,0,"ok");	} else {	    /* the header has been changed and the data mode of the waveform */	    /* COULD BE CHANGED the waveform has not been modified in any    */	    /* way, only the header has changed */	    /* Step 1: write the header into the temporary file           */	    /* Step 2: If the header changed in size OR the waveform      */            /*         format has changed                                 */	    /*            A: copy the waveform into the temp file         */	    /*            B: close the files                              */	    /*            C: delete the old file in favor of the new file */	    /*         Else the header has not changed in size.           */	    /*            A: write the header into the original file      */	    /*            B: Close both files                             */	    /*            C: delete the temporary file                    */	    FILE *fp;	    int samples_read, samples_written;	    /* Step 1: */	    spifr = sp->write_spifr;	    fp = ((spifr->waveform->sp_fp != FPNULL) ?		  (spifr->waveform->sp_fp) : 		  ((spifr->waveform->sp_fob->fp != FPNULL) ?		   (spifr->waveform->sp_fob->fp) : FPNULL));	    if (sp_verbose > 15)		fprintf(spfp,			"Proc %s: Writing header to temp file.  position %d\n",		       proc,ftell(fp));	    if (fp == FPNULL){		free_sphere_t(sp);		unlink(write_name);		if (write_name != CNULL) mtrf_free(write_name);		if (read_name != CNULL) mtrf_free(read_name);		return_err(proc,3000,3000,"Internal Error");	    }	    /* Write the header into the temporary file to compute the size  */	    /* of the header and then rewind back over the just written header*/	    rewind(fp);	    if (sp_write_header(fp,spifr->status->file_header,				&header_size,&data_size) < 0){		free_sphere_t(sp);		unlink(write_name);		if (write_name != CNULL) mtrf_free(write_name);		if (read_name != CNULL) mtrf_free(read_name);		return_err(proc,3001,3001,			   "Unable to update header in file");	    }	    rewind(fp);	    /* Step 2   -  -  if the header size or waveform has changed */	    if ((sp->read_spifr->waveform->header_data_size != header_size) || 		(w_spstat->file_encoding != r_spstat->file_encoding) ||		(w_spstat->file_compress != r_spstat->file_compress) ||		(w_spstat->file_sbf != r_spstat->file_sbf) ||		(w_spstat->channels != CHANNELSNULL)){		char *buff;		int ns, nc, in_nspb, out_nspb;		if (sp_verbose > 15) {		    fprintf(spfp,"Proc %s:   output header and/or",proc);		    fprintf(spfp,"data has changed, copying file.\n");		    fprintf(spfp,"Proc %s:       from %d to %d\n",proc, 			   sp->read_spifr->waveform->header_data_size,			   header_size);		}		ns = r_spstat->user_sample_count;		nc = r_spstat->user_channel_count;		in_nspb  = r_spstat->user_sample_n_bytes * 		    r_spstat->user_channel_count;		out_nspb = w_spstat->user_sample_n_bytes *		     w_spstat->user_channel_count;		if ((buff=mtrf_malloc(nc * in_nspb * 4096)) == CNULL) {		    free_sphere_t(sp);		    unlink(write_name);		    if (write_name != CNULL) mtrf_free(write_name);		    if (read_name != CNULL) mtrf_free(read_name);		    return_err(proc,3010,3010,			       "Unable to malloc transfer buffer space");		}		/* A: */		do {		    sp->open_mode = SP_mode_read;			    samples_read = sp_read_data(buff,4096,sp);		    if (samples_read > 0) {			sp->open_mode = SP_mode_write;			samples_written = sp_write_data(buff,samples_read,sp);			if (samples_written != samples_read){			    free_sphere_t(sp);			    unlink(write_name);			    if (write_name != CNULL) mtrf_free(write_name);			    if (read_name != CNULL) mtrf_free(read_name);			    return_err(proc,3012,3012,				       "Copy of waveform data failed");			}		    } else { 			if (sp_eof(sp) == 0) {			    free_sphere_t(sp);			    unlink(write_name);			    if (write_name != CNULL) mtrf_free(write_name);			    if (read_name != CNULL) mtrf_free(read_name);			    return_err(proc,3013,3013,				  "Error: Zero samples read while not at EOF");			}			if (sp_error(sp) >= 100) {			    /* a checksum error occured, close the sp and */			    /* delete the temp file */			    sp->open_mode = SP_mode_update;			    sp_print_return_status(spfp);			    free_sphere_t(sp);			    unlink(write_name);			    if (write_name != CNULL) mtrf_free(write_name);			    if (read_name != CNULL) mtrf_free(read_name);			    mtrf_free(buff);			    return_err(proc,3011,3011,				       "Error copying waveform data");			}		    }		    sp->open_mode = SP_mode_update;		} while (samples_read > 0);		mtrf_free(buff);		/* make sure the file is at eof (as if it were opened for   */		/* read) */		sp->open_mode = SP_mode_read;		if (! sp_eof(sp)){		    sp->open_mode = SP_mode_update;		    free_sphere_t(sp);		    unlink(write_name);		    if (write_name != CNULL) mtrf_free(write_name);		    if (read_name != CNULL) mtrf_free(read_name);		    return_err(proc,3012,3012,"Error copying waveform data");		}		sp->open_mode = SP_mode_write;		if ((ret=sp_close(sp)) != 0){		    unlink(write_name);		    if (write_name != CNULL) mtrf_free(write_name);		    if (read_name != CNULL) mtrf_free(read_name);		    return_child(proc,int,ret);		}		/* C: */		unlink(read_name);		rename(write_name,read_name);		if (write_name != CNULL) mtrf_free(write_name);		if (read_name != CNULL) mtrf_free(read_name);		return_success(proc,0,0,"ok");	    } else { 		/* A: */		spifr = sp->read_spifr;		fp = ((spifr->waveform->sp_fp != FPNULL) ? 		      (spifr->waveform->sp_fp) : 		      ((spifr->waveform->sp_fob->fp != FPNULL) ?		       (spifr->waveform->sp_fob->fp) : FPNULL));		if (fp == FPNULL)		    return_err(proc,3002,3002,"Internal Error");		rewind(fp);		if (sp_verbose > 15)		    fprintf(spfp,			 "Proc %s:   header size not changed.  position %d\n",			   proc,ftell(fp));		if (sp_write_header(fp,w_spstat->file_header,				    &header_size,&data_size) < 0){		    free_sphere_t(sp);		    unlink(write_name);		    if (write_name != CNULL) mtrf_free(write_name);		    if (read_name != CNULL) mtrf_free(read_name);		    return_err(proc,3003,3003,			       "Unable to update header in file");		}		/* B: */		free_sphere_t(sp);		/* C: */		unlink(write_name);		if (write_name != CNULL) mtrf_free(write_name);		if (read_name != CNULL) mtrf_free(read_name);		return_success(proc,0,0,"ok");	    }	}    }    /*  END of update mode file */    if (sp->open_mode == SP_mode_write) {	if (sp_verbose > 10) fprintf(spfp,				     "Proc %s:  Mode SP_mode_write\n",proc);	spifr = sp->write_spifr;	/* flush the data to the file */	if (spifr->status->is_disk_file)	    fob_fflush(spifr->waveform->sp_fob);	/* if the mode is write, update the sample_count and checksum */	/* field if needed.  If the checksum field exists, verify it, */	/* and warn if it's not the same */	/************ ONLY UPDATE FIELDS IF THE FILE IS NOT A STREAM *********/	if (spifr->status->is_disk_file){	    h_get_field(w_spstat->file_header, SAMPLE_COUNT_FIELD,			T_INTEGER, (void *)&lint);	    if (spifr->waveform->samples_written != lint) {		/* then update the field */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -