Exemple #1
0
static int parse_options(int argc, char **argv)
{
	int c, i;

	while ((c = getopt(argc, argv, "D:r:f:p:b:s:t:m:vq")) >= 0) {
		switch (c) {
		case 'D':
			devname = optarg;
			break;
		case 'r':
			rate = atoi(optarg);
			break;
		case 'p':
			periodsize = atoi(optarg);
			break;
		case 'b':
			bufsize = atoi(optarg);
			break;
		case 'c':
			channels = atoi(optarg);
			break;
		case 'f':
			format = snd_pcm_format_value(optarg);
			break;
		case 's':
			if (*optarg == 'p' || *optarg == 'P')
				stream = SND_PCM_STREAM_PLAYBACK;
			else if (*optarg == 'c' || *optarg == 'C')
				stream = SND_PCM_STREAM_CAPTURE;
			else {
				fprintf(stderr, "invalid stream direction\n");
				return 1;
			}
			break;
		case 't':
			num_threads = atoi(optarg);
			if (num_threads < 1 || num_threads > MAX_THREADS) {
				fprintf(stderr, "invalid number of threads\n");
				return 1;
			}
			break;
		case 'm':
			for (i = 0; i <= MODE_RANDOM; i++)
				if (mode_suffix[i] == *optarg)
					break;
			if (i > MODE_RANDOM) {
				fprintf(stderr, "invalid mode type\n");
				return 1;
			}
			running_mode = i;
			break;
		case 'v':
			show_value = 1;
			break;
		case 'q':
			quiet = 1;
			break;
		default:
			usage();
			return 1;
		}
	}
	return 0;
}
Exemple #2
0
int main(int argc, char **argv)
{
    char *device = NULL;
    int stream = SND_PCM_STREAM_PLAYBACK;
    int format = SND_PCM_FORMAT_UNKNOWN;
    int channels = 0;
    int rate = 0;
    snd_pcm_t *pcm;
    int c;

    while ((c = getopt(argc, argv, "D:s:f:c:r:")) != -1) {
        switch (c) {
        case 'D':
            device = optarg;
            break;
        case 's':
            if (*optarg == 'c' || *optarg == 'C')
                stream = SND_PCM_STREAM_CAPTURE;
            else
                stream = SND_PCM_STREAM_PLAYBACK;
            break;
        case 'f':
            format = snd_pcm_format_value(optarg);
            break;
        case 'c':
            channels = atoi(optarg);
            break;
        case 'r':
            rate = atoi(optarg);
            break;
        default:
            usage();
            return 1;
        }
    }

    if (argc <= optind) {
        usage();
        return 1;
    }

    if (!device) {
        printf("No device is specified\n");
        return 1;
    }

    if (snd_pcm_open(&pcm, device, stream, SND_PCM_NONBLOCK) < 0) {
        printf("Cannot open PCM stream %s for %s\n", device,
               snd_pcm_stream_name(stream));
        return 1;
    }

    switch (*argv[optind]) {
    case 'q':
        return query_chmaps(pcm);
    case 'g':
        return get_chmap(pcm, format, channels, rate);
    case 's':
        return set_chmap(pcm, format, channels, rate,
                         argc - optind - 1, argv + optind + 1);
    }
    usage();
    return 1;
}
Exemple #3
0
int main(int argc, char** argv) {
  int showhelp = 0;
  unsigned int channels = 1;
  snd_pcm_format_t formatf = SND_PCM_FORMAT_UNKNOWN;
  snd_pcm_format_t formatt = SND_PCM_FORMAT_UNKNOWN;

  int opt;
  while ((opt = getopt(argc, argv, "hc:f:t:")) != -1) {
    switch (opt) {
      case 'h':
        showhelp = 1;
        break;
      case 'c':
        channels = atoi(optarg);
        break;
      case 'f':
        formatf = snd_pcm_format_value(optarg);
        break;
      case 't':
        formatt = snd_pcm_format_value(optarg);
        break;
      default: /* '?' */
        showhelp = 1;
        break;
    }
  }

  if (showhelp || argc - optind < 2) {
    fprintf(stderr, "Usage: %s [-c channels] [-f fromFormat] "
        "[-t toFormat] <inputFile> <outputFile>\n", argv[0]);
    exit(EXIT_SUCCESS);
  }

  if (formatf == SND_PCM_FORMAT_UNKNOWN || formatt == SND_PCM_FORMAT_UNKNOWN) {
    fprintf(stderr, "Unknown format\n");
    exit(EXIT_FAILURE);
  }

  FILE* input = fopen(argv[optind], "r");
  if (input == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind], strerror(errno));
    exit(EXIT_FAILURE);
  }

  FILE* output = fopen(argv[optind + 1], "w");
  if (output == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind + 1], strerror(errno));
    exit(EXIT_FAILURE);
  }

  unsigned long int count = 0;
  unsigned long int bsize = 1000;
  unsigned long int rc = bsize;
  while (rc == bsize) {
    double** data;

    rc = readFile(input, bsize, channels, formatf, &data);
    rc = writeFile(output, rc, channels, formatt, data);
    count += rc;

    freeData(data, channels);
  }
  fprintf(stderr, "%lu samples converted\n", count);

  fclose(input);
  fclose(output);

  exit(EXIT_SUCCESS);
}
Exemple #4
0
int main(int argc, char** argv) {
  int i, j;
  int showhelp = 0;

  unsigned int channels = 1;
  int window_size = 1024;
  int shift = 256;
  char* somfile = NULL;
  snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

  int opt;
  while ((opt = getopt(argc, argv, "hw:s:f:o:")) != -1) {
    switch (opt) {
      case 'h':
        showhelp = 1;
        break;
      case 'w':
        window_size = atoi(optarg);
        break;
      case 's':
        shift = atoi(optarg);
        break;
      case 'f':
        format = snd_pcm_format_value(optarg);
        break;
      case 'o':
        somfile = strdup(optarg);
        break;
      default: /* '?' */
        showhelp = 1;
        break;
    }
  }

  if (showhelp || argc - optind < 1) {
    fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
        "[-f format] [-o somfile] <inputFile>\n", argv[0]);
    exit(EXIT_SUCCESS);
  }

  if (format == SND_PCM_FORMAT_UNKNOWN) {
    fprintf(stderr, "Unknown format\n");
    exit(EXIT_FAILURE);
  }

  if (somfile == NULL) {
    somfile = strdup(DEFAULT_SOMFILE);
  }

  // Load SOM model
  fprintf(stderr, "Read SOM from %s\n", somfile);
  FILE* file = fopen(somfile, "r");
  if (file == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", somfile,
        strerror(errno));
    exit(EXIT_FAILURE);
  }
  SOM* net = som_load(file);
  fclose(file);

  fprintf(stderr, "SOM Loaded (size: %d * %d, dimension: %d)\n",
      net->rows, net->cols, net->dims);
  assert(window_size / 2 + 1 == net->dims);

  // Load Data
  fprintf(stderr, "Loading Audio File %s (%d channel(s), %s)\n",
      argv[optind], channels, snd_pcm_format_name(format));
  FILE* input = fopen(argv[optind], "r");
  if (input == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind],
        strerror(errno));
    exit(EXIT_FAILURE);
  }

  double** data;
  unsigned long int count = pcm_size(input, channels, format);
  count = read_file(input, count, channels, format, &data);
  fprintf(stderr, "%lu samples read\n", count);
  fclose(input);

  // Transform
  fprintf(stderr, "Calculating STFT (windows size %d, shift %d)\n",
      window_size, shift);
  int nos = number_of_spectrum(count, window_size, shift);
  TimeFreq* tf = alloc_tf(window_size, nos);

  stft(data[0], count, window_size, shift, tf);
  free_data(data, channels);

  fprintf(stderr, "%d STFT Calculated\n", nos);

  // Mapping
  double* mspec = malloc(net->dims * sizeof(double));
  for (i = 0; i < nos; i++) {
    Spectra s = get_spectra(tf, i);
    for (j = 0; j < net->dims; j++) {
      mspec[j] = get_magnitude(s, j);
    }
    double dis;
    int id = som_map_dis(net, mspec, &dis);
    printf("%d ", id);
  }
  printf("\n");

  fprintf(stderr, "Mapping finished\n");

  // Free memory
  free_tf(tf);
  free(mspec);
  som_free(net);

  exit(EXIT_SUCCESS);
}
Exemple #5
0
int main(int argc, char *argv[])
{
	struct option long_option[] =
	{
		{"help", 0, NULL, 'h'},
		{"pdevice", 1, NULL, 'P'},
		{"cdevice", 1, NULL, 'C'},
		{"min", 1, NULL, 'm'},
		{"max", 1, NULL, 'M'},
		{"frames", 1, NULL, 'F'},
		{"format", 1, NULL, 'f'},
		{"channels", 1, NULL, 'c'},
		{"rate", 1, NULL, 'r'},
		{"seconds", 1, NULL, 's'},
		{"block", 0, NULL, 'b'},
		{"time", 1, NULL, 't'},
		{"poll", 0, NULL, 'p'},
		{"effect", 0, NULL, 'e'},
		{NULL, 0, NULL, 0},
	};
	snd_pcm_t *phandle, *chandle;
	char *buffer;
	int err, latency, morehelp;
	int ok;
	snd_timestamp_t p_tstamp, c_tstamp;
	ssize_t r;
	size_t frames_in, frames_out, in_max;
	int effect = 0;
	morehelp = 0;
	while (1) {
		int c;
		if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:s:bt:pe", long_option, NULL)) < 0)
			break;
		switch (c) {
		case 'h':
			morehelp++;
			break;
		case 'P':
			pdevice = strdup(optarg);
			break;
		case 'C':
			cdevice = strdup(optarg);
			break;
		case 'm':
			err = atoi(optarg) / 2;
			latency_min = err >= 4 ? err : 4;
			if (latency_max < latency_min)
				latency_max = latency_min;
			break;
		case 'M':
			err = atoi(optarg) / 2;
			latency_max = latency_min > err ? latency_min : err;
			break;
		case 'f':
			format = snd_pcm_format_value(optarg);
			if (format == SND_PCM_FORMAT_UNKNOWN) {
				printf("Unknown format, setting to default S16_LE\n");
				format = SND_PCM_FORMAT_S16_LE;
			}
			break;
		case 'c':
			err = atoi(optarg);
			channels = err >= 1 && err < 1024 ? err : 1;
			break;
		case 'r':
			err = atoi(optarg);
			rate = err >= 4000 && err < 200000 ? err : 44100;
			break;
		case 's':
			err = atoi(optarg);
			loop_sec = err >= 1 && err <= 100000 ? err : 30;
			break;
		case 'b':
			block = 1;
			break;
		case 't':
			tick_time = atoi(optarg);
			tick_time = tick_time < 0 ? 0 : tick_time;
			break;
		case 'p':
			use_poll = 1;
			break;
		case 'e':
			effect = 1;
			break;
		}
	}

	if (morehelp) {
		help();
		return 0;
	}
	err = snd_output_stdio_attach(&output, stdout, 0);
	if (err < 0) {
		printf("Output failed: %s\n", snd_strerror(err));
		return 0;
	}

	loop_limit = loop_sec * rate;
	latency = latency_min - 4;
	buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);

	setscheduler();

	printf("Playback device is %s\n", pdevice);
	printf("Capture device is %s\n", cdevice);
	printf("Parameters are %iHz, %s, %i channels, %s mode\n", rate, snd_pcm_format_name(format), channels, block ? "blocking" : "non-blocking");
	printf("Wanted tick time: %ius, poll mode: %s\n", tick_time, use_poll ? "yes" : "no");
	printf("Loop limit is %li frames, minimum latency = %i, maximum latency = %i\n", loop_limit, latency_min * 2, latency_max * 2);

	if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
		printf("Playback open error: %s\n", snd_strerror(err));
		return 0;
	}
	if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
		printf("Record open error: %s\n", snd_strerror(err));
		return 0;
	}

	/* initialize the filter sweep variables */
	if (effect) {
		fs = (float) rate;
		BW = FILTER_BANDWIDTH;

		lfo = 0;
		dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs;

		x[0] = (float*) malloc(channels*sizeof(float));		
		x[1] = (float*) malloc(channels*sizeof(float));		
		x[2] = (float*) malloc(channels*sizeof(float));		
		y[0] = (float*) malloc(channels*sizeof(float));		
		y[1] = (float*) malloc(channels*sizeof(float));		
		y[2] = (float*) malloc(channels*sizeof(float));		
	}
			  
	while (1) {
		frames_in = frames_out = 0;
		if (setparams(phandle, chandle, &latency) < 0)
			break;
		showlatency(latency);
		if (tick_time_ok)
			printf("Using tick time %ius\n", tick_time_ok);
		if ((err = snd_pcm_link(chandle, phandle)) < 0) {
			printf("Streams link error: %s\n", snd_strerror(err));
			exit(0);
		}
		if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
			fprintf(stderr, "silence error\n");
			break;
		}
		if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
			fprintf(stderr, "write error\n");
			break;
		}
		if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
			fprintf(stderr, "write error\n");
			break;
		}

		if ((err = snd_pcm_start(chandle)) < 0) {
			printf("Go error: %s\n", snd_strerror(err));
			exit(0);
		}
		gettimestamp(phandle, &p_tstamp);
		gettimestamp(chandle, &c_tstamp);
#if 0
		printf("Playback:\n");
		showstat(phandle, frames_out);
		printf("Capture:\n");
		showstat(chandle, frames_in);
#endif

		ok = 1;
		in_max = 0;
		while (ok && frames_in < loop_limit) {
			if (use_poll) {
				/* use poll to wait for next event */
				snd_pcm_wait(chandle, 1000);
			}
			if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0)
				ok = 0;
			else {
				if (effect)
					applyeffect(buffer,r);
			 	if (writebuf(phandle, buffer, r, &frames_out) < 0)
					ok = 0;
			}
		}
		if (ok)
			printf("Success\n");
		else
			printf("Failure\n");
		printf("Playback:\n");
		showstat(phandle, frames_out);
		printf("Capture:\n");
		showstat(chandle, frames_in);
		showinmax(in_max);
		if (p_tstamp.tv_sec == p_tstamp.tv_sec &&
		    p_tstamp.tv_usec == c_tstamp.tv_usec)
			printf("Hardware sync\n");
		snd_pcm_drop(chandle);
		snd_pcm_nonblock(phandle, 0);
		snd_pcm_drain(phandle);
		snd_pcm_nonblock(phandle, !block ? 1 : 0);
		if (ok) {
#if 1
			printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
			       p_tstamp.tv_sec,
			       (int)p_tstamp.tv_usec,
			       c_tstamp.tv_sec,
			       (int)c_tstamp.tv_usec,
			       timediff(p_tstamp, c_tstamp));
#endif
			break;
		}
		snd_pcm_unlink(chandle);
		snd_pcm_hw_free(phandle);
		snd_pcm_hw_free(chandle);
	}
	snd_pcm_close(phandle);
	snd_pcm_close(chandle);
	return 0;
}
Exemple #6
0
int main(int argc, char** argv) {
  int showhelp = 0;

  unsigned int channels = 1;
  int window_size = 1024;
  int shift = 256;
  int rate = 8000;
  snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

  int opt;
  while ((opt = getopt(argc, argv, "hw:s:r:f:")) != -1) {
    switch (opt) {
      case 'h':
        showhelp = 1;
        break;
      case 'w':
        window_size = atoi(optarg);
        break;
      case 's':
        shift = atoi(optarg);
        break;
      case 'r':
        rate = atoi(optarg);
        break;
      case 'f':
        format = snd_pcm_format_value(optarg);
        break;
      default: /* '?' */
        showhelp = 1;
        break;
    }
  }

  if (showhelp || argc - optind < 1) {
    fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
        "[-r sampling rate] [-f format] <inputFile>\n", argv[0]);
    exit(EXIT_SUCCESS);
  }

  if (format == SND_PCM_FORMAT_UNKNOWN) {
    fprintf(stderr, "Unknown format\n");
    exit(EXIT_FAILURE);
  }
 
  FILE* input = fopen(argv[optind], "r");
  if (input == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind], strerror(errno));
    exit(EXIT_FAILURE);
  }

  // Load Audio File
  double** data;
  unsigned long int count = pcm_size(input, channels, format);
  count = read_file(input, count, channels, format, &data);
  fprintf(stderr, "%lu samples read\n", count);
  fclose(input);

  // Transform
  int nos = number_of_spectrum(count, window_size, shift);
  TimeFreq* tf = alloc_tf(window_size, nos);

  stft(data[0], count, window_size, shift, tf);

  printf("set style line 11 lc rgb '#808080' lt 1\n"
      "set border 3 front ls 11\n"
      "set tics nomirror out scale 0.75\n"
      "unset key\n"
      "unset colorbox\n"
      "set palette defined (0 '#000090', 1 '#000fff', 2 '#0090ff', 3 '#0fffee', 4 '#90ff70', 5 '#ffee00', 6 '#ff7000', 7 '#ee0000', 8 '#7f0000')\n"
      "set xlabel 'Time (s)'\n"
      "set ylabel 'Frequency (Hz)'\n");
  printf("set yrange [0:%g]\n", (double) rate / 2.0);
  printf("set xrange [0:%g]\n", (double) count / rate);
  printf("plot '-' matrix using (($2 + 0.5) * %g) : (($1 + 0.5) * %g) : (log($3))"
    " with image\n", (double) shift / rate, 
    (double) rate / window_size);
  int i, j;
  for (i = 0; i < nos; i++) {
    Spectra s = get_spectra(tf, i);
    for (j = 0; j < window_size / 2 + 1; j++) {
      printf("%g ", get_magnitude(s, j));
    }
    printf("\n");
  }
  printf("e\n");

  // Free memory
  free_data(data, channels);
  free_tf(tf);

  exit(EXIT_SUCCESS);
}
Exemple #7
0
static int parse_config(int argc, char *argv[], snd_output_t *output,
			int cmdline)
{
	struct option long_option[] =
	{
		{"help", 0, NULL, 'h'},
		{"config", 1, NULL, 'g'},
		{"daemonize", 0, NULL, 'd'},
		{"pdevice", 1, NULL, 'P'},
		{"cdevice", 1, NULL, 'C'},
		{"pctl", 1, NULL, 'X'},
		{"cctl", 1, NULL, 'Y'},
		{"latency", 1, NULL, 'l'},
		{"tlatency", 1, NULL, 't'},
		{"format", 1, NULL, 'f'},
		{"channels", 1, NULL, 'c'},
		{"rate", 1, NULL, 'r'},
		{"buffer", 1, NULL, 'B'},
		{"period", 1, NULL, 'E'},
		{"seconds", 1, NULL, 's'},
		{"nblock", 0, NULL, 'b'},
		{"effect", 0, NULL, 'e'},
		{"verbose", 0, NULL, 'v'},
		{"resample", 0, NULL, 'n'},
		{"samplerate", 1, NULL, 'A'},
		{"sync", 1, NULL, 'S'},
		{"slave", 1, NULL, 'a'},
		{"thread", 1, NULL, 'T'},
		{"mixer", 1, NULL, 'm'},
		{"ossmixer", 1, NULL, 'O'},
		{"workaround", 1, NULL, 'w'},
		{"xrun", 0, NULL, 'U'},
		{NULL, 0, NULL, 0},
	};
	int err, morehelp;
	char *arg_config = NULL;
	char *arg_pdevice = NULL;
	char *arg_cdevice = NULL;
	char *arg_pctl = NULL;
	char *arg_cctl = NULL;
	unsigned int arg_latency_req = 0;
	unsigned int arg_latency_reqtime = 10000;
	snd_pcm_format_t arg_format = SND_PCM_FORMAT_S16_LE;
	unsigned int arg_channels = 2;
	unsigned int arg_rate = 48000;
	snd_pcm_uframes_t arg_buffer_size = 0;
	snd_pcm_uframes_t arg_period_size = 0;
	unsigned long arg_loop_time = ~0UL;
	int arg_nblock = 0;
	int arg_effect = 0;
	int arg_resample = 0;
	int arg_samplerate = SRC_SINC_FASTEST + 1;
	int arg_sync = SYNC_TYPE_AUTO;
	int arg_slave = SLAVE_TYPE_AUTO;
	int arg_thread = 0;
	struct loopback *loop = NULL;
	char *arg_mixers[MAX_MIXERS];
	int arg_mixers_count = 0;
	char *arg_ossmixers[MAX_MIXERS];
	int arg_ossmixers_count = 0;
	int arg_xrun = arg_default_xrun;
	int arg_wake = arg_default_wake;

	morehelp = 0;
	while (1) {
		int c;
		if ((c = getopt_long(argc, argv,
				"hdg:P:C:X:Y:l:t:F:f:c:r:s:benvA:S:a:m:T:O:w:UW:",
				long_option, NULL)) < 0)
			break;
		switch (c) {
		case 'h':
			morehelp++;
			break;
		case 'g':
			arg_config = strdup(optarg);
			break;
		case 'd':
			daemonize = 1;
			use_syslog = 1;
			openlog("alsaloop", LOG_NDELAY|LOG_PID, LOG_DAEMON);
			break;
		case 'P':
			arg_pdevice = strdup(optarg);
			break;
		case 'C':
			arg_cdevice = strdup(optarg);
			break;
		case 'X':
			arg_pctl = strdup(optarg);
			break;
		case 'Y':
			arg_cctl = strdup(optarg);
			break;
		case 'l':
			err = atoi(optarg);
			arg_latency_req = err >= 4 ? err : 4;
			break;
		case 't':
			err = atoi(optarg);
			arg_latency_reqtime = err >= 500 ? err : 500;
			break;
		case 'f':
			arg_format = snd_pcm_format_value(optarg);
			if (arg_format == SND_PCM_FORMAT_UNKNOWN) {
				logit(LOG_WARNING, "Unknown format, setting to default S16_LE\n");
				arg_format = SND_PCM_FORMAT_S16_LE;
			}
			break;
		case 'c':
			err = atoi(optarg);
			arg_channels = err >= 1 && err < 1024 ? err : 1;
			break;
		case 'r':
			err = atoi(optarg);
			arg_rate = err >= 4000 && err < 200000 ? err : 44100;
			break;
		case 'B':
			err = atoi(optarg);
			arg_buffer_size = err >= 32 && err < 200000 ? err : 0;
			break;
		case 'E':
			err = atoi(optarg);
			arg_period_size = err >= 32 && err < 200000 ? err : 0;
			break;
		case 's':
			err = atoi(optarg);
			arg_loop_time = err >= 1 && err <= 100000 ? err : 30;
			break;
		case 'b':
			arg_nblock = 1;
			break;
		case 'e':
			arg_effect = 1;
			break;
		case 'n':
			arg_resample = 1;
			break;
		case 'A':
			if (strcasecmp(optarg, "sincbest") == 0)
				arg_samplerate = SRC_SINC_BEST_QUALITY;
			else if (strcasecmp(optarg, "sincmedium") == 0)
				arg_samplerate = SRC_SINC_MEDIUM_QUALITY;
			else if (strcasecmp(optarg, "sincfastest") == 0)
				arg_samplerate = SRC_SINC_FASTEST;
			else if (strcasecmp(optarg, "zerohold") == 0)
				arg_samplerate = SRC_ZERO_ORDER_HOLD;
			else if (strcasecmp(optarg, "linear") == 0)
				arg_samplerate = SRC_LINEAR;
			else
				arg_samplerate = atoi(optarg);
			if (arg_samplerate < 0 || arg_samplerate > SRC_LINEAR)
				arg_sync = SRC_SINC_FASTEST;
			arg_samplerate += 1;
			break;
		case 'S':
			if (strcasecmp(optarg, "samplerate") == 0)
				arg_sync = SYNC_TYPE_SAMPLERATE;
			else if (optarg[0] == 'n')
				arg_sync = SYNC_TYPE_NONE;
			else if (optarg[0] == 's')
				arg_sync = SYNC_TYPE_SIMPLE;
			else if (optarg[0] == 'c')
				arg_sync = SYNC_TYPE_CAPTRATESHIFT;
			else if (optarg[0] == 'p')
				arg_sync = SYNC_TYPE_PLAYRATESHIFT;
			else if (optarg[0] == 'r')
				arg_sync = SYNC_TYPE_SAMPLERATE;
			else
				arg_sync = atoi(optarg);
			if (arg_sync < 0 || arg_sync > SYNC_TYPE_LAST)
				arg_sync = SYNC_TYPE_AUTO;
			break;
		case 'a':
			if (optarg[0] == 'a')
				arg_slave = SLAVE_TYPE_AUTO;
			else if (strcasecmp(optarg, "on") == 0)
				arg_slave = SLAVE_TYPE_ON;
			else if (strcasecmp(optarg, "off") == 0)
				arg_slave = SLAVE_TYPE_OFF;
			else
				arg_slave = atoi(optarg);
			if (arg_slave < 0 || arg_slave > SLAVE_TYPE_LAST)
				arg_slave = SLAVE_TYPE_AUTO;
			break;
		case 'T':
			arg_thread = atoi(optarg);
			if (arg_thread < 0)
				arg_thread = 10000000 + loopbacks_count;
			break;
		case 'm':
			if (arg_mixers_count >= MAX_MIXERS) {
				logit(LOG_CRIT, "Maximum redirected mixer controls reached (max %i)\n", (int)MAX_MIXERS);
				return EXIT_FAILURE;
			}
			arg_mixers[arg_mixers_count++] = optarg;
			break;
		case 'O':
			if (arg_ossmixers_count >= MAX_MIXERS) {
				logit(LOG_CRIT, "Maximum redirected mixer controls reached (max %i)\n", (int)MAX_MIXERS);
				return EXIT_FAILURE;
			}
			arg_ossmixers[arg_ossmixers_count++] = optarg;
			break;
		case 'v':
			verbose++;
			break;
		case 'w':
			if (strcasecmp(optarg, "serialopen") == 0)
				workarounds |= WORKAROUND_SERIALOPEN;
			break;
		case 'U':
			arg_xrun = 1;
			if (cmdline)
				arg_default_xrun = 1;
			break;
		case 'W':
			arg_wake = atoi(optarg);
			if (cmdline)
				arg_default_wake = arg_wake;
			break;
		}
	}

	if (morehelp) {
		help();
		return EXIT_SUCCESS;
	}
	if (arg_config == NULL) {
		struct loopback_handle *play;
		struct loopback_handle *capt;
		err = create_loopback_handle(&play, arg_pdevice, arg_pctl, "playback");
		if (err < 0) {
			logit(LOG_CRIT, "Unable to create playback handle.\n");
			return EXIT_FAILURE;
		}
		err = create_loopback_handle(&capt, arg_cdevice, arg_cctl, "capture");
		if (err < 0) {
			logit(LOG_CRIT, "Unable to create capture handle.\n");
			return EXIT_FAILURE;
		}
		err = create_loopback(&loop, play, capt, output);
		if (err < 0) {
			logit(LOG_CRIT, "Unable to create loopback handle.\n");
			return EXIT_FAILURE;
		}
		play->format = capt->format = arg_format;
		play->rate = play->rate_req = capt->rate = capt->rate_req = arg_rate;
		play->channels = capt->channels = arg_channels;
		play->buffer_size_req = capt->buffer_size_req = arg_buffer_size;
		play->period_size_req = capt->period_size_req = arg_period_size;
		play->resample = capt->resample = arg_resample;
		play->nblock = capt->nblock = arg_nblock ? 1 : 0;
		loop->latency_req = arg_latency_req;
		loop->latency_reqtime = arg_latency_reqtime;
		loop->sync = arg_sync;
		loop->slave = arg_slave;
		loop->thread = arg_thread;
		loop->xrun = arg_xrun;
		loop->wake = arg_wake;
		err = add_mixers(loop, arg_mixers, arg_mixers_count);
		if (err < 0) {
			logit(LOG_CRIT, "Unable to add mixer controls.\n");
			return EXIT_FAILURE;
		}
		err = add_oss_mixers(loop, arg_ossmixers, arg_ossmixers_count);
		if (err < 0) {
			logit(LOG_CRIT, "Unable to add ossmixer controls.\n");
			return EXIT_FAILURE;
		}
#ifdef USE_SAMPLERATE
		loop->src_enable = arg_samplerate > 0;
		if (loop->src_enable)
			loop->src_converter_type = arg_samplerate - 1;
#else
		if (arg_samplerate > 0) {
			logit(LOG_CRIT, "No libsamplerate support.\n");
			return EXIT_FAILURE;
		}
#endif
		set_loop_time(loop, arg_loop_time);
		add_loop(loop);
		return 0;
	}

	return parse_config_file(arg_config, output);
}
Exemple #8
-1
int main(int argc, char** argv) {
    int showhelp = 0;

    unsigned int channels = 1;
    int window_size = 1024;
    int shift = 256;
    snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

    int opt;
    while ((opt = getopt(argc, argv, "hw:s:f:")) != -1) {
        switch (opt) {
        case 'h':
            showhelp = 1;
            break;
        case 'w':
            window_size = atoi(optarg);
            break;
        case 's':
            shift = atoi(optarg);
            break;
        case 'f':
            format = snd_pcm_format_value(optarg);
            break;
        default: /* '?' */
            showhelp = 1;
            break;
        }
    }

    if (showhelp || argc - optind < 2) {
        fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
                "[-f format] <inputFile> <outputFile>\n" , argv[0]);
        exit(EXIT_SUCCESS);
    }

    if (format == SND_PCM_FORMAT_UNKNOWN) {
        fprintf(stderr, "Unknown format\n");
        exit(EXIT_FAILURE);
    }

    // Load Audio File
    FILE* input = fopen(argv[optind], "r");
    if (input == NULL) {
        fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind],
                strerror(errno));
        exit(EXIT_FAILURE);
    }

    double** data;
    unsigned long int count = pcm_size(input, channels, format);
    count = read_file(input, count, channels, format, &data);
    fprintf(stderr, "%lu samples read\n", count);
    fclose(input);

    // Transform
    int nos = number_of_spectrum(count, window_size, shift);
    TimeFreq* tf = alloc_tf(window_size, nos);
    stft(data[0], count, window_size, shift, tf);
    free(data[0]);

    // Spectral subtraction
    TimeFreq* ntf = alloc_tf(window_size, nos);
    do_filter(zerophase, NULL, tf, ntf);

    // Inverse transform
    count = istft_size(tf);
    data[0] = malloc(count * sizeof(double));
    istft(ntf, data[0]);

    FILE* output = fopen(argv[optind + 1], "w");
    if (output == NULL) {
        fprintf(stderr, "Cannot Open File %s : %s\n",
                argv[optind + 1], strerror(errno));
        exit(EXIT_FAILURE);
    }

    count = write_file(output, count, channels, format, data);
    fclose(output);

    free_data(data, channels);
    free_tf(tf);
    free_tf(ntf);

    exit(EXIT_SUCCESS);
}