From a56cbc54c51ea8853aecda7efb09251f6752c7d1 Mon Sep 17 00:00:00 2001 From: Eris Lund <38136789+0x5066@users.noreply.github.com> Date: Fri, 28 Nov 2025 01:10:11 +0100 Subject: [PATCH] High bitrate/samplerate is now correctly clipped (#1318) * High bitrate/samplerate is now correctly clipped This makes the ``kbps`` and ``kHz`` displays in the main window correctly emulate what Winamp does with high bitrates/samplerates. * Moved globals to be local to their designated functions Division is no longer performed in each if condition Default to displaying "0" for the kbps and khz fields (doesn't seem to trigger, though) * Use padStart and slice to more properly format the data Added comment by Justin Frankel on the meaning of H and C * Display "0" while gathering bitrate and khz * Remove logging of kbps in console Co-authored-by: Jordan Eldredge * Assign ``finalKhz`` properly * Make CI hopefully happy --------- Co-authored-by: Jordan Eldredge --- packages/webamp/js/reducers/tracks.ts | 33 +++++++++++++++++++++++++-- packages/webamp/js/selectors.ts | 4 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/webamp/js/reducers/tracks.ts b/packages/webamp/js/reducers/tracks.ts index d73276af..2eabf495 100644 --- a/packages/webamp/js/reducers/tracks.ts +++ b/packages/webamp/js/reducers/tracks.ts @@ -6,6 +6,35 @@ export interface TracksState { [id: string]: PlaylistTrack; } +function massageKhz(khz: number) { + let finalKhz: String; + const khzNum: number = Math.round(khz / 1000); + + // there is no real need to run a condition for below 100khz + // when the other conditions (hopefully) take over + // ...also to make CI happy + finalKhz = String(khzNum); + if (khzNum <= 10) finalKhz = String(khzNum).slice(0, 1).padStart(2, " "); + if (khzNum >= 100) finalKhz = String(khzNum).slice(1, 3); + return finalKhz; +} + +function massageKbps(kbps: number) { + let finalKbps: String; + const bitrateNum: number = Math.round(kbps / 1000); + + finalKbps = String(bitrateNum); // present as is + if (bitrateNum <= 100) finalKbps = String(bitrateNum).padStart(3, " "); + if (bitrateNum <= 10) finalKbps = String(bitrateNum).padStart(3, " "); + // from Justin Frankel directly: + // IIRC H was for "hundred" and "C" was thousand, + // though why it was for thousand I have no idea lol, maybe it was a mistake... + if (bitrateNum >= 1000) finalKbps = String(bitrateNum).slice(0, 2) + "H"; + if (bitrateNum >= 10000) + finalKbps = String(bitrateNum).slice(0, 1).padStart(2, " ") + "C"; + return finalKbps; +} + const defaultPlaylistState: TracksState = {}; const tracks = ( @@ -80,8 +109,8 @@ const tracks = ( artist, album, albumArtUrl, - kbps: bitrate != null ? String(Math.round(bitrate / 1000)) : kbps, - khz: sampleRate != null ? String(Math.round(sampleRate / 1000)) : khz, + kbps: bitrate != null ? massageKbps(bitrate) : kbps, + khz: sampleRate != null ? massageKhz(sampleRate) : khz, channels: numberOfChannels != null ? numberOfChannels : channels, }, }; diff --git a/packages/webamp/js/selectors.ts b/packages/webamp/js/selectors.ts index b97b42a3..f973ac04 100644 --- a/packages/webamp/js/selectors.ts +++ b/packages/webamp/js/selectors.ts @@ -686,14 +686,14 @@ export const getMarqueeText = (state: AppState): string => { export const getKbps = createSelector( getCurrentTrack, (track: PlaylistTrack | null): string | null => { - return track != null ? track.kbps || null : null; + return track != null ? track.kbps || "0".padStart(3, " ") : null; } ); export const getKhz = createSelector( getCurrentTrack, (track: PlaylistTrack | null): string | null => { - return track != null ? track.khz || null : null; + return track != null ? track.khz || "0".padStart(2, " ") : null; } );