Re: how to interpret tell_off, and the right way to use sox_seek

Jan Stary <[email protected]>
Newsgroups gmane.comp.audio.sox
Message-ID <[email protected]>
> First, it reads (which presumably moves the file pointer forwards),
> then it seeks back to where it was.
> I've verified in gdb that it actually does call sox_seek() (again and
> again and again, but not forever).  I have also verified in gdb that
> it is reading the same samples.

Ah, I see what your problem is now.

> So sox_seek() is definitely being called, and definitely working.
> But nevertheless, the reading done after the seeking eventually fails.

Here is my slight rewrite of your example:

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <sox.h>

int 
main(int argc, char **argv)
{
	sox_format_t *s;
	int32_t buf[1024];
	ssize_t r;
	int i;

	if (argc < 2)
		errx(1, "usage: ./soxseek input");
	if (sox_init() != SOX_SUCCESS)
		errx(1, "Cannot init libsox");
	if ((s = sox_open_read(*++argv, 0, 0, 0)) == NULL)
		errx(1, "Cannot open `%s'", *argv);
	for (i = 1; (r = sox_read(s, buf, 1024)) > 0; i++) {
		printf("[%04d] %zd samples, starting with %0x\n", i, r, *buf);
		if (sox_seek(s, 0, SOX_SEEK_SET) != SOX_SUCCESS)
			errx(1, "Cannot seek");
	}
	/* No way to test for sox_read() error */
	return 0;
}

(Note how I don't use sox_site_t for the sox_read() return value,
because it does not exist, eventhough that's what libsox(3) documents.)

$ sox -n /tmp/file.wav synth trim 0 $((1024 * 1000))s  
$ cc -o soxseek soxseek.c -lsox -I/usr/local/include/ -L/usr/local/lib
$ ./soxseek /tmp/file.wav 
[0000] 1024 samples, starting with 0
[0001] 1024 samples, starting with 0
[....]
[0999] 1024 samples, starting with 0
[1000] 1024 samples, starting with 0

So I think you are right. It does seek back to the begining of the sine wave
(thus reporting 0 as the first sample value in the buffer), but it gets
exhausted at EOF anyway. I suspect now it is a bug in sox_seek()
if we are calling it right.

> Certainly if you do analogous coding with calls to fread() and
> fseek(), it would not terminate.

Yes, the following will read the same file forever:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <err.h>

int 
main(int argc, char **argv)
{
	int fd;
	int32_t buf[1024];
	ssize_t r;
	int i;

	if (argc < 2)
		errx(1, "usage: ./seek input");
	if ((fd = open(*++argv, O_RDONLY)) == -1)
		err(1, NULL);
	for (i = 1; (r = read(fd, buf, 1024)) > 0; i++) {
		printf("[%04d] %zd samples, starting with %0x\n", i, r, *buf);
		if (lseek(fd, 0, SEEK_SET) == -1)
			err(1, NULL);
	}
	return (r != 0);
}


> What i'm trying to do now is to determine the correct way to use
> sox_seek() if i am using it incorrectly.  If i'm using it correctly,
> then i would like confirmation of my characterization (that it has
> some internal counter which is decremented until it hits zero).

I share your suspition now.

> (I did have a signal processing problem that i was considering
> earlier, but that's not relevant now because i avoided the sox_seek
> issue by rearranging the computation.  So i can do my dsp, but i do
> wonder about correct usage of sox_seek.)

Should we move this to sox-devel?

	Jan


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.