Skip to main content

Topic: ogg parameters problem libsndfile (Read 12378 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • hub2
  • [*]
ogg parameters problem libsndfile
Hi,
I made a function using libsndfile that should encode RAW PCM 16-bit file to OGG Vorbis file.Function that checks format says that something is wrong with parameters but when i tried to check what's wrong in this function manually i haven't found what's wrong.Could you tell me what's wrong here.
Here's my code.
Code: [Select]
static void encodeOgg (const char *infilename, const char *outfilename, int filetype)
{    static SAMPLE buffer [BUFFER_LEN];

    SNDFILE        *infile, *outfile;
    SF_INFO        sfinfo,sf_in;
    int            readcount;

    fflush (stdout);
    sf_in.samplerate=SAMPLE_RATE;
    sf_in.channels=NUM_CHANNELS;
    sf_in.format=SF_FORMAT_RAW | SF_FORMAT_PCM_16;
    if (! (infile = sf_open (infilename, SFM_READ, &sf_in))){
        error("Could not open input file");
        exit (1);
    }
    sfinfo.samplerate=SAMPLE_RATE;
    sfinfo.channels=NUM_CHANNELS;
    sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;

    if (! sf_format_check (&sfinfo)){    
        sf_close (infile);
        error("Invalid encoding\n");
        exit (1);
    }
    
    if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))){
        error("Error : could not open output file");
        exit (1);
    }

    while ((readcount = sf_read_short (infile, buffer, BUFFER_LEN)) > 0)
    {
        sf_write_short (outfile, buffer, readcount);
    }
    sf_close (infile);
    sf_close (outfile);
    return;
}

  • chi
  • [*]
ogg parameters problem libsndfile
Reply #1
Possible problem 1: Your sfinfo structure is not initialized. The fields that you don't set explicitly may contain anything at all. You should always call something like
Code: [Select]
memset(&sfinfo, 0, sizeof(sfinfo));
before using such a structure.

Possible problem 2: Vorbis support was introduced in libsndfile 1.0.18. Do you use at least this version?

Possible problem 3: Your code snippet doesn't show the actual values for the number of channels and for the sample rate. Perhaps they are invalid (e.g., the number of channels must not be 0).

Here is a minimal complete program:
Code: [Select]
#include <stdio.h>
#include <sndfile.h>

int main(void) {
    SF_INFO sfinfo;
    memset(&sfinfo, 0, sizeof(sfinfo));
    sfinfo.samplerate = 48000;
    sfinfo.channels = 3;
    sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
    if (sf_format_check(&sfinfo))
        printf("OK\n");
    return 0;
}

For me, it prints "OK". Try it on your system (first as-is, then with your values for sample rate and number of channels).

  • db1989
  • [*][*][*][*][*]
  • Global Moderator
ogg parameters problem libsndfile
Reply #2
Possible problem 1: Your sfinfo structure is not initialized. The fields that you don't set explicitly may contain anything at all. You should always call something like
Code: [Select]
memset(&sfinfo, 0, sizeof(sfinfo));
before using such a structure

I can’t determine which language this is, and please correct me if I’m wrong as I’m far from an expert, but as I understand it:
• In C, you can simply use struct MyStructType myStruct = {0};
• In C++, structures are initialised to zeroes or the equivalent empty data by default when using the default constructor.
Again, AFAIK.

  • chi
  • [*]
ogg parameters problem libsndfile
Reply #3
In C, you can simply use struct MyStructType myStruct = {0};


That’s right. I used the memset method here because it is the style preferred by libsndfile’s author, and used in the accompanying programs. (I am aware of the theoretical portability issues.)

However, the point here is that hub2’s code does not initialize the struct via either method. This is a bug. It may or may not cause his/her current problem.