forked from notthetup/resampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (68 loc) · 2.39 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
"use strict";
var resampler = require('./lib/resampler.js');
var dragDrop = require('drag-drop');
window.addEventListener('load', function(){
// dom elements
var fSelectOption = document.getElementById('freqSelect');
var messageBox = document.getElementById('message');
var input = document.getElementById('input');
var note = document.getElementById('note');
var spinner = document.getElementById('spinner');
//monkeypatch
var ddEventListeners = {};
var dropzone = document.querySelector('#dropzone');
var dEL = dropzone.addEventListener.bind(dropzone);
dropzone.addEventListener = function(event, callback, flag){
ddEventListeners[event] = callback;
dEL(event,callback,flag);
};
var disableDragDrop = function(elem) {
elem.removeEventListener ('dragenter', ddEventListeners.dragenter);
elem.removeEventListener('dragover', ddEventListeners.dragover);
elem.removeEventListener('drop', ddEventListeners.drop);
};
dragDrop('#dropzone', resampleDraggedFiles);
messageBox.addEventListener('click', function (){
input.click();
});
input.addEventListener('change', function(evt){
var chosenFile = evt.target.files[0];
if (chosenFile){
var chosenSampleRate = parseInt(fSelectOption.selectedOptions[0].value);
console.log(chosenFile,chosenSampleRate);
resampleFile(chosenFile,chosenSampleRate);
}
});
function resampleDraggedFiles(files){
var chosenFile = files[0] || files;
var chosenSampleRate = parseInt(fSelectOption.selectedOptions[0].value);
console.log(chosenFile,chosenSampleRate);
resampleFile(chosenFile,chosenSampleRate);
}
function resampleFile (file, targetSampleRate){
// note.messageBox.
note.style.display = "none";
spinner.style.display = "inherit";
input.disabled = true;
disableDragDrop(dropzone);
resampler(file, targetSampleRate, 24, function(event){
event.getFile(function(fileEvent){
console.log(fileEvent);
spinner.style.display = "none";
note.style.display = "inherit";
input.disabled = false;
dragDrop('#dropzone');
var a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";
a.href = fileEvent;
var fileExt = file.name.split('.').pop();
var fileName = file.name.substr(0, file.name.length-fileExt.length-1);
a.download = fileName + "_resampled."+ fileExt;
a.click();
window.URL.revokeObjectURL(fileEvent);
document.body.removeChild(a);
});
});
}
});