Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample Ter (Click) #1

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 60_Clicks_L.wav
Binary file not shown.
Binary file added 60_Clicks_R.wav
Binary file not shown.
51 changes: 48 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();

var sampleBis = document.querySelector('#sampleBis');
var sampleTer = document.querySelector('#sampleTer');

var sampleTerUrl = './60_Clicks_R.wav';
var sampleTerBuffer;
var sampleTerBufferSource;

var initBt = document.querySelector('#init');
var timeIt = document.querySelector('#time');
var playBt = document.querySelector('#play');
Expand Down Expand Up @@ -35,6 +41,35 @@ if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)
};


function loadBuffer(url) {
var request = new XMLHttpRequest();

request.open('GET', url);
request.responseType = 'arraybuffer';

request.onerror = function() {
alert('Failed to load ' + url + ': ' + request.status + ' ' + request.statusText);
};

request.onload = function() {
if (request.status >= 400) {
request.onerror();
return;
}

audioContext.decodeAudioData(request.response, function(loaded) {
sampleTerBuffer = loaded;
console.log(loaded);
}, function() {
alert('Failed to decode audio file ' + url);
});
};

request.send();

}


function getChart(data){
var chart = c3.generate({
bindto: '#chart',
Expand Down Expand Up @@ -68,10 +103,15 @@ playBt.onclick = function(evt){
initTime = audioContext.currentTime;
sampleBis.currentTime = 0;
sampleBis.play();
sampleTerBufferSource = audioContext.createBufferSource();
sampleTerBufferSource.buffer = sampleTerBuffer;
sampleTerBufferSource.connect(audioContext.destination);
sampleTerBufferSource.start(0);
};

stopBt.onclick = function(evt){
sampleBis.pause();
sampleTerBufferSource.stop(0);
};

timeIt.onclick = function(evt){
Expand All @@ -80,7 +120,7 @@ timeIt.onclick = function(evt){
val2.textContent = audioContext.currentTime;
val4.textContent = diff;
var currentTimeDiff = sampleBis.currentTime-diff;
val5.textContent = currentTimeDiff;
val5.textContent = '' + currentTimeDiff + ' (' + (currentTimeDiff * audioContext.sampleRate) + ')';
var now = new Date();
var ms = now.getMilliseconds();
if(now.getMilliseconds() < 100){
Expand All @@ -90,8 +130,10 @@ timeIt.onclick = function(evt){
data[0].push(d);
data[1].push(currentTimeDiff);
getChart(data);
val6.textContent = average(data[1].slice(1));
val7.textContent = standardDeviation(data[1].slice(1));
var avg = average(data[1].slice(1));
val6.textContent = '' + avg + ' (' + (avg * audioContext.sampleRate) + ')';
var stdev = standardDeviation(data[1].slice(1));
val7.textContent = '' + stdev + ' (' + (stdev * audioContext.sampleRate) + ')';
};


Expand All @@ -113,6 +155,9 @@ var initFunction = function() {
source.connect(audioContext.destination);
source.start(0);
val2.textContent = audioContext.currentTime;

console.log('init');
loadBuffer(sampleTerUrl);
};

initBt.onclick = initFunction;
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ <h1>Device Latency</h1>

<button id="init">Init AudioContext on iOS</button>
<br>
<audio src="gimme.mp3" id="sampleBis" auto controls></audio>
<audio src="60_Clicks_L.wav" id="sampleBis" auto controls></audio>
<audio src="60_Clicks_R.wav" id="sampleTer" auto controls></audio>


<br>
Expand Down