skware
Posts: 1
Joined: Sat Jul 24, 2021 6:52 pm

libsndfile can open file but cant read data

Sat Jul 24, 2021 10:34 pm

sndfile can open wav file and get the header information, but cant read any data. It also thinks there is only one frame despite there being 132301 items.
Excerpt from code:

Code: Select all

int main(int argc, char *argv[])
{
	printf("start\n");
	
	short *buf;
	int loop = INT_MAX;
		
	SNDFILE *sf;
	SF_INFO sf_info;
	int num_items;
	FILE *out;
	
	sf_info.format = 0;
	sf = sf_open(argv[1],SFM_READ,&sf_info);
	if (sf == NULL)
	{
		printf("Failed to open the file.\n");
		exit(-1);
	}
	//Print some of the sf_info, and figure out how much data to read.
	printf("frames=%d\n",sf_info.frames); 
	printf("samplerate=%d\n",sf_info.samplerate);
	printf("channels=%d\n",sf_info.channels);
	printf("format=%d\n",sf_info.format);
	printf("sections=%d\n",sf_info.sections);
	printf("seekable=%d\n",sf_info.seekable);
	
	num_items = sf_info.frames*sf_info.channels;
	printf("num_items=%d\n",num_items);
	//Allocate space for the data to be read, then read it.
	buf = (short *) malloc(num_items*sizeof(short));
	printf("Read %d items\n", sf_read_short(sf,buf,num_items));
	sf_close(sf);
	// code below only works for mono files, so abort here if not mono
	if (sf_info.channels > 1)
			pabort("Please use a mono wav file, not stereo.");
It prints out:

start
frames=1
samplerate=44100
channels=1
format=65538
sections=1
seekable=1
num_items=132301
Read 0 items

Return to “C/C++”