From d2eb67fd50368b94609aa4416ed5e0ae709a1d1f Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 25 Mar 2023 15:40:03 +0800 Subject: [PATCH] fix wavheader.hpp bug --- core/components/audio/wav_recorder.cpp | 37 ++++++++++++++++++++++++++++++++++ core/components/audio/wav_recorder.hpp | 26 ++++++++++++++++++++++++ core/components/audio/wavheader.hpp | 6 +++--- module.cmake | 9 +++------ 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 core/components/audio/wav_recorder.cpp create mode 100644 core/components/audio/wav_recorder.hpp diff --git a/core/components/audio/wav_recorder.cpp b/core/components/audio/wav_recorder.cpp new file mode 100644 index 0000000..58dce39 --- /dev/null +++ b/core/components/audio/wav_recorder.cpp @@ -0,0 +1,37 @@ +#include "wav_recorder.hpp" +using namespace std; +using namespace iflytop; +using namespace core; + +void WavRecorder::writeVoice(string filename, uint32_t sample_rate, uint16_t bits_per_sample, uint16_t num_channels, + const char* data, uint32_t size) { + ofstream file; + WAVHeader wavHeader; + // logger->info("writeVoice filename:{} {} {} {} {}", filename, sample_rate, bits_per_sample, num_channels, size); + file.open(filename, ios::out | ios::binary | ios::app); + + /** + * @brief 判断文件大小,文件大小大于等于44字节,说明文件已经存在,不需要写入头部 + */ + file.seekp(0, ios::end); + uint32_t filesize = file.tellp(); + + if (filesize < 44) { + wavHeader.initialize(sample_rate, bits_per_sample, num_channels, 0); + file.seekp(0, ios::beg); + file.write((char*)wavHeader.data(), wavHeader.size()); + } + file.seekp(0, ios::end); + file.write(data, size); + /** + * @brief 更新头部信息 + */ + file.seekp(4, ios::beg); + uint32_t chunk_size = filesize + size - 8; + file.write((char*)&chunk_size, 4); + file.seekp(40, ios::beg); + uint32_t subchunk2_size = filesize + size - 44; + file.write((char*)&subchunk2_size, 4); + + file.close(); +} \ No newline at end of file diff --git a/core/components/audio/wav_recorder.hpp b/core/components/audio/wav_recorder.hpp new file mode 100644 index 0000000..977a5f9 --- /dev/null +++ b/core/components/audio/wav_recorder.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytopcpp/core/components/audio/wavheader.hpp" +#include "iflytopcpp/core/spdlogfactory/logger.hpp" +#include "iflytopcpp/core/thread/thread.hpp" + +namespace iflytop { +namespace core { +class WavRecorder { + ENABLE_LOGGER(WavRecorder); + + public: + void writeVoice(string filename, uint32_t sample_rate, uint16_t bits_per_sample, uint16_t num_channels, + const char* data, uint32_t 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 index e7f48ea..b916bdf 100644 --- a/core/components/audio/wavheader.hpp +++ b/core/components/audio/wavheader.hpp @@ -76,12 +76,12 @@ class WAVHeader { WAVHeader() {} void initialize(uint32_t sample_rate, uint16_t bits_per_sample, uint16_t num_channels, uint32_t wavbinsize) { // 填充 RIFF 头 - strcpy(header.chunk_id, "RIFF"); + memcpy(header.chunk_id, "RIFF", 4); header.chunk_size = wavbinsize + 44 - 8; - strcpy(header.format, "WAVE"); + memcpy(header.format, "WAVE", 4); // 填充 fmt 子块 - strcpy(header.subchunk1_id, "fmt "); + memcpy(header.subchunk1_id, "fmt ", 4); header.subchunk1_size = 16; header.audio_format = 1; // PCM 编码 header.num_channels = num_channels; diff --git a/module.cmake b/module.cmake index c20eb41..12b3c6d 100644 --- a/module.cmake +++ b/module.cmake @@ -15,22 +15,19 @@ set(DEP_SRC dep/iflytopcpp/core/basic/signal/signal.cpp dep/iflytopcpp/core/thread/thread.cpp dep/iflytopcpp/core/zexception/zexception.cpp - dep/iflytopcpp/core/components/jobs/work_queue.cpp dep/iflytopcpp/core/components/jobs/thread_pool_task_scheduler.cpp - dep/iflytopcpp/core/components/uart/uart.cpp dep/iflytopcpp/core/components/modbus/modbus.cpp dep/iflytopcpp/core/components/modbus/zmodbus_common.c dep/iflytopcpp/core/components/modbus/zmodbus_master.c dep/iflytopcpp/core/components/modbus/zmodbus_slave.c - dep/iflytopcpp/core/linuxcoreutils/linuxcoreutils.cpp - dep/iflytopcpp/core/components/process/process_unix.cpp dep/iflytopcpp/core/components/process/process.cpp dep/iflytopcpp/core/components/timer/simple_timer.cpp # -) + dep/iflytopcpp/core/components/audio/wav_recorder.cpp) set(DEP_DEFINE ${DEP_DEFINE}) -set(PUBLIC_INCLUDE_DIRECTORIES ${PUBLIC_INCLUDE_DIRECTORIES} ./dep/iflytopcpp/core/spdlog/include/) +set(PUBLIC_INCLUDE_DIRECTORIES ${PUBLIC_INCLUDE_DIRECTORIES} + ./dep/iflytopcpp/core/spdlog/include/)