Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress the fake audio data #12680

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ const normalizeAmplitude = (data: number[]) => {
return data.map((n) => n * multiplier * 100);
};

/**
* Compresses an of values to a range between the threshold and the existing
* maximum.
*/
const compress = (array: number[], threshold: number) => {
const minValue = Math.min(...array);
const maxValue = Math.max(...array);

return array.map(
(x) =>
((x - minValue) / (maxValue - minValue)) * (maxValue - threshold) +
threshold,
);
};

/** Returns a string of the specified length, repeating the input string as necessary. */
function padString(str: string, length: number) {
// Repeat the string until it is longer than the desired length
Expand Down Expand Up @@ -74,8 +89,11 @@ function generateWaveform(url: string, bars: number) {
// Normalize the amplitude of the fake audio data
const normalized = normalizeAmplitude(shuffled);

// Compress the amplitude of the fake audio data, like a podcast would
const compressed = compress(normalized, 60);

// Return the normalized the amplitude of the fake audio data
return normalized;
return compressed;
}

type Theme = {
Expand Down Expand Up @@ -150,11 +168,11 @@ export const WaveForm = ({
})}
</g>

<clipPath id="buffer-clip-path">
<clipPath id={`buffer-clip-path-${id}`}>
<rect height="100" width={(buffer / 100) * totalWidth} />
</clipPath>

<clipPath id="progress-clip-path">
<clipPath id={`progress-clip-path-${id}`}>
<rect height="100" width={(progress / 100) * totalWidth} />
</clipPath>
</defs>
Expand All @@ -165,14 +183,14 @@ export const WaveForm = ({
{/* buffer wave */}
<use
href={`#bars-${id}`}
clipPath="url(#buffer-clip-path)"
clipPath={`url(#buffer-clip-path-${id})`}
fill={theme.buffer}
/>

{/* progress wave */}
<use
href={`#bars-${id}`}
clipPath="url(#progress-clip-path)"
clipPath={`url(#progress-clip-path-${id})`}
fill={theme.progress}
/>
</svg>
Expand Down
Loading