ogg parameters problem libsndfile |
![]() ![]() |
ogg parameters problem libsndfile |
Jan 15 2013, 19:34
Post
#1
|
|
|
Group: Members Posts: 3 Joined: 7-January 13 Member No.: 105674 |
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 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; } |
|
|
|
Jan 29 2013, 18:35
Post
#2
|
|
|
Group: Members Posts: 42 Joined: 27-November 11 Member No.: 95439 |
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 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 #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). |
|
|
|
Jan 29 2013, 19:01
Post
#3
|
|
|
Group: Super Moderator Posts: 4336 Joined: 23-June 06 Member No.: 32180 |
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 memset(&sfinfo, 0, sizeof(sfinfo)); before using such a structureI 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. |
|
|
|
Jan 30 2013, 15:07
Post
#4
|
|
|
Group: Members Posts: 42 Joined: 27-November 11 Member No.: 95439 |
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. |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 20th May 2013 - 19:22 |