Browse Source

添加binary.hpp 和 wavheader.hpp

disinfection_machine
zhaohe 2 years ago
parent
commit
59531ea26b
  1. 77
      core/basic/ds/binary.hpp
  2. 79
      core/components/audio/wavheader.hpp

77
core/basic/ds/binary.hpp

@ -0,0 +1,77 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
/**
* @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

79
core/components/audio/wavheader.hpp

@ -0,0 +1,79 @@
//
// Created by zwsd
//
#pragma once
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#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
Loading…
Cancel
Save