激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

香港云服务器
服務器之家 - 編程語言 - JavaScript - js教程 - wavesurfer.js繪制音頻波形圖的實現

wavesurfer.js繪制音頻波形圖的實現

2022-02-24 16:49GuaX js教程

這篇文章主要介紹了wavesurfer.js繪制音頻波形圖的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.查看效果圖

wavesurfer.js繪制音頻波形圖的實現

向前選中:

wavesurfer.js繪制音頻波形圖的實現

向后選中:

wavesurfer.js繪制音頻波形圖的實現

代碼如下(示例):

?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<template>
 <div class="waveSurfer">
  <div class="top">
   <span @click="leftSelect">向前選中</span>
   <span @click="rightSelect">向后選中</span>
   <span @click="Region">標注</span>
  </div>
  <!-- 時間軸 -->
  <div id="wave-timeline" />
  <!-- 頻譜圖 -->
  <div id="waveform">
   <progress
    id="progress"
    class="progress progress-striped"
    value="0"
    max="100"
   ></progress>
  </div>
  <div v-show="ppt" id="wave-spectrogram" class="mt-20" />
  <!-- 控制按鈕 -->
  <div class="title">
   <ul>
    <li>
     <span @click="zoomIn"></span>
    </li>
    <li>
     <span @click="rew"></span>
    </li>
    <li>
     <span :class="{ on: isPlay }" @click="plays"></span>
    </li>
    <li>
     <span @click="speek"></span>
    </li>
    <li>
     <span @click="zoomOut"></span>
    </li>
    <li>
     <span @click="replay"></span>
    </li>
    <li @click="toggleMute" :class="{ on: toggleMutebutton }" class="sound">
     <span></span>
    </li>
    <li>
     <input
      @mouseup="volumeBarHandle"
      v-model="volValue"
      type="range"
      min="0"
      max="1"
      value="0.8"
      step="0.01"
     />
    </li>
    <li @click="DoubleSpeed(index)">
     {{ speed[index] + " X" }}
    </li>
   </ul>
  </div>
 </div>
</template>
<script>
import WaveSurfer from "wavesurfer.js";
import Timeline from "wavesurfer.js/dist/plugin/wavesurfer.timeline.js";
import Regions from "wavesurfer.js/dist/plugin/wavesurfer.regions.js";
export default {
 data: function () {
  return {
   index: 0,
   speed: [1.0, 1.5, 2.0, 0.5],
   isPlay: false,
   ppt: false,
   ds: 1.0,
   zoomValue: 100,
   zoomMin: 100,
   fast: 3,
   back: 3,
   noteData: [],
   toggleMutebutton: true,
   volValue: 0,
   audioUrl: "",
   loading: true,
  };
 },
 // computed: {
 //  // 計算屬性的 getter
 //  getUrl: function() {
 //   // `this` 指向 vm 實例
 //   return this.$store.state.voicetrain.url
 //  }
 // },
 // watch: {
 //  getUrl(newUrl) {
 //   this.loading = true
 //   this.audioUrl = newUrl
 //   document.getElementById('waveform').innerHTML = ''
 //   this.init()
 //  }
 // },
 mounted() {
  this.audioUrl =
   "http://192.168.1.101:8080/api/files/20201104/62afa213458d44b0a99440b33fb694b9";
  this.init();
 },
 
 methods: {
  // 初始化
  init() {
   document.getElementById("progress").style.display = "block";
   this.$nextTick(() => {
    this.wavesurfer = WaveSurfer.create({
     container: "#waveform",
     cursorColor: "#DB7093", // 聲波播放進度線color
     audioRate: 1,
     scrollParent: true,
     backend: "WebAudio",
     barHeight: 1.5,
     waveColor: "#43d996", // 聲波color
     progressColor: "#43d996", // 已播放聲波color
     loaderColor: "#8B0000",
     hideScrollbar: false,
     autoCenter: true,
     height: 120,
     splitChannels: true,
     responsive: true,
     minPxPerSec: 1,
     plugins: [
      Timeline.create({
       container: "#wave-timeline",
       fontSize: 14,
       primaryFontColor: "#9191a5",
       secondaryFontColor: "#9191a5",
       primaryColor: "#9191a5",
       secondaryColor: "#9191a5",
      }),
      Regions.create({}),
     ],
    });
    this.wavesurfer.addRegion({
     loop: false,
     drag: false,
     resize: false,
     color: "rgba(254, 255, 255, 0.4)",
    });
    // 加載進度條
    this.wavesurfer.on("loading", function (percents) {
     document.getElementById("progress").value = percents;
    });
    this.wavesurfer.load(this.audioUrl);
    this.value = this.wavesurfer.getVolume() * 100; // 獲取音量
    this.zoomValue = this.wavesurfer.params.minPxPerSec;
    this.zoomMin = this.wavesurfer.params.minPxPerSec;
    this.wavesurfer.zoom(Number(this.zoomValue));
    this.wavesurfer.panner = this.wavesurfer.backend.ac.createPanner();
    this.wavesurfer.backend.setFilter(this.wavesurfer.panner);
    let _this = this;
    _this.wavesurfer.on("ready", function () {
     _this.wavesurfer.enableDragSelection({
      color: "rgba(0, 180, 0, 0.3)",
     });
     _this.wavesurfer.clearRegions();
     _this.wavesurfer.zoom(_this.zoomValue);
     // 音頻加載完成
     document.getElementById("progress").style.display = "none";
     document.getElementById("progress").value = 0;
     _this.isPlay = true;
     _this.wavesurfer.play(0);
    });
    document.getElementById("waveform").onclick = function () {
     _this.isPlay = false;
     _this.wavesurfer.clearRegions();
    };
    // 更新區(qū)域時。回調將接收該Region對象。
    // this.wavesurfer.on("region-updated", function (region) {
    //  region.playLoop(); // 循環(huán)播放選中區(qū)域
    //  _this.isPlay = true;
    // });
    _this.wavesurfer.on("region-created", _this.addRegion);
    _this.wavesurfer.on("region-click", _this.editAnnotation);
 
    _this.wavesurfer.on("finish", function () {
     _this.wavesurfer.play(0);
    });
   });
  },
  addRegion(params) {
   this.wavesurfer.clearRegions();
   params.handleLeftEl.style.backgroundColor = "transparent";
   params.handleRightEl.style.backgroundColor = "transparent";
  },
  toggleMute() {
   if (this.toggleMutebutton) {
    this.volumeCached = this.wavesurfer.getVolume();
    this.wavesurfer.setVolume(0);
    this.toggleMutebutton = false;
    this.volValue = 0;
   } else {
    if (this.volumeCached == 0) this.volumeCached = 1;
    this.wavesurfer.setVolume(this.volumeCached);
    this.toggleMutebutton = true;
    this.volValue = this.volumeCached;
   }
  },
  volumeBarHandle(e) {
   if (e.offsetX >= 0 && e.offsetX <= 80) {
    this.toggleMutebutton = true;
    this.wavesurfer.setVolume(e.offsetX / 80);
   } else if (e.offsetX < 0) {
    this.toggleMutebutton = false;
    this.wavesurfer.setVolume(0);
   } else {
    this.wavesurfer.setVolume(1);
    this.toggleMutebutton = true;
   }
  },
  // 標注
  Region() {
   console.log(
    Object.getOwnPropertyNames(this.wavesurfer.regions.list).length
   );
   if (
    Object.getOwnPropertyNames(this.wavesurfer.regions.list).length == 0
   ) {
    alert("請選擇波紋");
    return;
   }
   let start = 0,
    end = 0;
   for (var k in this.wavesurfer.regions.list) {
    let obj = this.wavesurfer.regions.list[k];
    start = obj.start.toFixed(2) * 1000;
    end = obj.end.toFixed(2) * 1000;
   }
   console.log(this.wavesurfer);
   console.log("開始", start);
   console.log("結束", end);
  },
  // 播放
  plays() {
   this.isPlay = !this.isPlay;
   this.wavesurfer.playPause(); //切換播放,應用播放或暫停
  },
  // 回退
  rew() {
   this.wavesurfer.skip(-this.back);
   this.goPlay();
  },
  // 快進
  speek() {
   this.wavesurfer.skip(this.fast);
   this.goPlay();
  },
  // 重載
  replay() {
   this.isPlay = true;
   this.wavesurfer.stop();
   this.wavesurfer.clearRegions();
   this.wavesurfer.play(0);
  },
  // 倍速
  DoubleSpeed(index) {
   if (index === 3) {
    this.index = 0;
    this.wavesurfer.setPlaybackRate(this.speed[this.index]);
   } else {
    this.index = index + 1;
    this.wavesurfer.setPlaybackRate(this.speed[this.index]);
   }
   console.log(this.wavesurfer);
  },
  // 縮放百分比顯示格式
  formatZoom(val) {
   return val + 100 + " 像素 / 秒";
  },
  // 點擊縮小
  zoomIn() {
   if (this.zoomValue >= 100) {
    return;
   }
   this.zoomValue += 1;
   this.wavesurfer.zoom(this.zoomValue);
  },
  // 點擊擴大
  zoomOut() {
   if (this.zoomValue < -100) {
    return;
   }
   this.zoomValue -= 1;
   this.wavesurfer.zoom(this.zoomValue);
  },
  // 縮放監(jiān)聽
  zoomChange() {
   this.wavesurfer.zoom(Number(this.zoomValue));
  },
  goPlay() {
   let start = this.wavesurfer.getCurrentTime();
   this.wavesurfer.play(start);
  },
  // 向前選中
  leftSelect() {
   let end = this.wavesurfer.getCurrentTime(); // 獲取當前播放位置
   this.waveRegion(this.wavesurfer, 0, end, "rgba(0,180,0,.3)", true);
  },
  // 向后選中
  rightSelect() {
   let start = this.wavesurfer.getCurrentTime(); // 獲取當前播放位置
   let end = this.wavesurfer.getDuration(); // 獲取音頻片段的持續(xù)時間
   this.waveRegion(this.wavesurfer, start, end, "rgba(0,180,0,.3)", true);
  },
  waveRegion(wavesurfer, start, end, color, clear) {
   if (!clear) {
    wavesurfer.clearRegions();
   }
   wavesurfer.addRegion({
    start: start,
    end: end,
    color: color,
    drag: false,
   });
  },
  // 區(qū)域點擊事件新建
  saveRegions() {
   console.log("聲紋點擊---");
   this.noteData = [];
   const _this = this;
   this.noteData = Object.keys(_this.wavesurfer.regions.list).map(function (
    id
   ) {
    const region = _this.wavesurfer.regions.list[id];
    return {
     id: id,
     edit: false,
     start: Math.round(region.start * 10) / 10,
     end: Math.round(region.end * 10) / 10,
     attributes: region.attributes,
     data: { note: region.data.note || "" },
    };
   });
  },
  // 區(qū)域點擊
  editAnnotation() {
   this.isPlay = false;
  },
  showNote(region) {
   if (!this.showNote.el) {
    this.showNote.el = document.querySelector("#subtitle");
   }
   this.showNote.el.textContent = region.data.note || "–";
  },
  // 設置音量
  setVolume(val) {
   console.log(val);
   this.wavesurfer.setVolume(val / 100);
  },
  // 實例點擊
  clearReagion() {
   this.wavesurfer.clearRegions();
  },
 },
};
</script>
<style lang="scss" scoped>
#waveform {
 position: relative;
}
.top {
 width: 100%;
 flex-basis: 70px;
 line-height: 40px;
 flex-shrink: 0;
 color: white;
 text-indent: 15px;
 span,
 el-slider {
  color: rgb(39, 39, 39);
  font-size: 13px;
  font-weight: 700;
  margin-right: 20px;
  padding: 4px 10px;
  border: 1px solid #ccc;
  border-radius: 10px;
 }
}
.title {
 width: 100%;
 flex-basis: 70px;
 line-height: 40px;
 text-align: left;
 flex-shrink: 0;
 color: white;
 text-indent: 15px;
 ul {
  list-style-type: none;
  padding-inline-start: 0;
  .speed {
   display: flex;
   flex-direction: column;
  }
  li {
   position: relative;
   display: inline-block;
   cursor: default;
   &:hover {
   }
   &:active {
   }
   span {
    display: inline-block;
    width: 30px;
    height: 30px;
    line-height: 30px;
   }
   &:nth-child(1) span {
    width: 27px;
    height: 27px;
    background: url("img/縮小.png") right;
    background-size: cover;
   }
   &:nth-child(2) span {
    background: url("img/kuaitui_bg.png") right;
    background-size: cover;
   }
   &:nth-child(3) {
    span {
     background: url("img/bofang_bg.png") right;
     background-size: cover;
    }
    .on {
     background: url("img/zanting_bg.png") right;
     background-size: cover;
    }
   }
   &:nth-child(4) span {
    background: url("img/kuaijin_bg.png") right;
    background-size: cover;
   }
   &:nth-child(5) span {
    background: url("img/縮放.png") right;
    background-size: cover;
   }
   &:nth-child(6) span {
    background: url("img/zhongbo.png") right;
    background-size: cover;
   }
   &:nth-child(9) {
    color: rgb(39, 39, 39);
    font-size: 13px;
    font-weight: 700;
   }
   &:nth-child(7) {
    background: none;
    span {
     width: 25px;
     height: 25px;
     background: url("img/靜音.png") no-repeat;
     background-size: cover;
    }
    &.on {
     span {
      width: 25px;
      height: 25px;
      background: url("img/喇叭.png") no-repeat;
      background-size: cover;
     }
    }
   }
   &:nth-child(8) {
    width: 80px;
    background: none;
    input {
     -webkit-appearance: none;
     -moz-appearance: none;
     -ms-appearance: none;
     width: 80px;
     height: 3px;
     background-color: #bbbbbb;
     position: absolute;
     left: 0;
     top: -14px;
 
     &::-webkit-slider-thumb {
      -webkit-appearance: none;
     }
     &::-moz-range-trackpseduo {
      -moz-appearance: none;
     }
     &::-ms-track {
      width: 100%;
      cursor: pointer;
      background: transparent; /* Hides the slider so custom styles can be added */
      border-color: transparent;
      color: transparent;
     }
     &:focus {
      outline: none;
     }
     &::-webkit-slider-thumb {
      -webkit-appearance: none;
      height: 9px;
      width: 9px;
      margin-top: -1px;
      background: #bbb;
      border-radius: 50%;
      border: solid 0.125em rgba(205, 224, 230, 0.5);
     }
     &::-moz-range-thumb {
      -moz-appearance: none;
      height: 6px;
      width: 6px;
      margin-top: -1px;
      background: #bbb;
      border-radius: 50%;
      border: solid 0.125em rgba(205, 224, 230, 0.5);
     }
     &::-ms-track {
      -moz-appearance: none;
      height: 6px;
      width: 6px;
      margin-top: -1px;
      background: #bbb;
      border-radius: 50%;
      border: solid 0.125em rgba(205, 224, 230, 0.5);
     }
    }
   }
  }
 }
}
#wave-timeline {
 height: 21px;
}
#waveform {
 width: 100%;
 flex-basis: 128px;
 flex-shrink: 0;
 position: relative;
}
#progress {
 position: absolute;
 width: 100%;
 height: 4px;
 background: #ccc;
 top: 48%;
 opacity: 0.7;
 z-index: 44;
}
.mt-20 {
 margin-top: 20px;
}
.mt-30 {
 margin-top: 30px;
}
.waveSurfer {
 width: 470px;
}
.waveSurfer >>> .el-slider__runway {
 margin: 6px 0;
}
</style>

鏈接:https://wavesurfer-js.org

到此這篇關于wavesurfer.js繪制音頻波形圖的實現的文章就介紹到這了,更多相關wavesurfer.js 音頻波形圖內容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/m0_37714008/article/details/109776548

延伸 · 閱讀

精彩推薦
992
主站蜘蛛池模板: 欧美日韩a∨毛片一区 | 国产成人精品区 | 亚洲精品wwww | 国产精品久久久久久久娇妻 | 欧美视屏一区二区 | 九九热精品在线视频 | 亚洲精品久久久久久下一站 | 看一级大毛片 | 日韩深夜视频 | 黄色免费高清网站 | 午夜精品在线播放 | 精品国产一区二 | 人成免费a级毛片 | 日韩视频―中文字幕 | 国产免费一级淫片a级中文 99国产精品自拍 | av手机免费在线观看 | 成人在线观看一区 | 美女色影院 | 中文在线观看免费视频 | h色视频网站 | 国产合集91合集久久日 | 毛片在线免费播放 | 中文字幕在线播放视频 | 日日草天天干 | 欧美成人h版在线观看 | 久久久久久久免费看 | 久久亚洲精品国产 | 欧美成人三级视频 | 日本一区二区精品 | 香蕉国产片 | 欧美成人高清视频 | 国产午夜精品一区二区三区嫩草 | 久久中文一区 | 免费国产一级淫片 | 99re66热这里只有精品8 | 成人午夜视频免费在线观看 | 久久精精品| 99精品视频一区二区三区 | av最新在线观看 | 制服丝袜日日夜夜 | 亚洲91网 |