• 工资「喂饱肚子」,副业「养活灵魂」!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

C++ – 为内存中音频裸流数据增加wav文件头

C++ 发布于2024-07-13 阅读 1,768次 0次评论 0次点赞 本文共2620个字,阅读需要7分钟。

1 C++为内存中的pcm音频数据增加wav文件头

最近面临这样的一个任务,在C++层使用tts进行语音合成之后需要将合成音频的base64字符串实时传递到web端,使用js在web端进行播放,而js在web端需要wav音频流,直接传递音频裸流到js中会播放出错。

所以需要先将C++生成的音频裸流加上wav文件头,然后将内存中的音频字节数据进行base64编码之后再传递到web中。

针对这个功能,我封装了一个C++文件头用于实现这个功能,代码如下,

#ifndef ADD_WAV_HEADER_H
#define ADD_WAV_HEADER_H

#include <vector>

#pragma pack(push, 1) // 确保结构体按1字节对齐

struct RIFFHeader {
    char riff[4]; // "RIFF"
    int fileSize; // 文件大小,不包括这8字节
    char wave[4]; // "WAVE"
};

struct FormatChunk {
    char fmt[4]; // "fmt "
    int chunkSize; // 子块大小,通常为16或18
    short audioFormat; // 音频格式,1为PCM
    short numChannels; // 通道数
    int sampleRate; // 采样率
    int byteRate; // 每秒字节数
    short blockAlign; // 块对齐
    short bitsPerSample; // 每个样本的位数
};

struct DataChunk {
    char data[4]; // "data"
    int dataSize; // 数据大小
};

#pragma pack(pop)

inline void AddWAVHeader(std::vector<char>& audioData, int sampleRate, int channels, int bitsPerSample) {
    int fileSize = audioData.size() + sizeof(RIFFHeader) + sizeof(FormatChunk) - 8;
    int byteRate = sampleRate * channels * bitsPerSample / 8;
    int blockAlign = channels * bitsPerSample / 8;

    RIFFHeader riffHeader = {
        {'R', 'I', 'F', 'F'},
        fileSize,
        {'W', 'A', 'V', 'E'}
    };

    FormatChunk formatChunk = {
        {'f', 'm', 't', ' '},
        sizeof(FormatChunk) - 8,
        1, // PCM
        (short)channels,
        sampleRate,
        byteRate,
        (short)blockAlign,
        (short)bitsPerSample
    };

    DataChunk dataChunk = {
        {'d', 'a', 't', 'a'},
        static_cast<int>(audioData.size())
    };

    // 将文件头添加到音频数据之前
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&dataChunk), reinterpret_cast<char*>(&dataChunk) + sizeof(dataChunk));
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&formatChunk), reinterpret_cast<char*>(&formatChunk) + sizeof(formatChunk));
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&riffHeader), reinterpret_cast<char*>(&riffHeader) + sizeof(riffHeader));
}

inline void AddWAVHeader(std::vector<uint8_t>& audioData, int sampleRate, int channels, int bitsPerSample) {
    int fileSize = audioData.size() + sizeof(RIFFHeader) + sizeof(FormatChunk) - 8;
    int byteRate = sampleRate * channels * bitsPerSample / 8;
    int blockAlign = channels * bitsPerSample / 8;

    RIFFHeader riffHeader = {
        {'R', 'I', 'F', 'F'},
        fileSize,
        {'W', 'A', 'V', 'E'}
    };

    FormatChunk formatChunk = {
        {'f', 'm', 't', ' '},
        sizeof(FormatChunk) - 8,
        1, // PCM
        (short)channels,
        sampleRate,
        byteRate,
        (short)blockAlign,
        (short)bitsPerSample
    };

    DataChunk dataChunk = {
        {'d', 'a', 't', 'a'},
        static_cast<int>(audioData.size())
    };

    // 将文件头添加到音频数据之前
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&dataChunk), reinterpret_cast<char*>(&dataChunk) + sizeof(dataChunk));
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&formatChunk), reinterpret_cast<char*>(&formatChunk) + sizeof(formatChunk));
    audioData.insert(audioData.begin(), reinterpret_cast<char*>(&riffHeader), reinterpret_cast<char*>(&riffHeader) + sizeof(riffHeader));
}

#endif // !ADD_WAV_HEADER_H

功能比较小也比较常规,做个备完,以免之后又要造重复的轮子。

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – 为内存中音频裸流数据增加wav文件头

原文链接:https://www.stubbornhuang.com/3051/

发布于:2024年07月13日 16:54:07

修改于:2024年07月13日 16:54:07

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
资源分享 - The Modern Vulkan Cookbook - A practical guide to 3D graphics and advanced real-time rendering techniques in Vulkan 英文PDF下载
计算几何与计算机图形学资源
下一篇
Assimp的aiMatrix4x4与glm的mat4相互转换
3D数学基础
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号