gpt4 book ai didi

c++ - 如何将 Arduino 的模拟输入读数从草图转换为 .WAV

转载 作者:行者123 更新时间:2023-12-05 08:48:06 26 4
gpt4 key购买 nike

我正在尝试用 arduino 构建一个录音机。现在,该设置有一个麦克风记录输入和输出到 Arduino uno 的模拟输入。

如何将(时间戳、数据)读数转换为 .WAV?我知道有更多的库能够做到这一点,但我想了解 .WAV 的结构,并希望能够用 C\C++ 编写我自己的脚本。

感谢关注!

最佳答案

第一步:定义wav头变量

/// The first 4 byte of a wav file should be the characters "RIFF" */
char chunkID[4] = {'R', 'I', 'F', 'F'};
/// 36 + SubChunk2Size
uint32_t chunkSize = 36; // You Don't know this until you write your data but at a minimum it is 36 for an empty file
/// "should be characters "WAVE"
char format[4] = {'W', 'A', 'V', 'E'};
/// " This should be the letters "fmt ", note the space character
char subChunk1ID[4] = {'f', 'm', 't', ' '};
///: For PCM == 16, since audioFormat == uint16_t
uint32_t subChunk1Size = 16;
///: For PCM this is 1, other values indicate compression
uint16_t audioFormat = 1;
///: Mono = 1, Stereo = 2, etc.
uint16_t numChannels = 1;
///: Sample Rate of file
uint32_t sampleRate = 44100;
///: SampleRate * NumChannels * BitsPerSample/8
uint32_t byteRate = 44100 * 2;
///: The number of byte for one frame NumChannels * BitsPerSample/8
uint16_t blockAlign = 2;
///: 8 bits = 8, 16 bits = 16
uint16_t bitsPerSample = 16;
///: Contains the letters "data"
char subChunk2ID[4] = {'d', 'a', 't', 'a'};
///: == NumSamples * NumChannels * BitsPerSample/8 i.e. number of byte in the data.
uint32_t subChunk2Size = 0; // You Don't know this until you write your data

这里有一些假设,例如音频质量。以上假定 CD 质量音频 16 位,44.1kHz。如果您需要其他内容,则需要修改相关值。

您还需要注意您的 Arduino 是否真的定期记录样本,或者这是否本质上是数据的超声处理。

第 2 步:打开 Wav 文件。

#include <SPI.h>
#include <SD.h>

File wavFile;
const char* filename = "data.wav";


void setup()
{

Serial.begin(9600);
while (!Serial);

if (!SD.begin(4))
while (1);

wavFile = SD.open(filename, FILE_WRITE);


if (!wavFile)
while (1);


}

第 3 步:将标题写入 Wav 文件。

鉴于 SD Library 的方式是有组织的,如果它们还不是 char,则必须将 header 的元素转换为 byte必须 可能有点强,但它确实可以更轻松地为 header 的每个元素维护正确的字节宽度,而不会弄得太多。

将 this 包装在它自己的函数中是有意义的。

void writeWavHeader()
{
wavFile.write(chunkID,4);
wavFile.write((byte*)&chunkSize,4);
wavFile.write(format,4);
wavFile.write(subChunk1ID,4);
wavFile.write((byte*)&subChunk1Size,4);
wavFile.write((byte*)&audioFormat,2);
wavFile.write((byte*)&numChannels,2);
wavFile.write((byte*)&sampleRate,4);
wavFile.write((byte*)&byteRate,4);
wavFile.write((byte*)&blockAlign,2);
wavFile.write((byte*)&bitsPerSample,2);
wavFile.write(subChunk2ID,4);
wavFile.write((byte*)&subChunk2Size,4);
}

第 4 步:将数据添加到 Wav 文件

wav header 告诉我们 chunkSize 和 subChunk2Size 取决于文件中的数据量。因此,如果您将数据附加到文件,这些将增长并且必须更改。

您可以通过多种方式获取有关添加数据的信息

  • 一次添加一个样本
  • 添加一个固定大小的缓冲区
  • 存储所有数据,最后全部添加

您选择什么方法将取决于您的实现,我们没有太多信息。为了争论,让我们一次写一个示例。这种方法应该证明需要什么,缺点(经常在文件中跳转,大量重复)应该立即显而易见。

void writeDataToWavFile(int data)
{
int16_t sampleValue = map(data, MIN_DATA_VALUE, MAX_DATA_VALUE,-32767,32767);

subChunk2Size += numChannels * bitsPerSample/8;
wavFile.seek(40);
wavFile.write((byte*)&subChunk2Size,4);

wavFile.seek(4);
chunkSize = 36 + subChunk2Size;
wavFile.write((byte*)&chunkSize,4);

wavFile.seek(wavFile.size()-1);
wavFile.write((byte*)&sampleValue,2);
}

MIN_DATA_VALUEMAX_DATA_VALUE 是您必须定义的内容。这一切都假设您正在使用整数数据。如果是 float ,则需要进行一些调整。

总而言之。

#include <SPI.h>
#include <SD.h>

int MIN_DATA_VALUE;
int MAX_DATA_VALUE;
File wavFile;
const char* filename = "data.wav";

/// The first 4 byte of a wav file should be the characters "RIFF" */
char chunkID[4] = {'R', 'I', 'F', 'F'};
/// 36 + SubChunk2Size
uint32_t chunkSize = 36; // You Don't know this until you write your data but at a minimum it is 36 for an empty file
/// "should be characters "WAVE"
char format[4] = {'W', 'A', 'V', 'E'};
/// " This should be the letters "fmt ", note the space character
char subChunk1ID[4] = {'f', 'm', 't', ' '};
///: For PCM == 16, since audioFormat == uint16_t
uint32_t subChunk1Size = 16;
///: For PCM this is 1, other values indicate compression
uint16_t audioFormat = 1;
///: Mono = 1, Stereo = 2, etc.
uint16_t numChannels = 1;
///: Sample Rate of file
uint32_t sampleRate = 44100;
///: SampleRate * NumChannels * BitsPerSample/8
uint32_t byteRate = 44100 * 2;
///: The number of byte for one frame NumChannels * BitsPerSample/8
uint16_t blockAlign = 2;
///: 8 bits = 8, 16 bits = 16
uint16_t bitsPerSample = 16;
///: Contains the letters "data"
char subChunk2ID[4] = {'d', 'a', 't', 'a'};
///: == NumSamples * NumChannels * BitsPerSample/8 i.e. number of byte in the data.
uint32_t subChunk2Size = 0; // You Don't know this until you write your data



void setup()
{

Serial.begin(9600);
while (!Serial);

if (!SD.begin(4))
while (1);

wavFile = SD.open(filename, FILE_WRITE);


if (!wavFile)
while (1);

writeWavHeader();

}

void loop()
{
int data = getSomeData();
writeDataToWavFile(data);
}

void writeWavHeader()
{
wavFile.seek(0);
wavFile.write(chunkID,4);
wavFile.write((byte*)&chunkSize,4);
wavFile.write(format,4);
wavFile.write(subChunk1ID,4);
wavFile.write((byte*)&subChunk1Size,4);
wavFile.write((byte*)&audioFormat,2);
wavFile.write((byte*)&numChannels,2);
wavFile.write((byte*)&sampleRate,4);
wavFile.write((byte*)&byteRate,4);
wavFile.write((byte*)&blockAlign,2);
wavFile.write((byte*)&bitsPerSample,2);
wavFile.write(subChunk2ID,4);
wavFile.write((byte*)&subChunk2Size,4);
}


void writeDataToWavFile(int data)
{
int16_t sampleValue = map(data, MIN_DATA_VALUE, MAX_DATA_VALUE,-32767,32767);

subChunk2Size += numChannels * bitsPerSample/8;
wavFile.seek(40);
wavFile.write((byte*)&subChunk2Size,4);

wavFile.seek(4);
chunkSize = 36 + subChunk2Size;
wavFile.write((byte*)&chunkSize,4);

wavFile.seek(wavFile.size()-1);
wavFile.write((byte*)&sampleValue,2);
}

总结

  • 您需要知道数据的最小/最大值或限制它们。
  • 您必须确保定期记录,否则您会得到人工制品。

关于c++ - 如何将 Arduino 的模拟输入读数从草图转换为 .WAV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66484763/

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