Beispiel #1
0
/*
 * Initialize stream for WAVE COMP 360 format (do *not* allocate the buffer)
 */
int sig_sphere_stream_init(sigstream_t *f, const char *fn)
{
  long n, rate;
  
  /* open file */
  if (fn) {
    if ((f->name = strdup(fn)) == NULL) {
      fprintf(stderr, "sig_wave_stream_init(): cannot set stream name %s\n", fn);
      return(SPRO_ALLOC_ERR);
    }
  }
  
  if ((f->f = sp_open((fn) ? (char *)(fn) : "-", "r")) == NULL) {
    fprintf(stderr, "sig_wave_stream_init(): cannot open SPHERE stream %s\n", (fn) ? (fn) : "stdin");
    return(SPRO_SIG_READ_ERR);
  }
  
  /* set convertion to PCM16 */
  if (sp_set_data_mode(f->f, "SE-PCM-2:DF-RAW") != 0) {
    fprintf(stderr, "sig_sphere_stream_init(): cannot set convertion mode to 16 bits/sample PCM\n");
    return(SPRO_SIG_READ_ERR);
  }

  /* read header fields */
  if (sp_h_get_field(f->f, "channel_count", T_INTEGER, (void *)&n) != 0
      || sp_h_get_field(f->f, "sample_rate", T_INTEGER, (void *)&rate) != 0) {
    fprintf(stderr, "sig_sphere_stream_init(): cannot read SPHERE header from stream %s \n", (fn) ? (fn) : "stdin");    
    return(SPRO_SIG_READ_ERR);
  }

  f->Fs = (float)rate;
  f->nchannels = (unsigned short)n;
  f->nbps = 2;

  return(0);
}
Beispiel #2
0
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");
}
Beispiel #3
0
int diff_waveforms(char *file1, char *file2,
		   char *conv_str1, char *conv_str2, int verbose, FILE *fp)
{
    SP_FILE *sp1, *sp2;
    char *buff1=CNULL, *buff2=CNULL;
    int n1, n2, i, fail=0, blocksize=512;
    int return_value = 0;
    SP_INTEGER samples_processed=0, file1_snb, file2_snb, file1_chcnt;
    SP_INTEGER file2_chcnt;

    if ((sp1 = sp_open(file1, "r")) == SPNULL){
	fail=1;
	if (verbose)
	    fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",file1,file2);
	if (verbose)
	    fprintf(fp,"sp_open failed on file %s\n",file1);
	sp_print_return_status(fp);
    }
    if ((sp2 = sp_open(file2, "r")) == SPNULL){
	if (fail == 0)
	    if (verbose)
	       fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",
		       file1,file2);
	fail=1;

	if (verbose)
	    fprintf(fp,"sp_open failed on file %s\n",file2);
	sp_print_return_status(fp);
	goto FATAL_QUIT;
    }    

    if (fail)
	goto FATAL_QUIT;

    if (conv_str1 != CNULL)
	if (sp_set_data_mode(sp1,conv_str1) != 0){
	    fail=1;
	    if (verbose)
	       fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",
		       file1,file2);
	    if (verbose)
		fprintf(fp,"sp_set_data_mode failed on file %s\n",file1);
	    sp_print_return_status(fp);
	}	    
    if (conv_str2 != CNULL)
	if (sp_set_data_mode(sp2,conv_str2) != 0){
	    if (fail == 0)
		if (verbose)
		    fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",
			    file1,file2);
	    fail=1;
	    if (verbose) 
		fprintf(fp,"sp_set_data_mode failed on file %s\n",file2);
	    sp_print_return_status(fp);
	}
    if (fail)
	goto FATAL_QUIT;

    if (sp_h_get_field(sp1,SAMPLE_N_BYTES_FIELD,
		       T_INTEGER,(void *)&file1_snb) != 0){
	fprintf(spfp,"Unable to retieve %s field from file '%s'\n",
		SAMPLE_N_BYTES_FIELD,file1);
	goto FATAL_QUIT;
    }
    if (sp_h_get_field(sp2,SAMPLE_N_BYTES_FIELD,
		       T_INTEGER,(void *)&file2_snb) != 0){
	fprintf(spfp,"Unable to retieve %s field from file '%s'\n",
		SAMPLE_N_BYTES_FIELD,file2);
	goto FATAL_QUIT;
    }
    if (sp_h_get_field(sp1,CHANNEL_COUNT_FIELD,
		       T_INTEGER,(void *)&file1_chcnt) != 0){
	fprintf(spfp,"Unable to retieve %s field from file '%s'\n",
		CHANNEL_COUNT_FIELD,file1);
	goto FATAL_QUIT;
    }
    if (sp_h_get_field(sp2,CHANNEL_COUNT_FIELD,
		       T_INTEGER,(void *)&file2_chcnt) != 0){
	fprintf(spfp,"Unable to retieve %s field from file '%s'\n",
		CHANNEL_COUNT_FIELD,file2);
	goto FATAL_QUIT;
    }


    if ((buff1 = (char *)sp_data_alloc(sp1,blocksize)) == CNULL){
	if (verbose) fprintf(fp,"Unable to malloc memory for buffer 1\n");
	goto FATAL_QUIT;
    }
    if ((buff2 = (char *)sp_data_alloc(sp2,blocksize)) == CNULL){
	if (verbose) fprintf(fp,"Unable to malloc memory for buffer 2\n");
	goto FATAL_QUIT;
    }

    if (file1_snb != file2_snb){
	if (verbose){
	    fprintf(fp,"Files '%s' and '%s' do not have the same",
		    file1,file2);
	    fprintf(fp," sample_n_bytes value, %d and %d respectively\n",
		    file1_snb,file2_snb);
	}
	goto FATAL_QUIT;
    }	

    if (file1_chcnt != file2_chcnt){
	if (verbose){
	    fprintf(fp,"Files '%s' and '%s' do not have the same",
		    file1,file2);
	    fprintf(fp," channel_count value, %d and %d respectively\n",
		    file1_chcnt,file2_chcnt);
	}
	goto FATAL_QUIT;
    }	

    do {
	int record_size = file1_snb * file1_chcnt;
	n1 = sp_read_data((char *)buff1,blocksize,sp1);
	if ((n1 == 0) && (sp1->read_spifr->waveform->samples_read !=
			  sp1->read_spifr->status->user_sample_count)){
	    sp_print_return_status(fp);
	    goto FATAL_QUIT;
	}
	n2 = sp_read_data((char *)buff2,blocksize,sp2);
	if ((n2 == 0) && (sp2->read_spifr->waveform->samples_read !=
			  sp2->read_spifr->status->user_sample_count)){
	    sp_print_return_status(fp);
	    goto FATAL_QUIT;
	}
	if (n1 != n2){
	    if (verbose)
		fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",
			file1,file2);
	    if (verbose) 
		fprintf(fp,"   %d samples read from '%s'\n",n1,file1);
	    if (verbose)
		fprintf(fp,"   %d samples read from '%s'\n",n2,file2);
	    goto FATAL_QUIT;
	}
	for (i=0; i<n1; i++) {
	    if (memcmp(buff1 + i*record_size,
		     buff2 + i*record_size,record_size)){
		int bn;
		if (verbose){
		  fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s sample %d\n",
			  file1,file2,samples_processed+i);
		    fprintf(fp,"    File 1:  ");
		    for (bn=0; bn < record_size; bn++) {
			fprintf(fp,"%2x ",
			     *((unsigned char *)(buff1 + i*record_size + bn)));
			if ((bn+1) % file1_snb == 0)
			    fprintf(fp,"| ");
		    }
		    fprintf(fp,"\n    File 2:  ");
		    for (bn=0; bn < record_size; bn++) {
			fprintf(fp,"%2x ",
			     *((unsigned char *)(buff2 + i*record_size + bn)));
			if ((bn+1) % file1_snb == 0)
			    fprintf(fp,"| ");
		    }
		    fprintf(fp,"\n");
		}
		goto FATAL_QUIT;
	    }
	}
	samples_processed += n1;
    } while (!sp_eof(sp1));
    if (!sp_eof(sp2)){
	if (verbose)
	    fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",file1,file2);
	if (verbose)
	    fprintf(fp,"   file %s not at eof\n",file2);
	goto FATAL_QUIT;
    }
    return_value = 0;
    goto CLEAN_UP;

  FATAL_QUIT:  /* Failed routine */
    return_value = 100;

  CLEAN_UP:
    if (sp1 != SPNULL) {
	if (buff1 != (char *)NULL) sp_data_free(sp1,buff1);
	sp_close(sp1);
    }
    if (sp2 != SPNULL) {
	if (buff2 != (char *)NULL) sp_data_free(sp2,buff2);
	sp_close(sp2);
    }
    return(return_value);
}