简体   繁体   中英

Restrict the height of the wave wavesurfer.js

In documentation there is written that Height of the waveform bars. Higher number than 1 will increase the waveform bar heights .Is there any possibility to restrict the wavebar till one or lower?

Any help would be aprreciated since I can't find a solution by myself

在此处输入图像描述

After Deep understanding, I have created my own custom function to optimize the height of the waves

 export const optimizeWavebar = (wave) => {
  let refine = { ...wave };
  var length = 150;
  var start = 0;
  var end = 150;

  let surfer = refine.backend.getPeaks(length, start, end);

  let cloned = [...surfer];

  let max = cloned.sort((a, b) => b - a);

  let height = max[0] > 0.6 ? 0.8 : 3;
  let filteredForm = max[0] > 0.6 ? max[0] : 0.2;

  return {
    data: surfer.map((index) =>
      index >= filteredForm
        ? filteredForm
        : index <= -filteredForm
        ? -filteredForm
        : index
    ),
    height: height,
  };
};

 const getRefinedPeak = async (wave, url) => {
    return new Promise((res, rej) => {
      const unOptimized = _.cloneDeep(wave);

      unOptimized.load(url);
      unOptimized.on("ready", function () {
        let response = optimizeWavebar(unOptimized);
        wave.params.barHeight = response.height;
        return res(response.data);
      });
    });
  };

  useEffect(() => {
    if (currentTrack && waveformRef.current) {
      setPlay(false);
      const options = formWaveSurferOptions(
        waveformRef.current,
        "#BFC9CA",
        true,
        4.5
      );

      wavesurfer.current = WaveSurfer.create(options);

      const refinedWavebars = getRefinedPeak(
        wavesurfer.current,
        currentTrack.url
      );

      refinedWavebars.then((res) => {
        wavesurfer.current.backend.setPeaks(res);
        wavesurfer.current.drawBuffer();
        wavesurfer.current.getArrayBuffer(currentTrack.url, (data) =>
          wavesurfer.loadArrayBuffer(data)
        );
        wavesurfer.current.load(currentTrack.url);

      });
    }

    return () => {
      wavesurfer.current?.destroy();
      setEnableBtn(true);
    };
  }, [currentTrack.id]);


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM