Example #1
0
int
main(int argc, char *argv[])
{
  double **sound, **image;
  int i;

  char path[MUT_MAX_PATH_LEN], name[MUT_MAX_NAME_LEN], ext[MUT_MAX_NAME_LEN];

  //=============================
  // parse command line arguments
  //=============================

  if (! mut_parse_cmd_line(argc, argv, arglist, NULL))
  {
    fprintf(stderr, "ASPERES %s\n", version);
    fprintf(stderr, "%s:\n%s.\n", err_1, mut_errmsg(mut_last_error()));
    return 1;
  }

  //==================================
  // check for help or version request
  //==================================
  
  if (help_req)
  {
    print_help();
    return 1;
  }

  if (vers_req)
  {
    print_version();
    return 1;
  }

  //===============
  // set up logging
  //===============

  mut_set_logname(logname);
  mut_setlog(1);
  unlink(logname);
  mut_get_date(date);
  message("ASPERES version %s (%s)", version, date);

  //=================
  // read config file
  //=================

  if (read_config_file(config_file) < 0)
  {
    message("%s %s", err_4, config_file);
    return 1;
  }

  //===============================
  // process command line arguments
  //===============================

  //======= program operation mode =======

  if (prog_mode_s[0] == 0)
  {
    message("%s", err_2);
    return 1;
  }

  if (strcmp(prog_mode_s, "anal") == 0)
    prog_mode = MODE_ANAL;
  else if (strcmp(prog_mode_s, "sine") == 0)
    prog_mode = MODE_SINE_SYNTH;
  else if (strcmp(prog_mode_s, "noise") == 0)
    prog_mode = MODE_NOISE_SYNTH;
  else
  {
    message("%s (%s)", err_3, prog_mode_s);
    return 1;
  }

  //======= file names =======

  if (input_file[0] == 0)
  {
    message("%s", err_5);
    return 1;
  }
      
  if (! mut_fname_split(input_file, path, name, ext))
  {
     message("%s", err_16);
     return 1;
  }

  if (prog_mode == MODE_ANAL)
  {
     if (strcmp(ext, ".wav") != 0)
     {
       message("%s", err_17);
       return 1;
     }
  }
  else
  {
     if (strcmp(ext, ".bmp") != 0)
     {
       message("%s", err_18);
       return 1;
     }
  }
  
  if (output_file[0] == 0) 
  {
    strcpy(output_file, path);
    strcat(output_file, name);
    strcat(output_file, "~");
    
    if (prog_mode == MODE_ANAL)
      strcat(output_file, ".bmp");
    else
      strcat(output_file, ".wav");
  }
 
  //======= WAV sample rate =======

  if (wav_rate_s[0] != 0)
  {
    if (! mut_stoi(wav_rate_s, MUT_BASE_DEC, &wav_rate))
    {
      message("%s '%s'", err_7, wav_rate_s);
      return 1;
    }
  }

  i = 0;
  while (std_wav_rates[i] != -1)
  {
    if (std_wav_rates[i] == wav_rate)
      break;
    else
      i++;
  }
    
  if (std_wav_rates[i] == -1)
  {
    message("'%s' %s", wav_rate_s, err_8);
    return 1;
  }

  //======= frequency limits =======

  if (low_freq_s[0] != 0)
  {
    if (! mut_stof(low_freq_s, &low_freq))
    {
      message("%s '%s'", err_7, low_freq_s);
      return 1;
    }
  }
      
  if (low_freq < MIN_LOW_FREQ || low_freq > MAX_LOW_FREQ)
  {
    message("%s", err_9);
    return 1;
  }

  if (high_freq_s[0] != 0)
  {
    if (! mut_stof(high_freq_s, &high_freq))
    {
      message("%s '%s'", err_7, high_freq_s);
      return 1;
    }
  }

  if (high_freq < MIN_HIGH_FREQ || high_freq > MAX_HIGH_FREQ)
  {
    message("%s", err_10);
    return 1;
  }
      
  //======= resolutions =======

  if (band_per_oct_s[0] != 0)
  {
    if (! mut_stof(band_per_oct_s, &band_per_oct))
    {
      message("%s '%s'", err_7, band_per_oct_s);
      return 1;
    }
  }
      
  if (band_per_oct < MIN_BPO || band_per_oct > MAX_BPO)
  {
    message("%s", err_11);
    return 1;
  }

  if (pix_per_sec_s[0] != 0)
  {
    if (! mut_stof(pix_per_sec_s, &pix_per_sec))
    {
      message("%s '%s'", err_7, pix_per_sec_s);
      return 1;
    }
  }

  if (pix_per_sec < MIN_PPS || pix_per_sec > MAX_PPS)
  {
    message("%s", err_12);
    return 1;
  }
      
  //======= brightness control =======

  if (gamma_corr_s[0] != 0)
  {
    if (! mut_stof(gamma_corr_s, &gamma_corr))
    {
      message("%s '%s'", err_7, gamma_corr_s);
      return 1;
    }
  }

  if (gamma_corr < MIN_GAMMA || gamma_corr > MAX_GAMMA)
  {
    message("%s", err_13);
    return 1;
  }

  //======= image dimensions =======

  if (img_width_s[0] != 0)
  {
    if (! mut_stoi(img_width_s, MUT_BASE_DEC, &img_width))
    {
      message("%s '%s'", err_7, img_width_s);
      return 1;
    }
  }

  if (img_height_s[0] != 0)
  {
    if (! mut_stoi(img_height_s, MUT_BASE_DEC, &img_height))
    {
      message("%s '%s'", err_7, img_height_s);
      return 1;
    }
  }

  //======= miscellaneous parameters =======

  if (use_linear)
    logbase = 1;
  else
    logbase = 2;

  //====================
  // open & create files
  //====================

  infile = fopen(input_file, "rb");
  if (infile == NULL)
  {
    message("%s", err_14);
    return 1;
  }

  outfile = fopen(output_file, "wb");
  if (outfile == NULL)
  {
    fclose(infile);
    message("%s", err_15);
    return 1;
  }

  //===================================
  // perform the requested operation(s)
  //===================================

  srand(time(NULL));

  if (prog_mode == MODE_ANAL)
  {
    message("Sound '%s' to spectrogram '%s'", input_file, output_file);
    sound = wav_in(infile, &channels, &samplecount, &wav_rate);
    setup
    (
      &img_height, samplecount, &wav_rate, &low_freq, &high_freq,
      &pix_per_sec, &band_per_oct, img_width, 0
    );

    start_time = gettime();
    image = anal
            (
              sound[0], samplecount, wav_rate,
              &img_width, img_height, band_per_oct, pix_per_sec, low_freq
            );

    if (gamma_corr != 1.0)
      brightness_control(image, img_height, img_width, 1.0 / gamma_corr);

    bmp_out(outfile, image, img_height, img_width);
  }

  if (prog_mode == MODE_SINE_SYNTH || prog_mode == MODE_NOISE_SYNTH)
  {
    message("Image '%s' to sound '%s'", input_file, output_file);
    sound = calloc(1, sizeof(double *));
    image = bmp_in(infile, &img_height, &img_width);

    setup
    (
      &img_height, samplecount, &wav_rate, &low_freq, &high_freq,
      &pix_per_sec, &band_per_oct, img_width, 1
    );

    if (gamma_corr != 1.0)
      brightness_control(image, img_height, img_width, gamma_corr);

    start_time = gettime();

    if (prog_mode == MODE_SINE_SYNTH)
      sound[0] = synt_sine
                 (
                   image, img_width, img_height, &samplecount, wav_rate,
                   low_freq, pix_per_sec, band_per_oct
                 );
    else
      sound[0] = synt_noise
                 (
                   image, img_width, img_height, &samplecount, wav_rate,
                   low_freq, pix_per_sec, band_per_oct
                 );

    wav_out(outfile, sound, 1, samplecount, wav_rate, WAV_FORMAT);
  }

  end_time = gettime();
  message("Processing time: %.3f s", (double) (end_time - start_time) / 1000.0);

  return 0;
}
Example #2
0
int main(int argc, char *argv[])
{
	log_file("Starting arss ...");
	test_fft();

	FILE *fin;
	FILE *fout;
	int32_t  i;
	double **sound, **image, basefreq=0, maxfreq=0, pixpersec=0, bpo=0, brightness=1;
	int32_t channels, samplecount=0, samplerate=0, Xsize=0, Ysize=0, format_param=0;
	int32_t clockb;
	char mode=0, *in_name=NULL, *out_name=NULL;

	// initialisation of global using defaults defined in dsp.h
	pi=PI_D;
	LOGBASE=LOGBASE_D;
	LOOP_SIZE_SEC=LOOP_SIZE_SEC_D;
	BMSQ_LUT_SIZE=BMSQ_LUT_SIZE_D;
	#ifdef QUIET
	quiet=1;
	#else
	quiet=0;
	#endif

	printf("The Analysis & Resynthesis Sound Spectrograph %s\n", version);

	srand(time(NULL));

	for (i=1; i<argc; i++)
	{
		if (strcmp(argv[i], "/?")==0)	// DOS friendly help
		{
			argv[i][0] = '-';
			argv[i][1] = 'h';
		}

		if (argv[i][0] != '-')				// if the argument is not a function
		{
			if (in_name==NULL)			// if the input file name hasn't been filled in yet
				in_name = argv[i];
			else
				if (out_name==NULL)		// if the input name has been filled but not the output name yet
					out_name = argv[i];
				else				// if both names have already been filled in
				{
					fprintf(stderr, "You can only have two file names as parameters.\nRemove parameter \"%s\".\nExiting with error.\n", argv[i]);
					exit(EXIT_FAILURE);
				}
		}
		else						// if the argument is a parameter
		{
			if (strcmp(argv[i], "--analysis")==0	|| strcmp(argv[i], "-a")==0)
				mode=1;

			if (strcmp(argv[i], "--sine")==0	|| strcmp(argv[i], "-s")==0)
				mode=2;

			if (strcmp(argv[i], "--noise")==0	|| strcmp(argv[i], "-n")==0)
				mode=3;

			if (strcmp(argv[i], "--quiet")==0	|| strcmp(argv[i], "-q")==0)
				quiet=1;

			if (strcmp(argv[i], "--linear")==0	|| strcmp(argv[i], "-l")==0)
				LOGBASE=1.0;

			if (strcmp(argv[i], "--sample-rate")==0	|| strcmp(argv[i], "-r")==0)
				if (str_isnumber(argv[++i]))
						samplerate = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--min-freq")==0	|| strcmp(argv[i], "-min")==0)
				if (str_isnumber(argv[++i]))
				{
					basefreq = atof(argv[i]);
					if (basefreq==0)
							basefreq = DBL_MIN;	// Set it to this extremely close-to-zero number so that it's considered set
				}
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--max-freq")==0	|| strcmp(argv[i], "-max")==0)
				if (str_isnumber(argv[++i]))
						maxfreq = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--bpo")==0		|| strcmp(argv[i], "-b")==0)
				if (str_isnumber(argv[++i]))
						bpo = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--pps")==0		|| strcmp(argv[i], "-p")==0)
				if (str_isnumber(argv[++i]))
						pixpersec = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--height")==0	|| strcmp(argv[i], "-y")==0)
				if (str_isnumber(argv[++i]))
						Ysize = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--width")==0	|| strcmp(argv[i], "-x")==0)
				if (str_isnumber(argv[++i]))
						Xsize = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--loop-size")==0)
				if (str_isnumber(argv[++i]))
						LOOP_SIZE_SEC = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--log-base")==0)
				if (str_isnumber(argv[++i]))
						LOGBASE = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--bmsq-lut-size")==0)
				if (str_isnumber(argv[++i]))
						BMSQ_LUT_SIZE = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--pi")==0)					// lol
				if (str_isnumber(argv[++i]))
						pi = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--format-param")==0	|| strcmp(argv[i], "-f")==0)
				if (str_isnumber(argv[++i]))
						format_param = atoi(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			if (strcmp(argv[i], "--brightness")==0	|| strcmp(argv[i], "-g")==0)
				if (str_isnumber(argv[++i]))
						brightness = atof(argv[i]);
				else
				{
					fprintf(stderr, MSG_NUMBER_EXPECTED, argv[i-1]);
					exit(EXIT_FAILURE);
				}

			// TODO implement --duration, -d

			if (strcmp(argv[i], "--version")==0	|| strcmp(argv[i], "-v")==0)
			{
				printf("Copyright (C) 2005-2008 Michel Rouzic\nProgram last modified by its author on %s\n", date);
				exit(EXIT_SUCCESS);
			}

			if (strcmp(argv[i], "--help")==0	|| strcmp(argv[i], "-h")==0)
			{
				print_help();
				exit(EXIT_SUCCESS);
			}

			if (strcmp(argv[i], "--adv-help")==0)
			{
				print_adv_help();
				exit(EXIT_SUCCESS);
			}



		}
	}

	if (in_name!=NULL)			// if in_name has already been filled in
	{
		fin=fopen(in_name, "rb");	// try to open it

		if (fin==NULL)
		{
			fprintf(stderr, "The input file %s could not be found\nExiting with error.\n", in_name);
			exit(EXIT_FAILURE);
		}
		printf("Input file : %s\n", in_name);
	}
	else
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please specify an input file.\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}

		printf("Type 'help' to read the manual page\n");

		do
		{
			printf("Input file : ");
			in_name=getstring();

			if (strcmp(in_name, "help")==0)		// if 'help' has been typed
			{
				fin=NULL;
				print_help();			// print the help
			}
			else
				fin=fopen(in_name, "rb");
		}
		while (fin==NULL);
	}

	if (out_name!=NULL)			// if out_name has already been filled in
	{
		fout=fopen(out_name, "wb");

		if (fout==NULL)
		{
			fprintf(stderr, "The output file %s could not be opened.\nPlease make sure it isn't opened by any other program and press Return.\nExiting with error.\n", out_name);
			exit(EXIT_FAILURE);
		}
		printf("Output file : %s\n", out_name);
	}
	else
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please specify an output file.\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}
		printf("Output file : ");
		out_name=getstring();
		fout=fopen(out_name, "wb");

		while (fout==NULL)
		{
			fprintf(stderr, "The output file %s could not be opened.\nPlease make sure it isn't opened by any other program and press Return.\n", out_name);
			getchar();
			fout=fopen(out_name, "wb");
		}
	}

	for (i=0; i<strlen(in_name); i++) if (in_name[i]>='A' && in_name[i]<='Z') in_name[i] -= 'A' - 'a';	// make the string lowercase
	if (mode==0 && strstr(in_name, ".wav")!=NULL && strstr(in_name, ".wav")[4]==0)
		mode=1;	// Automatic switch to the Analysis mode

	if (mode==0)
		do
		{
			if (quiet==1)
			{
				fprintf(stderr, "Please specify an operation mode.\nUse either --analysis (-a), --sine (-s) or --noise (-n).\nExiting with error.\n");
				exit(EXIT_FAILURE);
			}
			printf("Choose the mode (Press 1, 2 or 3) :\n\t1. Analysis\n\t2. Sine synthesis\n\t3. Noise synthesis\n> ");
			mode=getfloat();
		}
		while (mode!=1 && mode!=2 && mode!=3);

		
	if (mode==1)
	{
		sound=wav_in(fin, &channels, &samplecount, &samplerate);					// Sound input
		log_file("Read sound...");
		
		#ifdef DEBUG
		printf("samplecount : %i\nchannels : %i\n", samplecount, channels);
		#endif

		settingsinput(&Ysize, samplecount, &samplerate, &basefreq, maxfreq, &pixpersec, &bpo, Xsize, 0);	// User settings input
		image = anal(sound[0], samplecount, samplerate, &Xsize, Ysize, bpo, pixpersec, basefreq);	// Analysis
		if (brightness!=1.0)
			brightness_control(image, Ysize, Xsize, 1.0/brightness);
		bmp_out(fout, image, Ysize, Xsize);								// Image output
	}
	if (mode==2 || mode==3)
	{
		sound = calloc (1, sizeof(double *));
		image = bmp_in(fin, &Ysize, &Xsize);							// Image input

		if (format_param==0)				// if the output format parameter is undefined
			if (quiet==0)				// if prompting is allowed
				format_param = wav_out_param();
			else
				format_param = 32;		// default is 32

		settingsinput(&Ysize, samplecount, &samplerate, &basefreq, maxfreq, &pixpersec, &bpo, Xsize, 1);	// User settings input

		if (brightness!=1.0) brightness_control(image, Ysize, Xsize, brightness);

		if (mode==2)
			sound[0] = synt_sine(image, Xsize, Ysize, &samplecount, samplerate, basefreq, pixpersec, bpo);	// Sine synthesis
		else
			sound[0] = synt_noise(image, Xsize, Ysize, &samplecount, samplerate, basefreq, pixpersec, bpo);	// Noise synthesis

		wav_out(fout, sound, 1, samplecount, samplerate, format_param);
	}

	clockb=gettime();
	printf("Processing time : %.3f s\n", (double) (clockb-clocka)/1000.0); 

	win_return();

	return 0;
}