1
1
<script setup lang="ts">
2
2
import { nextTick , onMounted , ref , watch } from " vue" ;
3
3
import { WaveSurfer , type WaveSurferOptions } from " wavesurfer.js" ;
4
- import { SpectrogramPlugin } from " wavesurfer.js/dist/plugins/spectrogram" ;
4
+ import {
5
+ SpectrogramPlugin ,
6
+ type SpectrogramPluginOptions ,
7
+ } from " wavesurfer.js/dist/plugins/spectrogram" ;
5
8
6
9
import { useColorMap } from " @/use/colorMap" ;
7
10
8
11
const props = defineProps <{
9
12
src: string ;
13
+ vocalRange? : boolean ;
10
14
}>();
11
15
12
16
const { colors } = useColorMap ();
@@ -29,6 +33,21 @@ const getOptions = (): WaveSurferOptions => {
29
33
// const freq = 2 ** 14;
30
34
// const freq = 2 ** 15;
31
35
36
+ const spectrogramOptions: SpectrogramPluginOptions = {
37
+ container: spectrogramBox .value ,
38
+ windowFunc: " hann" ,
39
+ fftSamples: freq ,
40
+ colorMap: colors ,
41
+ labels: true ,
42
+ // noverlap: (2 ** 13) * 0.875,
43
+ // noverlap: freq * 0.875,
44
+ };
45
+
46
+ if (props .vocalRange ) {
47
+ spectrogramOptions .frequencyMin = 40 ;
48
+ spectrogramOptions .frequencyMax = 1.5 * 1000 ;
49
+ }
50
+
32
51
return {
33
52
container: waveBox .value ,
34
53
url: props .src ,
@@ -40,31 +59,27 @@ const getOptions = (): WaveSurferOptions => {
40
59
// minPxPerSec: 100,
41
60
interact: false ,
42
61
43
- plugins: [
44
- SpectrogramPlugin .create ({
45
- container: spectrogramBox .value ,
46
- windowFunc: " hann" ,
47
- fftSamples: freq ,
48
- colorMap: colors ,
49
- labels: true ,
50
- frequencyMin: 40 ,
51
- frequencyMax: 1.5 * 1000 ,
52
- // noverlap: (2 ** 13) * 0.875,
53
- // noverlap: freq * 0.875,
54
- }),
55
- ],
62
+ plugins: [SpectrogramPlugin .create (spectrogramOptions )],
56
63
};
57
64
};
58
65
59
- onMounted (async () => {
60
- await nextTick ();
66
+ const setupWaveSurfer = () => {
61
67
if (! waveBox .value ) {
62
68
throw new Error (" missing wave surfer refs" );
63
69
}
64
70
71
+ if (wave .value ) {
72
+ wave .value .destroy ();
73
+ }
74
+
65
75
wave .value = WaveSurfer .create (getOptions ());
66
76
wave .value .getMediaElement ().controls = true ;
67
- audioBox .value ?.append (wave .value .getMediaElement ());
77
+ audioBox .value ?.replaceChildren (wave .value .getMediaElement ());
78
+ };
79
+
80
+ onMounted (async () => {
81
+ await nextTick ();
82
+ setupWaveSurfer ();
68
83
});
69
84
70
85
watch (
@@ -91,6 +106,11 @@ watch(
91
106
}
92
107
},
93
108
);
109
+
110
+ watch (
111
+ () => props .vocalRange ,
112
+ () => setupWaveSurfer (),
113
+ );
94
114
</script >
95
115
96
116
<template >
0 commit comments