comparison core.js @ 1360:a9eb4f6443d8

Interfaces have startPlayback and stopPlayback methods to clean up code management. Looping playbacks now have a 2s cross-fade.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 26 Jan 2016 13:52:56 +0000
parents 89ad0630d860
children 329a558b7be5
comparison
equal deleted inserted replaced
1359:89ad0630d860 1360:a9eb4f6443d8
1058 return; 1058 return;
1059 } else { 1059 } else {
1060 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]); 1060 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
1061 } 1061 }
1062 if (this.loopPlayback) { 1062 if (this.loopPlayback) {
1063 var setTime = audioContext.currentTime+2;
1063 for (var i=0; i<this.audioObjects.length; i++) 1064 for (var i=0; i<this.audioObjects.length; i++)
1064 { 1065 {
1065 this.audioObjects[i].play(this.timer.getTestTime()+1); 1066 this.audioObjects[i].play(setTime-2);
1066 if (id == i) { 1067 if (id == i) {
1067 this.audioObjects[i].loopStart(); 1068 this.audioObjects[i].loopStart(setTime);
1068 } else { 1069 } else {
1069 this.audioObjects[i].loopStop(); 1070 this.audioObjects[i].loopStop(setTime);
1070 } 1071 }
1071 } 1072 }
1072 } else { 1073 } else {
1074 var setTime = audioContext.currentTime+0.1;
1073 for (var i=0; i<this.audioObjects.length; i++) 1075 for (var i=0; i<this.audioObjects.length; i++)
1074 { 1076 {
1075 if (i != id) { 1077 if (i != id) {
1076 this.audioObjects[i].outputGain.gain.value = 0.0; 1078 this.audioObjects[i].stop(setTime);
1077 this.audioObjects[i].stop();
1078 } else if (i == id) { 1079 } else if (i == id) {
1079 this.audioObjects[id].outputGain.gain.value = this.audioObjects[id].onplayGain; 1080 this.audioObjects[id].play(setTime);
1080 this.audioObjects[id].play(audioContext.currentTime+0.01);
1081 } 1081 }
1082 } 1082 }
1083 } 1083 }
1084 interfaceContext.playhead.start(); 1084 interfaceContext.playhead.start();
1085 } 1085 }
1086 }; 1086 };
1087 1087
1088 this.stop = function() { 1088 this.stop = function() {
1089 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1) 1089 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
1090 if (this.status == 1) { 1090 if (this.status == 1) {
1091 var setTime = audioContext.currentTime+0.1;
1091 for (var i=0; i<this.audioObjects.length; i++) 1092 for (var i=0; i<this.audioObjects.length; i++)
1092 { 1093 {
1093 this.audioObjects[i].stop(); 1094 this.audioObjects[i].stop(setTime);
1094 } 1095 }
1095 interfaceContext.playhead.stop(); 1096 interfaceContext.playhead.stop();
1096 this.status = 0; 1097 this.status = 0;
1097 } 1098 }
1098 }; 1099 };
1225 1226
1226 // Create a buffer and external gain control to allow internal patching of effects and volume leveling. 1227 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
1227 this.bufferNode = undefined; 1228 this.bufferNode = undefined;
1228 this.outputGain = audioContext.createGain(); 1229 this.outputGain = audioContext.createGain();
1229 1230
1230 // Default output gain to be zero
1231 this.outputGain.gain.value = 0.0;
1232 this.onplayGain = 1.0; 1231 this.onplayGain = 1.0;
1233 1232
1234 // Connect buffer to the audio graph 1233 // Connect buffer to the audio graph
1235 this.outputGain.connect(audioEngineContext.outputGain); 1234 this.outputGain.connect(audioEngineContext.outputGain);
1236 1235
1285 this.interfaceDOM.enable(); 1284 this.interfaceDOM.enable();
1286 } 1285 }
1287 this.storeDOM.setAttribute('presentedId',interfaceObject.getPresentedId()); 1286 this.storeDOM.setAttribute('presentedId',interfaceObject.getPresentedId());
1288 }; 1287 };
1289 1288
1290 this.loopStart = function() { 1289 this.loopStart = function(setTime) {
1291 this.outputGain.gain.value = this.onplayGain; 1290 this.outputGain.gain.linearRampToValueAtTime(this.onplayGain,setTime);
1292 this.metric.startListening(audioEngineContext.timer.getTestTime()); 1291 this.metric.startListening(audioEngineContext.timer.getTestTime());
1293 }; 1292 this.interfaceDOM.startPlayback();
1294 1293 };
1295 this.loopStop = function() { 1294
1295 this.loopStop = function(setTime) {
1296 if (this.outputGain.gain.value != 0.0) { 1296 if (this.outputGain.gain.value != 0.0) {
1297 this.outputGain.gain.value = 0.0; 1297 this.outputGain.gain.linearRampToValueAtTime(0.0,setTime);
1298 this.metric.stopListening(audioEngineContext.timer.getTestTime()); 1298 this.metric.stopListening(audioEngineContext.timer.getTestTime());
1299 } 1299 }
1300 this.interfaceDOM.stopPlayback();
1300 }; 1301 };
1301 1302
1302 this.play = function(startTime) { 1303 this.play = function(startTime) {
1303 if (this.bufferNode == undefined && this.buffer.buffer != undefined) { 1304 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
1304 this.bufferNode = audioContext.createBufferSource(); 1305 this.bufferNode = audioContext.createBufferSource();
1307 this.bufferNode.buffer = this.buffer.buffer; 1308 this.bufferNode.buffer = this.buffer.buffer;
1308 this.bufferNode.loop = audioEngineContext.loopPlayback; 1309 this.bufferNode.loop = audioEngineContext.loopPlayback;
1309 this.bufferNode.onended = function(event) { 1310 this.bufferNode.onended = function(event) {
1310 // Safari does not like using 'this' to reference the calling object! 1311 // Safari does not like using 'this' to reference the calling object!
1311 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition()); 1312 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
1312 event.currentTarget.owner.stop(); 1313 event.currentTarget.owner.stop(audioContext.currentTime+1);
1313 }; 1314 };
1314 if (this.bufferNode.loop == false) { 1315 if (this.bufferNode.loop == false) {
1315 this.metric.startListening(audioEngineContext.timer.getTestTime()); 1316 this.metric.startListening(audioEngineContext.timer.getTestTime());
1316 } 1317 this.outputGain.gain.setValueAtTime(this.onplayGain,startTime);
1318 this.interfaceDOM.startPlayback();
1319 } else {
1320 this.outputGain.gain.setValueAtTime(0.0,startTime);
1321 }
1317 this.bufferNode.start(startTime); 1322 this.bufferNode.start(startTime);
1318 } 1323 }
1319 }; 1324 };
1320 1325
1321 this.stop = function() { 1326 this.stop = function(stopTime) {
1327 this.outputGain.gain.cancelScheduledValues(audioContext.currentTime);
1322 if (this.bufferNode != undefined) 1328 if (this.bufferNode != undefined)
1323 { 1329 {
1324 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition()); 1330 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
1325 this.bufferNode.stop(0); 1331 this.bufferNode.stop(stopTime);
1326 this.bufferNode = undefined; 1332 this.bufferNode = undefined;
1327 } 1333 }
1334 this.outputGain.gain.value = 0.0;
1335 this.interfaceDOM.stopPlayback();
1328 }; 1336 };
1329 1337
1330 this.getCurrentPosition = function() { 1338 this.getCurrentPosition = function() {
1331 var time = audioEngineContext.timer.getTestTime(); 1339 var time = audioEngineContext.timer.getTestTime();
1332 if (this.bufferNode != undefined) { 1340 if (this.bufferNode != undefined) {