-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
index.js
114 lines (103 loc) · 2.84 KB
/
index.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
import React, { useCallback, useState, useRef } from "react";
import Script from "next/script";
import EventEmitter from "events";
import WaveformPlaylist from "waveform-playlist";
import { saveAs } from "file-saver";
export default function Home() {
const [ee] = useState(new EventEmitter());
const [toneCtx, setToneCtx] = useState(null);
const setUpChain = useRef();
const container = useCallback(
(node) => {
if (node !== null && toneCtx !== null) {
const playlist = WaveformPlaylist(
{
ac: toneCtx.rawContext,
samplesPerPixel: 100,
mono: true,
waveHeight: 100,
container: node,
state: "cursor",
colors: {
waveOutlineColor: "#E0EFF1",
timeColor: "grey",
fadeColor: "black",
},
controls: {
show: true,
width: 150,
},
zoomLevels: [100, 300, 500],
},
ee
);
ee.on("audiorenderingstarting", function (offlineCtx, a) {
// Set Tone offline to render effects properly.
const offlineContext = new Tone.OfflineContext(offlineCtx);
Tone.setContext(offlineContext);
setUpChain.current = a;
});
ee.on("audiorenderingfinished", function (type, data) {
//restore original ctx for further use.
Tone.setContext(toneCtx);
if (type === "wav") {
saveAs(data, "test.wav");
}
});
playlist.load([
{
src: "hello.mp3",
name: "Hello",
effects: function (graphEnd, masterGainNode, isOffline) {
const reverb = new Tone.Reverb(1.2);
if (isOffline) {
setUpChain.current.push(reverb.ready);
}
Tone.connect(graphEnd, reverb);
Tone.connect(reverb, masterGainNode);
return function cleanup() {
reverb.disconnect();
reverb.dispose();
};
},
},
]);
//initialize the WAV exporter.
playlist.initExporter();
}
},
[ee, toneCtx]
);
function handleLoad() {
setToneCtx(Tone.getContext());
}
return (
<>
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.37/Tone.js"
onLoad={handleLoad}
/>
<main>
<div>
<button
onClick={() => {
ee.emit("play");
}}
>
Play
</button>
</div>
<div>
<button
onClick={() => {
ee.emit("startaudiorendering", "wav");
}}
>
Download
</button>
</div>
<div ref={container}></div>
</main>
</>
);
}