FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
client-sdk-flutter/shared_cpp/audio_visualizer.cpp at main · livekit/client-sdk-flutter · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
livekit
/
client-sdk-flutter
Public
Notifications
You must be signed in to change notification settings
Fork
250
Star
416
Code
Issues
47
Pull requests
27
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
client-sdk-flutter
/
shared_cpp
/
audio_visualizer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
111 lines (91 loc) · 3.71 KB
Breadcrumbs
client-sdk-flutter
/
shared_cpp
/
audio_visualizer.cpp
Copy path
File metadata and controls
111 lines (91 loc) · 3.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
audio_visualizer.h
"
#
include
<
algorithm
>
#
include
<
chrono
>
double
CurrentTime
() {
return
static_cast
<
double
>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now
().
time_since_epoch
())
.
count
()) /
1000.0
;
}
int
magnitudeIndex
(std::vector<
float
> magnitudes,
float
frequency,
float
sampleRate) {
return
static_cast
<
int
>(
float
(magnitudes.
size
()) * frequency / sampleRate /
2
);
}
std::vector<
float
>
computeBands
(std::vector<
float
> magnitudes,
float
minFrequency,
float
maxFrequency,
int
bandsCount,
float
sampleRate) {
float
actualMaxFrequency =
std::min
(sampleRate /
2
, maxFrequency);
std::vector<
float
>
bandMagnitudes
(bandsCount,
0
.
0f
);
int
magLowerRange =
magnitudeIndex
(magnitudes, minFrequency, sampleRate);
int
magUpperRange =
magnitudeIndex
(magnitudes, actualMaxFrequency, sampleRate);
float
ratio =
float
(magUpperRange - magLowerRange) /
float
(bandsCount);
for
(
int
i =
0
; i < bandsCount; ++i) {
int
magsStartIdx =
static_cast
<
int
>(
floorf
(
float
(i) * ratio)) + magLowerRange;
int
magsEndIdx =
static_cast
<
int
>(
floorf
(
float
(i +
1
) * ratio)) + magLowerRange;
int
count = magsEndIdx - magsStartIdx;
if
(count >
0
) {
float
sum =
0
;
for
(
int
j = magsStartIdx; j < magsEndIdx; ++j) {
sum += magnitudes[j];
}
bandMagnitudes[i] = sum /
float
(count);
}
else
{
bandMagnitudes[i] = magnitudes[magsStartIdx];
}
}
return
bandMagnitudes;
}
//
/ Centers the sorted bands by placing higher values in the middle.
std::vector<
float
>
centerBands
(
const
std::vector<
float
> &sortedBands) {
std::vector<
float
>
centeredBands
(sortedBands.
size
(),
0
);
size_t
leftIndex = sortedBands.
size
() /
2
;
size_t
rightIndex = leftIndex;
for
(
size_t
index =
0
; index < sortedBands.
size
(); ++index) {
if
(index %
2
==
0
) {
//
Place value to the right
centeredBands[rightIndex] = sortedBands[index];
rightIndex +=
1
;
}
else
{
//
Place value to the left
leftIndex -=
1
;
centeredBands[leftIndex] = sortedBands[index];
}
}
return
centeredBands;
}
AudioVisualizer::AudioVisualizer
(
int
bands_count,
bool
is_centered,
double
smoothing_time_constant,
float
min_frequency,
float
max_frequency,
float
min_db,
float
max_db)
: bands_count_(bands_count), is_centered_(is_centered),
min_frequency_(min_frequency), max_frequency_(max_frequency),
min_db_(min_db), max_db_(max_db),
smoothing_time_constant_(smoothing_time_constant),
bands_(bands_count,
0
.
0f
),
fft_processor_(std::make_unique<FFTProcessor>(
FFTProcessor::
kDefaultFFTSize
, smoothing_time_constant_)) {}
AudioVisualizer::~AudioVisualizer
() {}
bool
AudioVisualizer::Process
(
const
int16_t
*audioData,
unsigned
int
numSamples,
float
sampleRate, std::vector<
float
> &output) {
fft_processor_->
WriteInput
(audioData, numSamples);
std::vector<
float
>
magnitudes
(FFTProcessor::
kDefaultFFTSize
/
2
,
0
.
0f
);
fft_processor_->
GetFloatFrequencyData
(magnitudes,
CurrentTime
());
auto
bands =
computeBands
(magnitudes, min_frequency_, max_frequency_,
bands_count_, sampleRate);
for
(
int
i =
0
; i < bands.
size
(); ++i) {
float
db =
1
.
0f
- (
fmax
(min_db_,
fmin
(max_db_, bands[i])) * -
1
.
0f
) /
100
.
0f
;
db =
std::sqrt
(db);
bands_[i] = db;
}
if
(is_centered_) {
std::sort
(bands_.
begin
(), bands_.
end
(), std::greater<
float
>());
bands_ =
centerBands
(bands_);
}
output = bands_;
return
true
;
}
Back
|
FazBrowse Home
|
New Git URL