From bc723ea200144bd6fa8a5dff4b9a780feda144fc Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 29 六月 2023 18:55:01 +0800
Subject: [PATCH] dcos
---
funasr/runtime/onnxruntime/src/audio.cpp | 191 ++++++++++++++++++++++++++++++++++++-----------
1 files changed, 145 insertions(+), 46 deletions(-)
diff --git a/funasr/runtime/onnxruntime/src/audio.cpp b/funasr/runtime/onnxruntime/src/audio.cpp
index 635c330..23d0010 100644
--- a/funasr/runtime/onnxruntime/src/audio.cpp
+++ b/funasr/runtime/onnxruntime/src/audio.cpp
@@ -11,6 +11,7 @@
using namespace std;
+namespace funasr {
// see http://soundfile.sapp.org/doc/WaveFormat/
// Note: We assume little endian here
struct WaveHeader {
@@ -128,33 +129,32 @@
start = 0;
};
AudioFrame::~AudioFrame(){};
-int AudioFrame::set_start(int val)
+int AudioFrame::SetStart(int val)
{
start = val < 0 ? 0 : val;
return start;
};
-int AudioFrame::set_end(int val)
+int AudioFrame::SetEnd(int val)
{
end = val;
len = end - start;
return end;
};
-int AudioFrame::get_start()
+int AudioFrame::GetStart()
{
return start;
};
-int AudioFrame::get_len()
+int AudioFrame::GetLen()
{
return len;
};
-int AudioFrame::disp()
+int AudioFrame::Disp()
{
- printf("not imp!!!!\n");
-
+ LOG(ERROR) << "Not imp!!!!";
return 0;
};
@@ -176,42 +176,37 @@
{
if (speech_buff != NULL) {
free(speech_buff);
-
}
-
if (speech_data != NULL) {
-
free(speech_data);
}
+ if (speech_char != NULL) {
+ free(speech_char);
+ }
}
-void Audio::disp()
+void Audio::Disp()
{
- printf("Audio time is %f s. len is %d\n", (float)speech_len / MODEL_SAMPLE_RATE,
- speech_len);
+ LOG(INFO) << "Audio time is " << (float)speech_len / MODEL_SAMPLE_RATE << " s. len is " << speech_len;
}
-float Audio::get_time_len()
+float Audio::GetTimeLen()
{
return (float)speech_len / MODEL_SAMPLE_RATE;
}
-void Audio::wavResample(int32_t sampling_rate, const float *waveform,
+void Audio::WavResample(int32_t sampling_rate, const float *waveform,
int32_t n)
{
- printf(
- "Creating a resampler:\n"
- " in_sample_rate: %d\n"
- " output_sample_rate: %d\n",
- sampling_rate, static_cast<int32_t>(MODEL_SAMPLE_RATE));
+ LOG(INFO) << "Creating a resampler:\n"
+ << " in_sample_rate: "<< sampling_rate << "\n"
+ << " output_sample_rate: " << static_cast<int32_t>(MODEL_SAMPLE_RATE);
float min_freq =
std::min<int32_t>(sampling_rate, MODEL_SAMPLE_RATE);
float lowpass_cutoff = 0.99 * 0.5 * min_freq;
int32_t lowpass_filter_width = 6;
- //FIXME
- //auto resampler = new LinearResample(
- // sampling_rate, model_sample_rate, lowpass_cutoff, lowpass_filter_width);
+
auto resampler = std::make_unique<LinearResample>(
sampling_rate, MODEL_SAMPLE_RATE, lowpass_cutoff, lowpass_filter_width);
std::vector<float> samples;
@@ -226,7 +221,7 @@
copy(samples.begin(), samples.end(), speech_data);
}
-bool Audio::loadwav(const char *filename, int32_t* sampling_rate)
+bool Audio::LoadWav(const char *filename, int32_t* sampling_rate)
{
WaveHeader header;
if (speech_data != NULL) {
@@ -240,7 +235,25 @@
std::ifstream is(filename, std::ifstream::binary);
is.read(reinterpret_cast<char *>(&header), sizeof(header));
if(!is){
- fprintf(stderr, "Failed to read %s\n", filename);
+ LOG(ERROR) << "Failed to read " << filename;
+ return false;
+ }
+
+ if (!header.Validate()) {
+ return false;
+ }
+
+ header.SeekToDataChunk(is);
+ if (!is) {
+ return false;
+ }
+
+ if (!header.Validate()) {
+ return false;
+ }
+
+ header.SeekToDataChunk(is);
+ if (!is) {
return false;
}
@@ -255,7 +268,7 @@
memset(speech_buff, 0, sizeof(int16_t) * speech_len);
is.read(reinterpret_cast<char *>(speech_buff), header.subchunk2_size);
if (!is) {
- fprintf(stderr, "Failed to read %s\n", filename);
+ LOG(ERROR) << "Failed to read " << filename;
return false;
}
speech_data = (float*)malloc(sizeof(float) * speech_len);
@@ -271,7 +284,7 @@
//resample
if(*sampling_rate != MODEL_SAMPLE_RATE){
- wavResample(*sampling_rate, speech_data, speech_len);
+ WavResample(*sampling_rate, speech_data, speech_len);
}
AudioFrame* frame = new AudioFrame(speech_len);
@@ -283,8 +296,47 @@
return false;
}
-bool Audio::loadwav(const char* buf, int nFileLen, int32_t* sampling_rate)
+bool Audio::LoadWav2Char(const char *filename, int32_t* sampling_rate)
{
+ WaveHeader header;
+ if (speech_char != NULL) {
+ free(speech_char);
+ }
+ offset = 0;
+ std::ifstream is(filename, std::ifstream::binary);
+ is.read(reinterpret_cast<char *>(&header), sizeof(header));
+ if(!is){
+ LOG(ERROR) << "Failed to read " << filename;
+ return false;
+ }
+ if (!header.Validate()) {
+ return false;
+ }
+ header.SeekToDataChunk(is);
+ if (!is) {
+ return false;
+ }
+ if (!header.Validate()) {
+ return false;
+ }
+ header.SeekToDataChunk(is);
+ if (!is) {
+ return false;
+ }
+
+ *sampling_rate = header.sample_rate;
+ // header.subchunk2_size contains the number of bytes in the data.
+ // As we assume each sample contains two bytes, so it is divided by 2 here
+ speech_len = header.subchunk2_size / 2;
+ speech_char = (char *)malloc(header.subchunk2_size);
+ memset(speech_char, 0, header.subchunk2_size);
+ is.read(speech_char, header.subchunk2_size);
+
+ return true;
+}
+
+bool Audio::LoadWav(const char* buf, int n_file_len, int32_t* sampling_rate)
+{
WaveHeader header;
if (speech_data != NULL) {
free(speech_data);
@@ -318,7 +370,7 @@
//resample
if(*sampling_rate != MODEL_SAMPLE_RATE){
- wavResample(*sampling_rate, speech_data, speech_len);
+ WavResample(*sampling_rate, speech_data, speech_len);
}
AudioFrame* frame = new AudioFrame(speech_len);
@@ -330,7 +382,7 @@
return false;
}
-bool Audio::loadpcmwav(const char* buf, int nBufLen, int32_t* sampling_rate)
+bool Audio::LoadPcmwav(const char* buf, int n_buf_len, int32_t* sampling_rate)
{
if (speech_data != NULL) {
free(speech_data);
@@ -340,7 +392,7 @@
}
offset = 0;
- speech_len = nBufLen / 2;
+ speech_len = n_buf_len / 2;
speech_buff = (int16_t*)malloc(sizeof(int16_t) * speech_len);
if (speech_buff)
{
@@ -361,7 +413,7 @@
//resample
if(*sampling_rate != MODEL_SAMPLE_RATE){
- wavResample(*sampling_rate, speech_data, speech_len);
+ WavResample(*sampling_rate, speech_data, speech_len);
}
AudioFrame* frame = new AudioFrame(speech_len);
@@ -373,7 +425,7 @@
return false;
}
-bool Audio::loadpcmwav(const char* filename, int32_t* sampling_rate)
+bool Audio::LoadPcmwav(const char* filename, int32_t* sampling_rate)
{
if (speech_data != NULL) {
free(speech_data);
@@ -386,12 +438,15 @@
FILE* fp;
fp = fopen(filename, "rb");
if (fp == nullptr)
+ {
+ LOG(ERROR) << "Failed to read " << filename;
return false;
+ }
fseek(fp, 0, SEEK_END);
- uint32_t nFileLen = ftell(fp);
+ uint32_t n_file_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
- speech_len = (nFileLen) / 2;
+ speech_len = (n_file_len) / 2;
speech_buff = (int16_t*)malloc(sizeof(int16_t) * speech_len);
if (speech_buff)
{
@@ -412,7 +467,7 @@
//resample
if(*sampling_rate != MODEL_SAMPLE_RATE){
- wavResample(*sampling_rate, speech_data, speech_len);
+ WavResample(*sampling_rate, speech_data, speech_len);
}
AudioFrame* frame = new AudioFrame(speech_len);
@@ -425,7 +480,34 @@
}
-int Audio::fetch_chunck(float *&dout, int len)
+bool Audio::LoadPcmwav2Char(const char* filename, int32_t* sampling_rate)
+{
+ if (speech_char != NULL) {
+ free(speech_char);
+ }
+ offset = 0;
+
+ FILE* fp;
+ fp = fopen(filename, "rb");
+ if (fp == nullptr)
+ {
+ LOG(ERROR) << "Failed to read " << filename;
+ return false;
+ }
+ fseek(fp, 0, SEEK_END);
+ uint32_t n_file_len = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ speech_len = (n_file_len) / 2;
+ speech_char = (char *)malloc(n_file_len);
+ memset(speech_char, 0, n_file_len);
+ fread(speech_char, sizeof(int16_t), n_file_len/2, fp);
+ fclose(fp);
+
+ return true;
+}
+
+int Audio::FetchChunck(float *&dout, int len)
{
if (offset >= speech_align_len) {
dout = NULL;
@@ -446,14 +528,14 @@
}
}
-int Audio::fetch(float *&dout, int &len, int &flag)
+int Audio::Fetch(float *&dout, int &len, int &flag)
{
if (frame_queue.size() > 0) {
AudioFrame *frame = frame_queue.front();
frame_queue.pop();
- dout = speech_data + frame->get_start();
- len = frame->get_len();
+ dout = speech_data + frame->GetStart();
+ len = frame->GetLen();
delete frame;
flag = S_END;
return 1;
@@ -462,7 +544,7 @@
}
}
-void Audio::padding()
+void Audio::Padding()
{
float num_samples = speech_len;
float frame_length = 400;
@@ -499,27 +581,44 @@
delete frame;
}
-void Audio::split(Model* pRecogObj)
+void Audio::Split(OfflineStream* offline_stream)
{
AudioFrame *frame;
frame = frame_queue.front();
frame_queue.pop();
- int sp_len = frame->get_len();
+ int sp_len = frame->GetLen();
delete frame;
frame = NULL;
std::vector<float> pcm_data(speech_data, speech_data+sp_len);
- vector<std::vector<int>> vad_segments = pRecogObj->vad_seg(pcm_data);
+ vector<std::vector<int>> vad_segments = (offline_stream->vad_handle)->Infer(pcm_data);
int seg_sample = MODEL_SAMPLE_RATE/1000;
for(vector<int> segment:vad_segments)
{
frame = new AudioFrame();
int start = segment[0]*seg_sample;
int end = segment[1]*seg_sample;
- frame->set_start(start);
- frame->set_end(end);
+ frame->SetStart(start);
+ frame->SetEnd(end);
frame_queue.push(frame);
frame = NULL;
}
}
+
+
+void Audio::Split(VadModel* vad_obj, vector<std::vector<int>>& vad_segments, bool input_finished)
+{
+ AudioFrame *frame;
+
+ frame = frame_queue.front();
+ frame_queue.pop();
+ int sp_len = frame->GetLen();
+ delete frame;
+ frame = NULL;
+
+ std::vector<float> pcm_data(speech_data, speech_data+sp_len);
+ vad_segments = vad_obj->Infer(pcm_data, input_finished);
+}
+
+} // namespace funasr
\ No newline at end of file
--
Gitblit v1.9.1