-
-
Notifications
You must be signed in to change notification settings - Fork 966
/
get-waveform-portion.ts
42 lines (39 loc) · 1.08 KB
/
get-waveform-portion.ts
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
import {getWaveformSamples} from './get-wave-form-samples';
import type {AudioData} from './types';
type Bar = {
index: number;
amplitude: number;
};
/**
* @description Takes bulky waveform data (for example fetched by getAudioData()) and returns a trimmed and simplified version of it, for simpler visualization
* @see [Documentation](https://www.remotion.dev/docs/get-waveform-portion)
*/
export const getWaveformPortion = ({
audioData,
startTimeInSeconds,
durationInSeconds,
numberOfSamples,
}: {
audioData: AudioData;
startTimeInSeconds: number;
durationInSeconds: number;
numberOfSamples: number;
}): Bar[] => {
const startSample = Math.floor(
(startTimeInSeconds / audioData.durationInSeconds) *
audioData.channelWaveforms[0].length,
);
const endSample = Math.floor(
((startTimeInSeconds + durationInSeconds) / audioData.durationInSeconds) *
audioData.channelWaveforms[0].length,
);
return getWaveformSamples(
audioData.channelWaveforms[0].slice(startSample, endSample),
numberOfSamples,
).map((w, i) => {
return {
index: i,
amplitude: w,
};
});
};