FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
client-sdk-flutter/shared_cpp/audio_renderer.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_renderer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
160 lines (135 loc) · 5.42 KB
Breadcrumbs
client-sdk-flutter
/
shared_cpp
/
audio_renderer.cpp
Copy path
File metadata and controls
160 lines (135 loc) · 5.42 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
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
157
158
159
160
#
include
"
audio_renderer.h
"
#
include
<
algorithm
>
#
include
<
cstring
>
namespace
{
std::vector<
int16_t
>
UpsampleAudio
(
const
int16_t
*src,
int
src_frames,
int
out_frames,
int
channels) {
std::vector<
int16_t
>
out
(
static_cast
<
size_t
>(out_frames) * channels);
//
Edge case: a single source frame — just repeat it.
if
(src_frames <=
1
) {
for
(
int
f =
0
; f < out_frames; ++f) {
for
(
int
ch =
0
; ch < channels; ++ch) {
out[f * channels + ch] = src[ch];
}
}
return
out;
}
double
ratio =
static_cast
<
double
>(src_frames) /
static_cast
<
double
>(out_frames);
for
(
int
f =
0
; f < out_frames; ++f) {
double
src_pos = f * ratio;
int
idx =
std::min
(
static_cast
<
int
>(src_pos), src_frames -
2
);
float
frac =
static_cast
<
float
>(src_pos - idx);
for
(
int
ch =
0
; ch < channels; ++ch) {
int16_t
s0 = src[idx * channels + ch];
int16_t
s1 = src[(idx +
1
) * channels + ch];
int
sample =
static_cast
<
int
>(s0 + frac * (s1 - s0));
sample =
std::clamp
(sample,
static_cast
<
int
>(
INT16_MIN
),
static_cast
<
int
>(
INT16_MAX
));
out[f * channels + ch] =
static_cast
<
int16_t
>(sample);
}
}
return
out;
}
std::vector<
int16_t
>
DownsampleAudio
(
const
int16_t
*src,
int
src_frames,
int
out_frames,
int
src_rate,
int
target_rate,
int
channels) {
std::vector<
int16_t
>
out
(
static_cast
<
size_t
>(out_frames) * channels);
double
ratio =
static_cast
<
double
>(src_rate) /
static_cast
<
double
>(target_rate);
for
(
int
f =
0
; f < out_frames; ++f) {
int
src_start =
static_cast
<
int
>(f * ratio);
int
src_end =
std::min
(
static_cast
<
int
>((f +
1
) * ratio), src_frames);
for
(
int
ch =
0
; ch < channels; ++ch) {
int64_t
sum =
0
;
for
(
int
i = src_start; i < src_end; ++i) {
sum += src[i * channels + ch];
}
int
count = src_end - src_start;
out[f * channels + ch] =
count >
0
?
static_cast
<
int16_t
>(std::clamp<
int64_t
>(
sum / count,
INT16_MIN
,
INT16_MAX
))
:
0
;
}
}
return
out;
}
}
//
namespace
RendererAudioFormat
RendererAudioFormat::FromValues
(
const
std::string &common_format,
int
sample_rate,
int
channels) {
RendererAudioFormat format;
format.
common_format
= common_format.
empty
() ?
"
int16
"
: common_format;
format.
sample_rate
= sample_rate >
0
? sample_rate :
48000
;
format.
channels
= channels >
0
? channels :
1
;
return
format;
}
std::vector<
int16_t
>
ResampleAudio
(
const
int16_t
*src,
int
src_frames,
int
src_rate,
int
target_rate,
int
channels,
int
&out_frames) {
if
(src_rate == target_rate || src_frames <=
0
|| channels <=
0
) {
out_frames = src_frames;
return
std::vector<
int16_t
>(
src, src +
static_cast
<
size_t
>(
std::max
(src_frames,
0
)) * channels);
}
out_frames =
static_cast
<
int
>((
static_cast
<
int64_t
>(src_frames) *
target_rate) /
src_rate);
if
(out_frames <=
0
) {
out_frames =
0
;
return
{};
}
if
(target_rate > src_rate) {
return
UpsampleAudio
(src, src_frames, out_frames, channels);
}
return
DownsampleAudio
(src, src_frames, out_frames, src_rate, target_rate,
channels);
}
AudioConversionResult
ConvertAudioData
(
const
void
*audio_data,
int
bits_per_sample,
int
sample_rate,
size_t
number_of_channels,
size_t
number_of_frames,
const
RendererAudioFormat &target_format) {
AudioConversionResult result;
//
WebRTC AudioTrackSink always delivers 16-bit signed int16 PCM.
if
(bits_per_sample !=
16
|| number_of_channels ==
0
||
number_of_frames ==
0
) {
return
result;
}
int
channels =
static_cast
<
int
>(number_of_channels);
int
src_frames =
static_cast
<
int
>(number_of_frames);
const
int16_t
*src =
reinterpret_cast
<
const
int16_t
*>(audio_data);
int
out_frames =
0
;
std::vector<
int16_t
> resampled =
ResampleAudio
(
src, src_frames, sample_rate, target_format.
sample_rate
, channels,
out_frames);
if
(out_frames <=
0
) {
return
result;
}
int
requested_channels =
std::max
(target_format.
channels
,
1
);
int
out_channels =
std::min
(requested_channels, channels);
result.
frame_length
= out_frames;
result.
channels
= out_channels;
if
(target_format.
common_format
==
"
float32
"
) {
result.
data
.
resize
(
static_cast
<
size_t
>(out_frames) * out_channels *
4
);
for
(
int
f =
0
; f < out_frames; ++f) {
for
(
int
ch =
0
; ch < out_channels; ++ch) {
float
sample = resampled[f * channels + ch] /
32767
.
0f
;
size_t
offset = (
static_cast
<
size_t
>(f) * out_channels + ch) *
4
;
//
memcpy relies on the host being little-endian, true for the
//
x86/x64/ARM desktop targets this plugin builds for.
std::memcpy
(&result.
data
[offset], &sample,
sizeof
(
float
));
}
}
}
else
{
result.
data
.
resize
(
static_cast
<
size_t
>(out_frames) * out_channels *
2
);
for
(
int
f =
0
; f < out_frames; ++f) {
for
(
int
ch =
0
; ch < out_channels; ++ch) {
int16_t
sample = resampled[f * channels + ch];
size_t
offset = (
static_cast
<
size_t
>(f) * out_channels + ch) *
2
;
result.
data
[offset] =
static_cast
<
uint8_t
>(sample &
0xFF
);
result.
data
[offset +
1
] =
static_cast
<
uint8_t
>((sample >>
8
) &
0xFF
);
}
}
}
result.
success
=
true
;
return
result;
}
Back
|
FazBrowse Home
|
New Git URL