blob: d54be71bea86608f8323ff3ae01942659250d883 (
plain)
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
|
/* -*- mode:javascript; coding:utf-8; -*- */
'use strict';
(function (window) {
var LOOP_COUNT = 4;
function sample(json) {
var x = JSON.parse(json);
this.seq = x.seq || 0;
this.timestamp = x.timestamp || 0;
this.url = x.url;
}
sample.prototype.gt = function (x) {
return this.timestamp > x.timestamp || this.timestamp === x.timestamp && this.seq > x.seq;
};
sample.prototype.load = function (context) {
var self = this;
if (self.url) {
var request = new XMLHttpRequest();
request.open('GET', self.url, true);
request.responseType = 'arraybuffer';
request.onload = function (v) {
if (v && v.target && v.target.status == 200) {
context.decodeAudioData(request.response, function (buffer) {
if (buffer) {
self.buffer = buffer;
if (self.onload) {
self.onload();
}
console.log('sample.load: finished ', self.url);
}
else {
console.error('sample.load: no decoded data', self);
}
}, function (e) {
console.error('sample.load: decodeAudioData error', e, self);
});
}
};
request.onerror = function (e) {
var s = (e && e.target && e.target.status && e.target.statusText
? (e.target.status + ' ' + e.target.statusText)
: 'error');
console.error('sample.load: ' + s + ' while loading', self);
};
request.send();
}
else {
console.error('sample.load: no URL');
}
};
sample.prototype.play = function (context) {
var self = this;
if (self.buffer) {
var delay = self.buffer.duration * LOOP_COUNT
var source = context.createBufferSource();
source.buffer = self.buffer;
source.loop = true;
source.connect(context.destination);
source.start(context.currentTime);
source.stop(context.currentTime + delay);
window.setTimeout(function () {
if (self.onended) {
self.onended();
}
}, delay * 1000);
// does not work
//source.onended = function (e) {
// console.log(e);
//};
}
else {
console.error('sample.play: no buffer', self);
}
};
window['Sample'] = sample;
})(window);
|