gpt4 book ai didi

c++ - 来自捕获的 PCM 样本数据的 WAV 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:33:00 24 4
gpt4 key购买 nike

我使用 NI 数据采集模块以 48ksps 的速度“现场”捕获了数 Gb 的样本数据。我想根据这些数据创建一个 WAV 文件。

我之前使用 MATLAB 加载数据,将其标准化为 16 位 PCM 范围,然后将其写为 WAV 文件。然而,MATLAB 在文件大小方面犹豫不决,因为它在“内存中”执行所有操作。

理想情况下,我会使用 C++ 或 C(C# 是一个选项)执行此操作,或者如果有现成的实用程序,我会使用它。是否有一种简单的方法(即现有库)获取原始 PCM 缓冲区、指定采样率、位深度并将其打包到 WAV 文件中?

要处理大型数据集,它需要能够以 block 的形式附加数据,因为不一定可以将整个数据集读入内存。

我知道我可以使用格式规范从头开始做这件事,但我不想重新发明轮子,或者如果我能帮忙的话,也不想花时间修复错误。

最佳答案

有趣的是,我在代码的 stackoverflow 解析中发现了一个错误,它不支持行尾的\字符,如下所示,很遗憾

//stolen from OGG Vorbis pcm to wav conversion rountines, sorry
#define VERSIONSTRING "OggDec 1.0\n"

static int quiet = 0;
static int bits = 16;
static int endian = 0;
static int raw = 0;
static int sign = 1;
unsigned char headbuf[44]; /* The whole buffer */







#define WRITE_U32(buf, x) *(buf) = (unsigned char)((x)&0xff);\
*((buf)+1) = (unsigned char)(((x)>>8)&0xff);\
*((buf)+2) = (unsigned char)(((x)>>16)&0xff);\
*((buf)+3) = (unsigned char)(((x)>>24)&0xff);

#define WRITE_U16(buf, x) *(buf) = (unsigned char)((x)&0xff);\
*((buf)+1) = (unsigned char)(((x)>>8)&0xff);

/*
* Some of this based on ao/src/ao_wav.c
*/
static int
write_prelim_header (FILE * out, int channels, int samplerate)
{

int knownlength = 0;

unsigned int size = 0x7fffffff;
// int channels = 2;
// int samplerate = 44100;//change this to 48000
int bytespersec = channels * samplerate * bits / 8;
int align = channels * bits / 8;
int samplesize = bits;

if (knownlength)
size = (unsigned int) knownlength;

memcpy (headbuf, "RIFF", 4);
WRITE_U32 (headbuf + 4, size - 8);
memcpy (headbuf + 8, "WAVE", 4);
memcpy (headbuf + 12, "fmt ", 4);
WRITE_U32 (headbuf + 16, 16);
WRITE_U16 (headbuf + 20, 1); /* format */
WRITE_U16 (headbuf + 22, channels);
WRITE_U32 (headbuf + 24, samplerate);
WRITE_U32 (headbuf + 28, bytespersec);
WRITE_U16 (headbuf + 32, align);
WRITE_U16 (headbuf + 34, samplesize);
memcpy (headbuf + 36, "data", 4);
WRITE_U32 (headbuf + 40, size - 44);

if (fwrite (headbuf, 1, 44, out) != 44)
{
printf ("ERROR: Failed to write wav header: %s\n", strerror (errno));
return 1;
}

return 0;
}

static int
rewrite_header (FILE * out, unsigned int written)
{
unsigned int length = written;

length += 44;

WRITE_U32 (headbuf + 4, length - 8);
WRITE_U32 (headbuf + 40, length - 44);
if (fseek (out, 0, SEEK_SET) != 0)
{
printf ("ERROR: Failed to seek on seekable file: %s\n",
strerror (errno));
return 1;
}

if (fwrite (headbuf, 1, 44, out) != 44)
{
printf ("ERROR: Failed to write wav header: %s\n", strerror (errno));
return 1;
}
return 0;
}

关于c++ - 来自捕获的 PCM 样本数据的 WAV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1460007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com