-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsampler.js
142 lines (125 loc) · 4.08 KB
/
sampler.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
(function(exports) {
var Slice = function() {
this.url = '';
this.offset = 0;
this.decay = 2000;
this.gain = 1.0;
this.pitch = 1.0;
}
var Sampler = function() {
var contextObject = window.AudioContext || window.webkitAudioContext || null;
if (contextObject === null) {
throw new Error('AudioContext not supported. :(');
}
this.context = new contextObject();
this.samples = {};
this.comp = this.context.createDynamicsCompressor();
this.comp.threshold = -20;
this.maxsamples = 44100 * 10;
this.comp.ratio = 10;
this.comp.connect(this.context.destination);
this.mixer = this.context.createGain();
this.mixer.connect(this.comp);
}
Sampler.prototype.getSamples = function(url) {
if (url) {
if (typeof(this.samples[url]) != 'undefined') {
if (this.samples[url].state == 'ready') {
return this.samples[url].buffer.getChannelData(0);
}
}
}
return null;
}
Sampler.prototype.preload = function(url, callback) {
var _this = this;
if (url) {
if (typeof(this.samples[url]) != 'undefined') {
if (this.samples[url].state == 'loading') {
if (callback) {
this.samples[url].callbacks.push(callback);
}
return;
}
if (this.samples[url].state == 'ready') {
if (callback) {
callback(_this);
}
return;
}
}
console.log('Sampler preload url: ' + url);
var obj = {
state: 'loading',
url: url,
buffer: null,
element: null,
callbacks: callback ? [callback] : []
}
var proxyurl = 'http://lab.possan.se:1234/' + url.replace('http://', '').replace('https://', '');
console.log('proxy url', proxyurl);
var request = new XMLHttpRequest();
request.open("GET", proxyurl, true);
request.responseType = "arraybuffer";
request.onload = function() {
var audioData = request.response;
console.log('audio onload', audioData);
_this.context.decodeAudioData(audioData, function(tmpbuffer) {
var monobuffer = tmpbuffer.getChannelData(0);
console.log('monobuffer', monobuffer.length);
var maxlength = _this.maxsamples;
var newlength = Math.min(maxlength, monobuffer.length);
console.log('newlength', newlength);
var tmpbuffer2 = _this.context.createBuffer(1, maxlength, 44100);
var audioData2 = tmpbuffer2.getChannelData(0)
// var audioData2 = new Float32Array(newlength);
for(var i=0; i<newlength; i++) {
audioData2[i] = monobuffer[i];
}
// tmpbuffer.getChannelData(0).set(audioData2);
obj.buffer = tmpbuffer2;// _this.context.createBuffer(tmpbuffer, true);
obj.state = 'ready';
obj.callbacks.forEach(function(cb) {
cb(obj);
});
}, function() {});
};
request.send();
this.samples[url] = obj;
}
}
Sampler.prototype.trigSlice = function(slice) {
this._trig(slice.url, slice.offset, slice.decay, slice.gain, slice.pitch);
}
Sampler.prototype._trig = function(url, starttime, decay, gain, pitch) {
if (url) {
var samp = this.samples[url];
if (typeof(samp) == 'undefined') {
this.preload(url);
}
samp = this.samples[url];
if (samp.state == 'ready') {
// console.log('starting ' + url + ', offset=' + starttime+ ', decay='+decay+', gain='+gain+', pitch='+pitch);
var tmpgain = this.context.createGain();
tmpgain.gain.value = gain;
// tmpgain.gain.linearRampToValueAtTime(this.context.currentTime + decay / 1000.0 * 0.00, gain || 1.00);
tmpgain.gain.linearRampToValueAtTime(gain, this.context.currentTime + decay / 1000.0 * 0.75);
tmpgain.gain.linearRampToValueAtTime(0.00, this.context.currentTime + decay / 1000.0 * 1.00);
tmpgain.connect(this.mixer);
var tmpsource = this.context.createBufferSource();
// console.log(tmpsource);
tmpsource.buffer = samp.buffer;
tmpsource.connect(tmpgain);
tmpsource.playbackRate.value = pitch || 1.0;
tmpsource.start(0, starttime / 1000.0, decay / 1000.0);
}
if (samp.state == 'loading') {
console.log('not playing ' + url + ', still loading...');
// this.samples[url].play();
// do something.
}
}
}
exports.Slice = Slice;
exports.Sampler = Sampler;
})(window || this);