-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudiosis.js
106 lines (99 loc) · 3.15 KB
/
Audiosis.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
(function(Scratch) {
'use strict';
let recordingElement = document.createElement("audio")
recordingElement.volume = 0
let recording = false
let gotMicrophone = false
let recordedChunks = []
let recordedClips = []
let recordingname = "Sample"
class audiosis {
getInfo () {
return {
id: 'audiosis',
name: 'Audiosis',
blocks: [
{
opcode: 'requestMicInput',
blockType: Scratch.BlockType.COMMAND,
text: 'Request Microphone Input'
},
{
opcode: 'getRecordedData',
blockType: Scratch.BlockType.REPORTER,
text: 'Microphone Data'
},
{
opcode: 'setRecording',
blockType: Scratch.BlockType.COMMAND,
text: 'Turn recording [rec] with the clip name of [name]',
arguments:{
rec:{
type: Scratch.ArgumentType.STRING,
menu: "OnOffMenu"
},
name:{
type: Scratch.ArgumentType.STRING,
defaultValue: "Audio Clip"
}
}
}
] ,
menus: {
OnOffMenu: {
acceptReporters: true,
items: ['true', "false"]
},
}
};
}
setRecording({rec,name}){
if (rec == 'true'){
recording = true;
recordingname = name
}
else{
recording = false
new Promise( resolve=>{
let reader = new FileReader();
//convert to mp3
let blob = new Blob(recordedChunks, {'type':'audio/mp3'})
recordedClips.push({
name:recordingname,
data:reader.readAsDataURL(blob)
})
});
}
}
requestMicInput({}){
if (!gotMicrophone) {
navigator.mediaDevices.getUserMedia({
audio: true,
video: false
}).then((mediaStream) => {
const audio = recordingElement;
audio.srcObject = mediaStream;
audio.onloadedmetadata = () => {
gotMicrophone = true
console.log("Metadata Loaded")
audio.play();
const Recorder = new MediaRecorder(mediaStream ,{
audioBitsPerSecond : 48000});
Recorder.start(16);
Recorder.ondataavailable = (event) => {
if(recording){
console.log(event.data)
recordedChunks.push(event.data);
}
}
};
}).catch(function(error) {
return error.name + " " + error.message;
})
}
}
getRecordedData({}) {
}
}
Scratch.extensions.register(new audiosis());
})(Scratch);