Skip to content

Commit af92869

Browse files
author
Matthew Klein
committed
Add a checkbox to toggle spectrogram to just vocal range
1 parent 34f16f6 commit af92869

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

Diff for: src/components/SpectrogramGraph.vue

+37-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<script setup lang="ts">
22
import { nextTick, onMounted, ref, watch } from "vue";
33
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";
58
69
import { useColorMap } from "@/use/colorMap";
710
811
const props = defineProps<{
912
src: string;
13+
vocalRange?: boolean;
1014
}>();
1115
1216
const { colors } = useColorMap();
@@ -29,6 +33,21 @@ const getOptions = (): WaveSurferOptions => {
2933
// const freq = 2 ** 14;
3034
// const freq = 2 ** 15;
3135
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+
3251
return {
3352
container: waveBox.value,
3453
url: props.src,
@@ -40,31 +59,27 @@ const getOptions = (): WaveSurferOptions => {
4059
// minPxPerSec: 100,
4160
interact: false,
4261
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)],
5663
};
5764
};
5865
59-
onMounted(async () => {
60-
await nextTick();
66+
const setupWaveSurfer = () => {
6167
if (!waveBox.value) {
6268
throw new Error("missing wave surfer refs");
6369
}
6470
71+
if (wave.value) {
72+
wave.value.destroy();
73+
}
74+
6575
wave.value = WaveSurfer.create(getOptions());
6676
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();
6883
});
6984
7085
watch(
@@ -91,6 +106,11 @@ watch(
91106
}
92107
},
93108
);
109+
110+
watch(
111+
() => props.vocalRange,
112+
() => setupWaveSurfer(),
113+
);
94114
</script>
95115

96116
<template>

Diff for: src/views/SpectrogramExperiment.vue

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@ import { toColor, useColorMap } from "@/use/colorMap";
88
const { colors } = useColorMap();
99
1010
const audioSource = ref<string>();
11+
const vocalRange = ref(false);
1112
</script>
1213

1314
<template>
1415
<div class="spectrogram-experiment">
1516
<AudioPicker v-model="audioSource" />
1617

18+
<div class="options">
19+
<label for="vocal-range">Limit to vocal range</label>
20+
<input id="vocal-range" v-model="vocalRange" type="checkbox" />
21+
</div>
22+
1723
<div class="spectrogram-wrapper">
1824
<template v-if="audioSource">
19-
<SpectrogramGraph :src="audioSource" class="spectrogram" />
25+
<SpectrogramGraph
26+
:src="audioSource"
27+
:vocal-range="vocalRange"
28+
class="spectrogram"
29+
/>
2030
</template>
2131
</div>
2232
</div>

0 commit comments

Comments
 (0)