示例#1
0
int rfc1035_send_tcp(int fd, const char *query, unsigned query_len)
{
int	n=query_len+2;
char	*buf=malloc(n);
char	*p;
int	pl;

	if (!buf)	return (-1);

	buf[0]=query_len >> 8;
	buf[1]=query_len;

	memcpy(buf+2, query, query_len);

	p=buf;
	pl=n;
	while (pl)
	{
	int	i=sox_write(fd, p, pl);

		if (i < 0 && errno == EINTR)	continue;
		if (i <= 0)	break;
		p += i;
		pl -= i;
	}
	free(buf);

	if (pl)	return (-1);
	return (0);
}
示例#2
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;
}
示例#3
0
void bob::io::audio::Writer::append(const blitz::Array<double,2>& data) {

  if (!m_opened) {
    boost::format m("audio writer for file `%s' is closed and cannot be written to");
    m % m_filename;
    throw std::runtime_error(m.str());
  }

  if (!m_typeinfo.shape[0]) /* set for the first time */ {
    m_file->signal.channels = data.extent(0);
    m_typeinfo.shape[0] = data.extent(0);
    m_typeinfo.update_strides();
  }

  //checks data specifications
  if (m_typeinfo.shape[0] != (size_t)data.extent(0)) {
    boost::format m("input sample size for file `%s' should have %d rows");
    m % m_filename % m_typeinfo.shape[0];
    throw std::runtime_error(m.str());
  }

  size_t written = 0;
  for (int i=0; i<data.extent(1); i++) {
    for (int j=0; j<data.extent(0); ++j) {
      m_buffer[j] = (sox_sample_t)(data(j, i) * bob::io::audio::SOX_CONVERSION_COEF);
    }
    written += sox_write(m_file.get(), m_buffer.get(), m_typeinfo.shape[0]);
  }

  // updates internal counters
  m_file->signal.length += written * m_file->signal.channels;
  m_typeinfo.shape[1] += written;
  m_typeinfo.update_strides();

  if (written != (size_t)data.extent(1)) {
    boost::format m("I was asked to append %d samples to file `%s', but `sox_write()' managed to append %d samples only - this is not a definitive error, the stream is still sane");
    m % data.extent(1) % m_filename % written;
    throw std::runtime_error(m.str());
  }
}
示例#4
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);
}
示例#5
0
文件: example1.c 项目: vkeep/sox
     * 0 samples is returned does it indicate that end-of-file has been reached
     * or an error has occurred */
    if (!*osamp && in->sox_errno)
        fprintf(stderr, "%s: %s\n", in->filename, in->sox_errstr);
    return *osamp? SOX_SUCCESS : SOX_EOF;
}

/* The function that will be called to output samples from the effects chain.
 * In this example, we store the samples in a SoX-opened audio file.
 * In a different application, they might perhaps be analysed in some way,
 * or displayed as a wave-form */
static int output_flow(sox_effect_t *effp LSX_UNUSED, sox_sample_t const * ibuf,
                       sox_sample_t * obuf LSX_UNUSED, size_t * isamp, size_t * osamp)
{
    /* Write out *isamp samples */
    size_t len = sox_write(out, ibuf, *isamp);

    /* len is the number of samples that were actually written out; if this is
     * different to *isamp, then something has gone wrong--most often, it's
     * out of disc space */
    if (len != *isamp) {
        fprintf(stderr, "%s: %s\n", out->filename, out->sox_errstr);
        return SOX_EOF;
    }

    /* Outputting is the last `effect' in the effect chain so always passes
     * 0 samples on to the next effect (as there isn't one!) */
    *osamp = 0;

    (void)effp;   /* This parameter is not needed in this example */
示例#6
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;

}