0

I'm making a program that records from microphone and then encodes it to OGG file using libsndfile.

About a month ago I made a console version of this program just to be sure that recording and encoding function is good. And now when I started doing this program to be a window application I found out that the only thing that is wrong is the function that encodes to ogg.

It's not a compiler or linker error but a runtime error. When I call sf_format_check function it returns false so it has a problem with parameters of the output file. So I started manually checking if it's in sf_format_check function and everything was correct. But when I compiled the old console version it all worked.

So here's my question What can be the reason of this behaviour?

Here's my function.

static void encodeOgg (const char *infilename, const char *outfilename, int filetype)
{   
  static short buffer [BUFFER_LEN] ;

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

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

  if (! sf_format_check (&sfinfo)){ //Here's the place where function exits
    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 ;
}
alpereira7
  • 1,522
  • 3
  • 17
  • 30
hub2
  • 11
  • 1
  • 4

1 Answers1

0

Assign each value from sf_in to sfinfo instead of using a direct assign of structures (sfinfo=sf_in;)

sfinfo.sf_count_t=sf_in.sf_count_t;
...
sfinfo.seekable=sf_in.seekable;


typedef struct
      {    sf_count_t  frames ;     /* Used to be called samples. */
           int         samplerate ;
           int         channels ;
           int         format ;
           int         sections ;
           int         seekable ;
       } SF_INFO ;