0

我正在制作一个从麦克风录制的程序,然后使用 libsndfile 将其编码为 OGG 文件。

大约一个月前,我制作了这个程序的控制台版本,以确保录制和编码功能良好。现在,当我开始将这个程序作为一个窗口应用程序时,我发现唯一错误的是编码为 ogg 的函数。

这不是编译器或链接器错误,而是运行时错误。当我调用sf_format_check函数时,它返回 false,因此输出文件的参数有问题。所以我开始手动检查它是否sf_format_check正常工作并且一切都正确。但是当我编译旧的控制台版本时,它一切正常。

所以这是我的问题,这种行为的原因是什么?

这是我的功能。

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 ;
}
4

1 回答 1

0

将 sf_in 中的每个值分配给 sfinfo,而不是使用结构的直接分配 (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 ;
于 2013-01-15T22:11:52.517 回答