【问题标题】:WAV writing function can't sync after writing to stereo写入立体声后 WAV 写入功能无法同步
【发布时间】:2020-05-24 23:16:45
【问题描述】:

我正在尝试使用以下函数从交错输入缓冲区创建 2 声道立体声 .wav 文件。该函数在稍微修改以写入 1 通道单声道文件时工作正常,但在其当前状态下,它返回异常抛出错误:

void audiowrite(double* inputBuffer, int bufferLength, int fs, std::string outputFileLocation){
    SF_INFO info;
    info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
    info.channels = 2;
    info.samplerate = fs;

    SNDFILE* sndFile = sf_open(outputFileLocation.c_str(), SFM_WRITE, &info);

    sf_writef_double(sndFile, inputBuffer, bufferLength);

    sf_write_sync(sndFile); //Exception thrown - access violation reading location
    sf_close(sndFile);
}

我几乎可以肯定问题出在此函数中,因为将输入缓冲区更改为不同的值不会改变任何内容。如果有人能够立即看到此功能有什么问题,我将不胜感激。

【问题讨论】:

  • 你检查过文件没有到达EOF吗?
  • 只是为了排除显而易见的问题——我相信,在尝试使用它之前,您确实检查了sndFile 是否是NULL

标签: c++ buffer wav file-writing libsndfile


【解决方案1】:

我的最佳猜测和这个函数的经典错误是inputBuffer 太短了。

当您使用sf_writef_double 时,第三个参数的单位是帧(即sample*nb_channels)。

来自documentation

对于帧数函数,该数组应足够大,以容纳等于帧数和通道数的乘积的项目数。

您可以通过以下任一方式修复它:

double inputBuffer[bufferLength];
...
void audiowrite(double* inputBuffer, int bufferLength, int fs, std::string outputFileLocation){
...
  sf_writef_double(sndFile, inputBuffer, bufferLength/2); // Unit is frame
...
}

或者

double inputBuffer[bufferLength];
...
void audiowrite(double* inputBuffer, int bufferLength, int fs, std::string outputFileLocation){
...
  sf_write_double(sndFile, inputBuffer, bufferLength); // Remove the 'f' unit is sample not frame
...
}

这也可以解释为什么单声道不会发生该错误,因为在这种情况下,帧 = 样本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2015-04-03
    相关资源
    最近更新 更多