Exemplo n.º 1
0
void free_input()
{
    int i;
    if( input_strs_args )
    {
        for( i = 0; i < size_input_prm; ++i )
        {
            if( input_strs_args[i] )
            {
                free( input_strs_args[i] );
            }
        }
        free( input_strs_args );
    }
    if( input_strs_file )
    {
        for( i = 0; i < size_input_prm; ++i )
        {
            if( input_strs_file[i] )
            {
                free( input_strs_file[i] );
            }
        }
        free( input_strs_file );
    }
    for( i = 0; i < size_input_prm; ++i )
    {
        if( input_prm[i]->ptr )
        {
            if( strcmp( input_prm[i]->type, TO_STR(PTR_DOUBLE9) ) == 0 )
            {
                free(*(PTR_DOUBLE9*)input_prm[i]->ptr);
            }
            else if( strcmp( input_prm[i]->type, TO_STR(PTR_INT) ) == 0 )
            {
                free(*(PTR_INT*)input_prm[i]->ptr);
            }
            else if( strcmp( input_prm[i]->type, TO_STR(PTR_DOUBLE) ) == 0 )
            {
                free(*(PTR_DOUBLE*)input_prm[i]->ptr);
            }
            else if( strcmp( input_prm[i]->type, TO_STR(PTR_STR) ) == 0 )
            {
                free(*(PTR_STR*)input_prm[i]->ptr);
            }
            else if( strcmp( input_prm[i]->type, TO_STR(STR) ) == 0 && input_prm[i]->size == -1 )
            {
                free(*(STR*)input_prm[i]->ptr);
            }
        }
    }
    free_rand();
}
Exemplo n.º 2
0
int embed(char *infile_path, char *outfile_path, char *orig_outfile_path)
{ //{{{
  SF_INFO	sfinfo;		// struct with info on the samplerate, number of channels, etc
  SNDFILE   	*s_infile;
  SNDFILE   	*s_outfile;
  SNDFILE			*s_orig_out;

  fftw_plan 	*ft_forward;
  fftw_plan 	*ft_reverse;
  double	*time_buffer;
  complex	*freq_buffer;

  void		(*embed_to_signal)(complex *, int);
	int channels;
  
  if(wmark->type == FH_EMBED)
    embed_to_signal = &fh_embed_to_signal;
  else
    embed_to_signal = &ss_embed_to_signal;

  seed_rand(wmark->key_seed);
  set_rand(wmark->len); 

  //
  // Open input file and output file
  // -------------------------------

  // The pre-open SF_INFO should have format = 0, everything else will be set in the open call
  sfinfo.format = 0;
  
  // s_infile = input audio file
  if(!(s_infile = sf_open(infile_path, SFM_READ, &sfinfo))){
    fprintf(stderr,"error opening the following file for reading: %s\n", infile_path);
    return 1;
  }

  // s_outfile = watermaked audio file
  if(!(s_outfile = sf_open(outfile_path, SFM_WRITE, &sfinfo))){
    fprintf(stderr,"error opening the following file for writing: %s\n", outfile_path);
    return 1;
  }

  // s_orig_out = watermaked audio file
  if(!(s_orig_out = sf_open(orig_outfile_path, SFM_WRITE, &sfinfo))){
    fprintf(stderr,"error opening the following file for writing: %s\n", orig_outfile_path);
    return 1;
  }

	channels = sfinfo.channels;
	if(embed_verbose)
		print_sfile_info(sfinfo);

  //
  // Read, process, and write data
  // -----------------------------

  time_buffer = (double *) malloc(sizeof(double) * BUFFER_LEN * channels);
  freq_buffer = (complex *) fftw_malloc(sizeof(complex) * FREQ_LEN * channels);
  
	ft_forward = (fftw_plan *)malloc(sizeof(fftw_plan) * sfinfo.channels);
	ft_reverse = (fftw_plan *)malloc(sizeof(fftw_plan) * sfinfo.channels);

	for(int i = 0; i < channels; i++){
		ft_forward[i] = fftw_plan_dft_r2c_1d(BUFFER_LEN, time_buffer+i*BUFFER_LEN,
																				 freq_buffer+i*FREQ_LEN, FFTW_ESTIMATE);
		ft_reverse[i] = fftw_plan_dft_c2r_1d(BUFFER_LEN, freq_buffer+i*FREQ_LEN,
																			 time_buffer+i*BUFFER_LEN, FFTW_ESTIMATE);
	}

  int bytes_read;

  int counter = 0;
  while(bytes_read = sf_read_double(s_infile, time_buffer, BUFFER_LEN*channels)){
		// embed to orig_out to account for some of the floating point errors
		sf_write_double(s_orig_out, time_buffer, bytes_read);
      
    // only embed in a frame where we read in the full amount for the DFT
		if(bytes_read == BUFFER_LEN*channels){

			// if there are, say, 2 channels a,b, the audio will be stored as
			// a1, b1, a2, b2, ...
			// convert it to a1, a2, ... , b1, b2, ...
			deinterleave_channels(time_buffer, channels);

			for(int i = 0; i < channels; i++)
				fftw_execute(ft_forward[i]);

			// DEBUG print embed read info
			if(counter == embed_debug){
				printf("org: ");
				print_pow_density(freq_buffer, 10);
			}

			embed_to_signal(freq_buffer, FREQ_LEN*channels);

			// DEBUG print embed read info
			if(counter == embed_debug){
				printf("new: ");
				print_pow_density(freq_buffer, 10);
			}

			for(int i = 0; i < channels; i++)
				fftw_execute(ft_reverse[i]);

			// The DFT naturally multiplies every array element by the size of the array, reverse this
			array_div(BUFFER_LEN, time_buffer, BUFFER_LEN*channels);

			interleave_channels(time_buffer, channels);

		} // if(bytes_read == BUFFER_LEN)
		sf_write_double(s_outfile, time_buffer, bytes_read);

			//if(counter == embed_debug){
			//	deinterleave_channels(time_buffer, channels);
			//	for(int i = 0; i < channels; i++)
			//		fftw_execute(ft_forward[i]);
			//		printf("pt:  "); print_pow_density(freq_buffer,10);
			//		printf("cpt: "); print_complex_array(freq_buffer,10);

			//	for(int i = 0; i < channels; i++)
			//		fftw_execute(ft_reverse[i]);
			//	interleave_channels(time_buffer, channels);
			//	array_div(BUFFER_LEN, time_buffer, BUFFER_LEN*channels);
			//}

		counter++;
		embed_iteration++;
	} //while

  //
  // free everything
  // ---------------

	for(int i = 0; i < channels; i++){
		fftw_destroy_plan(ft_forward[i]);
		fftw_destroy_plan(ft_reverse[i]);
	}
	free(ft_forward);
	free(ft_reverse);
  free(time_buffer);
  fftw_free(freq_buffer);
  free_rand();
  sf_close(s_infile);
  sf_close(s_outfile);
  sf_close(s_orig_out);
} //}}}