forked from TaoistKing/AudioPlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_plc.cc
156 lines (131 loc) · 4.4 KB
/
test_plc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include "common_audio/signal_processing/include/signal_processing_library.h"
#include "neteq/expand.h"
#include "neteq/merge.h"
#include "neteq/audio_multi_vector.h"
#include "neteq/sync_buffer.h"
#include "neteq/background_noise.h"
#include "neteq/random_vector.h"
#include "neteq/post_decode_vad.h"
static const int kInputSizeMs = 50;
static const int kOutputSizeMs = 30;
static const size_t kMaxFrameSize = 5760; // 120 ms @ 48 kHz.
static const size_t kSyncBufferSize = kMaxFrameSize + 60 * 48;
#define kMode_Normal 0
#define kMode_Expand 1
#define kMode_Merge 2
using namespace webrtc;
int main(){
printf("hello! plc!\n");
//test signal processing lib
int16_t input[16] = {100};
WebRtcSpl_ComplexBitReverse(input, 3);
int ret = WebRtcSpl_ComplexFFT(input, 3, 1);
printf("fft result: %d\n", ret);
//test plc
int fs_hz = 48000;
size_t channels = 1;
std::unique_ptr<BackgroundNoise> background_noise_;
std::unique_ptr<AudioMultiVector> algorithm_buffer_;
std::unique_ptr<SyncBuffer> sync_buffer_;
std::unique_ptr<Expand> expand_;
std::unique_ptr<ExpandFactory> expand_factory_;
std::unique_ptr<Merge> merge_;
RandomVector random_vector_;
std::unique_ptr<PostDecodeVad> vad_;
int fs_mult_ = fs_hz / 8000;
int samples_10ms = static_cast<size_t>(10 * 8 * fs_mult_);
int input_size_samples_ = static_cast<size_t>(kInputSizeMs * 8 * fs_mult_);
int output_size_samples_ = static_cast<size_t>(kOutputSizeMs * 8 * fs_mult_);
int decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
// Reinit post-decode VAD with new sample rate.
vad_.reset(new PostDecodeVad());
vad_->Init();
// Delete algorithm buffer and create a new one.
algorithm_buffer_.reset(new AudioMultiVector(channels));
// Delete sync buffer and create a new one.
sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
// Delete BackgroundNoise object and create a new one.
background_noise_.reset(new BackgroundNoise(channels));
// Reset random vector.
random_vector_.Reset();
expand_factory_.reset(new ExpandFactory);
expand_.reset(expand_factory_->Create(background_noise_.get(),
sync_buffer_.get(), &random_vector_,
fs_hz, channels));
merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
// Move index so that we create a small set of future samples (all 0).
sync_buffer_->set_next_index(sync_buffer_->next_index() -
expand_->overlap_length());
FILE *pcm = fopen("48.pcm", "rb");
if(pcm == NULL){
printf("open pcm file failed!\n");
return 0;
}
FILE *outfile = fopen("plc.pcm", "wb");
AudioFrame frame;
AudioMultiVector *mv = algorithm_buffer_.get();
AudioVector &v = (*mv)[0];
int16_t buf[480];
int read;
int last_mode = kMode_Normal;
int count = 0;
while(!feof(pcm)){
read = fread(buf, sizeof(int16_t), samples_10ms, pcm);
if(read != samples_10ms){
printf("read: %d\n", read);
break;
}
count++;
int lost = (count % 10 == 0);
//int lost = 0;
if(!lost){
printf("not lost\n");
if(last_mode == kMode_Expand){
//merge
merge_->Process(buf, samples_10ms, algorithm_buffer_.get());
last_mode = kMode_Merge;
expand_->Reset();
}else{
//normal_->Process(buf, samples_10ms, last_mode, algorithm_buffer_.get());
//normal
v.PushBack(buf, samples_10ms);
last_mode = kMode_Normal;
}
sync_buffer_->PushBack(*algorithm_buffer_);
algorithm_buffer_->Clear();
}else{
printf("lost\n");
//lost. do plc and get 10 ms data
while(sync_buffer_->FutureLength() - expand_->overlap_length() < samples_10ms){
algorithm_buffer_->Clear();
int return_value = expand_->Process(algorithm_buffer_.get());
size_t length = algorithm_buffer_->Size();
if (return_value < 0) {
printf("expand return %d, length:%lu\n", return_value, length);
break;
}
if(expand_->MuteFactor(0) == 0){
printf("only noise generated\n");
}else{
printf("more than noise generated\n");
}
sync_buffer_->PushBack(*algorithm_buffer_);
last_mode = kMode_Expand;
algorithm_buffer_->Clear();
printf("adding %zu plc samples\n", length);
}
}
if(sync_buffer_->FutureLength() > samples_10ms){
sync_buffer_->GetNextAudioInterleaved(samples_10ms, &frame);
if(frame.data()){
fwrite(frame.data(), sizeof(int16_t), samples_10ms, outfile);
}
}
}
fflush(outfile);
fclose(outfile);
fclose(pcm);
printf("leaving application!\n");
return 0;
}