Search code examples
c++qtaudiofilterlibsndfile

Error : major format is 0 (libsndfile, Qt)


I keep getting error mentioned in topic (no additional messages) when I'm trying to open the file in Qt using libsndfile library in following code:

    SNDFILE * outfile;
    SF_INFO sfinfo;

    //preparing output file
    sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
    sfinfo.channels = 1;
    sfinfo.samplerate = 44100;

    memset( &sfinfo, 0, sizeof(SF_INFO));
    const char* path = "RainFilter.wav";

    outfile = sf_open(path, SFM_WRITE, &sfinfo);

    if(!(outfile))
    {
        std::cout << "Failed to create output file" << std::endl;
        sf_perror(outfile);
        return;
    }

This seems to be a problem with major part of the format property of sfinfo. I've tried most of the other formats, including numerical values such as:

sfinfo.format = 0x0B0000 | 0x0006;

What should I do? Is it the library-linking problem, environment problem or is the code incorrect? The path is irrelevant in this case, I've tried pointing to a different directory and the problem persists.


Solution

  • Move the call to

        memset(&sfinfo, 0, sizeof(sfinfo));
    
        //preparing output file
        sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
        sfinfo.channels = 1;
        sfinfo.samplerate = 44100;
    

    to before the assignment to the elements of sfinfo. Even better, remove the call to memset, and zero-initialize the structure properly with:

        SFINFO sfinfo = {0};
        sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
        sfinfo.channels = 1;
        sfinfo.samplerate = 44100;