@@ -24,27 +24,6 @@ const BAR_STEP = 3;
2424
2525type BarGeometry = { x : number ; width : number ; height : number } ;
2626
27- function mapWaveformBars (
28- peaks : readonly number [ ] ,
29- width : number ,
30- height : number ,
31- trimStartFraction : number ,
32- trimEndFraction : number ,
33- ) : BarGeometry [ ] {
34- const bars = decimatePeaks (
35- peaks ,
36- trimStartFraction ,
37- trimEndFraction ,
38- Math . max ( 1 , Math . ceil ( width / BAR_STEP ) ) ,
39- ) ;
40- const barWidth = Math . max ( 1 , width / bars . length ) ;
41- return bars . map ( ( amplitude , index ) => ( {
42- x : ( index * width ) / bars . length ,
43- width : barWidth ,
44- height : Math . max ( 3 , amplitude * height ) ,
45- } ) ) ;
46- }
47-
4827function paintWaveformBars (
4928 context : CanvasRenderingContext2D ,
5029 bars : readonly BarGeometry [ ] ,
@@ -62,6 +41,41 @@ function paintWaveformBars(
6241 } ) ;
6342}
6443
44+ export function drawWaveformCanvas (
45+ canvas : HTMLCanvasElement ,
46+ peaks : readonly number [ ] ,
47+ muted : boolean ,
48+ trimStartFraction : number ,
49+ trimEndFraction : number ,
50+ ) {
51+ const width = Math . max ( 1 , canvas . clientWidth ) ;
52+ const height = Math . max ( 1 , canvas . clientHeight ) ;
53+ const scale = window . devicePixelRatio || 1 ;
54+ canvas . width = Math . ceil ( width * scale ) ;
55+ canvas . height = Math . ceil ( height * scale ) ;
56+ const context = canvas . getContext ( "2d" ) ;
57+ if ( ! context ) return ;
58+ context . scale ( scale , scale ) ;
59+ context . clearRect ( 0 , 0 , width , height ) ;
60+ const amplitudes = decimatePeaks (
61+ peaks ,
62+ trimStartFraction ,
63+ trimEndFraction ,
64+ Math . max ( 1 , Math . ceil ( width / BAR_STEP ) ) ,
65+ ) ;
66+ const bars = amplitudes . map ( ( amplitude , index ) => ( {
67+ x : ( index * width ) / amplitudes . length ,
68+ width : Math . max ( 1 , width / amplitudes . length ) ,
69+ height : Math . max ( 3 , amplitude * height ) ,
70+ } ) ) ;
71+ const channelToken = muted ? "--timeline-waveform-muted-rgb" : "--timeline-waveform-bar-rgb" ;
72+ const waveformBarRgb = getComputedStyle ( canvas ) . getPropertyValue ( channelToken ) ;
73+ const waveformBaselineRgb = getComputedStyle ( canvas ) . getPropertyValue (
74+ "--timeline-waveform-baseline-rgb" ,
75+ ) ;
76+ paintWaveformBars ( context , bars , height , waveformBarRgb , waveformBaselineRgb , amplitudes ) ;
77+ }
78+
6579function extractPeaks ( channelData : Float32Array , barCount : number ) : number [ ] {
6680 const peaks : number [ ] = [ ] ;
6781 const samplesPerBar = Math . floor ( channelData . length / barCount ) ;
@@ -168,34 +182,7 @@ export const AudioWaveform = memo(function AudioWaveform({
168182 const draw = useCallback ( ( ) => {
169183 const canvas = canvasRef . current ;
170184 if ( ! canvas || ! peaks ) return ;
171- const width = Math . max ( 1 , canvas . clientWidth ) ;
172- const height = Math . max ( 1 , canvas . clientHeight ) ;
173- const scale = window . devicePixelRatio || 1 ;
174- canvas . width = Math . ceil ( width * scale ) ;
175- canvas . height = Math . ceil ( height * scale ) ;
176- const context = canvas . getContext ( "2d" ) ;
177- if ( ! context ) return ;
178- context . scale ( scale , scale ) ;
179- context . clearRect ( 0 , 0 , width , height ) ;
180- const mappedBars = mapWaveformBars (
181- peaks ,
182- width ,
183- height ,
184- trimStartFraction ?? 0 ,
185- trimEndFraction ?? 1 ,
186- ) ;
187- const amplitudes = decimatePeaks (
188- peaks ,
189- trimStartFraction ?? 0 ,
190- trimEndFraction ?? 1 ,
191- mappedBars . length ,
192- ) ;
193- const channelToken = muted ? "--timeline-waveform-muted-rgb" : "--timeline-waveform-bar-rgb" ;
194- const waveformBarRgb = getComputedStyle ( canvas ) . getPropertyValue ( channelToken ) ;
195- const waveformBaselineRgb = getComputedStyle ( canvas ) . getPropertyValue (
196- "--timeline-waveform-baseline-rgb" ,
197- ) ;
198- paintWaveformBars ( context , mappedBars , height , waveformBarRgb , waveformBaselineRgb , amplitudes ) ;
185+ drawWaveformCanvas ( canvas , peaks , muted , trimStartFraction ?? 0 , trimEndFraction ?? 1 ) ;
199186 } , [ muted , peaks , trimEndFraction , trimStartFraction ] ) ;
200187
201188 const setCanvasRef = useCallback (
0 commit comments