add aec3 code.

This commit is contained in:
luocai 2024-09-06 16:35:51 +08:00
parent c6debcc62a
commit 43166b5474
17 changed files with 1487 additions and 20 deletions

View File

@ -6,6 +6,7 @@ add_executable(Record main.cpp
FFmpegResample.h FFmpegResample.cpp FFmpegResample.h FFmpegResample.cpp
EchoRecord.cpp EchoRecord.cpp
Player.cpp Player.cpp
ProcessFile.cpp
Recorder.cpp Recorder.cpp
SpeexDsp.h SpeexDsp.cpp SpeexDsp.h SpeexDsp.cpp
Utility.h Utility.cpp Utility.h Utility.cpp
@ -21,6 +22,7 @@ target_include_directories(Record
PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/amazon-kinesis-video-streams-webrtc-sdk-c/include PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/amazon-kinesis-video-streams-webrtc-sdk-c/include
PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/speexdsp-1.2.1/include PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/speexdsp-1.2.1/include
PRIVATE ${FFMPEG_INCLUDE_DIR} PRIVATE ${FFMPEG_INCLUDE_DIR}
# PRIVATE ${CMAKE_SOURCE_DIR}/rkap/include
) )
target_link_directories(Record target_link_directories(Record
@ -33,6 +35,7 @@ target_link_directories(Record
PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/amazon-kinesis-video-streams-webrtc-sdk-c/lib PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/amazon-kinesis-video-streams-webrtc-sdk-c/lib
PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/usrsctp-0.9.5.0/lib PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/usrsctp-0.9.5.0/lib
PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/libsrtp-2.6.0/lib PRIVATE /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/libsrtp-2.6.0/lib
# PRIVATE ${CMAKE_SOURCE_DIR}/rkap/lib
) )
target_link_libraries(Record target_link_libraries(Record
@ -73,4 +76,6 @@ target_link_libraries(Record
PRIVATE dl PRIVATE dl
PRIVATE z PRIVATE z
PRIVATE ${FFMPEG_LIBRARY} PRIVATE ${FFMPEG_LIBRARY}
# PRIVATE RKAP_Common
# PRIVATE RKAP_3A
) )

View File

@ -1,4 +1,5 @@
#include "BoostLog.h" #include "BoostLog.h"
#include "DateTime.h"
#include "SpeexDsp.h" #include "SpeexDsp.h"
#include "Utility.h" #include "Utility.h"
#include "WebRtcAecm.h" #include "WebRtcAecm.h"
@ -39,6 +40,21 @@ void EchoRecordTask::setDsp(Dsp dsp) {
} }
} }
void EchoRecordTask::setDumpEnabled(bool enabled) {
if (enabled) {
auto date = DateTime::currentDateTime().toString("%Y%m%d%H%M%S");
std::ostringstream oss;
oss << DumpPath << "/mic_" << date << ".pcm";
auto filePath = oss.str();
m_micOfs = std::make_shared<std::ofstream>(filePath, std::ofstream::binary);
oss.str("");
oss << DumpPath << "/speak_" << dspToString(m_dsp) << "_" << date << ".pcm";
filePath = oss.str();
m_outOfs = std::make_shared<std::ofstream>(filePath, std::ofstream::binary);
}
}
void EchoRecordTask::setChannels(int channels) { void EchoRecordTask::setChannels(int channels) {
if (m_channels != channels) { if (m_channels != channels) {
m_channels = channels; m_channels = channels;
@ -54,6 +70,7 @@ void EchoRecordTask::run() {
LOG(info) << "dsp use: " << dspToString(m_dsp); LOG(info) << "dsp use: " << dspToString(m_dsp);
RkAudio::Format format; RkAudio::Format format;
format.sampleRate = 16000;
format.channels = m_channels; format.channels = m_channels;
format.period = 10; format.period = 10;
@ -77,6 +94,9 @@ void EchoRecordTask::run() {
m_input = std::make_shared<RkAudio::Input>(); m_input = std::make_shared<RkAudio::Input>();
m_input->setDataCallback([this, format](const RkAudio::Frame &frame) { m_input->setDataCallback([this, format](const RkAudio::Frame &frame) {
if (m_micOfs) {
m_micOfs->write(reinterpret_cast<const char *>(frame.data), frame.byteSize);
}
memcpy(m_nearendBuffer.data(), frame.data, frame.byteSize); memcpy(m_nearendBuffer.data(), frame.data, frame.byteSize);
if (m_dsp == Speex) { if (m_dsp == Speex) {
m_speex->echoPlayback(reinterpret_cast<const int16_t *>(m_farendBuffer.data())); m_speex->echoPlayback(reinterpret_cast<const int16_t *>(m_farendBuffer.data()));
@ -100,6 +120,10 @@ void EchoRecordTask::run() {
m_d->nearendBuffer->CopyTo(config, reinterpret_cast<int16_t *>(m_outBuffer.data())); m_d->nearendBuffer->CopyTo(config, reinterpret_cast<int16_t *>(m_outBuffer.data()));
} }
if (m_outOfs) {
m_outOfs->write(reinterpret_cast<const char *>(m_outBuffer.data()), m_outBuffer.size());
}
if (m_channels == 2) { if (m_channels == 2) {
m_output->write(frame.data, frame.byteSize); m_output->write(frame.data, frame.byteSize);
} else if (m_channels == 1) { } else if (m_channels == 1) {

114
Record/ProcessFile.cpp Normal file
View File

@ -0,0 +1,114 @@
#include "IoContext.h"
#include "RKAP_3A.h"
#include "SpeexDsp.h"
#include "WebRtcAecm.h"
#include "api/audio/echo_canceller3_config.h"
#include "api/audio/echo_canceller3_factory.h"
#include "main.h"
#include "modules/audio_processing/aec3/echo_canceller3.h"
#include <boost/asio/post.hpp>
#include <sstream>
class ProcessFileTaskPrivate {
public:
void initialize(int sampleRate, int channels, int period) {
std::unique_ptr<webrtc::EchoCanceller3Factory> factory = std::make_unique<webrtc::EchoCanceller3Factory>();
echoCanceller = factory->Create(sampleRate, channels, channels);
nearendBuffer = std::make_unique<webrtc::AudioBuffer>(sampleRate, channels, sampleRate, channels, sampleRate, channels);
farendBuffer = std::make_unique<webrtc::AudioBuffer>(sampleRate, channels, sampleRate, channels, sampleRate, channels);
linearOutputBuffer = std::make_unique<webrtc::AudioBuffer>(sampleRate, channels, sampleRate, channels, sampleRate, channels);
RKAP_3A_Init(&m_vqe, AEC_TX_TYPE);
}
std::unique_ptr<webrtc::EchoControl> echoCanceller;
std::unique_ptr<webrtc::AudioBuffer> nearendBuffer;
std::unique_ptr<webrtc::AudioBuffer> farendBuffer;
std::unique_ptr<webrtc::AudioBuffer> linearOutputBuffer;
RKAP_AEC_State m_vqe;
};
ProcessFileTask::ProcessFileTask() : m_d{new ProcessFileTaskPrivate()} {
}
ProcessFileTask::~ProcessFileTask() {
if (m_d != nullptr) {
delete m_d;
}
}
void ProcessFileTask::setDsp(Dsp dsp) {
if (m_dsp != dsp) {
m_dsp = dsp;
}
}
// ./Record --file --dsp=aecm
void ProcessFileTask::run() {
m_speakerIfs = std::make_shared<std::ifstream>("/sdcard/speaker_16k.pcm", std::ifstream::binary);
m_micIfs = std::make_shared<std::ifstream>("/sdcard/micin_16k.pcm", std::ifstream::binary);
std::ostringstream oss;
oss << DumpPath << "/out_" << dspToString(m_dsp) << "_16k.pcm";
m_outFilename = oss.str();
m_ofs = std::make_shared<std::ofstream>(m_outFilename, std::ofstream::binary);
// m_speakerIfs = std::make_shared<std::ifstream>("/sdcard/speaker_8k.pcm", std::ifstream::binary);
// m_micIfs = std::make_shared<std::ifstream>("/sdcard/micin_8k.pcm", std::ifstream::binary);
// m_ofs = std::make_shared<std::ofstream>("/sdcard/out_8k.pcm", std::ofstream::binary);
m_webRtcAecm = std::make_shared<WebRtcAecm>();
m_webRtcAecm->start(16000, 1, 10);
m_speex = std::make_shared<SpeexDsp>();
m_speex->start(16000, 1, 10);
m_d->initialize(16000, 1, 10);
m_begin = std::chrono::system_clock::now();
process();
}
void ProcessFileTask::process() {
using namespace Amass;
auto ioConext = Singleton<IoContext>::instance();
if (m_speakerIfs && m_micIfs && *m_speakerIfs && *m_micIfs) {
char farendBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
char nearendBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
char outBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
// char farendBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
// char nearendBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
// char outBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
m_speakerIfs->read(farendBuffer, sizeof(farendBuffer));
m_micIfs->read(nearendBuffer, sizeof(nearendBuffer));
if (m_dsp == Speex) {
m_speex->echoPlayback(reinterpret_cast<const int16_t *>(farendBuffer));
m_speex->echoCapture(reinterpret_cast<const int16_t *>(nearendBuffer), reinterpret_cast<int16_t *>(outBuffer));
} else if (m_dsp == AecMobile) {
m_webRtcAecm->echoPlayback(reinterpret_cast<const int16_t *>(farendBuffer), sizeof(farendBuffer) / 2);
m_webRtcAecm->echoCancellation(reinterpret_cast<int16_t *>(nearendBuffer), nullptr, reinterpret_cast<int16_t *>(outBuffer),
sizeof(farendBuffer) / 2);
} else if (m_dsp == Aec3) {
webrtc::StreamConfig config(16000, 1); // 单声道
m_d->nearendBuffer->CopyFrom(reinterpret_cast<const int16_t *>(nearendBuffer), config);
m_d->farendBuffer->CopyFrom(reinterpret_cast<const int16_t *>(farendBuffer), config);
m_d->echoCanceller->AnalyzeRender(m_d->farendBuffer.get());
m_d->echoCanceller->AnalyzeCapture(m_d->nearendBuffer.get());
m_d->echoCanceller->ProcessCapture(m_d->nearendBuffer.get(), false);
// m_d->echoCanceller->ProcessCapture(&nearendBuffer, &linearOutputBuffer, /*level_change=*/false);
m_d->nearendBuffer->CopyTo(config, reinterpret_cast<int16_t *>(outBuffer));
} else if (m_dsp == Vqe) {
}
m_ofs->write(outBuffer, sizeof(outBuffer));
boost::asio::post(*ioConext->ioContext(), [this]() { process(); });
} else {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - m_begin);
LOG(info) << "process file finished, out: " << m_outFilename << ", elapsed: " << elapsed;
std::exit(0);
}
}

View File

@ -5,12 +5,8 @@
#include <sstream> #include <sstream>
void RecorderTask::run() { void RecorderTask::run() {
constexpr auto path = "/sdcard/data";
if (!std::filesystem::exists(path)) {
std::filesystem::create_directory(path);
}
std::ostringstream oss; std::ostringstream oss;
oss << path << "/" << DateTime::currentDateTime().toString("%Y%m%d%H%M%S") << ".pcm"; oss << DumpPath << "/" << DateTime::currentDateTime().toString("%Y%m%d%H%M%S") << ".pcm";
auto filePath = oss.str(); auto filePath = oss.str();
m_ofs = std::make_shared<std::ofstream>(filePath, std::ofstream::binary); m_ofs = std::make_shared<std::ofstream>(filePath, std::ofstream::binary);

View File

@ -1,17 +1,14 @@
#include "main.h" #include "main.h"
#include "BoostLog.h" #include "BoostLog.h"
#include "DateTime.h"
#include "FFmpegResample.h" #include "FFmpegResample.h"
#include "IoContext.h" #include "IoContext.h"
#include "OpusCodec.h"
#include "RkAudio.h"
#include "WebRTCPublisher.h" #include "WebRTCPublisher.h"
#include <boost/asio/signal_set.hpp> #include <boost/asio/signal_set.hpp>
// #include <boost/program_options.hpp>
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp> #include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
#include <com/amazonaws/kinesis/video/webrtcclient/Include.h> #include <com/amazonaws/kinesis/video/webrtcclient/Include.h>
#include <filesystem>
#include <fstream> #include <fstream>
#include <rkmedia/rkmedia_api.h> #include <rkmedia/rkmedia_api.h>
@ -34,9 +31,11 @@ int main(int argc, char **argv) {
("echo", "Self-recording and self-play test") ("echo", "Self-recording and self-play test")
("record", "Record to file.") ("record", "Record to file.")
("play", "Play pcm file.") ("play", "Play pcm file.")
("dsp", boost::program_options::value<std::string>(), "vqe, speex, aecm") ("file", "process file.")
("dsp", boost::program_options::value<std::string>()->default_value("aecm"), "vqe, speex, aecm")
("channels", boost::program_options::value<int>(), "set audio channles") ("channels", boost::program_options::value<int>(), "set audio channles")
("path", boost::program_options::value<std::string>(), "file path") ("path", boost::program_options::value<std::string>(), "file path")
("dump,d", boost::program_options::value<bool>()->default_value(false), "dump file.")
; ;
// clang-format on // clang-format on
@ -51,19 +50,15 @@ int main(int argc, char **argv) {
std::shared_ptr<Task> task; std::shared_ptr<Task> task;
if (variablesMap.count("echo")) { if (variablesMap.count("echo")) {
Dsp dsp = Vqe;
if (variablesMap.count("dsp")) {
dsp = dspFromString(variablesMap["dsp"].as<std::string>());
}
int channels = 2; int channels = 2;
if (variablesMap.count("channels")) { if (variablesMap.count("channels")) {
channels = variablesMap["channels"].as<int>(); channels = variablesMap["channels"].as<int>();
} }
auto t = std::make_shared<EchoRecordTask>(); auto t = std::make_shared<EchoRecordTask>();
t->setDsp(dsp); t->setDsp(dspFromString(variablesMap["dsp"].as<std::string>()));
t->setChannels(channels); t->setChannels(channels);
t->setDumpEnabled(variablesMap["dump"].as<bool>());
task = std::dynamic_pointer_cast<Task>(t); task = std::dynamic_pointer_cast<Task>(t);
} else if (variablesMap.count("record")) { } else if (variablesMap.count("record")) {
task = std::make_shared<RecorderTask>(); task = std::make_shared<RecorderTask>();
@ -82,6 +77,10 @@ int main(int argc, char **argv) {
t->setChannels(channels); t->setChannels(channels);
t->setPath(path); t->setPath(path);
task = std::dynamic_pointer_cast<Task>(t); task = std::dynamic_pointer_cast<Task>(t);
} else if (variablesMap.count("file")) {
auto t = std::make_shared<ProcessFileTask>();
t->setDsp(dspFromString(variablesMap["dsp"].as<std::string>()));
task = std::dynamic_pointer_cast<Task>(t);
} }
if (!task) { if (!task) {
@ -89,6 +88,9 @@ int main(int argc, char **argv) {
std::cout << optionsDescription << std::endl; std::cout << optionsDescription << std::endl;
return 2; return 2;
} }
if (!std::filesystem::exists(DumpPath)) {
std::filesystem::create_directory(DumpPath);
}
try { try {
LOG(info) << "app start."; LOG(info) << "app start.";

View File

@ -15,6 +15,8 @@ enum Dsp {
Aec3, Aec3,
}; };
constexpr auto DumpPath = "/sdcard/data";
class Task { class Task {
public: public:
virtual void run() = 0; virtual void run() = 0;
@ -54,6 +56,7 @@ public:
EchoRecordTask(); EchoRecordTask();
~EchoRecordTask(); ~EchoRecordTask();
void setDsp(Dsp dsp); void setDsp(Dsp dsp);
void setDumpEnabled(bool enabled);
void setChannels(int channels); void setChannels(int channels);
void run() final; void run() final;
@ -70,6 +73,31 @@ private:
std::vector<uint8_t> m_farendBuffer; std::vector<uint8_t> m_farendBuffer;
EchoRecordPrivate *m_d = nullptr; EchoRecordPrivate *m_d = nullptr;
std::shared_ptr<std::ofstream> m_outOfs;
std::shared_ptr<std::ofstream> m_micOfs;
};
class ProcessFileTaskPrivate;
class ProcessFileTask : public Task {
public:
ProcessFileTask();
~ProcessFileTask();
void setDsp(Dsp dsp);
void run() final;
protected:
void process();
private:
ProcessFileTaskPrivate *m_d = nullptr;
Dsp m_dsp = Speex;
std::shared_ptr<SpeexDsp> m_speex;
std::shared_ptr<WebRtcAecm> m_webRtcAecm;
std::shared_ptr<std::ifstream> m_speakerIfs;
std::shared_ptr<std::ifstream> m_micIfs;
std::shared_ptr<std::ofstream> m_ofs;
std::string m_outFilename;
std::chrono::system_clock::time_point m_begin;
}; };
Dsp dspFromString(const std::string &dsp); Dsp dspFromString(const std::string &dsp);

View File

@ -30,6 +30,8 @@ add_library(VocieProcess
common_audio/channel_buffer.h common_audio/channel_buffer.cc common_audio/channel_buffer.h common_audio/channel_buffer.cc
common_audio/fir_filter_neon.h common_audio/fir_filter_neon.cc common_audio/fir_filter_neon.h common_audio/fir_filter_neon.cc
common_audio/ring_buffer.h common_audio/ring_buffer.c common_audio/ring_buffer.h common_audio/ring_buffer.c
common_audio/wav_file.h common_audio/wav_file.cc
common_audio/wav_header.h common_audio/wav_header.cc
common_audio/resampler/push_sinc_resampler.h common_audio/resampler/push_sinc_resampler.cc common_audio/resampler/push_sinc_resampler.h common_audio/resampler/push_sinc_resampler.cc
common_audio/resampler/sinc_resampler.h common_audio/resampler/sinc_resampler_neon.cc common_audio/resampler/sinc_resampler.h common_audio/resampler/sinc_resampler_neon.cc
@ -74,6 +76,8 @@ add_library(VocieProcess
rtc_base/strings/string_builder.h rtc_base/strings/string_builder.cc rtc_base/strings/string_builder.h rtc_base/strings/string_builder.cc
rtc_base/system/file_wrapper.h rtc_base/system/file_wrapper.cc
modules/audio_processing/audio_buffer.h modules/audio_processing/audio_buffer.cc modules/audio_processing/audio_buffer.h modules/audio_processing/audio_buffer.cc
modules/audio_processing/high_pass_filter.h modules/audio_processing/high_pass_filter.cc modules/audio_processing/high_pass_filter.h modules/audio_processing/high_pass_filter.cc
modules/audio_processing/splitting_filter.h modules/audio_processing/splitting_filter.cc modules/audio_processing/splitting_filter.h modules/audio_processing/splitting_filter.cc
@ -153,10 +157,10 @@ add_library(VocieProcess
target_compile_definitions(VocieProcess target_compile_definitions(VocieProcess
PRIVATE NOMINMAX # <windows.h> PRIVATE NOMINMAX # <windows.h>
PRIVATE RTC_DISABLE_LOGGING # PRIVATE RTC_DISABLE_LOGGING
PUBLIC RTC_DISABLE_METRICS # PUBLIC RTC_DISABLE_METRICS
PUBLIC WEBRTC_HAS_NEON PUBLIC WEBRTC_HAS_NEON
PUBLIC WEBRTC_APM_DEBUG_DUMP=0 PUBLIC WEBRTC_APM_DEBUG_DUMP=1
$<$<PLATFORM_ID:Windows>:WEBRTC_WIN> $<$<PLATFORM_ID:Windows>:WEBRTC_WIN>
$<$<PLATFORM_ID:Linux>:WEBRTC_POSIX WEBRTC_LINUX> $<$<PLATFORM_ID:Linux>:WEBRTC_POSIX WEBRTC_LINUX>
) )

View File

@ -0,0 +1,290 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "common_audio/wav_file.h"
#include <errno.h>
#include <algorithm>
#include <array>
#include <cstdio>
#include <type_traits>
#include <utility>
#include "common_audio/include/audio_util.h"
#include "rtc_base/checks.h"
#include "rtc_base/system/arch.h"
namespace webrtc {
namespace {
static_assert(std::is_trivially_destructible<WavFormat>::value, "");
// Checks whether the format is supported or not.
bool FormatSupported(WavFormat format) {
// Only PCM and IEEE Float formats are supported.
return format == WavFormat::kWavFormatPcm ||
format == WavFormat::kWavFormatIeeeFloat;
}
// Doesn't take ownership of the file handle and won't close it.
class WavHeaderFileReader : public WavHeaderReader {
public:
explicit WavHeaderFileReader(FileWrapper* file) : file_(file) {}
WavHeaderFileReader(const WavHeaderFileReader&) = delete;
WavHeaderFileReader& operator=(const WavHeaderFileReader&) = delete;
size_t Read(void* buf, size_t num_bytes) override {
size_t count = file_->Read(buf, num_bytes);
pos_ += count;
return count;
}
bool SeekForward(uint32_t num_bytes) override {
bool success = file_->SeekRelative(num_bytes);
if (success) {
pos_ += num_bytes;
}
return success;
}
int64_t GetPosition() override { return pos_; }
private:
FileWrapper* file_;
int64_t pos_ = 0;
};
constexpr size_t kMaxChunksize = 4096;
} // namespace
WavReader::WavReader(absl::string_view filename)
: WavReader(FileWrapper::OpenReadOnly(filename)) {}
WavReader::WavReader(FileWrapper file) : file_(std::move(file)) {
RTC_CHECK(file_.is_open())
<< "Invalid file. Could not create file handle for wav file.";
WavHeaderFileReader readable(&file_);
size_t bytes_per_sample;
RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format_,
&bytes_per_sample, &num_samples_in_file_,
&data_start_pos_));
num_unread_samples_ = num_samples_in_file_;
RTC_CHECK(FormatSupported(format_)) << "Non-implemented wav-format";
}
void WavReader::Reset() {
RTC_CHECK(file_.SeekTo(data_start_pos_))
<< "Failed to set position in the file to WAV data start position";
num_unread_samples_ = num_samples_in_file_;
}
size_t WavReader::ReadSamples(const size_t num_samples,
int16_t* const samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
size_t num_samples_left_to_read = num_samples;
size_t next_chunk_start = 0;
while (num_samples_left_to_read > 0 && num_unread_samples_ > 0) {
const size_t chunk_size = std::min(
std::min(kMaxChunksize, num_samples_left_to_read), num_unread_samples_);
size_t num_bytes_read;
size_t num_samples_read;
if (format_ == WavFormat::kWavFormatIeeeFloat) {
std::array<float, kMaxChunksize> samples_to_convert;
num_bytes_read = file_.Read(samples_to_convert.data(),
chunk_size * sizeof(samples_to_convert[0]));
num_samples_read = num_bytes_read / sizeof(samples_to_convert[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] = FloatToS16(samples_to_convert[j]);
}
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatPcm);
num_bytes_read = file_.Read(&samples[next_chunk_start],
chunk_size * sizeof(samples[0]));
num_samples_read = num_bytes_read / sizeof(samples[0]);
}
RTC_CHECK(num_samples_read == 0 || (num_bytes_read % num_samples_read) == 0)
<< "Corrupt file: file ended in the middle of a sample.";
RTC_CHECK(num_samples_read == chunk_size || file_.ReadEof())
<< "Corrupt file: payload size does not match header.";
next_chunk_start += num_samples_read;
num_unread_samples_ -= num_samples_read;
num_samples_left_to_read -= num_samples_read;
}
return num_samples - num_samples_left_to_read;
}
size_t WavReader::ReadSamples(const size_t num_samples, float* const samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
size_t num_samples_left_to_read = num_samples;
size_t next_chunk_start = 0;
while (num_samples_left_to_read > 0 && num_unread_samples_ > 0) {
const size_t chunk_size = std::min(
std::min(kMaxChunksize, num_samples_left_to_read), num_unread_samples_);
size_t num_bytes_read;
size_t num_samples_read;
if (format_ == WavFormat::kWavFormatPcm) {
std::array<int16_t, kMaxChunksize> samples_to_convert;
num_bytes_read = file_.Read(samples_to_convert.data(),
chunk_size * sizeof(samples_to_convert[0]));
num_samples_read = num_bytes_read / sizeof(samples_to_convert[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] =
static_cast<float>(samples_to_convert[j]);
}
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
num_bytes_read = file_.Read(&samples[next_chunk_start],
chunk_size * sizeof(samples[0]));
num_samples_read = num_bytes_read / sizeof(samples[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] =
FloatToFloatS16(samples[next_chunk_start + j]);
}
}
RTC_CHECK(num_samples_read == 0 || (num_bytes_read % num_samples_read) == 0)
<< "Corrupt file: file ended in the middle of a sample.";
RTC_CHECK(num_samples_read == chunk_size || file_.ReadEof())
<< "Corrupt file: payload size does not match header.";
next_chunk_start += num_samples_read;
num_unread_samples_ -= num_samples_read;
num_samples_left_to_read -= num_samples_read;
}
return num_samples - num_samples_left_to_read;
}
void WavReader::Close() {
file_.Close();
}
WavWriter::WavWriter(absl::string_view filename,
int sample_rate,
size_t num_channels,
SampleFormat sample_format)
// Unlike plain fopen, OpenWriteOnly takes care of filename utf8 ->
// wchar conversion on windows.
: WavWriter(FileWrapper::OpenWriteOnly(filename),
sample_rate,
num_channels,
sample_format) {}
WavWriter::WavWriter(FileWrapper file,
int sample_rate,
size_t num_channels,
SampleFormat sample_format)
: sample_rate_(sample_rate),
num_channels_(num_channels),
num_samples_written_(0),
format_(sample_format == SampleFormat::kInt16
? WavFormat::kWavFormatPcm
: WavFormat::kWavFormatIeeeFloat),
file_(std::move(file)) {
// Handle errors from the OpenWriteOnly call in above constructor.
RTC_CHECK(file_.is_open()) << "Invalid file. Could not create wav file.";
RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, format_,
num_samples_written_));
// Write a blank placeholder header, since we need to know the total number
// of samples before we can fill in the real data.
static const uint8_t blank_header[MaxWavHeaderSize()] = {0};
RTC_CHECK(file_.Write(blank_header, WavHeaderSize(format_)));
}
void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file"
#endif
for (size_t i = 0; i < num_samples; i += kMaxChunksize) {
const size_t num_remaining_samples = num_samples - i;
const size_t num_samples_to_write =
std::min(kMaxChunksize, num_remaining_samples);
if (format_ == WavFormat::kWavFormatPcm) {
RTC_CHECK(
file_.Write(&samples[i], num_samples_to_write * sizeof(samples[0])));
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
std::array<float, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = S16ToFloat(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
}
num_samples_written_ += num_samples_to_write;
RTC_CHECK_GE(num_samples_written_,
num_samples_to_write); // detect size_t overflow
}
}
void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file"
#endif
for (size_t i = 0; i < num_samples; i += kMaxChunksize) {
const size_t num_remaining_samples = num_samples - i;
const size_t num_samples_to_write =
std::min(kMaxChunksize, num_remaining_samples);
if (format_ == WavFormat::kWavFormatPcm) {
std::array<int16_t, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = FloatS16ToS16(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
std::array<float, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = FloatS16ToFloat(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
}
num_samples_written_ += num_samples_to_write;
RTC_CHECK(num_samples_written_ >=
num_samples_to_write); // detect size_t overflow
}
}
void WavWriter::Close() {
RTC_CHECK(file_.Rewind());
std::array<uint8_t, MaxWavHeaderSize()> header;
size_t header_size;
WriteWavHeader(num_channels_, sample_rate_, format_, num_samples_written_,
header.data(), &header_size);
RTC_CHECK(file_.Write(header.data(), header_size));
RTC_CHECK(file_.Close());
}
} // namespace webrtc

View File

@ -0,0 +1,115 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef COMMON_AUDIO_WAV_FILE_H_
#define COMMON_AUDIO_WAV_FILE_H_
#include <stdint.h>
#include <cstddef>
#include <string>
#include "common_audio/wav_header.h"
#include "rtc_base/system/file_wrapper.h"
namespace webrtc {
// Interface to provide access WAV file parameters.
class WavFile {
public:
enum class SampleFormat { kInt16, kFloat };
virtual ~WavFile() {}
virtual int sample_rate() const = 0;
virtual size_t num_channels() const = 0;
virtual size_t num_samples() const = 0;
};
// Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV
// files. All error handling is by calls to RTC_CHECK(), making it unsuitable
// for anything but debug code.
class WavWriter final : public WavFile {
public:
// Opens a new WAV file for writing.
WavWriter(absl::string_view filename,
int sample_rate,
size_t num_channels,
SampleFormat sample_format = SampleFormat::kInt16);
WavWriter(FileWrapper file,
int sample_rate,
size_t num_channels,
SampleFormat sample_format = SampleFormat::kInt16);
// Closes the WAV file, after writing its header.
~WavWriter() { Close(); }
WavWriter(const WavWriter&) = delete;
WavWriter& operator=(const WavWriter&) = delete;
// Write additional samples to the file. Each sample is in the range
// [-32768.0,32767.0], and there must be the previously specified number of
// interleaved channels.
void WriteSamples(const float* samples, size_t num_samples);
void WriteSamples(const int16_t* samples, size_t num_samples);
int sample_rate() const override { return sample_rate_; }
size_t num_channels() const override { return num_channels_; }
size_t num_samples() const override { return num_samples_written_; }
private:
void Close();
const int sample_rate_;
const size_t num_channels_;
size_t num_samples_written_;
WavFormat format_;
FileWrapper file_;
};
// Follows the conventions of WavWriter.
class WavReader final : public WavFile {
public:
// Opens an existing WAV file for reading.
explicit WavReader(absl::string_view filename);
explicit WavReader(FileWrapper file);
// Close the WAV file.
~WavReader() { Close(); }
WavReader(const WavReader&) = delete;
WavReader& operator=(const WavReader&) = delete;
// Resets position to the beginning of the file.
void Reset();
// Returns the number of samples read. If this is less than requested,
// verifies that the end of the file was reached.
size_t ReadSamples(size_t num_samples, float* samples);
size_t ReadSamples(size_t num_samples, int16_t* samples);
int sample_rate() const override { return sample_rate_; }
size_t num_channels() const override { return num_channels_; }
size_t num_samples() const override { return num_samples_in_file_; }
private:
void Close();
int sample_rate_;
size_t num_channels_;
WavFormat format_;
size_t num_samples_in_file_;
size_t num_unread_samples_;
FileWrapper file_;
int64_t
data_start_pos_; // Position in the file immediately after WAV header.
};
} // namespace webrtc
#endif // COMMON_AUDIO_WAV_FILE_H_

View File

@ -0,0 +1,435 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Based on the WAV file format documentation at
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ and
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
#include "common_audio/wav_header.h"
#include <cstring>
#include <limits>
#include <string>
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/sanitizer.h"
#include "rtc_base/system/arch.h"
namespace webrtc {
namespace {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Code not working properly for big endian platforms."
#endif
#pragma pack(2)
struct ChunkHeader {
uint32_t ID;
uint32_t Size;
};
static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size");
#pragma pack(2)
struct RiffHeader {
ChunkHeader header;
uint32_t Format;
};
static_assert(sizeof(RiffHeader) == sizeof(ChunkHeader) + 4, "RiffHeader size");
// We can't nest this definition in WavHeader, because VS2013 gives an error
// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
#pragma pack(2)
struct FmtPcmSubchunk {
ChunkHeader header;
uint16_t AudioFormat;
uint16_t NumChannels;
uint32_t SampleRate;
uint32_t ByteRate;
uint16_t BlockAlign;
uint16_t BitsPerSample;
};
static_assert(sizeof(FmtPcmSubchunk) == 24, "FmtPcmSubchunk size");
const uint32_t kFmtPcmSubchunkSize =
sizeof(FmtPcmSubchunk) - sizeof(ChunkHeader);
// Pack struct to avoid additional padding bytes.
#pragma pack(2)
struct FmtIeeeFloatSubchunk {
ChunkHeader header;
uint16_t AudioFormat;
uint16_t NumChannels;
uint32_t SampleRate;
uint32_t ByteRate;
uint16_t BlockAlign;
uint16_t BitsPerSample;
uint16_t ExtensionSize;
};
static_assert(sizeof(FmtIeeeFloatSubchunk) == 26, "FmtIeeeFloatSubchunk size");
const uint32_t kFmtIeeeFloatSubchunkSize =
sizeof(FmtIeeeFloatSubchunk) - sizeof(ChunkHeader);
// Simple PCM wav header. It does not include chunks that are not essential to
// read audio samples.
#pragma pack(2)
struct WavHeaderPcm {
RiffHeader riff;
FmtPcmSubchunk fmt;
struct {
ChunkHeader header;
} data;
};
static_assert(sizeof(WavHeaderPcm) == kPcmWavHeaderSize,
"no padding in header");
// IEEE Float Wav header, includes extra chunks necessary for proper non-PCM
// WAV implementation.
#pragma pack(2)
struct WavHeaderIeeeFloat {
RiffHeader riff;
FmtIeeeFloatSubchunk fmt;
struct {
ChunkHeader header;
uint32_t SampleLength;
} fact;
struct {
ChunkHeader header;
} data;
};
static_assert(sizeof(WavHeaderIeeeFloat) == kIeeeFloatWavHeaderSize,
"no padding in header");
uint32_t PackFourCC(char a, char b, char c, char d) {
uint32_t packed_value =
static_cast<uint32_t>(a) | static_cast<uint32_t>(b) << 8 |
static_cast<uint32_t>(c) << 16 | static_cast<uint32_t>(d) << 24;
return packed_value;
}
std::string ReadFourCC(uint32_t x) {
return std::string(reinterpret_cast<char*>(&x), 4);
}
uint16_t MapWavFormatToHeaderField(WavFormat format) {
switch (format) {
case WavFormat::kWavFormatPcm:
return 1;
case WavFormat::kWavFormatIeeeFloat:
return 3;
case WavFormat::kWavFormatALaw:
return 6;
case WavFormat::kWavFormatMuLaw:
return 7;
}
RTC_CHECK_NOTREACHED();
}
WavFormat MapHeaderFieldToWavFormat(uint16_t format_header_value) {
if (format_header_value == 1) {
return WavFormat::kWavFormatPcm;
}
if (format_header_value == 3) {
return WavFormat::kWavFormatIeeeFloat;
}
RTC_CHECK(false) << "Unsupported WAV format";
}
uint32_t RiffChunkSize(size_t bytes_in_payload, size_t header_size) {
return static_cast<uint32_t>(bytes_in_payload + header_size -
sizeof(ChunkHeader));
}
uint32_t ByteRate(size_t num_channels,
int sample_rate,
size_t bytes_per_sample) {
return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample);
}
uint16_t BlockAlign(size_t num_channels, size_t bytes_per_sample) {
return static_cast<uint16_t>(num_channels * bytes_per_sample);
}
// Finds a chunk having the sought ID. If found, then `readable` points to the
// first byte of the sought chunk data. If not found, the end of the file is
// reached.
bool FindWaveChunk(ChunkHeader* chunk_header,
WavHeaderReader* readable,
const std::string sought_chunk_id) {
RTC_DCHECK_EQ(sought_chunk_id.size(), 4);
while (true) {
if (readable->Read(chunk_header, sizeof(*chunk_header)) !=
sizeof(*chunk_header))
return false; // EOF.
if (ReadFourCC(chunk_header->ID) == sought_chunk_id)
return true; // Sought chunk found.
// Ignore current chunk by skipping its payload.
if (!readable->SeekForward(chunk_header->Size))
return false; // EOF or error.
}
}
bool ReadFmtChunkData(FmtPcmSubchunk* fmt_subchunk, WavHeaderReader* readable) {
// Reads "fmt " chunk payload.
if (readable->Read(&(fmt_subchunk->AudioFormat), kFmtPcmSubchunkSize) !=
kFmtPcmSubchunkSize)
return false;
const uint32_t fmt_size = fmt_subchunk->header.Size;
if (fmt_size != kFmtPcmSubchunkSize) {
// There is an optional two-byte extension field permitted to be present
// with PCM, but which must be zero.
int16_t ext_size;
if (kFmtPcmSubchunkSize + sizeof(ext_size) != fmt_size)
return false;
if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
return false;
if (ext_size != 0)
return false;
}
return true;
}
void WritePcmWavHeader(size_t num_channels,
int sample_rate,
size_t bytes_per_sample,
size_t num_samples,
uint8_t* buf,
size_t* header_size) {
RTC_CHECK(buf);
RTC_CHECK(header_size);
*header_size = kPcmWavHeaderSize;
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
const size_t bytes_in_payload = bytes_per_sample * num_samples;
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
header.fmt.header.Size = kFmtPcmSubchunkSize;
header.fmt.AudioFormat = MapWavFormatToHeaderField(WavFormat::kWavFormatPcm);
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
header.fmt.SampleRate = sample_rate;
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
// Do an extra copy rather than writing everything to buf directly, since buf
// might not be correctly aligned.
memcpy(buf, &header, *header_size);
}
void WriteIeeeFloatWavHeader(size_t num_channels,
int sample_rate,
size_t bytes_per_sample,
size_t num_samples,
uint8_t* buf,
size_t* header_size) {
RTC_CHECK(buf);
RTC_CHECK(header_size);
*header_size = kIeeeFloatWavHeaderSize;
auto header = rtc::MsanUninitialized<WavHeaderIeeeFloat>({});
const size_t bytes_in_payload = bytes_per_sample * num_samples;
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
header.fmt.header.Size = kFmtIeeeFloatSubchunkSize;
header.fmt.AudioFormat =
MapWavFormatToHeaderField(WavFormat::kWavFormatIeeeFloat);
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
header.fmt.SampleRate = sample_rate;
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
header.fmt.ExtensionSize = 0;
header.fact.header.ID = PackFourCC('f', 'a', 'c', 't');
header.fact.header.Size = 4;
header.fact.SampleLength = static_cast<uint32_t>(num_channels * num_samples);
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
// Do an extra copy rather than writing everything to buf directly, since buf
// might not be correctly aligned.
memcpy(buf, &header, *header_size);
}
// Returns the number of bytes per sample for the format.
size_t GetFormatBytesPerSample(WavFormat format) {
switch (format) {
case WavFormat::kWavFormatPcm:
// Other values may be OK, but for now we're conservative.
return 2;
case WavFormat::kWavFormatALaw:
case WavFormat::kWavFormatMuLaw:
return 1;
case WavFormat::kWavFormatIeeeFloat:
return 4;
}
RTC_CHECK_NOTREACHED();
}
bool CheckWavParameters(size_t num_channels,
int sample_rate,
WavFormat format,
size_t bytes_per_sample,
size_t num_samples) {
// num_channels, sample_rate, and bytes_per_sample must be positive, must fit
// in their respective fields, and their product must fit in the 32-bit
// ByteRate field.
if (num_channels == 0 || sample_rate <= 0 || bytes_per_sample == 0)
return false;
if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
return false;
if (num_channels > std::numeric_limits<uint16_t>::max())
return false;
if (static_cast<uint64_t>(bytes_per_sample) * 8 >
std::numeric_limits<uint16_t>::max())
return false;
if (static_cast<uint64_t>(sample_rate) * num_channels * bytes_per_sample >
std::numeric_limits<uint32_t>::max())
return false;
// format and bytes_per_sample must agree.
switch (format) {
case WavFormat::kWavFormatPcm:
// Other values may be OK, but for now we're conservative:
if (bytes_per_sample != 1 && bytes_per_sample != 2)
return false;
break;
case WavFormat::kWavFormatALaw:
case WavFormat::kWavFormatMuLaw:
if (bytes_per_sample != 1)
return false;
break;
case WavFormat::kWavFormatIeeeFloat:
if (bytes_per_sample != 4)
return false;
break;
default:
return false;
}
// The number of bytes in the file, not counting the first ChunkHeader, must
// be less than 2^32; otherwise, the ChunkSize field overflows.
const size_t header_size = kPcmWavHeaderSize - sizeof(ChunkHeader);
const size_t max_samples =
(std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample;
if (num_samples > max_samples)
return false;
// Each channel must have the same number of samples.
if (num_samples % num_channels != 0)
return false;
return true;
}
} // namespace
bool CheckWavParameters(size_t num_channels,
int sample_rate,
WavFormat format,
size_t num_samples) {
return CheckWavParameters(num_channels, sample_rate, format,
GetFormatBytesPerSample(format), num_samples);
}
void WriteWavHeader(size_t num_channels,
int sample_rate,
WavFormat format,
size_t num_samples,
uint8_t* buf,
size_t* header_size) {
RTC_CHECK(buf);
RTC_CHECK(header_size);
const size_t bytes_per_sample = GetFormatBytesPerSample(format);
RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format,
bytes_per_sample, num_samples));
if (format == WavFormat::kWavFormatPcm) {
WritePcmWavHeader(num_channels, sample_rate, bytes_per_sample, num_samples,
buf, header_size);
} else {
RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
WriteIeeeFloatWavHeader(num_channels, sample_rate, bytes_per_sample,
num_samples, buf, header_size);
}
}
bool ReadWavHeader(WavHeaderReader* readable,
size_t* num_channels,
int* sample_rate,
WavFormat* format,
size_t* bytes_per_sample,
size_t* num_samples,
int64_t* data_start_pos) {
// Read using the PCM header, even though it might be float Wav file
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
// Read RIFF chunk.
if (readable->Read(&header.riff, sizeof(header.riff)) != sizeof(header.riff))
return false;
if (ReadFourCC(header.riff.header.ID) != "RIFF")
return false;
if (ReadFourCC(header.riff.Format) != "WAVE")
return false;
// Find "fmt " and "data" chunks. While the official Wave file specification
// does not put requirements on the chunks order, it is uncommon to find the
// "data" chunk before the "fmt " one. The code below fails if this is not the
// case.
if (!FindWaveChunk(&header.fmt.header, readable, "fmt ")) {
RTC_LOG(LS_ERROR) << "Cannot find 'fmt ' chunk.";
return false;
}
if (!ReadFmtChunkData(&header.fmt, readable)) {
RTC_LOG(LS_ERROR) << "Cannot read 'fmt ' chunk.";
return false;
}
if (!FindWaveChunk(&header.data.header, readable, "data")) {
RTC_LOG(LS_ERROR) << "Cannot find 'data' chunk.";
return false;
}
// Parse needed fields.
*format = MapHeaderFieldToWavFormat(header.fmt.AudioFormat);
*num_channels = header.fmt.NumChannels;
*sample_rate = header.fmt.SampleRate;
*bytes_per_sample = header.fmt.BitsPerSample / 8;
const size_t bytes_in_payload = header.data.header.Size;
if (*bytes_per_sample == 0)
return false;
*num_samples = bytes_in_payload / *bytes_per_sample;
const size_t header_size = *format == WavFormat::kWavFormatPcm
? kPcmWavHeaderSize
: kIeeeFloatWavHeaderSize;
if (header.riff.header.Size < RiffChunkSize(bytes_in_payload, header_size))
return false;
if (header.fmt.ByteRate !=
ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
return false;
if (header.fmt.BlockAlign != BlockAlign(*num_channels, *bytes_per_sample))
return false;
if (!CheckWavParameters(*num_channels, *sample_rate, *format,
*bytes_per_sample, *num_samples)) {
return false;
}
*data_start_pos = readable->GetPosition();
return true;
}
} // namespace webrtc

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef COMMON_AUDIO_WAV_HEADER_H_
#define COMMON_AUDIO_WAV_HEADER_H_
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include "rtc_base/checks.h"
namespace webrtc {
// Interface providing header reading functionality.
class WavHeaderReader {
public:
// Returns the number of bytes read.
virtual size_t Read(void* buf, size_t num_bytes) = 0;
virtual bool SeekForward(uint32_t num_bytes) = 0;
virtual ~WavHeaderReader() = default;
virtual int64_t GetPosition() = 0;
};
// Possible WAV formats.
enum class WavFormat {
kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample.
kWavFormatIeeeFloat = 3, // IEEE float.
kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law.
kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law.
};
// Header sizes for supported WAV formats.
constexpr size_t kPcmWavHeaderSize = 44;
constexpr size_t kIeeeFloatWavHeaderSize = 58;
// Returns the size of the WAV header for the specified format.
constexpr size_t WavHeaderSize(WavFormat format) {
if (format == WavFormat::kWavFormatPcm) {
return kPcmWavHeaderSize;
}
RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
return kIeeeFloatWavHeaderSize;
}
// Returns the maximum size of the supported WAV formats.
constexpr size_t MaxWavHeaderSize() {
return std::max(WavHeaderSize(WavFormat::kWavFormatPcm),
WavHeaderSize(WavFormat::kWavFormatIeeeFloat));
}
// Return true if the given parameters will make a well-formed WAV header.
bool CheckWavParameters(size_t num_channels,
int sample_rate,
WavFormat format,
size_t num_samples);
// Write a kWavHeaderSize bytes long WAV header to buf. The payload that
// follows the header is supposed to have the specified number of interleaved
// channels and contain the specified total number of samples of the specified
// type. The size of the header is returned in header_size. CHECKs the input
// parameters for validity.
void WriteWavHeader(size_t num_channels,
int sample_rate,
WavFormat format,
size_t num_samples,
uint8_t* buf,
size_t* header_size);
// Read a WAV header from an implemented WavHeaderReader and parse the values
// into the provided output parameters. WavHeaderReader is used because the
// header can be variably sized. Returns false if the header is invalid.
bool ReadWavHeader(WavHeaderReader* readable,
size_t* num_channels,
int* sample_rate,
WavFormat* format,
size_t* bytes_per_sample,
size_t* num_samples,
int64_t* data_start_pos);
} // namespace webrtc
#endif // COMMON_AUDIO_WAV_HEADER_H_

View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/system/file_wrapper.h"
#include <stddef.h>
#include <cerrno>
#include <cstdint>
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#ifdef _WIN32
#include <Windows.h>
#else
#endif
#include <utility>
namespace webrtc {
namespace {
FILE* FileOpen(absl::string_view file_name_utf8, bool read_only, int* error) {
RTC_CHECK_EQ(file_name_utf8.find_first_of('\0'), absl::string_view::npos)
<< "Invalid filename, containing NUL character";
std::string file_name(file_name_utf8);
#if defined(_WIN32)
int len = MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, nullptr, 0);
std::wstring wstr(len, 0);
MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, &wstr[0], len);
FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
#else
FILE* file = fopen(file_name.c_str(), read_only ? "rb" : "wb");
#endif
if (!file && error) {
*error = errno;
}
return file;
}
} // namespace
// static
FileWrapper FileWrapper::OpenReadOnly(absl::string_view file_name_utf8) {
return FileWrapper(FileOpen(file_name_utf8, true, nullptr));
}
// static
FileWrapper FileWrapper::OpenWriteOnly(absl::string_view file_name_utf8,
int* error /*=nullptr*/) {
return FileWrapper(FileOpen(file_name_utf8, false, error));
}
FileWrapper::FileWrapper(FileWrapper&& other) {
operator=(std::move(other));
}
FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
Close();
file_ = other.file_;
other.file_ = nullptr;
return *this;
}
bool FileWrapper::SeekRelative(int64_t offset) {
RTC_DCHECK(file_);
return fseek(file_, rtc::checked_cast<long>(offset), SEEK_CUR) == 0;
}
bool FileWrapper::SeekTo(int64_t position) {
RTC_DCHECK(file_);
return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0;
}
absl::optional<size_t> FileWrapper::FileSize() {
if (file_ == nullptr)
return absl::nullopt;
long original_position = ftell(file_);
if (original_position < 0)
return absl::nullopt;
int seek_error = fseek(file_, 0, SEEK_END);
if (seek_error)
return absl::nullopt;
long file_size = ftell(file_);
seek_error = fseek(file_, original_position, SEEK_SET);
if (seek_error)
return absl::nullopt;
return rtc::checked_cast<size_t>(file_size);
}
bool FileWrapper::Flush() {
RTC_DCHECK(file_);
return fflush(file_) == 0;
}
size_t FileWrapper::Read(void* buf, size_t length) {
RTC_DCHECK(file_);
return fread(buf, 1, length, file_);
}
bool FileWrapper::ReadEof() const {
RTC_DCHECK(file_);
return feof(file_);
}
bool FileWrapper::Write(const void* buf, size_t length) {
RTC_DCHECK(file_);
return fwrite(buf, 1, length, file_) == length;
}
bool FileWrapper::Close() {
if (file_ == nullptr)
return true;
bool success = fclose(file_) == 0;
file_ = nullptr;
return success;
}
FILE* FileWrapper::Release() {
FILE* file = file_;
file_ = nullptr;
return file;
}
} // namespace webrtc

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
// Implementation that can read (exclusive) or write from/to a file.
namespace webrtc {
// This class is a thin wrapper around FILE*. It's main features are that it
// owns the FILE*, calling fclose on destruction, and that on windows, file
// names passed to the open methods are always treated as utf-8, regardless of
// system code page.
// Most of the methods return only a success/fail indication. When needed, an
// optional argument |int* error| should be added to all methods, in the same
// way as for the OpenWriteOnly methods.
class FileWrapper final {
public:
// Opens a file, in read or write mode. Use the is_open() method on the
// returned object to check if the open operation was successful. On failure,
// and if `error` is non-null, the system errno value is stored at |*error|.
// The file is closed by the destructor.
static FileWrapper OpenReadOnly(absl::string_view file_name_utf8);
static FileWrapper OpenWriteOnly(absl::string_view file_name_utf8,
int* error = nullptr);
FileWrapper() = default;
// Takes over ownership of `file`, closing it on destruction. Calling with
// null `file` is allowed, and results in a FileWrapper with is_open() false.
explicit FileWrapper(FILE* file) : file_(file) {}
~FileWrapper() { Close(); }
// Copying is not supported.
FileWrapper(const FileWrapper&) = delete;
FileWrapper& operator=(const FileWrapper&) = delete;
// Support for move semantics.
FileWrapper(FileWrapper&&);
FileWrapper& operator=(FileWrapper&&);
// Returns true if a file has been opened. If the file is not open, no methods
// but is_open and Close may be called.
bool is_open() const { return file_ != nullptr; }
// Closes the file, and implies Flush. Returns true on success, false if
// writing buffered data fails. On failure, the file is nevertheless closed.
// Calling Close on an already closed file does nothing and returns success.
bool Close();
// Releases and returns the wrapped file without closing it. This call passes
// the ownership of the file to the caller, and the wrapper is no longer
// responsible for closing it. Similarly the previously wrapped file is no
// longer available for the wrapper to use in any aspect.
FILE* Release();
// Write any buffered data to the underlying file. Returns true on success,
// false on write error. Note: Flushing when closing, is not required.
bool Flush();
// Seeks to the beginning of file. Returns true on success, false on failure,
// e.g., if the underlying file isn't seekable.
bool Rewind() { return SeekTo(0); }
// TODO(nisse): The seek functions are used only by the WavReader. If that
// code is demoted to test code, seek functions can be deleted from this
// utility.
// Seek relative to current file position.
bool SeekRelative(int64_t offset);
// Seek to given position.
bool SeekTo(int64_t position);
// Returns the file size or -1 if a size could not be determined.
// (A file size might not exists for non-seekable files or file-like
// objects, for example /dev/tty on unix.)
absl::optional<size_t> FileSize();
// Returns number of bytes read. Short count indicates EOF or error.
size_t Read(void* buf, size_t length);
// If the most recent Read() returned a short count, this methods returns true
// if the short count was due to EOF, and false it it was due to some i/o
// error.
bool ReadEof() const;
// Returns true if all data was successfully written (or buffered), or false
// if there was an error. Writing buffered data can fail later, and is
// reported with return value from Flush or Close.
bool Write(const void* buf, size_t length);
private:
FILE* file_ = nullptr;
};
} // namespace webrtc
#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_

63
rkap/include/RKAP_3A.h Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2020, Fuzhou Rockchip Electronics Co., Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Fuzhou Rockchip Electronics Co., Ltd. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __RKAP_3A_H__
#define __RKAP_3A_H__
#include "RKAP_Common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct RKAP_AEC_State_S
{
/* Basic info */
int swSampleRate; /* 8k~48k */
int swFrameLen; /* frame time only 16ms|20ms */
const char *pathPara;
} RKAP_AEC_State;
typedef enum AecTransType
{
AEC_TX_TYPE = 0,
AEC_RX_TYPE
} RKAP_AEC_TRANS_ENUM;
extern RKAP_Handle RKAP_3A_Init(RKAP_AEC_State *st, RKAP_AEC_TRANS_ENUM transType);
extern void RKAP_3A_Destroy(RKAP_Handle handle);
extern int RKAP_3A_Process(RKAP_Handle handle, short *pfSigIn,
short *pfSigRef, short *pfSigOut);
extern void RKAP_3A_DumpVersion(void);
#ifdef __cplusplus
}
#endif
#endif /* __RKAP_3A_H__ */

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2020, Fuzhou Rockchip Electronics Co., Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Fuzhou Rockchip Electronics Co., Ltd. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __RKAP_COMMON_H__
#define __RKAP_COMMON_H__
typedef void *RKAP_Handle;
enum
{
/* ANR Requests */
AP_ANR_REQ_SET = 0x0,
AP_ANR_REQ_GET,
AP_ANR_REQ_RATE,
AP_ANR_REQ_FRMLEN,
AP_ANR_REQ_BAND_NUM,
AP_ANR_REQ_NOISE_FACTOR,
AP_ANR_REQ_SWIN_NUM,
AP_ANR_REQ_G_MIN,
/* AEC Requests */
AP_AEC_REQ_BASE,
};
#endif /* __RKAP_COMMON_H__ */

BIN
rkap/lib/libRKAP_3A.so Normal file

Binary file not shown.

BIN
rkap/lib/libRKAP_Common.so Normal file

Binary file not shown.