Browse Source

fix wavheader.hpp bug

disinfection_machine
zhaohe 2 years ago
parent
commit
d2eb67fd50
  1. 37
      core/components/audio/wav_recorder.cpp
  2. 26
      core/components/audio/wav_recorder.hpp
  3. 6
      core/components/audio/wavheader.hpp
  4. 9
      module.cmake

37
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();
}

26
core/components/audio/wav_recorder.hpp

@ -0,0 +1,26 @@
#pragma once
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#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

6
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;

9
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/)
Loading…
Cancel
Save