-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEchoAudio.cpp
145 lines (122 loc) · 3.8 KB
/
EchoAudio.cpp
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
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <liquid/liquid.h>
#include "EchoAudio.h"
#include "Spectrum.h"
#include "gui_speech.h"
#include "PeakLevelDetector.h"
static shared_ptr<EchoAudio> sp_echo;
#define dB2mag(x) pow(10.0, (x) / 20.0)
bool EchoAudio::create_modulator(AudioOutput *audio_ouput, AudioInput *audio_input)
{
if (sp_echo != nullptr)
return false;
sp_echo = make_shared<EchoAudio>(audio_ouput, audio_input);
sp_echo->echo_thread = std::thread(&EchoAudio::operator(), sp_echo);
return true;
}
void EchoAudio::destroy_modulator()
{
if (sp_echo == nullptr)
return;
sp_echo->stop_flag = true;
sp_echo->echo_thread.join();
sp_echo.reset();
}
EchoAudio::EchoAudio(AudioOutput *audio_ouput, AudioInput *audio_input)
: Demodulator( audio_ouput, audio_input)
{
}
EchoAudio::~EchoAudio()
{
}
void EchoAudio::process(const IQSampleVector &samples_in, SampleVector &audio)
{
}
void EchoAudio::operator()()
{
const auto startTime = std::chrono::high_resolution_clock::now();
auto timeLastPrint = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point now, start1;
SampleVector audiosamples, audioframes,filter;
IQSampleVector buf_mod, buf_filter;
AudioProcessor Speech;
liquid_ampmodem_type am_mode{LIQUID_AMPMODEM_USB};
Speech.prepareToPlay(audio_output->get_samplerate());
Speech.setThresholdDB(gspeech.get_threshold());
Speech.setRatio(gspeech.get_ratio());
audioInputBuffer->clear();
if (gspeech.get_speech_mode())
audioInputBuffer->set_gain(20);
float mod_index = 0.99f; // modulation index (bandwidth)
ampmodem modAM = ampmodem_create(mod_index, am_mode, 1);
ampmodem demod = ampmodem_create(mod_index, am_mode, 1);
setBandPassFilter(2700.0f, 2000.0f, 500.0f, 150.0f);
while (!stop_flag.load())
{
if (!audioInputBuffer->read(audiosamples))
continue;
if (gspeech.get_speech_mode())
{
audioInputBuffer->set_gain(20);
Speech.setRelease(gspeech.get_release());
Speech.setRatio(gspeech.get_ratio());
Speech.setAtack(gspeech.get_atack());
Speech.setThresholdDB(gspeech.get_threshold());
Speech.processBlock(audiosamples);
}
else
audioInputBuffer->set_gain(0);
for (auto &col : audiosamples)
{
complex<float> f;
ampmodem_modulate(modAM, col, &f);
//printf("%f;%f;%f \n", col, f.real(), f.imag());
buf_mod.push_back(f);
}
audiosamples.clear();
executeBandpassFilter(buf_mod, buf_filter);
buf_mod.clear();
for (auto col : buf_filter)
{
float v;
ampmodem_demodulate(demod, (liquid_float_complex)col, &v);
audiosamples.push_back(v);
}
buf_filter.clear();
calc_af_level(audiosamples);
set_signal_strength();
audio_output->adjust_gain(audiosamples);
for (auto &col : audiosamples)
{
// split the stream in blocks of samples of the size framesize
audioframes.insert(audioframes.end(), col);
if (audioframes.size() == audio_output->get_framesize())
{
if ((audio_output->queued_samples() / 2) < 4096)
{
SampleVector audio_stereo;
mono_to_left_right(audioframes, audio_stereo);
audio_output->write(audio_stereo);
audioframes.clear();
}
else
audioframes.clear();
}
}
audiosamples.clear();
now = std::chrono::high_resolution_clock::now();
auto process_time1 = std::chrono::duration_cast<std::chrono::microseconds>(now - start1);
if (timeLastPrint + std::chrono::seconds(10) < now)
{
timeLastPrint = now;
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
printf("peak %f db gain %f db threshold %f ratio %f atack %f release %f\n", Speech.getPeak(), Speech.getGain(), Speech.getThreshold(), Speech.getRatio(), Speech.getAtack(), Speech.getRelease());
printf("rms %f \n", get_if_level());
}
}
ampmodem_destroy(modAM);
ampmodem_destroy(demod);
printf("exit EchoAudio\n");
}