-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
150 lines (135 loc) · 4.61 KB
/
run.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use strict";
// problem with MP3...
// disabled gsuiComButton can still be clicked...
document.body.append(
GSUcreateDiv( { id: "main" },
GSUcreateDiv( { id: "head" },
GSUcreateDiv( { id: "title", class: "gsui-ellipsis" },
GSUcreateSpan( null, "Ogg/Opus encoder" ),
GSUcreateSpan( null, "by GridSound" ),
),
),
GSUcreateDiv( { id: "body" },
GSUcreateInput( { id: "inputfile", type: "file" } ),
GSUcreateButton( { id: "droparea" },
GSUcreateSpan( null, "Drop your audio file" ),
GSUcreateSpan( null, "or click to open a dialog window" ),
),
GSUcreateDiv( { id: "file" },
GSUcreateDiv( { class: "fileLine" }, GSUcreateSpan( null, "name: " ), GSUcreateSpan() ),
GSUcreateDiv( { class: "fileLine" }, GSUcreateSpan( null, "MIME: " ), GSUcreateSpan() ),
GSUcreateDiv( { class: "fileLine" }, GSUcreateSpan( null, "size: " ), GSUcreateSpan() ),
GSUcreateButton( { id: "fileCancel", class: "gsuiIcon", "data-icon": "close" } ),
),
GSUcreateDiv( { id: "convert" },
GSUcreateDiv( { id: "convertBtns" },
GSUcreateElement( "gsui-com-button", { id: "convertBtn", text: "Convert", type: "submit" } ),
GSUcreateElement( "gsui-com-button", { id: "downloadBtn", text: "Download" } ),
),
GSUcreateDiv( { id: "progress" },
GSUcreateDiv( { id: "progressIn" } ),
),
),
),
),
GSUcreateDiv( { id: "foot" },
GSUcreateDiv( { id: "readme" },
GSUcreateSpan( null, "Thanks to Rillke and its repo " ),
GSUcreateAExt( { href: "https://github.com/Rillke/opusenc.js" }, "github.com/Rillke/opusenc.js" ),
),
GSUcreateSpan( { id: "copyright" },
`© ${ ( new Date() ).getFullYear() } `,
GSUcreateA( { href: "https://gridsound.com" }, "gridsound.com" ),
" all rights reserved",
),
),
);
const elMain = document.querySelector( "#main" );
const elBtnArea = document.querySelector( "#droparea" );
const elInputFile = document.querySelector( "#inputfile" );
const elFileName = document.querySelector( ".fileLine:nth-child(1) span:last-child" );
const elFileType = document.querySelector( ".fileLine:nth-child(2) span:last-child" );
const elFileSize = document.querySelector( ".fileLine:nth-child(3) span:last-child" );
const elFileCancel = document.querySelector( "#fileCancel" );
const elConvertBtn = document.querySelector( "#convertBtn" );
const elDownloadBtn = document.querySelector( "#downloadBtn" );
const elProgress = document.querySelector( "#progressIn" );
const worker = new Worker( "worker/EmsWorkerProxy.js" );
let file;
let newBlob;
let convertStarted = false;
elBtnArea.onclick = () => elInputFile.click();
elInputFile.onchange = () => setFile( elInputFile.files[ 0 ] );
elConvertBtn.onclick = () => convert();
elDownloadBtn.onclick = () => download();
elFileCancel.onclick = () => {
setFile();
progress( 0 );
convertStarted = false;
elMain.classList.remove( "loaded" );
elMain.classList.remove( "ready" );
GSUsetAttribute( elConvertBtn, "disabled", false );
GSUsetAttribute( elConvertBtn, "loading", false );
};
document.body.ondragover = GSUnoopFalse;
document.body.ondragstart = GSUnoopFalse;
document.body.ondrop = e => {
GSUgetFilesDataTransfert( e.dataTransfer.items ).then( f => setFile( f[ 0 ] ) );
return false;
};
worker.onmessage = e => {
if ( e.data ) {
const val = e.data.values;
switch( e.data.reply ) {
case "progress":
if ( val[ 1 ] ) {
progress( val[ 0 ] / val[ 1 ] );
}
break;
case "done":
for ( const fileName in val ) {
newBlob = val[ fileName ].blob;
}
GSUsetAttribute( elConvertBtn, "disabled", true );
GSUsetAttribute( elConvertBtn, "loading", false );
elMain.classList.add( "ready" );
break;
}
}
};
function setFile( f ) {
if ( !f || !convertStarted ) {
file = f;
elMain.classList.toggle( "loaded", !!f );
elFileName.textContent = f?.name;
elFileType.textContent = f?.type;
elFileSize.textContent = f?.size;
}
}
function convert() {
if ( file && !convertStarted ) {
convertStarted = true;
GSUsetAttribute( elConvertBtn, "loading", true );
const rdr = new FileReader();
rdr.addEventListener( "loadend", () => {
worker.postMessage( {
command: "encode",
args: [ file.name, "encoded.opus" ],
outData: { "encoded.opus": { "MIME": "audio/ogg" } },
fileData: { [ file.name ]: new Uint8Array( rdr.result ) },
} );
} );
rdr.readAsArrayBuffer( file );
}
}
function progress( p ) {
elProgress.style.width = `${ p * 100 }%`;
}
function download() {
if ( newBlob ) {
const name = elFileName.textContent;
const lastDot = name.lastIndexOf( "." );
const newName = `${ lastDot < 0 ? name : name.substring( 0, lastDot ) }.opus`;
GSUdownloadBlob( newName, newBlob );
}
}