From 59531ea26be163327f7cbc6bc31b9845521afff5 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Wed, 8 Mar 2023 15:34:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0binary.hpp=20=E5=92=8C=20wavh?= =?UTF-8?q?eader.hpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/basic/ds/binary.hpp | 77 ++++++++++++++++++++++++++++++++++++ core/components/audio/wavheader.hpp | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 core/basic/ds/binary.hpp create mode 100644 core/components/audio/wavheader.hpp diff --git a/core/basic/ds/binary.hpp b/core/basic/ds/binary.hpp new file mode 100644 index 0000000..2ebae52 --- /dev/null +++ b/core/basic/ds/binary.hpp @@ -0,0 +1,77 @@ +#pragma once +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief + * + * service: + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +namespace core { +using namespace std; +using namespace core; +class Binary { + uint8_t* m_data = nullptr; + size_t m_size = 0; + + public: + Binary(int size) { + m_data = (uint8_t*)malloc(size); + m_size = size; + } + Binary(uint8_t* data, size_t size) { + m_data = (uint8_t*)malloc(size); + m_size = size; + memcpy(m_data, data, size); + } + Binary(const Binary& other) { + m_data = (uint8_t*)malloc(other.m_size); + m_size = other.m_size; + memcpy(m_data, other.m_data, m_size); + } + Binary(Binary&& other) { + m_data = other.m_data; + m_size = other.m_size; + other.m_data = nullptr; + other.m_size = 0; + } + ~Binary() { + if (m_data) { + free(m_data); + m_data = nullptr; + } + } + + uint8_t* data() const { return m_data; } + + size_t size() const { return m_size; } + + void resize(size_t size) { + uint8_t* newdata = (uint8_t*)realloc(m_data, size); + if (newdata) { + m_data = newdata; + m_size = size; + } + } +}; +} // namespace core +} // namespace iflytop \ No newline at end of file diff --git a/core/components/audio/wavheader.hpp b/core/components/audio/wavheader.hpp new file mode 100644 index 0000000..567b300 --- /dev/null +++ b/core/components/audio/wavheader.hpp @@ -0,0 +1,79 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytopcpp/core/spdlogfactory/logger.hpp" +#include "iflytopcpp/core/thread/thread.hpp" + +/** + * @brief + * + * service: + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +namespace core { +class WAVHeader { + public: + struct WAVHeaderBinary { + char chunk_id[4]; // 固定为 "RIFF" + uint32_t chunk_size; // 文件总大小 - 8 + char format[4]; // 固定为 "WAVE" + char subchunk1_id[4]; // 固定为 "fmt " + uint32_t subchunk1_size; // 固定为 16 + uint16_t audio_format; // 编码格式,1 表示 PCM 编码 + uint16_t num_channels; // 声道数,1 为单声道,2 为立体声 + uint32_t sample_rate; // 采样率,如 44100 + uint32_t byte_rate; // 数据传输速率,即采样率 * 声道数 * 位数 / 8 + uint16_t block_align; // 数据块对齐,即每个采样点占用的字节数,如 2 表示 16 位,即 2 字节 + uint16_t bits_per_sample; // 位数,如 8、16、24、32 + char subchunk2_id[4]; // 固定为 "data" + uint32_t subchunk2_size; // 数据长度,即文件总大小 - 44 + }; + WAVHeaderBinary header; + + public: + WAVHeader(uint32_t sample_rate, uint16_t bits_per_sample, uint16_t num_channels, uint32_t num_samples) { + // 填充 RIFF 头 + strcpy(header.chunk_id, "RIFF"); + header.chunk_size = num_samples * num_channels * bits_per_sample / 8 + 44; + strcpy(header.format, "WAVE"); + + // 填充 fmt 子块 + strcpy(header.subchunk1_id, "fmt "); + header.subchunk1_size = 16; + header.audio_format = 1; // PCM 编码 + header.num_channels = num_channels; + header.sample_rate = sample_rate; + header.bits_per_sample = bits_per_sample; + header.byte_rate = sample_rate * num_channels * bits_per_sample / 8; + header.block_align = num_channels * bits_per_sample / 8; + + // 填充 data 子块 + strcpy(header.subchunk2_id, "data"); + header.subchunk2_size = num_samples * num_channels * bits_per_sample / 8; + } + + uint8_t* data() { return (uint8_t*)&header; } + size_t size() { return sizeof(header); } +}; + +} // namespace core +} // namespace iflytop \ No newline at end of file