-
Notifications
You must be signed in to change notification settings - Fork 45
/
file.js
81 lines (73 loc) · 2.31 KB
/
file.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
import angular from 'angular';
import { saveAs } from 'file-saver';
import { Sound } from '../../lib';
export var fileStorage = ['$q', function($q) {
var download = function(blob, filename) {
saveAs(blob, filename);
};
var uploadFile = function(file) {
var deferred = $q.defer();
var reader = new FileReader();
reader.addEventListener('load', function() {
deferred.resolve({name: file.name, data: reader.result});
});
reader.addEventListener('error', function() {
deferred.reject(reader.error);
});
reader.readAsText(file);
return deferred.promise;
}
var upload = function() {
var input = document.createElement('input');
input.type = 'file';
input.multiple = true;
var deferred = $q.defer();
angular.element(input).on('change', function() {
var filePromises = [];
for (var i = 0; i < input.files.length; i++) {
var file = input.files[i];
if (file) {
filePromises.push(uploadFile(file));
}
}
$q.all(filePromises).then(function(msgs) {
deferred.resolve(msgs);
}, function(err) {
deferred.reject(err);
});
});
input.focus();
input.click();
// Note: if the file picker dialog is cancelled, we never reject the
// promise so we leak some memory. Detecting cancel is tricky:
// https://stackoverflow.com/questions/4628544/how-to-detect-when-cancel-is-clicked-on-file-input
return deferred.promise;
};
this.downloadWav = function(clip, basename) {
var blob = new Blob([clip.toWavBytes()], {type: 'audio/wav'});
download(blob, basename + '.wav');
};
this.saveJfxr = function(sound, basename) {
var json = sound.serialize();
var blob = new Blob([json], {type: 'application/json'});
download(blob, basename + '.jfxr');
};
this.loadJfxrs = function() {
return upload().then(function(msgs) {
var sounds = [];
for (var i = 0; i < msgs.length; i++) {
var msg = msgs[i];
var sound = new Sound();
try {
sound.parse(msg.data);
} catch (ex) {
console.error('Could not parse sound', ex); // eslint-disable-line no-console
continue;
}
sound.name = msg.name.replace(/\.jfxr$/, '');
sounds.push(sound);
}
return sounds;
});
};
}];