Loading a specific flac file causes sf_readf_float to return 0 too early. #25

Closed
kmatheussen opened this Issue Nov 11, 2012 · 3 comments

Comments

Projects
None yet
2 participants

This soundfile:

http://dl.dropbox.com/u/4814054/Kick06.flac

This program:

include <sndfile.h>

include <stdio.h>

include <string.h>

int main(int argc, char **argv){
SF_INFO sf_info; memset(&sf_info,0,sizeof(sf_info));

SNDFILE *sndfile = sf_open(argv[1],SFM_READ,&sf_info);

int frames_read = 0;
while(frames_read < sf_info.frames){
float samples[1024*sf_info.channels];

int read_now = sf_readf_float(sndfile, samples, 1024);
printf("read_now: %d. frames_read: %d. num_frames: %d.\n",read_now,frames_read,(int)sf_info.frames);

frames_read += read_now;

}

sf_close(sndfile);

return 0;
}

Owner

erikd commented Nov 11, 2012

Sorry, this code:

while(frames_read < sf_info.frames){
    float samples[1024*sf_info.channels];

    int read_now = sf_readf_float(sndfile, samples, 1024);
    printf("read_now: %d. frames_read: %d. num_frames: %d.\n",read_now,
               frames_read,(int)sf_info.frames);

    frames_read += read_now;
}

should be considered broken.

The value in sf_info.frames can be incorrect (some validation and correction is done, but it is not foolproof). The correct usage is something like:

float samples[1024*sf_info.channels];
while((read_now = sf_readf_float(sndfile, samples, 1024)) > 0){

    printf("read_now: %d. frames_read: %d. num_frames: %d.\n",read_now,
               frames_read,(int)sf_info.frames);

    frames_read += read_now;
}

PS : The only way to ensure that sf_info.frames is correct is to read in the whole file and that would be slow for anything other than the shortest of files.

erikd closed this Nov 11, 2012

I thought so. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment