comparison js/core.js @ 2460:0ff4a9925597

Implementation for #1
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Wed, 08 Jun 2016 14:12:42 +0100
parents 8931ca5beb03
children c81d73038425
comparison
equal deleted inserted replaced
2459:e8145f5f49b7 2460:0ff4a9925597
1352 1352
1353 this.copyBuffer = function(preSilenceTime,postSilenceTime) { 1353 this.copyBuffer = function(preSilenceTime,postSilenceTime) {
1354 // Copies the entire bufferObj. 1354 // Copies the entire bufferObj.
1355 if (preSilenceTime == undefined) {preSilenceTime = 0;} 1355 if (preSilenceTime == undefined) {preSilenceTime = 0;}
1356 if (postSilenceTime == undefined) {postSilenceTime = 0;} 1356 if (postSilenceTime == undefined) {postSilenceTime = 0;}
1357 var copy = new this.constructor();
1358 copy.url = this.url;
1359 var preSilenceSamples = secondsToSamples(preSilenceTime,this.buffer.sampleRate); 1357 var preSilenceSamples = secondsToSamples(preSilenceTime,this.buffer.sampleRate);
1360 var postSilenceSamples = secondsToSamples(postSilenceTime,this.buffer.sampleRate); 1358 var postSilenceSamples = secondsToSamples(postSilenceTime,this.buffer.sampleRate);
1361 var newLength = this.buffer.length+preSilenceSamples+postSilenceSamples; 1359 var newLength = this.buffer.length+preSilenceSamples+postSilenceSamples;
1362 copy.buffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate); 1360 var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate);
1363 // Now we can use some efficient background copy schemes if we are just padding the end 1361 // Now we can use some efficient background copy schemes if we are just padding the end
1364 if (preSilenceSamples == 0 && typeof copy.buffer.copyToChannel == "function") { 1362 if (preSilenceSamples == 0 && typeof copybuffer.copyToChannel == "function") {
1365 for (var c=0; c<this.buffer.numberOfChannels; c++) { 1363 for (var c=0; c<this.buffer.numberOfChannels; c++) {
1366 copy.buffer.copyToChannel(this.buffer.getChannelData(c),c); 1364 copybuffer.copyToChannel(this.buffer.getChannelData(c),c);
1367 } 1365 }
1368 } else { 1366 } else {
1369 for (var c=0; c<this.buffer.numberOfChannels; c++) { 1367 for (var c=0; c<this.buffer.numberOfChannels; c++) {
1370 var src = this.buffer.getChannelData(c); 1368 var src = this.buffer.getChannelData(c);
1371 var dst = copy.buffer.getChannelData(c); 1369 var dst = copybuffer.getChannelData(c);
1372 for (var n=0; n<src.length; n++) 1370 for (var n=0; n<src.length; n++)
1373 dst[n+preSilenceSamples] = src[n]; 1371 dst[n+preSilenceSamples] = src[n];
1374 } 1372 }
1375 } 1373 }
1376 // Copy in the rest of the buffer information 1374 // Copy in the rest of the buffer information
1377 copy.buffer.lufs = this.buffer.lufs; 1375 copybuffer.lufs = this.buffer.lufs;
1378 copy.buffer.playbackGain = this.buffer.playbackGain; 1376 copybuffer.playbackGain = this.buffer.playbackGain;
1379 return copy; 1377 return copybuffer;
1378 }
1379
1380 this.cropBuffer = function(startTime, stopTime) {
1381 // Copy and return the cropped buffer
1382 var start_sample = Math.floor(startTime*this.buffer.sampleRate);
1383 var stop_sample = Math.floor(stopTime*this.buffer.sampleRate);
1384 var newLength = stop_sample - start_sample;
1385 var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate);
1386 // Now we can use some efficient background copy schemes if we are just padding the end
1387 for (var c=0; c<this.buffer.numberOfChannels; c++) {
1388 var buffer = this.buffer.getChannelData(c);
1389 var sub_frame = buffer.subarray(start_sample,stop_sample);
1390 if (typeof copybuffer.copyToChannel == "function") {
1391 copybuffer.copyToChannel(sub_frame,c);
1392 } else {
1393 var dst = copybuffer.getChannelData(c);
1394 for (var n=0; n<newLength; n++)
1395 dst[n] = src[n+start_sample];
1396 }
1397 }
1398 return copybuffer;
1380 } 1399 }
1381 }; 1400 };
1382 1401
1383 this.loadPageData = function(page) { 1402 this.loadPageData = function(page) {
1384 // Load the URL from pages 1403 // Load the URL from pages
1399 } 1418 }
1400 }; 1419 };
1401 1420
1402 this.play = function(id) { 1421 this.play = function(id) {
1403 // Start the timer and set the audioEngine state to playing (1) 1422 // Start the timer and set the audioEngine state to playing (1)
1404 if (this.status == 0 && this.loopPlayback) { 1423 if (this.status == 0) {
1405 // Check if all audioObjects are ready 1424 // Check if all audioObjects are ready
1406 if(this.checkAllReady()) 1425 this.bufferReady(id);
1407 {
1408 this.status = 1;
1409 this.setSynchronousLoop();
1410 }
1411 } 1426 }
1412 else 1427 else
1413 { 1428 {
1414 this.status = 1; 1429 this.status = 1;
1415 } 1430 }
1560 var lengthDiff = length - ao.buffer.buffer.length; 1575 var lengthDiff = length - ao.buffer.buffer.length;
1561 ao.buffer = ao.buffer.copyBuffer(0,samplesToSeconds(lengthDiff,ao.buffer.buffer.sampleRate)); 1576 ao.buffer = ao.buffer.copyBuffer(0,samplesToSeconds(lengthDiff,ao.buffer.buffer.sampleRate));
1562 } 1577 }
1563 }; 1578 };
1564 1579
1580 this.bufferReady = function(id) {
1581 if(this.checkAllReady()) {
1582 if (this.synchPlayback){this.setSynchronousLoop();}
1583 this.status = 1;
1584 return true;
1585 }
1586 return false;
1587 }
1588
1565 this.exportXML = function() 1589 this.exportXML = function()
1566 { 1590 {
1567 1591
1568 }; 1592 };
1569 1593
1608 return; 1632 return;
1609 } 1633 }
1610 this.buffer = callee; 1634 this.buffer = callee;
1611 var preSilenceTime = this.specification.preSilence || this.specification.parent.preSilence || specification.preSilence || 0.0; 1635 var preSilenceTime = this.specification.preSilence || this.specification.parent.preSilence || specification.preSilence || 0.0;
1612 var postSilenceTime = this.specification.postSilence || this.specification.parent.postSilence || specification.postSilence || 0.0; 1636 var postSilenceTime = this.specification.postSilence || this.specification.parent.postSilence || specification.postSilence || 0.0;
1637 var startTime = this.specification.startTime;
1638 var stopTime = this.specification.stopTime;
1639 var copybuffer = new callee.constructor();
1640 if (isFinite(startTime) || isFinite(stopTime)) {
1641 copybuffer.buffer = callee.cropBuffer(startTime,stopTime);
1642 }
1613 if (preSilenceTime != 0 || postSilenceTime != 0) { 1643 if (preSilenceTime != 0 || postSilenceTime != 0) {
1614 this.buffer = callee.copyBuffer(preSilenceTime,postSilenceTime); 1644 if (copybuffer.buffer == undefined) {
1615 } 1645 copybuffer.buffer = callee.copyBuffer(preSilenceTime,postSilenceTime);
1616 this.state = 1; 1646 } else {
1647 copybuffer.buffer = copybuffer.copyBuffer(preSilenceTime,postSilenceTime);
1648 }
1649 }
1650
1617 var targetLUFS = this.specification.parent.loudness || specification.loudness; 1651 var targetLUFS = this.specification.parent.loudness || specification.loudness;
1618 if (typeof targetLUFS === "number") 1652 if (typeof targetLUFS === "number" && isFinite(targetLUFS))
1619 { 1653 {
1620 this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs); 1654 this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
1621 } else { 1655 } else {
1622 this.buffer.buffer.playbackGain = 1.0; 1656 this.buffer.buffer.playbackGain = 1.0;
1623 } 1657 }
1624 if (this.interfaceDOM != null) { 1658 if (this.interfaceDOM != null) {
1625 this.interfaceDOM.enable(); 1659 this.interfaceDOM.enable();
1626 } 1660 }
1627 this.onplayGain = decibelToLinear(this.specification.gain)*(this.buffer.buffer.playbackGain||1.0); 1661 this.onplayGain = decibelToLinear(this.specification.gain)*(this.buffer.buffer.playbackGain||1.0);
1628 this.storeDOM.setAttribute('playGain',linearToDecibel(this.onplayGain)); 1662 this.storeDOM.setAttribute('playGain',linearToDecibel(this.onplayGain));
1663 this.state = 1;
1664 audioEngineContext.bufferReady(id);
1629 }; 1665 };
1630 1666
1631 this.bindInterface = function(interfaceObject) 1667 this.bindInterface = function(interfaceObject)
1632 { 1668 {
1633 this.interfaceDOM = interfaceObject; 1669 this.interfaceDOM = interfaceObject;