-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (68 loc) · 2.66 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
(function () {
// https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
// https://tonejs.github.io/docs/13.8.25/UserMedia#close
//https://github.com/Tonejs/Tone.js/blob/13.8.25/Tone/component/Channel.js#L46
// https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API/Using_the_MediaStream_Recording_API
const callBackack = (src) => {
// debugger//blob created
//the player
var player = new Tone.Player({
"url": `${src}`,
"loop": true,
"loopStart": 0.5,
"loopEnd": 0.7,
}).toMaster();
//bind the interface
document.querySelector("tone-player").bind(player);
document.querySelector("tone-play-toggle").bind(player);
}
playMusicReturnBlob(callBackack)
}())
//https://tonejs.github.io/examples/player.html
function playMusicReturnBlob(_cb) {
let BlobToReturn
console.clear();
let micElement = document.querySelector("tone-microphone")
let toneOscillElement = document.querySelector("tone-oscilloscope")
const audio = document.querySelector('audio');
// UPDATE: there is a problem in chrome with starting audio context
// before a user gesture. This fixes it.
var started = false;
document.documentElement.addEventListener('keyup', (event) => {
if (started) return;
if(event.code !== "Enter")return
started = true;
//you probably DONT want to connect the microphone
//directly to the master output because of feedback.
var mic = new Tone.UserMedia();
//bind the interface
// debugger
micElement.bind(mic);
toneOscillElement.bind(mic);
// const synth = new Tone.Synth();
let chunks = [];
const actx = Tone.context;
const dest = actx.createMediaStreamDestination();
// https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
const recorder = new MediaRecorder(dest.stream);
recorder.start()
recorder.ondataavailable = evt => chunks.push(evt.data)
mic.connect(dest)
mic.open().then((event) => {
console.log("Stream is open")
})
document.documentElement.addEventListener('keyup', (event) => {
if(event.code !== "Space")return
recorder.stop()
})
recorder.onstop = function(e) {
console.log("recorder stopped");
let blob = new Blob(chunks, { type: 'audio/ogg; codecs=opus' });
let source = URL.createObjectURL(blob);
audio.src = source
console.log(source)
mic.close()
_cb(source)
}
});
}