Exemplo n.º 1
0
/*
 * On an alsa capable system, plays an audio file starting 10 seconds in.
 * Copes with sample-rate and channel change if necessary since its
 * common for audio drivers to support a subset of rates and channel
 * counts.
 * E.g. example3 song2.ogg
 *
 * Can easily be changed to work with other audio device drivers supported
 * by libSoX; e.g. "oss", "ao", "coreaudio", etc.
 * See the soxformat(7) manual page.
 */
int main(int argc, char * argv[])
{
  static sox_format_t * in, * out; /* input and output files */
  sox_effects_chain_t * chain;
  sox_effect_t * e;
  sox_signalinfo_t interm_signal;
  char * args[10];

  assert(argc == 2);
  sox_globals.output_message_handler = output_message;
  sox_globals.verbosity = 1;

  assert(sox_init() == SOX_SUCCESS);
  assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
  /* Change "alsa" in this line to use an alternative audio device driver: */
  assert(out= sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL));

  chain = sox_create_effects_chain(&in->encoding, &out->encoding);

  interm_signal = in->signal; /* NB: deep copy */

  e = sox_create_effect(sox_find_effect("input"));
  args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
  free(e);

  e = sox_create_effect(sox_find_effect("trim"));
  args[0] = "10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
  free(e);

  if (in->signal.rate != out->signal.rate) {
    e = sox_create_effect(sox_find_effect("rate"));
    assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
    assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
    free(e);
  }

  if (in->signal.channels != out->signal.channels) {
    e = sox_create_effect(sox_find_effect("channels"));
    assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
    assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
    free(e);
  }

  e = sox_create_effect(sox_find_effect("output"));
  args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
  free(e);

  sox_flow_effects(chain, NULL, NULL);

  sox_delete_effects_chain(chain);
  sox_close(out);
  sox_close(in);
  sox_quit();

  return 0;
}
Exemplo n.º 2
0
static THTensor * libsox_(read_audio_file)(const char *file_name)
{
  // Create sox objects and read into int32_t buffer
  sox_format_t *fd;
  fd = sox_open_read(file_name, NULL, NULL, NULL);
  if (fd == NULL)
    abort_("[read_audio_file] Failure to read file");
  
  int nchannels = fd->signal.channels;
  long buffer_size = fd->signal.length;
  int32_t *buffer = (int32_t *)malloc(sizeof(int32_t) * buffer_size);
  size_t samples_read = sox_read(fd, buffer, buffer_size);
  if (samples_read == 0)
    abort_("[read_audio_file] Empty file or read failed in sox_read");
  // alloc tensor 
  THTensor *tensor = THTensor_(newWithSize2d)(nchannels, samples_read / nchannels );
  tensor = THTensor_(newContiguous)(tensor);
  real *tensor_data = THTensor_(data)(tensor);
  // convert audio to dest tensor 
  int x,k;
  for (k=0; k<nchannels; k++) {
    for (x=0; x<samples_read/nchannels; x++) {
      *tensor_data++ = (real)buffer[x*nchannels+k];
    }
  }
  // free buffer and sox structures
  sox_close(fd);
  free(buffer);
  THTensor_(free)(tensor);

  // return tensor 
  return tensor;
}
Exemplo n.º 3
0
int main(int argc, char** argv) {
    if (argc != 1) {
        fprintf(stderr, "usage: %s < input_file\n", argv[0]);
        exit(1);
    }

    const int in_channels = 2, in_samples = 512, sample_rate = 44100;

    if (sox_init() != SOX_SUCCESS) {
        oops("sox_init()");
    }

    sox_signalinfo_t out_si = {};
    out_si.rate = sample_rate;
    out_si.channels = in_channels;
    out_si.precision = SOX_SAMPLE_PRECISION;

    sox_format_t* output
        = sox_open_write("default", &out_si, NULL, "alsa", NULL, NULL);
    if (!output) {
        oops("sox_open_read()");
    }

    sox_sample_t samples[in_samples * in_channels];

    float input[in_samples * in_channels];

    size_t clips = 0; SOX_SAMPLE_LOCALS;

    for (;;) {
        ssize_t sz = read(STDIN_FILENO, input, sizeof(input));
        if (sz < 0) {
            oops("read(stdin)");
        }
        if (sz == 0) {
            break;
        }

        const size_t n_samples = sz / sizeof(float);

        for (size_t n = 0; n < n_samples; n++) {
            samples[n] = SOX_FLOAT_32BIT_TO_SAMPLE(input[n], clips);
        }

        if (sox_write(output, samples, n_samples) != n_samples) {
            oops("sox_write()");
        }
    }

    if (sox_close(output) != SOX_SUCCESS) {
        oops("sox_close()");
    }

    if (sox_quit() != SOX_SUCCESS) {
        oops("sox_quit()");
    }

    return 0;
}
Exemplo n.º 4
0
int rfc1035_open_tcp(struct rfc1035_res *res, const RFC1035_ADDR *addr)
{
RFC1035_NETADDR addrbuf;
int	af;
const struct sockaddr *addrptr;
int	addrptrlen;

int	fd=rfc1035_mksocket(SOCK_STREAM, 0, &af);

	if (fd < 0)	return (-1);

	if (rfc1035_mkaddress(af, &addrbuf,
		addr, htons(53),
		&addrptr, &addrptrlen))
	{
		close(fd);
		return (-1);
	}

	if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NDELAY) < 0)
	{
		sox_close(fd);
		return (-1);
	}

	if (sox_connect(fd, addrptr, addrptrlen) == 0)
		return (fd);

	if (errno == EINPROGRESS || errno == EAGAIN)
	{
	unsigned	w=res->rfc1035_timeout_initial;

		if (!w)	w=DEFAULT_INITIAL_TIMEOUT;
		if (rfc1035_wait_query(fd, w) == 0 &&
			(sox_connect(fd, addrptr, addrptrlen) == 0
				|| errno == EISCONN))
		{
			return (fd);
		}
	}
	sox_close(fd);
	return (-1);
}
Exemplo n.º 5
0
struct rfc1035_reply *rfc1035_resolve_multiple(
			struct rfc1035_res *res,
			int opcode, int options,
			const struct rfc1035_query *queries,
			unsigned nqueries)
{
struct	querybuf qbuf;
int	udpfd;
int	attempt;
const RFC1035_ADDR *ns;
unsigned nscount;
unsigned current_timeout, timeout_backoff;
unsigned nbackoff, backoff_num;
int	af;
static const char fakereply[]={0, 0, 0, RFC1035_RCODE_SERVFAIL,
		0, 0,
		0, 0,
		0, 0,
		0, 0};

	nscount=rfc1035_init_nscount(res);
	ns=rfc1035_init_nsget(res);

	if (res->rfc1035_good_ns >= nscount)
		res->rfc1035_good_ns=0;

	qbuf.qbuflen=0;
	if ( rfc1035_mkquery(res,
		opcode, options, queries, nqueries, &putqbuf, &qbuf))
	{
		errno=EINVAL;
		return (0);
	}

	/* Prepare the UDP socket */

	if ((udpfd=rfc1035_open_udp(&af)) < 0)	return (0);

	/* Keep trying until we get an answer from a nameserver */

	current_timeout=res->rfc1035_timeout_initial;
	nbackoff=res->rfc1035_timeout_backoff;
	if (!current_timeout)	current_timeout=DEFAULT_INITIAL_TIMEOUT;
	if (!nbackoff)	nbackoff=DEFAULT_MAXIMUM_BACKOFF;

	timeout_backoff=current_timeout;
    for (backoff_num=0; backoff_num < nbackoff; backoff_num++,
					current_timeout *= timeout_backoff)


	for ( attempt=0; attempt < nscount; ++attempt)
	{
	int	nbytes;
	char	*reply;
	struct	rfc1035_reply *rfcreply=0;

	const RFC1035_ADDR *sin=&ns[(res->rfc1035_good_ns+attempt) % nscount];
	int	sin_len=sizeof(*sin);

	int	dotcp=0, isaxfr=0;
	unsigned i;

		for (i=0; i<nqueries; i++)
			if (queries[i].qtype == RFC1035_TYPE_AXFR)
			{
				dotcp=1;
				isaxfr=1;
				break;
			}

		if (isaxfr && nqueries > 1)
			return (rfc1035_replyparse(fakereply,
				sizeof(fakereply)));

		if (!dotcp)
		{
		/* Send the query via UDP */
		RFC1035_NETADDR	addrbuf;
		const struct sockaddr *addrptr;
		int	addrptrlen;

			if (rfc1035_mkaddress(af, &addrbuf,
				sin, htons(53),
				&addrptr, &addrptrlen))
				continue;

			if ((reply=rfc1035_query_udp(res, udpfd, addrptr,
				addrptrlen, qbuf.qbuf, qbuf.qbuflen, &nbytes,
					current_timeout)) == 0)
				continue;

			res->rfc1035_good_ns= (res->rfc1035_good_ns + attempt) %
					nscount;

		/* Parse the reply */

			rfcreply=rfc1035_replyparse(reply, nbytes);
			if (!rfcreply)
			{
				free(reply);
				if (errno == ENOMEM)	break;
				continue;
			/* Bad response from the server, try the next one. */
			}
			rfcreply->mallocedbuf=reply;
		/*
		** If the reply came back with the truncated bit set,
		** retry the query via TCP.
		*/

			if (rfcreply->tc)
			{
				dotcp=1;
				rfc1035_replyfree(rfcreply);
			}
		}

		if (dotcp)
		{
		int	tcpfd;
		struct	rfc1035_reply *firstreply=0, *lastreply=0;

			if ((tcpfd=rfc1035_open_tcp(res, sin)) < 0)
				continue;	/*
						** Can't connect via TCP,
						** try the next server.
						*/

			reply=rfc1035_query_tcp(res, tcpfd, qbuf.qbuf,
				qbuf.qbuflen, &nbytes, current_timeout);

			if (!reply)
			{
				sox_close(tcpfd);
				continue;
			}

			res->rfc1035_good_ns= (res->rfc1035_good_ns
					+ attempt) % nscount;

			rfcreply=rfc1035_replyparse(reply, nbytes);
			if (!rfcreply)
			{
				free(reply);
				sox_close(tcpfd);
				continue;
			}
			rfcreply->mallocedbuf=reply;
			firstreply=lastreply=rfcreply;
			while (isaxfr && rfcreply->rcode == 0)
			{
				if ((reply=rfc1035_recv_tcp(res,
					tcpfd, &nbytes, current_timeout))==0)
					break;
				
				rfcreply=rfc1035_replyparse(reply, nbytes);
				if (!rfcreply)
				{
					free(reply);
					rfc1035_replyfree(firstreply);
					firstreply=0;
					break;
				}
				rfcreply->mallocedbuf=reply;
				lastreply->next=rfcreply;
				lastreply=rfcreply;

				if ( rfcreply->ancount &&
					rfcreply->anptr[0].rrtype ==
						RFC1035_TYPE_SOA)
					break;
			}
			sox_close(tcpfd);
			if (!firstreply)
				return (0);
			rfcreply=firstreply;
		}
		memcpy(&rfcreply->server_addr, sin, sin_len);
		sox_close(udpfd);
		return (rfcreply);
	}

	/*
	** Return a fake server failure reply, when we couldn't contact
	** any name server.
	*/
	sox_close(udpfd);
	return (rfc1035_replyparse(fakereply, sizeof(fakereply)));
}
Exemplo n.º 6
0
const char *tcpremoteinfo(const RFC1035_ADDR *laddr, int lport,
	const RFC1035_ADDR *raddr, int rport, const char **ostype)
{
int	fd;
time_t	current_time, max_time;
fd_set	fds;
struct	timeval	tv;
static char buf[512];
char	*bufptr;
int	bufleft, n;
char	*p;
char	*q;
RFC1035_NETADDR	sin;
const struct sockaddr *addr;
int	addrlen;

	fd=rfc1035_mksocket(SOCK_STREAM, 0, &n);
	if (fd < 0)	return (0);

	if (rfc1035_mkaddress(n, &sin, laddr, 0, &addr, &addrlen) < 0)
	{
		close(fd);
		return (0);
	}

	if (sox_bind(fd, addr, addrlen) < 0)
	{
		sox_close(fd);
		return (0);
	}

	time (&current_time);
	max_time=current_time+30;

	if (rfc1035_mkaddress(n, &sin, raddr, htons(113), &addr, &addrlen) < 0)
	{
		sox_close(fd);
		return (0);
	}

	if (s_connect(fd, addr, addrlen, max_time - current_time) < 0)
	{
		sox_close(fd);
		return (0);
	}

	sprintf(buf, "%d,%d\r\n", ntohs(rport), ntohs(lport));
	bufptr=buf;
	bufleft=strlen(buf);
	while (bufleft)
	{
		time(&current_time);
		if (current_time >= max_time)
		{
			sox_close(fd);
			return (0);
		}

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		tv.tv_sec=max_time-current_time;
		tv.tv_usec=0;
		if (sox_select(fd+1, 0, &fds, 0, &tv) != 1 ||
			!FD_ISSET(fd, &fds))
		{
			sox_close(fd);
			return (0);
		}
		n=sox_write(fd, bufptr, bufleft);
		if (n <= 0)
		{
			sox_close(fd);
			return (0);
		}
		bufptr += n;
		bufleft -= n;
	}

	bufptr=buf;
	bufleft=sizeof(buf);
	do
	{
		if (bufleft == 0)
		{
			sox_close(fd);
			return (0);
		}

		time(&current_time);
		if (current_time >= max_time)
		{
			sox_close(fd);
			return (0);
		}

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		tv.tv_sec=max_time-current_time;
		tv.tv_usec=0;
		if (sox_select(fd+1, &fds, 0, 0, &tv) != 1 ||
			!FD_ISSET(fd, &fds))
		{
			sox_close(fd);
			return (0);
		}

		n=sox_read(fd, bufptr, bufleft);
		if (n <= 0)
		{
			sox_close(fd);
			return (0);
		}
		bufptr += n;
		bufleft -= n;
	} while (bufptr[-1] != '\n');
	sox_close(fd);
	bufptr[-1]=0;
	--bufptr;
	if (bufptr > buf && bufptr[-1] == '\r')
		bufptr[-1]=0;

	if ((p=strchr(buf, ':')) == 0)
		return (0);

	q=++p;
	if ((p=strchr(p, ':')) == 0)
		return (0);

	*p++=0;
	q=strtok(q, " \t");
	if (!q || strcmp(q, "USERID"))	return (0);
	if (ostype)	*ostype=p;
	if ((p=strchr(p, ':')) == 0)
		return (0);
	*p++=0;
	while (*p && (*p == ' ' || *p == '\t'))	p++;
	return (p);
}
Exemplo n.º 7
0
/*
 * Reads input file and displays a few seconds of wave-form, starting from
 * a given time through the audio.   E.g. example2 song2.au 30.75 1
 */
int main(int argc, char * argv[])
{
  sox_format_t * in;
  sox_sample_t * buf;
  size_t blocks, block_size;
  /* Period of audio over which we will measure its volume in order to
   * display the wave-form: */
  static const double block_period = 0.025; /* seconds */
  double start_secs = 0, period = 2;
  char dummy;
  uint64_t seek;

  /* All libSoX applications must start by initialising the SoX library */
  assert(sox_init() == SOX_SUCCESS);

  assert(argc > 1);
  ++argv, --argc; /* Move to 1st parameter */

  /* Open the input file (with default parameters) */
  assert(in = sox_open_read(*argv, NULL, NULL, NULL));
  ++argv, --argc; /* Move past this parameter */

  if (argc) { /* If given, read the start time: */
    assert(sscanf(*argv, "%lf%c", &start_secs, &dummy) == 1);
    ++argv, --argc; /* Move past this parameter */
  }

  if (argc) { /* If given, read the period of time to display: */
    assert(sscanf(*argv, "%lf%c", &period, &dummy) == 1);
    ++argv, --argc; /* Move past this parameter */
  }

  /* Calculate the start position in number of samples: */
  seek = start_secs * in->signal.rate * in->signal.channels + .5;
  /* Make sure that this is at a `wide sample' boundary: */
  seek -= seek % in->signal.channels;
  /* Move the file pointer to the desired starting position */
  assert(sox_seek(in, seek, SOX_SEEK_SET) == SOX_SUCCESS);

  /* Convert block size (in seconds) to a number of samples: */
  block_size = block_period * in->signal.rate * in->signal.channels + .5;
  /* Make sure that this is at a `wide sample' boundary: */
  block_size -= block_size % in->signal.channels;
  /* Allocate a block of memory to store the block of audio samples: */
  assert(buf = malloc(sizeof(sox_sample_t) * block_size));

  /* This example program requires that the audio has precisely 2 channels: */
  assert(in->signal.channels == 2);

  /* Read and process blocks of audio for the selected period or until EOF: */
  for (blocks = 0; sox_read(in, buf, block_size) == block_size && blocks * block_period < period; ++blocks) {
    double left = 0, right = 0;
    size_t i;
    static const char line[] = "===================================";
    int l, r;

    for (i = 0; i < block_size; ++i) {
      SOX_SAMPLE_LOCALS;
      /* convert the sample from SoX's internal format to a `double' for
       * processing in this application: */
      double sample = SOX_SAMPLE_TO_FLOAT_64BIT(buf[i],);

      /* The samples for each channel are interleaved; in this example
       * we allow only stereo audio, so the left channel audio can be found in
       * even-numbered samples, and the right channel audio in odd-numbered
       * samples: */
      if (i & 1)
        right = max(right, fabs(sample)); /* Find the peak volume in the block */
      else
        left = max(left, fabs(sample)); /* Find the peak volume in the block */
    }

    /* Build up the wave form by displaying the left & right channel
     * volume as a line length: */
    l = (1 - left) * 35 + .5;
    r = (1 - right) * 35 + .5;
    printf("%8.3f%36s|%s\n", start_secs + blocks * block_period, line + l, line + r);
  }

  /* All done; tidy up: */
  free(buf);
  sox_close(in);
  sox_quit();
  return 0;
}
Exemplo n.º 8
0
int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s input_file > output_file\n", argv[0]);
        exit(1);
    }

    const int out_channels = 2, sample_rate = 44100;

    if (sox_init() != SOX_SUCCESS) {
        oops("sox_init()");
    }

    sox_format_t* input = sox_open_read(argv[1], NULL, NULL, NULL);
    if (!input) {
        oops("sox_open_read()");
    }

    sox_signalinfo_t out_si = {};
    out_si.rate = sample_rate;
    out_si.channels = out_channels;
    out_si.precision = SOX_SAMPLE_PRECISION;

    sox_effect_handler_t out_handler = {
        "stdout", NULL, SOX_EFF_MCHAN, NULL, NULL, stdout_writer, NULL, NULL, NULL, 0
    };

    sox_effects_chain_t* chain
        = sox_create_effects_chain(&input->encoding, NULL);
    if (!chain) {
        oops("sox_create_effects_chain()");
    }

    {
        sox_effect_t* effect = sox_create_effect(sox_find_effect("input"));
        if (!effect) {
            oops("sox_create_effect(input)");
        }

        char* args[1] = { (char*)input };
        if (sox_effect_options(effect, 1, args) != SOX_SUCCESS) {
            oops("sox_effect_options(input)");
        }

        if (sox_add_effect(
                chain, effect, &input->signal, &out_si) != SOX_SUCCESS) {
            oops("sox_add_effect(input)");
        }

        free(effect);
    }

    if (input->signal.rate != out_si.rate) {
        {
            sox_effect_t* effect = sox_create_effect(sox_find_effect("gain"));
            if (!effect) {
                oops("sox_create_effect(gain)");
            }

            const char* args[] = { "-h" };

            if (sox_effect_options(effect, 1, (char**)args) != SOX_SUCCESS) {
                oops("sox_effect_options(gain)");
            }

            if (sox_add_effect(
                    chain, effect, &input->signal, &out_si) != SOX_SUCCESS) {
                oops("sox_add_effect(gain)");
            }

            free(effect);
        }

        {
            sox_effect_t* effect = sox_create_effect(sox_find_effect("rate"));
            if (!effect) {
                oops("sox_create_effect(rate)");
            }

            const char* args[] = { "-Q", "7", "-b", "99.7" };

            if (sox_effect_options(effect, 4, (char**)args) != SOX_SUCCESS) {
                oops("sox_effect_options(rate)");
            }

            if (sox_add_effect(
                    chain, effect, &input->signal, &out_si) != SOX_SUCCESS) {
                oops("sox_add_effect(rate)");
            }

            free(effect);
        }
    }

    if (input->signal.channels != out_si.channels) {
        sox_effect_t* effect = sox_create_effect(sox_find_effect("channels"));
        if (!effect) {
            oops("sox_create_effect(channels)");
        }

        if (sox_effect_options(effect, 0, NULL) != SOX_SUCCESS) {
            oops("sox_effect_options(channels)");
        }

        if (sox_add_effect(
                chain, effect, &input->signal, &out_si) != SOX_SUCCESS) {
            oops("sox_add_effect(channels)");
        }

        free(effect);
    }

    {
        sox_effect_t* effect = sox_create_effect(&out_handler);
        if (!effect) {
            oops("sox_create_effect(output)");
        }

        if (sox_add_effect(
                chain, effect, &input->signal, &out_si) != SOX_SUCCESS) {
            oops("sox_add_effect(output)");
        }

        free(effect);
    }

    sox_flow_effects(chain, NULL, NULL);

    sox_delete_effects_chain(chain);

    if (sox_close(input) != SOX_SUCCESS) {
        oops("sox_close()");
    }

    if (sox_quit() != SOX_SUCCESS) {
        oops("sox_quit()");
    }

    return 0;
}
Exemplo n.º 9
0
/*
 * Reads input file, applies vol & flanger effects, stores in output file.
 * E.g. example1 monkey.au monkey.aiff
 */
int main(int argc, char * argv[]){

	static sox_format_t * in, * out; /* input and output files */
	sox_effects_chain_t * chain;
  	sox_effect_t * e;
  	char * args[10];

  	assert(argc == 3);

  	/* All libSoX applications must start by initialising the SoX library */
  	assert(sox_init() == SOX_SUCCESS);

  	/* Open the input file (with default parameters) */
  	assert(in = sox_open_read(argv[1], NULL, NULL, NULL));

  	/* Open the output file; we must specify the output signal characteristics.
  	* Since we are using only simple effects, they are the same as the input
  	* file characteristics */
  	assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));

  	/* Create an effects chain; some effects need to know about the input
  	* or output file encoding so we provide that information here */
  	chain = sox_create_effects_chain(&in->encoding, &out->encoding);

  	/* The first effect in the effect chain must be something that can source
   	* samples; in this case, we use the built-in handler that inputs
   	* data from an audio file */
  	e = sox_create_effect(sox_find_effect("input"));
  	args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  	/* This becomes the first `effect' in the chain */
  	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

  	/* Create the `vol' effect, and initialise it with the desired parameters: */
  	e = sox_create_effect(sox_find_effect("vol"));
  	args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  	/* Add the effect to the end of the effects processing chain: */
	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

  	/* Create the `flanger' effect, and initialise it with default parameters: */
  	e = sox_create_effect(sox_find_effect("flanger"));
  	assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
  	/* Add the effect to the end of the effects processing chain: */
  	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

  	/* The last effect in the effect chain must be something that only consumes
  	* samples; in this case, we use the built-in handler that outputs
   	* data to an audio file */
  	e = sox_create_effect(sox_find_effect("output"));
  	args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

  	/* Flow samples through the effects processing chain until EOF is reached */
  	sox_flow_effects(chain, NULL, NULL);

  	/* All done; tidy up: */
  	sox_delete_effects_chain(chain);
  	sox_close(out);
  	sox_close(in);
  	sox_quit();
  	return 0;
}
Exemplo n.º 10
0
int main(int argc, char* argv[])
{
   static sox_format_t *in_file, *out_file;
   sox_sample_t *buffer;
   size_t read;
   size_t sample_count;
   unsigned int sample_order[SONG_LENGTH_S];

   if(argc != 3) {
      usage();
      exit(EXIT_FAILURE);
   }

   if (sox_init() != SOX_SUCCESS) {
      fprintf(stderr, "error: could not initialize Sox\n");
      exit(EXIT_FAILURE);
   }

   if((in_file = sox_open_read(argv[1], NULL, NULL, NULL)) == NULL) {
      fprintf(stderr, "error could not read input file\n");
      exit(EXIT_FAILURE);
   }

   if((out_file = sox_open_write(argv[2], &in_file->signal,
                     NULL, "wav", NULL, NULL)) == NULL) {
      fprintf(stderr, "error could not open output file\n");
      exit(EXIT_FAILURE);
   }


   sample_count = SONG_LENGTH_S * in_file->signal.rate * in_file->signal.channels;

   buffer = (sox_sample_t *) malloc(sizeof(sox_sample_t) * sample_count);

   randomize_byte_order(sample_order);

   if (sox_read(in_file, buffer, sample_count) != sample_count) {
      fprintf(stderr, "Incorrect number of samples read");
   }

   unsigned int i=0;

   for(i=0;i<SONG_LENGTH_S;i++) {
      sox_write(out_file, buffer + (sample_order[i] * (((unsigned int)(in_file->signal.rate)
                                                    * in_file->signal.channels))),
                                                    in_file->signal.rate * in_file->signal.channels);
   }

   free(buffer);

   sox_close(in_file);
   sox_close(out_file);


   sox_quit();



   return 0;

}