简体   繁体   English

如何在sndfile中写入24位pcm样本?

[英]How to write 24 bit pcm samples in sndfile?

I have file which is opened with SF_FORMAT_WAV|SF_FORMAT_FLOAT but have samples in 24 bit format. 我有使用SF_FORMAT_WAV|SF_FORMAT_FLOAT打开的文件,但具有24位格式的样本。 Sndfile docs says that the data type used by the calling program and the data format of the file do not need to be the same so using sf_write_int() i can write 32 bit integer samples and library converts to float on the fly. Sndfile文档说,调用程序使用的数据类型和文件的数据格式不必相同,因此使用sf_write_int()可以编写32位整数样本,并且库转换可以即时进行浮点运算。 But what about 24 bit. 但是24位呢? Do i pack it into int and then write using sf_write_int() or something else. 我是否将其打包为int ,然后使用sf_write_int()或其他方式编写。

It should be possible to just scale it by 256 to make it 32-bit and use sf_write_int . 应该可以将其缩放至256以使其成为32位并使用sf_write_int If you were expecting libsndfile to do any dithering or something for you this would be a bad idea, but as far as I can tell that's not in its job description. 如果您期望libsndfile为您做任何抖动或某事,那将是个坏主意,但据我所知,这不在其职务说明中。

You have to define the format of your output file to be SF_FORMAT_WAV|SF_FORMAT_PCM_24; 您必须将输出文件的格式定义为SF_FORMAT_WAV|SF_FORMAT_PCM_24; . When you do that, whatever your internal data are ( float , int16 ...), your output file will be written with the requested format. 当您执行此操作时,无论您的内部数据是什么( floatint16 ...),您的输出文件都将以请求的格式写入。

Here is an example where I read a file (a simple 0.5 second sine at FS of 44100 Hz designed with Audacity) with floating point 32 bit data. 这是一个示例,其中我读取了一个文件(带有Audacity设计的44100 Hz FS的0.5秒正弦),该文件具有浮点32位数据。 My data is also float internally. 我的数据也在内部浮动。 Then I write a 24 bit PCM file. 然后,我编写一个24位PCM文件。

#include <stdio.h>
#include <stdlib.h>
#include "sndfile.h"

#define DATA_TO_CONVERT     22050

int main(void)
{
  char *inFileName, *outFileName;
  SNDFILE *inFile, *outFile;
  SF_INFO inFileInfo, outFileInfo;

  float dataBuffer[DATA_TO_CONVERT];

  // Input file 32 bit float
  inFileName = "sine_32.wav";
  inFile = sf_open(inFileName, SFM_READ, &inFileInfo);

  // Output file 24 bit
  outFileName = "sine_24.wav";
  outFileInfo.frames        = inFileInfo.frames;
  outFileInfo.samplerate    = inFileInfo.samplerate;
  outFileInfo.channels      = inFileInfo.channels;
  outFileInfo.format        = SF_FORMAT_WAV|SF_FORMAT_PCM_24; // Wanted output format
  outFile = sf_open(outFileName, SFM_WRITE, &outFileInfo);

  sf_read_float(inFile, dataBuffer, DATA_TO_CONVERT);
  sf_write_float(outFile, dataBuffer, DATA_TO_CONVERT);
  sf_close(inFile);
  sf_close(outFile);

  // Check Output file
  sf_open(outFileName, SFM_READ, &outFileInfo);
  printf("Output File format : 0x%x\n", outFileInfo.format);

  return 0;
}

The console output is: 控制台输出为:

Output File format : 0x10003 输出文件格式:0x10003

Which means SF_FORMAT_WAV|SF_FORMAT_PCM_24 . 这意味着SF_FORMAT_WAV|SF_FORMAT_PCM_24

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM