Skip to content

Commit c8460fb

Browse files
style: optimize bitrate rounding logic in inferG726Bitrate
Replaced manual loop with a more reliable reduction method to find the closest supported G.726 bitrate and fixed potential falsy value bugs.
1 parent 3a6da97 commit c8460fb

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

‎server/src/services/file/FileService.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ const inferChannels = (codec?: AudioCodec): ChannelConfig | undefined => {
4444
};
4545

4646
const inferG726Bitrate = (bitrate?: number | null): number | undefined => {
47-
if (!bitrate || !Number.isFinite(bitrate)) return 32;
47+
48+
if (bitrate===null ||bitrate===undefined || !Number.isFinite(bitrate)){
49+
return 32;
50+
}
4851
const kbps = bitrate >= 1000 ? Math.round(bitrate / 1000) : bitrate;
4952
const supported = [16, 24, 32,40];
50-
let closest = supported[0]!;
51-
for (const value of supported) {
52-
if (Math.abs(value - kbps) < Math.abs(closest - kbps)) {
53-
closest = value;
54-
}
55-
}
56-
return closest;
53+
return supported.reduce((prev,curr)=>Math.abs(curr-kbps)< Math.abs(prev-kbps)?curr:prev);
5754
};
5855

5956
const buildDecodedOutputPath = (filename: string): string => {

0 commit comments

Comments
 (0)