当前位置: 首页 > news >正文

vue中使用wavesurfer.js绘制波形图和频谱图

在这里插入图片描述

新的实现方式:vue使用Canvas绘制频谱图

安装wavesurfer.js

npm install wavesurfer.js

第一版:

组件特点:

  • 一次性加载好所有的数据;
<template><div class="audio-visualizer-container"><div class="visualization-container"><div ref="waveform" class="waveform"></div><div ref="spectrogram" class="spectrogram"></div><div v-if="loading" class="loading-indicator">音频加载中...</div><div v-if="error" class="error-message">{{ error }}</div></div><div class="audio-controls"><audioref="audioPlayer"controls@play="startPlay"@pause="stopPlay"@seeked="handleSeek"controlsList="nodownload noplaybackrate"></audio></div></div>
</template><script>
import axios from 'axios'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import WaveSurfer from 'wavesurfer.js'
import Spectrogram from 'wavesurfer.js/dist/plugins/spectrogram.esm.js'
// https://juejin.cn/post/6979191645916889095export default {name: 'AudioWaveform',props: {audioUrl: {type: String,required: true,},},data() {return {wavesurfer: null,spectrogramPlugin: null,isPlaying: false,audioBlobUrl: null,loading: false,error: null,isUserInteraction: false, // 标记是否是用户交互}},watch: {audioUrl(newVal) {this.handleAudioUrl(newVal)},},mounted() {this.initWaveSurfer()this.handleAudioUrl(this.audioUrl)},beforeDestroy() {this.cleanup()},methods: {async initWaveSurfer() {try {this.wavesurfer = WaveSurfer.create({container: this.$refs.waveform,waveColor: '#48a1e0',progressColor: '#25ebd7',cursorColor: '#333',// cursorWidth: 1,// barWidth: 2,// barRadius: 3,height: 150,sampleRate: 8000, // 明确指定采样率// normalize: true,// backend: 'WebAudio',// renderFunction: (channels, ctx) => {//   console.log('Custom render function called!') // 确保执行//   // this.drawWaveform(ctx, channels[0]) // 使用第一个声道数据绘制波形//   const { width, height } = ctx.canvas//   const channelData = channels[0] // 使用左声道数据//   const dataLength = channelData.length//   const step = Math.max(1, Math.floor(dataLength / width)) // 确保步长≥1,避免除零//   ctx.beginPath()//   ctx.lineWidth = 1//   ctx.strokeStyle = '#48a1e0' // 波形颜色//   // 中心线位置(对称波形)//   const centerY = height / 2//   for (let i = 0; i < width; i++) {//     // 使用 step 控制数据采样间隔//     const dataIndex = Math.min(Math.floor(i * step), dataLength - 1) // 防止数组越界//     const value = channelData[dataIndex] // 获取振幅值(-1 到 1)//     // 映射振幅到 Canvas 高度//     const amplitude = value * centerY//     console.log(`绘制点: x=${i},value=${value} amplitude=${amplitude} realMv=${this.calcRealMv(value)}`) // 调试输出//     const x = i//     const y = centerY - amplitude // 向上为正,向下为负//     if (i === 0) {//       ctx.moveTo(x, y)//     } else {//       ctx.lineTo(x, y)//     }//   }//   ctx.stroke() // 绘制路径//   ctx.closePath()// },})// 初始化频谱图插件this.spectrogramPlugin = this.wavesurfer.registerPlugin(Spectrogram.create({container: this.$refs.spectrogram,// labels: true,// labelsBackground: 'rgba(0,0,0,0.1)', //频率标签的背景height: 150,fftSamples: 1024,
http://www.xdnf.cn/news/1209169.html

相关文章:

  • sqli-labs通关笔记-第25关GET字符注入(过滤or和and 脚本法)
  • buuctf_crypto26-30
  • 基于变频与移相混合控制(PFM+PSM)的全桥LLC谐振变换器仿真模型
  • 车载诊断架构 --- 关于诊断时间参数P4的浅析
  • QML 3D曲面图(Surface3D)技术
  • K-近邻算法(KNN算法)的K值的选取--交叉验证+网格搜索
  • 【C++算法】72.队列+宽搜_二叉树的最大宽度
  • adb reboot 与 adb shell svc power reboot 的区别
  • 【C++】1. C++基础知识
  • 【HTML】浅谈 script 标签的 defer 和 async
  • 企业高性能web服务器
  • EnergyMath芯详科技 EMS4100/MES4000/MES3900
  • 如何保证DoIP的网络安全?
  • 基于 xlsx-js-style 的 Excel 导出工具实现导出excel
  • 40+个常用的Linux指令——下
  • haproxy应用详解
  • 从github同步新项目的两次挫折-2025.7.29
  • 【WRF工具】服务器中安装编译GrADS
  • 信创国产Linux操作系统汇总:从桌面到服务器,百花齐放
  • 【Golang】Go语言Map数据类型
  • 随缘玩 一: 代理模式
  • 计算器4.0:新增页签功能梳理页面,通过IO流实现在用户本地存储数据
  • MySQL数据库 mysql常用命令
  • 再谈亚马逊云科技(AWS)上海AI研究院7月22日关闭事件
  • 实现视频实时马赛克
  • P1098 [NOIP 2007 提高组] 字符串的展开
  • python案例:基于python 神经网络cnn和LDA主题分析的旅游景点满意度分析
  • 小程序中事件对象的属性与方法
  • 微算法科技(NASDAQ:MLGO)应用区块链联邦学习(BlockFL)架构,实现数据的安全传输
  • Django自带的加密算法