Newer
Older
// Temporary patch until all browsers support unprefixed context.
AudioContext = AudioContext || webkitAudioContext;
// var context;
var context = new AudioContext();
// context = new (window.AudioContext || window.webkitAudioContext)();
var source;
var jsProcessor = 0;
var analyser;
var output = 0;
var bufferLoader;
var myBufferData = new Module.VectorDouble();//this data will be used to make an envelope. Value and time to value in ms.
var sampleIsLoaded = false;
// var vectorTools = new Module.vectorTools();
// init() once the page has finished loading.
window.onload = init;
function setup(){
console.log("non-overrided setup");
// setNumOutputChannels(3);
}
// this will be overriden in the users script
function play(){
// output = 0;
// console.log("non-overrided play happening");
// use to set num channels and set output as an array
// not currently working!
function setNumOutputChannels(numChannelsOut_){
if(numChannelsOut_ > 1){
OutputIsArray(true, numChannelsOut_);
} else {
OutputIsArray(false, numChannelsOut_);
}
source.disconnect();
jsProcessor.disconnect();
analyser.disconnect();
initAudio(numChannelsOut_);
}
function OutputIsArray(isArray, numChannelsOut_){
if(isArray){
output = new Array(numChannelsOut_);
for(var i = 0; i < numChannelsOut_; i++){
output[i] = 0;
}
} else {
output = 0;
var outputErrorLogged = false;
// function handling audio processing
// called byjsProcessor
function process(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
for (var i = 0; i < outputLength; ++i) {
play();
if(output instanceof Array){
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = output[channel];
}
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = output;
}
}
}
}
// var myArrayBuffer = audioCtx.createBuffer(2, 4096, context.sampleRate);
function initAudio(numChannels) {
jsProcessor = context.createScriptProcessor(4096, numChannels, numChannels);
jsProcessor.onaudioprocess = process;
analyser = context.createAnalyser();
analyser.fftSize = 2048;
// // Connect the processing graph: source -> jsProcessor -> analyser -> destination
source.connect(jsProcessor);
jsProcessor.connect(analyser);
analyser.connect(context.destination);
}
initAudio(2);
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
setup();
}
// function loadSampleBuffers(url){
// bufferLoader = new BufferLoader(
// context,
// [url],
// finishedLoading
// );
// bufferLoader.load();
// // var sources = [];
// // for(var i = 0; i < urls.length; i++){
// // var sourceI = context.createBufferSource();
// // sourceI.buffer = bufferList[i];
// // }
// }
function loadSample(url, samplePlayer) {
var data = [];
samplePlayer.clear();
sampleIsLoaded = false;
// Load asynchronously
var request = new XMLHttpRequest();
request.addEventListener("load", transferComplete);
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
context.decodeAudioData(
request.response,
function(buffer) {
// source.buffer = buffer;
// source.loop = true;
// source.start(0);
data = buffer.getChannelData(0);
if(data){
Module.vectorTools.clearVectorDbl(myBufferData);
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData);
sampleIsLoaded = true;
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
};
request.send();
}
function transferComplete(evt) {
console.log("The transfer is complete.");
}
// function playSound(buffer) {
// var source = context.createBufferSource(); // creates a sound source
// source.buffer = buffer; // tell the source which sound to play
// source.connect(context.destination); // connect the source to the context's destination (the speakers)
// source.start(0); // play the source now
// // note: on older systems, may have to use deprecated noteOn(time);
// }